diff --git a/api/src/controllers/lessonController.js b/api/src/controllers/lessonController.js index 22a862d3e..49494b906 100644 --- a/api/src/controllers/lessonController.js +++ b/api/src/controllers/lessonController.js @@ -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')) ) @@ -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 @@ -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') } @@ -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 @@ -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 }, { @@ -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, @@ -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') } @@ -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 @@ -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 }, {