diff --git a/consts/validation.js b/consts/validation.js index 8a793840..0acf1c21 100644 --- a/consts/validation.js +++ b/consts/validation.js @@ -24,7 +24,8 @@ const enums = { OFFER_STATUS_ENUM: ['active', 'draft', 'closed'], NOTIFICATION_TYPE_ENUM: ['new', 'requested', 'active', 'declined', 'updated', 'closed', 'deleted'], QUESTION_TYPE_ENUM: ['multipleChoice', 'openAnswer', 'oneAnswer'], - QUIZ_VIEW_ENUM: ['Stepper', 'Scroll'] + QUIZ_VIEW_ENUM: ['Stepper', 'Scroll'], + RESOURCES_TYPES_ENUM: ['lessons', 'attachments', 'questions', 'quizzes'] } module.exports = { diff --git a/models/attachment.js b/models/attachment.js index 090c3af8..95d34ef0 100644 --- a/models/attachment.js +++ b/models/attachment.js @@ -1,6 +1,14 @@ const { ATTACHMENT, USER, RESOURCES_CATEGORY } = require('~/consts/models') const { Schema, model } = require('mongoose') -const { FIELD_CANNOT_BE_EMPTY, FIELD_CANNOT_BE_SHORTER, FIELD_CANNOT_BE_LONGER } = require('~/consts/errors') +const { + FIELD_CANNOT_BE_EMPTY, + FIELD_CANNOT_BE_SHORTER, + FIELD_CANNOT_BE_LONGER, + ENUM_CAN_BE_ONE_OF +} = require('~/consts/errors') +const { + enums: { RESOURCES_TYPES_ENUM } +} = require('~/consts/validation') const attachmentSchema = new Schema( { @@ -34,6 +42,14 @@ const attachmentSchema = new Schema( type: Schema.Types.ObjectId, ref: RESOURCES_CATEGORY, default: null + }, + resourceType: { + type: String, + enum: { + values: RESOURCES_TYPES_ENUM, + message: ENUM_CAN_BE_ONE_OF('resource type', RESOURCES_TYPES_ENUM) + }, + default: RESOURCES_TYPES_ENUM[1] } }, { diff --git a/models/lesson.js b/models/lesson.js index 394457e8..a4e2889d 100644 --- a/models/lesson.js +++ b/models/lesson.js @@ -1,6 +1,15 @@ const { Schema, model } = require('mongoose') -const { FIELD_CANNOT_BE_EMPTY, FIELD_CANNOT_BE_SHORTER, FIELD_CANNOT_BE_LONGER } = require('~/consts/errors') +const { + FIELD_CANNOT_BE_EMPTY, + FIELD_CANNOT_BE_SHORTER, + FIELD_CANNOT_BE_LONGER, + ENUM_CAN_BE_ONE_OF +} = require('~/consts/errors') const { USER, LESSON, ATTACHMENT, RESOURCES_CATEGORY } = require('~/consts/models') +const { + enums: { RESOURCES_TYPES_ENUM } +} = require('~/consts/validation') + const lessonSchema = new Schema( { author: { @@ -36,6 +45,14 @@ const lessonSchema = new Schema( type: Schema.Types.ObjectId, ref: RESOURCES_CATEGORY, default: null + }, + resourceType: { + type: String, + enum: { + values: RESOURCES_TYPES_ENUM, + message: ENUM_CAN_BE_ONE_OF('resource type', RESOURCES_TYPES_ENUM) + }, + default: RESOURCES_TYPES_ENUM[0] } }, { diff --git a/models/question.js b/models/question.js index ed8fe00b..c582531b 100644 --- a/models/question.js +++ b/models/question.js @@ -1,6 +1,6 @@ const { Schema, model } = require('mongoose') const { - enums: { QUESTION_TYPE_ENUM } + enums: { QUESTION_TYPE_ENUM, RESOURCES_TYPES_ENUM } } = require('~/consts/validation') const { QUESTION, USER, RESOURCES_CATEGORY } = require('~/consts/models') const { @@ -56,6 +56,14 @@ const questionSchema = new Schema( type: Schema.Types.ObjectId, ref: USER, required: [true, FIELD_CANNOT_BE_EMPTY('author')] + }, + resourceType: { + type: String, + enum: { + values: RESOURCES_TYPES_ENUM, + message: ENUM_CAN_BE_ONE_OF('resource type', RESOURCES_TYPES_ENUM) + }, + default: RESOURCES_TYPES_ENUM[2] } }, { timestamps: true, versionKey: false } diff --git a/models/quiz.js b/models/quiz.js index 19b96ba8..d6cf9919 100644 --- a/models/quiz.js +++ b/models/quiz.js @@ -1,9 +1,14 @@ const { Schema, model } = require('mongoose') const { QUIZ, USER, RESOURCES_CATEGORY, QUESTION } = require('~/consts/models') -const { FIELD_CANNOT_BE_EMPTY, FIELD_CANNOT_BE_LONGER, FIELD_CANNOT_BE_SHORTER, ENUM_CAN_BE_ONE_OF } = require('~/consts/errors') -const { enums: { - QUIZ_VIEW_ENUM -} } = require('~/consts/validation') +const { + FIELD_CANNOT_BE_EMPTY, + FIELD_CANNOT_BE_LONGER, + FIELD_CANNOT_BE_SHORTER, + ENUM_CAN_BE_ONE_OF +} = require('~/consts/errors') +const { + enums: { QUIZ_VIEW_ENUM, RESOURCES_TYPES_ENUM } +} = require('~/consts/validation') const quizSchema = new Schema( { @@ -33,6 +38,14 @@ const quizSchema = new Schema( ref: RESOURCES_CATEGORY, default: null }, + resourceType: { + type: String, + enum: { + values: RESOURCES_TYPES_ENUM, + message: ENUM_CAN_BE_ONE_OF('resource type', RESOURCES_TYPES_ENUM) + }, + default: RESOURCES_TYPES_ENUM[3] + }, settings: { view: { type: String, diff --git a/services/course.js b/services/course.js index d2ddc72c..81535abe 100644 --- a/services/course.js +++ b/services/course.js @@ -21,18 +21,18 @@ const courseService = { }, getCourseById: async (id) => { - return await Course.findById(id).lean().exec() + return await Course.findById(id) + .populate([ + { path: 'sections.lessons', select: '_id title resourceType' }, + { path: 'sections.attachments', select: '_id fileName resourceType' }, + { path: 'sections.quizzes', select: '_id title resourceType' } + ]) + .lean() + .exec() }, createCourse: async (author, data) => { - const { - title, - description, - category, - subject, - proficiencyLevel, - sections - } = data + const { title, description, category, subject, proficiencyLevel, sections } = data return await Course.create({ title, @@ -46,7 +46,7 @@ const courseService = { }, updateCourse: async (userId, data) => { - const { id, title, description, attachments, rewriteAttachments = false } = data + const { id, title, description, category, subject, proficiencyLevel, sections } = data const course = await Course.findById(id).exec() @@ -60,11 +60,7 @@ const courseService = { throw createForbiddenError() } - if (attachments) { - course.attachments = rewriteAttachments ? attachments : course.attachments.concat(attachments) - } - - const updateData = { title, description } + const updateData = { title, description, category, subject, proficiencyLevel, sections } for (const key in updateData) { const value = updateData[key]