From f17f533cc0a7c28f2ba6003c8a11c15ed77811f8 Mon Sep 17 00:00:00 2001 From: Harikrishnan Gopal <118685394+hk151109@users.noreply.github.com> Date: Thu, 23 Jan 2025 10:58:28 +0530 Subject: [PATCH] consolidated --- server/routes/consolidatedRoutes.js | 82 +++++++++++++++++++---------- 1 file changed, 54 insertions(+), 28 deletions(-) diff --git a/server/routes/consolidatedRoutes.js b/server/routes/consolidatedRoutes.js index 0aa832c..e136ae2 100644 --- a/server/routes/consolidatedRoutes.js +++ b/server/routes/consolidatedRoutes.js @@ -6,43 +6,69 @@ const Faculty = require("../models/Faculty"); router.get("/consolidated-table", async (req, res) => { try { - // Fetch all appointments const appointments = await Appointment.find(); - // Fetch all courses const courses = await Course.find(); - // Fetch all faculties const faculties = await Faculty.find(); - // Prepare data for the table - const tableData = appointments.map((appointment, index) => { - const course = courses.find((c) => c.courseId === appointment.courseId); - const faculty = faculties.find((f) => f.facultyId === appointment.facultyId); + // Group by facultyId and courseId + const groupedData = {}; - return { - srNo: index + 1, - semester: course?.semester || "", - courseCode: course?.courseId || "", - courseName: course?.name || "", - examType: course?.scheme || "", - year: course?.program || "", - marks: "100+25", // Replace with actual marks logic if required - Name: faculty?.name || "", - affiliation: "KJSCE", // Replace with actual affiliation if needed - qualification: "M.E.", // Replace with actual qualification if needed - experience: "20+", // Replace with actual experience logic if required - oralPractical: appointment.task === "oralsPracticals" ? "✔" : "", - assessment: appointment.task === "assessment" ? "✔" : "", - reassessment: appointment.task === "reassessment" ? "✔" : "", - paperSetting: appointment.task === "paperSetting" ? "✔" : "", - moderation: appointment.task === "moderation" ? "✔" : "", - pwdPaperSetting: appointment.task === "pwdPaperSetter" ? "✔" : "", - }; + appointments.forEach((appointment) => { + const key = `${appointment.facultyId}-${appointment.courseId}`; + if (!groupedData[key]) { + groupedData[key] = { + facultyId: appointment.facultyId, + courseId: appointment.courseId, + facultyName: appointment.facultyName, + courseName: appointment.courseName, + semester: "", + examType: "", + year: "", + marks: "100+25", // Dummy value, replace with actual logic + affiliation: "KJSCE", // Replace with actual value + qualification: "M.E.", // Replace with actual value + experience: "20+", // Dummy value + tasks: new Set(), // Use a set to avoid duplicate tasks + }; + } + groupedData[key].tasks.add(appointment.task); }); + // Add course details to the grouped data + Object.values(groupedData).forEach((data) => { + const course = courses.find((c) => c.courseId === data.courseId); + if (course) { + data.semester = course.semester; + data.examType = course.scheme; + data.year = course.program; + } + }); + + // Format the tasks into separate columns + const tableData = Object.values(groupedData).map((data, index) => ({ + srNo: index + 1, + semester: data.semester, + courseCode: data.courseId, + courseName: data.courseName, + examType: data.examType, + year: data.year, + marks: data.marks, + Name: data.facultyName, + affiliation: data.affiliation, + qualification: data.qualification, + experience: data.experience, + oralPractical: data.tasks.has("oralsPracticals") ? "✔" : "", + assessment: data.tasks.has("assessment") ? "✔" : "", + reassessment: data.tasks.has("reassessment") ? "✔" : "", + paperSetting: data.tasks.has("paperSetting") ? "✔" : "", + moderation: data.tasks.has("moderation") ? "✔" : "", + pwdPaperSetting: data.tasks.has("pwdPaperSetter") ? "✔" : "", + })); + res.status(200).json(tableData); } catch (error) { - console.error("Error generating consolidated table:", error); - res.status(500).json({ message: "Failed to generate table" }); + console.error("Error fetching consolidated table data:", error); + res.status(500).json({ message: "Failed to fetch consolidated table data" }); } });