diff --git a/backend/src/controllers/doctor.controller.js b/backend/src/controllers/doctor.controller.js index a7087a3..dde079f 100644 --- a/backend/src/controllers/doctor.controller.js +++ b/backend/src/controllers/doctor.controller.js @@ -16,38 +16,35 @@ export const getAllDoctors = async (req, res) => { orderBy: {name: "asc"}, }); - const formatted = doctors.map((doc, index) => { - return { - SL_NO: String(index + 1), - doctorId: doc.doctorId, - name: doc.name, - designation: doc.designation, - workingStatus: doc.workingStatus, - qualification: doc.qualification, + const formatted = doctors.map((doc, index) => ({ + SL_NO: String(index + 1), + doctorId: doc.doctorId, + name: doc.name, + designation: doc.designation, + workingStatus: doc.workingStatus, + qualification: doc.qualification, - departments: doc.departments.map((d) => { - const t = d.timing || {}; + departments: doc.departments.map((d) => { + const t = d.timing || {}; - const timingArray = [ - t.monday && `Monday ${t.monday}`, - t.tuesday && `Tuesday ${t.tuesday}`, - t.wednesday && `Wednesday ${t.wednesday}`, - t.thursday && `Thursday ${t.thursday}`, - t.friday && `Friday ${t.friday}`, - t.saturday && `Saturday ${t.saturday}`, - t.sunday && `Sunday ${t.sunday}`, - t.additional && t.additional, - ].filter(Boolean); + const timingArray = [ + t.monday && `Monday ${t.monday}`, + t.tuesday && `Tuesday ${t.tuesday}`, + t.wednesday && `Wednesday ${t.wednesday}`, + t.thursday && `Thursday ${t.thursday}`, + t.friday && `Friday ${t.friday}`, + t.saturday && `Saturday ${t.saturday}`, + t.sunday && `Sunday ${t.sunday}`, + t.additional && t.additional, + ].filter(Boolean); - return { - departmentId: d.department.departmentId, - departmentName: d.department.name, - - timing: timingArray.join(" & "), - }; - }), - }; - }); + return { + departmentId: d.department.departmentId, + departmentName: d.department.name, + timing: timingArray.join(" & "), + }; + }), + })); res.status(200).json({ success: true, @@ -113,6 +110,54 @@ export const getDoctorByDoctorId = async (req, res) => { } }; +// get doctors by department +export const getDoctorsByDepartmentId = async (req, res) => { + try { + const {Department_ID} = req.query; + + if (!Department_ID) { + return res.status(400).json({ + success: false, + message: "Department_ID is required", + }); + } + + const department = await prisma.department.findUnique({ + where: {departmentId: Department_ID}, + }); + + if (!department) { + return res.status(404).json({ + success: false, + message: "Department not found", + }); + } + + const doctors = await prisma.doctorDepartment.findMany({ + where: {departmentId: department.id}, + include: { + doctor: true, + }, + }); + + const result = doctors.map((d) => ({ + GG_ID: d.doctor.doctorId, + Name: d.doctor.name, + })); + + res.status(200).json({ + success: true, + data: result, + }); + } catch (error) { + console.error(error); + res.status(500).json({ + success: false, + message: "Failed to fetch doctors", + }); + } +}; + // add doctors export const createDoctor = async (req, res) => { try { @@ -184,20 +229,14 @@ export const updateDoctor = async (req, res) => { }); if (!doctor) { - return res.status(404).json({ - success: false, - message: "Doctor not found", - }); + return res + .status(404) + .json({success: false, message: "Doctor not found"}); } await prisma.doctor.update({ where: {id: doctor.id}, - data: { - name, - designation, - workingStatus, - qualification, - }, + data: {name, designation, workingStatus, qualification}, }); const oldRelations = await prisma.doctorDepartment.findMany({ @@ -229,25 +268,24 @@ export const updateDoctor = async (req, res) => { }); if (dep.timing) { + const {id, doctorDepartmentId, createdAt, updatedAt, ...cleanTiming} = + dep.timing; + await prisma.doctorTiming.create({ data: { doctorDepartmentId: doctorDepartment.id, - ...dep.timing, + ...cleanTiming, }, }); } } - res.status(200).json({ - success: true, - message: "Doctor updated successfully", - }); + res + .status(200) + .json({success: true, message: "Doctor updated successfully"}); } catch (error) { - console.error(error); - res.status(500).json({ - success: false, - message: "Failed to update doctor", - }); + console.error("Update Error:", error); + res.status(500).json({success: false, message: "Failed to update doctor"}); } }; //delete doctor @@ -256,13 +294,6 @@ export const deleteDoctor = async (req, res) => { try { const {doctorId} = req.params; - if (!doctorId) { - return res.status(400).json({ - success: false, - message: "Doctor ID is required", - }); - } - const doctor = await prisma.doctor.findUnique({ where: {doctorId}, }); @@ -270,7 +301,7 @@ export const deleteDoctor = async (req, res) => { if (!doctor) { return res.status(404).json({ success: false, - message: `Doctor with ID ${doctorId} not found`, + message: "Doctor not found", }); } @@ -294,7 +325,7 @@ export const deleteDoctor = async (req, res) => { res.status(200).json({ success: true, - message: `Doctor ${doctorId} deleted successfully`, + message: "Doctor deleted successfully", }); } catch (error) { console.error(error); @@ -320,23 +351,19 @@ export const getDoctorTimings = async (req, res) => { }); const result = doctors.map((doc) => { - let timing = {}; - - if (doc.departments.length > 0) { - timing = doc.departments[0].timing ?? {}; - } + const timing = doc.departments[0]?.timing || {}; return { Doctor_ID: doc.doctorId, Doctor: doc.name, - Monday: timing?.monday ?? "", - Tuesday: timing?.tuesday ?? "", - Wednesday: timing?.wednesday ?? "", - Thursday: timing?.thursday ?? "", - Friday: timing?.friday ?? "", - Saturday: timing?.saturday ?? "", - Sunday: timing?.sunday ?? "", - Additional: timing?.additional ?? "", + Monday: timing.monday || "", + Tuesday: timing.tuesday || "", + Wednesday: timing.wednesday || "", + Thursday: timing.thursday || "", + Friday: timing.friday || "", + Saturday: timing.saturday || "", + Sunday: timing.sunday || "", + Additional: timing.additional || "", }; }); @@ -380,26 +407,11 @@ export const getDoctorTimingById = async (req, res) => { const result = { doctorId: doctor.doctorId, doctorName: doctor.name, - - departments: doctor.departments.map((d) => { - const t = d.timing || {}; - - return { - departmentId: d.department.departmentId, - departmentName: d.department.name, - - timing: { - monday: t.monday || "", - tuesday: t.tuesday || "", - wednesday: t.wednesday || "", - thursday: t.thursday || "", - friday: t.friday || "", - saturday: t.saturday || "", - sunday: t.sunday || "", - additional: t.additional || "", - }, - }; - }), + departments: doctor.departments.map((d) => ({ + departmentId: d.department.departmentId, + departmentName: d.department.name, + timing: d.timing || {}, + })), }; res.status(200).json({