diff --git a/src/app/modules/Course/course.model.ts b/src/app/modules/Course/course.model.ts index 0316917..f5bad06 100644 --- a/src/app/modules/Course/course.model.ts +++ b/src/app/modules/Course/course.model.ts @@ -5,45 +5,55 @@ import { TPreRequisiteCourses, } from './course.interface'; -const preRequisiteCoursesSchema = new Schema({ - course: { - type: Schema.Types.ObjectId, - ref: 'Course', +const preRequisiteCoursesSchema = new Schema( + { + course: { + type: Schema.Types.ObjectId, + ref: 'Course', + }, + isDeleted: { + type: Boolean, + default: false, + }, }, - isDeleted: { - type: Boolean, - default: false, + { + _id: false, }, -}); +); -const courseSchema = new Schema({ - title: { - type: String, - unique: true, - trim: true, - required: true, - }, - prefix: { - type: String, - trim: true, - required: true, - }, - code: { - type: Number, - trim: true, - required: true, - }, - credits: { - type: Number, - trim: true, - required: true, +const courseSchema = new Schema( + { + title: { + type: String, + unique: true, + trim: true, + required: true, + }, + prefix: { + type: String, + trim: true, + required: true, + }, + code: { + type: Number, + trim: true, + required: true, + }, + credits: { + type: Number, + trim: true, + required: true, + }, + preRequisiteCourses: [preRequisiteCoursesSchema], + isDeleted: { + type: Boolean, + default: false, + }, }, - preRequisiteCourses: [preRequisiteCoursesSchema], - isDeleted: { - type: Boolean, - default: false, + { + timestamps: true, }, -}); +); export const Course = model('Course', courseSchema); diff --git a/src/app/modules/Course/course.service.ts b/src/app/modules/Course/course.service.ts index 55c354a..4601c6e 100644 --- a/src/app/modules/Course/course.service.ts +++ b/src/app/modules/Course/course.service.ts @@ -13,7 +13,8 @@ const createCourseIntoDB = async (payload: TCourse) => { const getAllCoursesFromDB = async (query: Record) => { const courseQuery = new QueryBuilder( - Course.find().populate('preRequisiteCourses.course'), + Course.find(), + // .populate('preRequisiteCourses.course'), query, ) .search(CourseSearchableFields) @@ -40,7 +41,6 @@ const updateCourseIntoDB = async (id: string, payload: Partial) => { try { session.startTransaction(); - //step1: basic course info update const updatedBasicCourseInfo = await Course.findByIdAndUpdate( id, @@ -53,7 +53,7 @@ const updateCourseIntoDB = async (id: string, payload: Partial) => { ); if (!updatedBasicCourseInfo) { - throw new AppError(httpStatus.BAD_REQUEST, 'Failed to update course'); + throw new AppError(httpStatus.BAD_REQUEST, 'Failed to update course!'); } // check if there is any pre requisite courses to update @@ -78,7 +78,7 @@ const updateCourseIntoDB = async (id: string, payload: Partial) => { ); if (!deletedPreRequisiteCourses) { - throw new AppError(httpStatus.BAD_REQUEST, 'Failed to update course'); + throw new AppError(httpStatus.BAD_REQUEST, 'Failed to update course!'); } // filter out the new course fields @@ -99,18 +99,18 @@ const updateCourseIntoDB = async (id: string, payload: Partial) => { ); if (!newPreRequisiteCourses) { - throw new AppError(httpStatus.BAD_REQUEST, 'Failed to update course'); + throw new AppError(httpStatus.BAD_REQUEST, 'Failed to update course!'); } - - const result = await Course.findById(id).populate( - 'preRequisiteCourses.course', - ); - - return result; } await session.commitTransaction(); await session.endSession(); + + const result = await Course.findById(id).populate( + 'preRequisiteCourses.course', + ); + + return result; } catch (err) { await session.abortTransaction(); await session.endSession(); diff --git a/src/app/modules/academicDepartment/academicDepartment.model.ts b/src/app/modules/academicDepartment/academicDepartment.model.ts index 0660b13..e97fec1 100644 --- a/src/app/modules/academicDepartment/academicDepartment.model.ts +++ b/src/app/modules/academicDepartment/academicDepartment.model.ts @@ -20,7 +20,6 @@ const academicDepartmentSchema = new Schema( }, ); - academicDepartmentSchema.pre('save', async function (next) { const isDepartmentExist = await AcademicDepartment.findOne({ name: this.name,