fix: doctor toggle logic

This commit is contained in:
Kailasdevdas
2026-05-13 14:19:42 +05:30
parent 199797fdf4
commit 988fbd28f1
3 changed files with 47 additions and 34 deletions
+36 -31
View File
@@ -273,42 +273,47 @@ export const updateDoctor = async (req, res) => {
}, },
}); });
const oldRelations = await prisma.doctorDepartment.findMany({ const hasTimingData = departments?.some(
where: {doctorId: doctor.id}, (dep) => dep.timing && Object.keys(dep.timing).length > 0,
}); );
for (const rel of oldRelations) { if (departments && Array.isArray(departments) && hasTimingData) {
await prisma.doctorTiming.deleteMany({ const oldRelations = await prisma.doctorDepartment.findMany({
where: {doctorDepartmentId: rel.id}, where: {doctorId: doctor.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) { for (const rel of oldRelations) {
const {id, doctorDepartmentId, createdAt, updatedAt, ...cleanTiming} = await prisma.doctorTiming.deleteMany({
dep.timing; where: {doctorDepartmentId: rel.id},
await prisma.doctorTiming.create({
data: {doctorDepartmentId: newDD.id, ...cleanTiming},
}); });
} }
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 res
+3 -1
View File
@@ -8,8 +8,10 @@ export interface Doctor {
designation?: string; designation?: string;
workingStatus?: string; workingStatus?: string;
qualification?: string; qualification?: string;
isActive: boolean;
globalSortOrder: number;
departments: { departments?: {
departmentId: string; departmentId: string;
timing?: { timing?: {
monday?: string; monday?: string;
+8 -2
View File
@@ -158,8 +158,14 @@ export default function DoctorPage() {
const handleToggleStatus = async (doc: any) => { const handleToggleStatus = async (doc: any) => {
try { try {
const updatedDoc = { ...doc, isActive: !doc.isActive }; const newStatus = !doc.isActive;
await updateDoctorApi(doc.doctorId, updatedDoc);
const payload = {
isActive: newStatus,
};
await updateDoctorApi(doc.doctorId, payload);
fetchAll(); fetchAll();
} catch (err) { } catch (err) {
console.error("Failed to update status", err); console.error("Failed to update status", err);