-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathcourse.service.ts
More file actions
174 lines (153 loc) · 4.19 KB
/
course.service.ts
File metadata and controls
174 lines (153 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import httpStatus from 'http-status';
import mongoose from 'mongoose';
import QueryBuilder from '../../builder/QueryBuilder';
import AppError from '../../errors/AppError';
import { CourseSearchableFields } from './course.constant';
import { TCourse, TCoursefaculty } from './course.interface';
import { Course, CourseFaculty } from './course.model';
const createCourseIntoDB = async (payload: TCourse) => {
const result = await Course.create(payload);
return result;
};
const getAllCoursesFromDB = async (query: Record<string, unknown>) => {
const courseQuery = new QueryBuilder(
Course.find(),
// .populate('preRequisiteCourses.course'),
query,
)
.search(CourseSearchableFields)
.filter()
.sort()
.paginate()
.fields();
const result = await courseQuery.modelQuery;
return result;
};
const getSingleCourseFromDB = async (id: string) => {
const result = await Course.findById(id).populate(
'preRequisiteCourses.course',
);
return result;
};
const updateCourseIntoDB = async (id: string, payload: Partial<TCourse>) => {
const { preRequisiteCourses, ...courseRemainingData } = payload;
const session = await mongoose.startSession();
try {
session.startTransaction();
//step1: basic course info update
const updatedBasicCourseInfo = await Course.findByIdAndUpdate(
id,
courseRemainingData,
{
new: true,
runValidators: true,
session,
},
);
if (!updatedBasicCourseInfo) {
throw new AppError(httpStatus.BAD_REQUEST, 'Failed to update course!');
}
// check if there is any pre requisite courses to update
if (preRequisiteCourses && preRequisiteCourses.length > 0) {
// filter out the deleted fields
const deletedPreRequisites = preRequisiteCourses
.filter((el) => el.course && el.isDeleted)
.map((el) => el.course);
const deletedPreRequisiteCourses = await Course.findByIdAndUpdate(
id,
{
$pull: {
preRequisiteCourses: { course: { $in: deletedPreRequisites } },
},
},
{
new: true,
runValidators: true,
session,
},
);
if (!deletedPreRequisiteCourses) {
throw new AppError(httpStatus.BAD_REQUEST, 'Failed to update course!');
}
// filter out the new course fields
const newPreRequisites = preRequisiteCourses?.filter(
(el) => el.course && !el.isDeleted,
);
const newPreRequisiteCourses = await Course.findByIdAndUpdate(
id,
{
$addToSet: { preRequisiteCourses: { $each: newPreRequisites } },
},
{
new: true,
runValidators: true,
session,
},
);
if (!newPreRequisiteCourses) {
throw new AppError(httpStatus.BAD_REQUEST, 'Failed to update course!');
}
}
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();
throw new AppError(httpStatus.BAD_REQUEST, 'Failed to update course');
}
};
const deleteCourseFromDB = async (id: string) => {
const result = await Course.findByIdAndUpdate(
id,
{ isDeleted: true },
{
new: true,
},
);
return result;
};
const assignFacultiesWithCourseIntoDB = async (
id: string,
payload: Partial<TCoursefaculty>,
) => {
const result = await CourseFaculty.findByIdAndUpdate(
id,
{
course: id,
$addToSet: { faculties: { $each: payload } },
},
{
upsert: true,
new: true,
},
);
return result;
};
const removeFacultiesFromCourseFromDB = async (
id: string,
payload: Partial<TCoursefaculty>,
) => {
const result = await CourseFaculty.findByIdAndUpdate(
id,
{
$pull: { faculties: { $in: payload } },
},
{
new: true,
},
);
return result;
};
export const CourseServices = {
createCourseIntoDB,
getAllCoursesFromDB,
getSingleCourseFromDB,
updateCourseIntoDB,
deleteCourseFromDB,
assignFacultiesWithCourseIntoDB,
removeFacultiesFromCourseFromDB,
};