groupedByCourse
This commit is contained in:
@@ -73,4 +73,72 @@ router.get("/consolidated-table", async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
router.get("/course-consolidated", async (req, res) => {
|
||||
try {
|
||||
const appointments = await Appointment.find();
|
||||
const courses = await Course.find();
|
||||
|
||||
// Group appointments by courseId and task
|
||||
const groupedByCourse = {};
|
||||
|
||||
appointments.forEach((appointment) => {
|
||||
const courseId = appointment.courseId;
|
||||
|
||||
if (!groupedByCourse[courseId]) {
|
||||
groupedByCourse[courseId] = {
|
||||
courseId: courseId,
|
||||
courseName: appointment.courseName,
|
||||
tasks: {
|
||||
oralsPracticals: new Set(),
|
||||
assessment: new Set(),
|
||||
reassessment: new Set(),
|
||||
paperSetting: new Set(),
|
||||
moderation: new Set(),
|
||||
pwdPaperSetter: new Set(),
|
||||
},
|
||||
semester: "",
|
||||
examType: "",
|
||||
year: "",
|
||||
};
|
||||
}
|
||||
|
||||
// Add the faculty name to the appropriate task
|
||||
if (appointment.task && groupedByCourse[courseId].tasks[appointment.task]) {
|
||||
groupedByCourse[courseId].tasks[appointment.task].add(appointment.facultyName);
|
||||
}
|
||||
});
|
||||
|
||||
// Add course details to grouped data
|
||||
Object.values(groupedByCourse).forEach((group) => {
|
||||
const course = courses.find((c) => c.courseId === group.courseId);
|
||||
|
||||
if (course) {
|
||||
group.semester = course.semester;
|
||||
group.examType = course.scheme;
|
||||
group.year = course.program;
|
||||
}
|
||||
});
|
||||
|
||||
// Format the consolidated data
|
||||
const consolidatedData = Object.values(groupedByCourse).map((course, index) => ({
|
||||
courseCode: course.courseId,
|
||||
courseName: course.courseName,
|
||||
semester: course.semester,
|
||||
examType: course.examType,
|
||||
year: course.year,
|
||||
oralPracticalTeachers: Array.from(course.tasks.oralsPracticals),
|
||||
assessmentTeachers: Array.from(course.tasks.assessment),
|
||||
reassessmentTeachers: Array.from(course.tasks.reassessment),
|
||||
paperSettingTeachers: Array.from(course.tasks.paperSetting),
|
||||
moderationTeachers: Array.from(course.tasks.moderation),
|
||||
pwdPaperSettingTeachers: Array.from(course.tasks.pwdPaperSetter),
|
||||
}));
|
||||
|
||||
res.status(200).json(consolidatedData);
|
||||
} catch (error) {
|
||||
console.error("Error fetching course consolidated data:", error);
|
||||
res.status(500).json({ message: "Failed to fetch course consolidated data" });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user