Skip to content

Commit

Permalink
fix lesson api validation
Browse files Browse the repository at this point in the history
  • Loading branch information
neil-sweetcodepro committed May 11, 2022
1 parent 3151ff7 commit 5a2b1e4
Showing 1 changed file with 43 additions and 18 deletions.
61 changes: 43 additions & 18 deletions api/src/controllers/lessonController.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,20 @@ const getLessonById = async (req, res) => {
const lessonSchema = {
body: Joi.object({
title: Joi.string().required(),
lesson_order: Joi.number().required(),
text_heading: Joi.alternatives().try(
lessonOrder: Joi.number().required(),
textHeader: Joi.alternatives().try(
Joi.string(),
Joi.array().items(Joi.string())
),
text_description: Joi.alternatives().try(
textDescription: Joi.alternatives().try(
Joi.string(),
Joi.array().items(Joi.string())
),
photo_description: Joi.alternatives().try(
photoDescription: Joi.alternatives().try(
Joi.string(),
Joi.array().items(Joi.string())
),
description_order: Joi.alternatives().try(
descriptionOrder: Joi.alternatives().try(
Joi.string().valid('text', 'image'),
Joi.array().items(Joi.string().valid('text', 'image'))
)
Expand All @@ -83,7 +83,7 @@ const lessonSchema = {

const addLesson = async (req, res) => {
const { baseUrl, user, files, body } = req
const { title, description_order: descriptionOrder, lesson_order: lessonOrder } = body
const { title, descriptionOrder, lessonOrder } = body
const courseId = getCourseIdFromUrl(baseUrl)
const thumbnail = files.thumbnail?.[0]?.filename || null

Expand Down Expand Up @@ -111,11 +111,24 @@ const addLesson = async (req, res) => {

if (descriptionOrder) {

const richtextCount = Array.isArray(body.text_description) ? body.text_description.length : 1
const richtextCount = Array.isArray(body.textDescription) ? body.textDescription.length : 1
const photoCount = files.lessons?.length || 0

if (photoCount !== body.descriptionOrder.filter(i => i === 'image').length) {
throw new BadRequestError('Photo details don\'t match the ordering details')
}

if (richtextCount !== body.descriptionOrder.filter(i => i === 'text').length) {
throw new BadRequestError('Text details don\'t match the ordering details')
}

if (richtextCount > 1 && body.textDescription?.length !== body.textHeader?.length) {
throw new BadRequestError('Number of text header and description items do not match')
}

const orderCount = Array.isArray(descriptionOrder) ? descriptionOrder.length : 1

// add validation if texts and photo tallies the order count
// validation if texts and photo tallies the order count
if (photoCount + richtextCount !== orderCount) {
throw new BadRequestError('Contents don\'t match the ordering count')
}
Expand All @@ -124,8 +137,8 @@ const addLesson = async (req, res) => {
if (order === 'text') {
return db.Text.create({
richtextId,
textHeading: Array.isArray(body.text_heading) ? body.text_heading.shift() : body.text_heading,
textDescription: Array.isArray(body.text_description) ? body.text_description.shift() : body.text_description,
textHeading: Array.isArray(body.textHeader) ? body.textHeader.shift() : body.textHeader,
textDescription: Array.isArray(body.textDescription) ? body.textDescription.shift() : body.textDescription,
order: index + 1
}, {
transaction
Expand All @@ -135,7 +148,7 @@ const addLesson = async (req, res) => {
return db.Photo.create({
richtextId,
lessonImg: `lessons/${files.lessons.shift().filename}`,
photoDescription: Array.isArray(body.photo_description) ? body.photo_description.shift() : body.photo_description,
photoDescription: Array.isArray(body.photoDescription) ? body.photoDescription.shift() : body.photoDescription,
isImgDesc: false,
order: index + 1
}, {
Expand Down Expand Up @@ -216,12 +229,11 @@ const updateLesson = async (req, res) => {
throw new NotFoundError()
}

console.log(lesson.course.creatorId,user.id)
if (lesson.course.creatorId !== user.id) {
throw new ForbiddenRequestError()
}

const { title, description_order: descriptionOrder, lesson_order: lessonOrder } = body
const { title, descriptionOrder, lessonOrder } = body
const thumbnail = files.thumbnail?.[0]?.filename || null
const lessonData = {
thumbnail: thumbnail ? `thumbnail/${thumbnail}` : null,
Expand All @@ -235,11 +247,24 @@ const updateLesson = async (req, res) => {
const richtextId = richtext.id

if (descriptionOrder) {
const richtextCount = Array.isArray(body.text_description) ? body.text_description.length : 1
const richtextCount = Array.isArray(body.textDescription) ? body.textDescription.length : 1
const photoCount = files.lessons?.length || 0

if (photoCount !== body.descriptionOrder.filter(i => i === 'image').length) {
throw new BadRequestError('Photo details don\'t match the ordering details')
}

if (richtextCount !== body.descriptionOrder.filter(i => i === 'text').length) {
throw new BadRequestError('Text details don\'t match the ordering details')
}

if (richtextCount > 1 && body.textDescription?.length !== body.textHeader?.length) {
throw new BadRequestError('Number of text header and description items do not match')
}

const orderCount = Array.isArray(descriptionOrder) ? descriptionOrder.length : 1

// add validation if texts and photo tallies the order count
// validation if texts and photo tallies the order count
if (photoCount + richtextCount !== orderCount) {
throw new BadRequestError('Contents don\'t match the ordering count')
}
Expand All @@ -248,8 +273,8 @@ const updateLesson = async (req, res) => {
if (order === 'text') {
return db.Text.create({
richtextId,
textHeading: Array.isArray(body.text_heading) ? body.text_heading.shift() : body.text_heading,
textDescription: Array.isArray(body.text_description) ? body.text_description.shift() : body.text_description,
textHeading: Array.isArray(body.textHeader) ? body.textHeader.shift() : body.textHeader,
textDescription: Array.isArray(body.textDescription) ? body.textDescription.shift() : body.textDescription,
order: index + 1
}, {
transaction
Expand All @@ -259,7 +284,7 @@ const updateLesson = async (req, res) => {
return db.Photo.create({
richtextId,
lessonImg: `courses/${files.lessons.shift().filename}`,
photoDescription: Array.isArray(body.photo_description) ? body.photo_description.shift() : body.photo_description,
photoDescription: Array.isArray(body.photoDescription) ? body.photoDescription.shift() : body.photoDescription,
isImgDesc: false,
order: index + 1
}, {
Expand Down

0 comments on commit 5a2b1e4

Please sign in to comment.