diff --git a/backend/src/controllers/doctor.controller.js b/backend/src/controllers/doctor.controller.js index 99e89ad..ae4cdca 100644 --- a/backend/src/controllers/doctor.controller.js +++ b/backend/src/controllers/doctor.controller.js @@ -340,7 +340,7 @@ export const createDoctor = async (req, res) => { //update doctors export const updateDoctor = async (req, res) => { try { - const {doctorId} = req.params; + const {doctorId, action} = req.params; const { name, designation, @@ -359,6 +359,33 @@ export const updateDoctor = async (req, res) => { tags, specializations, } = req.body; + if (!doctorId) { + return res.status(400).json({ + success: false, + message: "Doctor ID is required", + }); + } + const doctor = await prisma.doctor.findUnique({where: {doctorId}}); + if (!doctor) + return res + .status(404) + .json({success: false, message: "Doctor not found"}); + if (action === "toggleStatus") { + await prisma.doctor.update({ + where: {id: doctor.id}, + data: { + isActive: !doctor.isActive, + }, + }); + + return res.status(200).json({ + success: true, + message: `Doctor has been ${ + doctor.isActive ? "deactivated" : "activated" + } successfully`, + }); + } + const messages = []; if (!doctorId) messages.push("Doctor ID is required"); if (!name?.trim()) messages.push("Doctor name is required"); @@ -375,11 +402,6 @@ export const updateDoctor = async (req, res) => { message: messages.join(", "), }); } - const doctor = await prisma.doctor.findUnique({where: {doctorId}}); - if (!doctor) - return res - .status(404) - .json({success: false, message: "Doctor not found"}); await prisma.doctor.update({ where: {id: doctor.id}, @@ -396,74 +418,7 @@ export const updateDoctor = async (req, res) => { globalSortOrder !== undefined ? Number(globalSortOrder) : undefined, }, }); - const existingDepartments = await prisma.doctorDepartment.findMany({ - where: { - doctorId: doctor.id, - }, - include: { - timing: true, - }, - }); - for (const dep of departments) { - const department = await prisma.department.findUnique({ - where: {departmentId: dep.departmentId}, - }); - - if (!department) continue; - - const existing = existingDepartments.find( - (d) => d.departmentId === department.id, - ); - - const newSortOrder = - dep.sortOrder !== undefined ? Number(dep.sortOrder) : 0; - - const isSameDepartment = existing && existing.sortOrder === newSortOrder; - - const isSameTiming = - JSON.stringify(existing?.timing || {}) === - JSON.stringify(dep.timing || {}); - - if (isSameDepartment && isSameTiming) { - continue; - } - - let doctorDepartment = existing; - - if (!existing) { - doctorDepartment = await prisma.doctorDepartment.create({ - data: { - doctorId: doctor.id, - departmentId: department.id, - sortOrder: newSortOrder, - }, - }); - } else if (existing.sortOrder !== newSortOrder) { - doctorDepartment = await prisma.doctorDepartment.update({ - where: {id: existing.id}, - data: { - sortOrder: newSortOrder, - }, - }); - } - - if (dep.timing) { - if (existing?.timing) { - await prisma.doctorTiming.update({ - where: {id: existing.timing.id}, - data: dep.timing, - }); - } else { - await prisma.doctorTiming.create({ - data: { - doctorDepartmentId: doctorDepartment.id, - ...dep.timing, - }, - }); - } - } - } if (doctor.seoId) { await prisma.seo.update({ where: { @@ -498,9 +453,66 @@ export const updateDoctor = async (req, res) => { }); } - const hasTimingData = departments?.some( - (dep) => dep.timing && Object.keys(dep.timing).length > 0, - ); + // Update Departments & Timings + if (Array.isArray(departments)) { + const oldRelations = await prisma.doctorDepartment.findMany({ + where: { + doctorId: doctor.id, + }, + include: { + timing: true, + }, + }); + + // Delete old timings + for (const rel of oldRelations) { + if (rel.timing) { + await prisma.doctorTiming.deleteMany({ + where: { + doctorDepartmentId: rel.id, + }, + }); + } + } + + // Delete old departments + await prisma.doctorDepartment.deleteMany({ + where: { + doctorId: doctor.id, + }, + }); + + // Recreate departments + timings + for (const dep of departments) { + const department = await prisma.department.findUnique({ + where: { + departmentId: dep.departmentId, + }, + }); + + if (!department) continue; + + const doctorDepartment = await prisma.doctorDepartment.create({ + data: { + doctorId: doctor.id, + departmentId: department.id, + sortOrder: dep.sortOrder !== undefined ? Number(dep.sortOrder) : 0, + }, + }); + + if (dep.timing && Object.keys(dep.timing).length > 0) { + const {id, doctorDepartmentId, createdAt, updatedAt, ...cleanTiming} = + dep.timing; + + await prisma.doctorTiming.create({ + data: { + doctorDepartmentId: doctorDepartment.id, + ...cleanTiming, + }, + }); + } + } + } // Update Specializations if (Array.isArray(specializations)) { @@ -523,45 +535,6 @@ export const updateDoctor = async (req, res) => { } } - if (departments && Array.isArray(departments) && hasTimingData) { - const oldRelations = await prisma.doctorDepartment.findMany({ - where: {doctorId: doctor.id}, - }); - - for (const rel of oldRelations) { - await prisma.doctorTiming.deleteMany({ - where: {doctorDepartmentId: rel.id}, - }); - } - - await prisma.doctorDepartment.deleteMany({ - where: {doctorId: doctor.id}, - }); - - for (const dep of departments) { - const targetDept = await prisma.department.findUnique({ - where: {departmentId: dep.departmentId}, - }); - if (!targetDept) continue; - - const newDD = await prisma.doctorDepartment.create({ - data: { - doctorId: doctor.id, - departmentId: targetDept.id, - sortOrder: dep.sortOrder !== undefined ? Number(dep.sortOrder) : 0, - }, - }); - - if (dep.timing) { - const {id, doctorDepartmentId, createdAt, updatedAt, ...cleanTiming} = - dep.timing; - await prisma.doctorTiming.create({ - data: {doctorDepartmentId: newDD.id, ...cleanTiming}, - }); - } - } - } - res .status(200) .json({success: true, message: "Doctor updated successfully"}); diff --git a/backend/src/routes/doctor.routes.js b/backend/src/routes/doctor.routes.js index c8309c1..01b6601 100644 --- a/backend/src/routes/doctor.routes.js +++ b/backend/src/routes/doctor.routes.js @@ -21,7 +21,7 @@ router.get("/getTimings/:doctorId", getDoctorTimingById); router.get("/:doctorId", getDoctorByDoctorId); router.post("/", jwtAuthMiddleware, createDoctor); -router.patch("/:doctorId", jwtAuthMiddleware, updateDoctor); +router.patch("/:doctorId/:action", jwtAuthMiddleware, updateDoctor); router.delete("/:doctorId", jwtAuthMiddleware, deleteDoctor); export default router; diff --git a/frontend/src/api/doctor.ts b/frontend/src/api/doctor.ts index d7ae82f..940a3f1 100644 --- a/frontend/src/api/doctor.ts +++ b/frontend/src/api/doctor.ts @@ -53,9 +53,10 @@ export const createDoctorApi = async (data: Doctor) => { export const updateDoctorApi = async ( doctorId: string, data: Partial, + action: "toggleStatus" | "updateDetails" = "updateDetails", ) => { try { - const res = await apiClient.patch(`/doctors/${doctorId}`, data); + const res = await apiClient.patch(`/doctors/${doctorId}/${action}`, data); toast.success("Doctor updated successfully"); diff --git a/frontend/src/pages/Doctor.tsx b/frontend/src/pages/Doctor.tsx index df7a7ab..b84ee08 100644 --- a/frontend/src/pages/Doctor.tsx +++ b/frontend/src/pages/Doctor.tsx @@ -188,7 +188,7 @@ export default function DoctorPage() { isActive: newStatus, }; - await updateDoctorApi(doc.doctorId, payload); + await updateDoctorApi(doc.doctorId, payload, "toggleStatus"); fetchAll(); } catch (err) {