Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expanded the resource type field, added needed fields to edit course #672

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion consts/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
18 changes: 17 additions & 1 deletion models/attachment.js
Original file line number Diff line number Diff line change
@@ -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(
{
Expand Down Expand Up @@ -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]
}
},
{
Expand Down
19 changes: 18 additions & 1 deletion models/lesson.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down Expand Up @@ -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]
}
},
{
Expand Down
10 changes: 9 additions & 1 deletion models/question.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 }
Expand Down
21 changes: 17 additions & 4 deletions models/quiz.js
Original file line number Diff line number Diff line change
@@ -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(
{
Expand Down Expand Up @@ -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,
Expand Down
26 changes: 11 additions & 15 deletions services/course.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()

Expand All @@ -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]
Expand Down
Loading