2026-03-13 14:54:47 +05:30
|
|
|
import prisma from "../prisma/client.js";
|
|
|
|
|
|
|
|
|
|
// get doctors
|
|
|
|
|
|
|
|
|
|
export const getAllDoctors = async (req, res) => {
|
|
|
|
|
try {
|
2026-05-11 00:04:22 +05:30
|
|
|
const {admin} = req.query;
|
|
|
|
|
|
2026-03-13 14:54:47 +05:30
|
|
|
const doctors = await prisma.doctor.findMany({
|
2026-05-11 00:04:22 +05:30
|
|
|
where: admin === "true" ? {} : {isActive: true},
|
2026-03-13 14:54:47 +05:30
|
|
|
include: {
|
2026-05-20 10:15:53 +05:30
|
|
|
seo: true,
|
2026-03-13 14:54:47 +05:30
|
|
|
departments: {
|
|
|
|
|
include: {
|
|
|
|
|
department: true,
|
|
|
|
|
timing: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-05-20 10:15:53 +05:30
|
|
|
specializations: {
|
|
|
|
|
orderBy: {
|
|
|
|
|
createdAt: "asc",
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-03-13 14:54:47 +05:30
|
|
|
},
|
2026-05-11 00:04:22 +05:30
|
|
|
orderBy: [{globalSortOrder: "asc"}, {name: "asc"}],
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
|
|
|
|
|
2026-04-08 16:40:42 +05:30
|
|
|
const formatted = doctors.map((doc, index) => ({
|
|
|
|
|
SL_NO: String(index + 1),
|
|
|
|
|
doctorId: doc.doctorId,
|
|
|
|
|
name: doc.name,
|
2026-04-14 17:33:21 +05:30
|
|
|
image: doc.image ?? "",
|
2026-04-08 16:40:42 +05:30
|
|
|
designation: doc.designation,
|
|
|
|
|
workingStatus: doc.workingStatus,
|
|
|
|
|
qualification: doc.qualification,
|
2026-05-11 00:04:22 +05:30
|
|
|
isActive: doc.isActive,
|
2026-05-20 10:15:53 +05:30
|
|
|
experience: doc.experience,
|
|
|
|
|
professionalSummary: doc.professionalSummary,
|
2026-05-11 00:04:22 +05:30
|
|
|
globalSortOrder: doc.globalSortOrder,
|
2026-05-20 10:15:53 +05:30
|
|
|
specializations: doc.specializations.map((item) => ({
|
|
|
|
|
id: item.id,
|
|
|
|
|
name: item.name,
|
|
|
|
|
description: item.description,
|
|
|
|
|
})),
|
|
|
|
|
seo: {
|
|
|
|
|
seoTitle: doc.seo?.seoTitle ?? "",
|
|
|
|
|
metaDescription: doc.seo?.metaDescription ?? "",
|
|
|
|
|
focusKeyphrase: doc.seo?.focusKeyphrase ?? "",
|
|
|
|
|
slug: doc.seo?.slug ?? "",
|
|
|
|
|
tags: doc.seo?.tags ?? [],
|
|
|
|
|
|
|
|
|
|
ogTitle: doc.seo?.ogTitle ?? "",
|
|
|
|
|
ogDescription: doc.seo?.ogDescription ?? "",
|
|
|
|
|
ogImage: doc.seo?.ogImage ?? "",
|
|
|
|
|
},
|
2026-04-08 16:40:42 +05:30
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
departmentId: d.department.departmentId,
|
|
|
|
|
departmentName: d.department.name,
|
|
|
|
|
timing: timingArray.join(" & "),
|
2026-05-11 00:04:22 +05:30
|
|
|
deptSortOrder: d.sortOrder,
|
2026-04-08 16:40:42 +05:30
|
|
|
};
|
|
|
|
|
}),
|
|
|
|
|
}));
|
2026-03-13 14:54:47 +05:30
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
data: formatted,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
success: false,
|
|
|
|
|
message: "Failed to fetch doctors",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// get doctor by id
|
|
|
|
|
|
|
|
|
|
export const getDoctorByDoctorId = async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const {doctorId} = req.params;
|
|
|
|
|
|
|
|
|
|
const doctor = await prisma.doctor.findUnique({
|
|
|
|
|
where: {doctorId},
|
|
|
|
|
include: {
|
2026-05-20 10:15:53 +05:30
|
|
|
seo: true,
|
|
|
|
|
specializations: true,
|
2026-03-13 14:54:47 +05:30
|
|
|
departments: {
|
|
|
|
|
include: {
|
|
|
|
|
department: true,
|
|
|
|
|
timing: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!doctor) {
|
|
|
|
|
return res.status(404).json({
|
|
|
|
|
success: false,
|
|
|
|
|
message: "Doctor not found",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const response = {
|
|
|
|
|
doctorId: doctor.doctorId,
|
|
|
|
|
name: doctor.name,
|
2026-04-14 17:33:21 +05:30
|
|
|
image: doctor.image ?? "",
|
2026-03-13 14:54:47 +05:30
|
|
|
designation: doctor.designation,
|
|
|
|
|
workingStatus: doctor.workingStatus,
|
|
|
|
|
qualification: doctor.qualification,
|
2026-05-20 10:15:53 +05:30
|
|
|
experience: doctor.experience,
|
|
|
|
|
professionalSummary: doctor.professionalSummary,
|
|
|
|
|
seo: {
|
|
|
|
|
seoTitle: doctor.seo?.seoTitle ?? "",
|
|
|
|
|
metaDescription: doctor.seo?.metaDescription ?? "",
|
|
|
|
|
focusKeyphrase: doctor.seo?.focusKeyphrase ?? "",
|
|
|
|
|
slug: doctor.seo?.slug ?? "",
|
|
|
|
|
tags: doctor.seo?.tags ?? [],
|
|
|
|
|
ogTitle: doctor.seo?.ogTitle ?? "",
|
|
|
|
|
ogDescription: doctor.seo?.ogDescription ?? "",
|
|
|
|
|
ogImage: doctor.seo?.ogImage ?? "",
|
|
|
|
|
},
|
|
|
|
|
specializations:
|
|
|
|
|
doctor.specializations?.map((item) => ({
|
|
|
|
|
id: item.id,
|
|
|
|
|
name: item.name,
|
|
|
|
|
description: item.description,
|
|
|
|
|
})) ?? [],
|
2026-03-17 16:22:37 +05:30
|
|
|
departments: doctor.departments.map((d) => ({
|
|
|
|
|
departmentId: d.department.departmentId,
|
|
|
|
|
departmentName: d.department.name,
|
|
|
|
|
timing: d.timing || {},
|
|
|
|
|
})),
|
2026-03-13 14:54:47 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
data: response,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
success: false,
|
|
|
|
|
message: "Failed to fetch doctor",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-08 16:40:42 +05:30
|
|
|
// 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",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-11 00:04:22 +05:30
|
|
|
const doctorsInDept = await prisma.doctorDepartment.findMany({
|
|
|
|
|
where: {
|
|
|
|
|
departmentId: department.id,
|
|
|
|
|
doctor: {isActive: true},
|
|
|
|
|
},
|
2026-04-08 16:40:42 +05:30
|
|
|
include: {
|
|
|
|
|
doctor: true,
|
|
|
|
|
},
|
2026-05-11 00:04:22 +05:30
|
|
|
orderBy: {sortOrder: "asc"},
|
2026-04-08 16:40:42 +05:30
|
|
|
});
|
|
|
|
|
|
2026-05-11 00:04:22 +05:30
|
|
|
const result = doctorsInDept.map((d) => ({
|
2026-04-08 16:40:42 +05:30
|
|
|
GG_ID: d.doctor.doctorId,
|
|
|
|
|
Name: d.doctor.name,
|
2026-04-16 11:17:42 +05:30
|
|
|
image: d.doctor.image ?? "",
|
2026-05-11 00:04:22 +05:30
|
|
|
designation: d.doctor.designation,
|
|
|
|
|
hierarchyOrder: d.sortOrder,
|
2026-04-08 16:40:42 +05:30
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
data: result,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
success: false,
|
|
|
|
|
message: "Failed to fetch doctors",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-13 14:54:47 +05:30
|
|
|
// add doctors
|
|
|
|
|
export const createDoctor = async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const {
|
|
|
|
|
doctorId,
|
|
|
|
|
name,
|
2026-04-14 17:33:21 +05:30
|
|
|
image,
|
2026-03-13 14:54:47 +05:30
|
|
|
designation,
|
|
|
|
|
workingStatus,
|
|
|
|
|
qualification,
|
2026-05-11 00:04:22 +05:30
|
|
|
isActive,
|
|
|
|
|
globalSortOrder,
|
2026-03-13 14:54:47 +05:30
|
|
|
departments,
|
2026-05-20 10:15:53 +05:30
|
|
|
experience,
|
|
|
|
|
professionalSummary,
|
|
|
|
|
seoTitle,
|
|
|
|
|
metaDescription,
|
|
|
|
|
focusKeyphrase,
|
|
|
|
|
slug,
|
|
|
|
|
tags,
|
|
|
|
|
specializations,
|
|
|
|
|
ogTitle,
|
|
|
|
|
ogDescription,
|
|
|
|
|
ogImage,
|
2026-03-13 14:54:47 +05:30
|
|
|
} = req.body;
|
2026-05-20 10:15:53 +05:30
|
|
|
const seo = await prisma.seo.create({
|
|
|
|
|
data: {
|
|
|
|
|
seoTitle,
|
|
|
|
|
metaDescription,
|
|
|
|
|
focusKeyphrase,
|
|
|
|
|
slug: slug ? slug : null,
|
|
|
|
|
tags: tags || [],
|
|
|
|
|
|
|
|
|
|
// Open Graph
|
|
|
|
|
ogTitle,
|
|
|
|
|
ogDescription,
|
|
|
|
|
ogImage,
|
|
|
|
|
},
|
|
|
|
|
});
|
2026-03-13 14:54:47 +05:30
|
|
|
|
|
|
|
|
const doctor = await prisma.doctor.create({
|
|
|
|
|
data: {
|
|
|
|
|
doctorId,
|
|
|
|
|
name,
|
2026-04-14 17:33:21 +05:30
|
|
|
image,
|
2026-03-13 14:54:47 +05:30
|
|
|
designation,
|
|
|
|
|
workingStatus,
|
|
|
|
|
qualification,
|
2026-05-20 10:15:53 +05:30
|
|
|
experience: experience ? Number(experience) : null,
|
|
|
|
|
professionalSummary,
|
|
|
|
|
seoId: seo.id,
|
2026-05-11 00:04:22 +05:30
|
|
|
isActive: isActive !== undefined ? isActive : true,
|
|
|
|
|
globalSortOrder:
|
|
|
|
|
globalSortOrder !== undefined ? Number(globalSortOrder) : 0,
|
2026-03-13 14:54:47 +05:30
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2026-03-17 16:22:37 +05:30
|
|
|
for (const dep of departments) {
|
2026-03-13 14:54:47 +05:30
|
|
|
const department = await prisma.department.findUnique({
|
2026-03-17 16:22:37 +05:30
|
|
|
where: {departmentId: dep.departmentId},
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
|
|
|
|
|
2026-03-17 16:22:37 +05:30
|
|
|
if (!department) continue;
|
|
|
|
|
|
2026-03-13 14:54:47 +05:30
|
|
|
const doctorDepartment = await prisma.doctorDepartment.create({
|
|
|
|
|
data: {
|
|
|
|
|
doctorId: doctor.id,
|
|
|
|
|
departmentId: department.id,
|
2026-05-11 00:04:22 +05:30
|
|
|
sortOrder: dep.sortOrder !== undefined ? Number(dep.sortOrder) : 0,
|
2026-03-13 14:54:47 +05:30
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2026-03-17 16:22:37 +05:30
|
|
|
if (dep.timing) {
|
2026-03-13 14:54:47 +05:30
|
|
|
await prisma.doctorTiming.create({
|
|
|
|
|
data: {
|
|
|
|
|
doctorDepartmentId: doctorDepartment.id,
|
2026-03-17 16:22:37 +05:30
|
|
|
...dep.timing,
|
2026-03-13 14:54:47 +05:30
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-05-20 10:15:53 +05:30
|
|
|
if (specializations?.length) {
|
|
|
|
|
await prisma.doctorSpecialization.createMany({
|
|
|
|
|
data: specializations
|
|
|
|
|
.filter((item) => item.name?.trim())
|
|
|
|
|
.map((item) => ({
|
|
|
|
|
name: item.name.trim(),
|
|
|
|
|
description: item.description?.trim() || null,
|
|
|
|
|
doctorId: doctor.id,
|
|
|
|
|
})),
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-03-13 14:54:47 +05:30
|
|
|
|
|
|
|
|
res.status(201).json({
|
|
|
|
|
success: true,
|
|
|
|
|
message: "Doctor created successfully",
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
success: false,
|
|
|
|
|
message: "Failed to create doctor",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//update doctors
|
|
|
|
|
export const updateDoctor = async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const {doctorId} = req.params;
|
2026-04-14 17:33:21 +05:30
|
|
|
const {
|
|
|
|
|
name,
|
|
|
|
|
designation,
|
|
|
|
|
image,
|
|
|
|
|
workingStatus,
|
|
|
|
|
qualification,
|
2026-05-11 00:04:22 +05:30
|
|
|
isActive,
|
|
|
|
|
globalSortOrder,
|
2026-04-14 17:33:21 +05:30
|
|
|
departments,
|
2026-05-20 10:15:53 +05:30
|
|
|
experience,
|
|
|
|
|
professionalSummary,
|
|
|
|
|
seoTitle,
|
|
|
|
|
metaDescription,
|
|
|
|
|
focusKeyphrase,
|
|
|
|
|
slug,
|
|
|
|
|
tags,
|
|
|
|
|
specializations,
|
2026-04-14 17:33:21 +05:30
|
|
|
} = req.body;
|
2026-03-13 14:54:47 +05:30
|
|
|
|
2026-05-11 00:04:22 +05:30
|
|
|
const doctor = await prisma.doctor.findUnique({where: {doctorId}});
|
|
|
|
|
if (!doctor)
|
2026-04-08 16:40:42 +05:30
|
|
|
return res
|
|
|
|
|
.status(404)
|
|
|
|
|
.json({success: false, message: "Doctor not found"});
|
2026-03-13 14:54:47 +05:30
|
|
|
|
2026-03-17 16:22:37 +05:30
|
|
|
await prisma.doctor.update({
|
2026-03-13 14:54:47 +05:30
|
|
|
where: {id: doctor.id},
|
2026-05-11 00:04:22 +05:30
|
|
|
data: {
|
|
|
|
|
name,
|
|
|
|
|
designation,
|
|
|
|
|
image,
|
|
|
|
|
workingStatus,
|
|
|
|
|
qualification,
|
|
|
|
|
isActive,
|
2026-05-20 10:15:53 +05:30
|
|
|
experience: experience ? Number(experience) : null,
|
|
|
|
|
professionalSummary,
|
2026-05-11 00:04:22 +05:30
|
|
|
globalSortOrder:
|
|
|
|
|
globalSortOrder !== undefined ? Number(globalSortOrder) : undefined,
|
|
|
|
|
},
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
2026-05-20 10:43:05 +05:30
|
|
|
const existingDepartments = await prisma.doctorDepartment.findMany({
|
|
|
|
|
where: {
|
|
|
|
|
doctorId: doctor.id,
|
|
|
|
|
},
|
|
|
|
|
include: {
|
|
|
|
|
timing: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-20 10:28:46 +05:30
|
|
|
for (const dep of departments) {
|
|
|
|
|
const department = await prisma.department.findUnique({
|
|
|
|
|
where: {departmentId: dep.departmentId},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!department) continue;
|
|
|
|
|
|
2026-05-20 10:43:05 +05:30
|
|
|
const existing = existingDepartments.find(
|
|
|
|
|
(d) => d.departmentId === department.id,
|
|
|
|
|
);
|
2026-05-20 10:28:46 +05:30
|
|
|
|
2026-05-20 10:43:05 +05:30
|
|
|
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({
|
2026-05-20 10:28:46 +05:30
|
|
|
data: {
|
2026-05-20 10:43:05 +05:30
|
|
|
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,
|
2026-05-20 10:28:46 +05:30
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-05-20 10:43:05 +05:30
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-05-20 10:28:46 +05:30
|
|
|
}
|
2026-05-20 10:15:53 +05:30
|
|
|
if (doctor.seoId) {
|
|
|
|
|
await prisma.seo.update({
|
|
|
|
|
where: {
|
|
|
|
|
id: doctor.seoId,
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
seoTitle,
|
|
|
|
|
metaDescription,
|
|
|
|
|
focusKeyphrase,
|
|
|
|
|
slug: slug ? slug : null,
|
|
|
|
|
tags: tags || [],
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
const seo = await prisma.seo.create({
|
|
|
|
|
data: {
|
|
|
|
|
seoTitle,
|
|
|
|
|
metaDescription,
|
|
|
|
|
focusKeyphrase,
|
|
|
|
|
slug: slug ? slug : null,
|
|
|
|
|
tags: tags || [],
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await prisma.doctor.update({
|
|
|
|
|
where: {
|
|
|
|
|
id: doctor.id,
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
seoId: seo.id,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-03-13 14:54:47 +05:30
|
|
|
|
2026-05-13 14:19:42 +05:30
|
|
|
const hasTimingData = departments?.some(
|
|
|
|
|
(dep) => dep.timing && Object.keys(dep.timing).length > 0,
|
|
|
|
|
);
|
2026-03-17 16:22:37 +05:30
|
|
|
|
2026-05-20 10:15:53 +05:30
|
|
|
// Update Specializations
|
|
|
|
|
if (Array.isArray(specializations)) {
|
|
|
|
|
await prisma.doctorSpecialization.deleteMany({
|
|
|
|
|
where: {
|
|
|
|
|
doctorId: doctor.id,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (specializations.length) {
|
|
|
|
|
await prisma.doctorSpecialization.createMany({
|
|
|
|
|
data: specializations
|
|
|
|
|
.filter((item) => item.name?.trim())
|
|
|
|
|
.map((item) => ({
|
|
|
|
|
name: item.name.trim(),
|
|
|
|
|
description: item.description?.trim() || null,
|
|
|
|
|
doctorId: doctor.id,
|
|
|
|
|
})),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-13 14:19:42 +05:30
|
|
|
if (departments && Array.isArray(departments) && hasTimingData) {
|
|
|
|
|
const oldRelations = await prisma.doctorDepartment.findMany({
|
|
|
|
|
where: {doctorId: doctor.id},
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
|
|
|
|
|
2026-05-13 14:19:42 +05:30
|
|
|
for (const rel of oldRelations) {
|
|
|
|
|
await prisma.doctorTiming.deleteMany({
|
|
|
|
|
where: {doctorDepartmentId: rel.id},
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-03-13 14:54:47 +05:30
|
|
|
|
2026-05-13 14:19:42 +05:30
|
|
|
await prisma.doctorDepartment.deleteMany({
|
|
|
|
|
where: {doctorId: doctor.id},
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
|
|
|
|
|
2026-05-13 14:19:42 +05:30
|
|
|
for (const dep of departments) {
|
|
|
|
|
const targetDept = await prisma.department.findUnique({
|
|
|
|
|
where: {departmentId: dep.departmentId},
|
|
|
|
|
});
|
|
|
|
|
if (!targetDept) continue;
|
2026-04-08 16:40:42 +05:30
|
|
|
|
2026-05-13 14:19:42 +05:30
|
|
|
const newDD = await prisma.doctorDepartment.create({
|
|
|
|
|
data: {
|
|
|
|
|
doctorId: doctor.id,
|
|
|
|
|
departmentId: targetDept.id,
|
|
|
|
|
sortOrder: dep.sortOrder !== undefined ? Number(dep.sortOrder) : 0,
|
|
|
|
|
},
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
2026-05-13 14:19:42 +05:30
|
|
|
|
|
|
|
|
if (dep.timing) {
|
|
|
|
|
const {id, doctorDepartmentId, createdAt, updatedAt, ...cleanTiming} =
|
|
|
|
|
dep.timing;
|
|
|
|
|
await prisma.doctorTiming.create({
|
|
|
|
|
data: {doctorDepartmentId: newDD.id, ...cleanTiming},
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-03-13 14:54:47 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-08 16:40:42 +05:30
|
|
|
res
|
|
|
|
|
.status(200)
|
|
|
|
|
.json({success: true, message: "Doctor updated successfully"});
|
2026-03-13 14:54:47 +05:30
|
|
|
} catch (error) {
|
2026-04-08 16:40:42 +05:30
|
|
|
console.error("Update Error:", error);
|
|
|
|
|
res.status(500).json({success: false, message: "Failed to update doctor"});
|
2026-03-13 14:54:47 +05:30
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
//delete doctor
|
|
|
|
|
|
|
|
|
|
export const deleteDoctor = async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const {doctorId} = req.params;
|
|
|
|
|
|
|
|
|
|
const doctor = await prisma.doctor.findUnique({
|
|
|
|
|
where: {doctorId},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!doctor) {
|
|
|
|
|
return res.status(404).json({
|
|
|
|
|
success: false,
|
2026-04-08 16:40:42 +05:30
|
|
|
message: "Doctor not found",
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const doctorDepartments = await prisma.doctorDepartment.findMany({
|
|
|
|
|
where: {doctorId: doctor.id},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for (const dd of doctorDepartments) {
|
|
|
|
|
await prisma.doctorTiming.deleteMany({
|
|
|
|
|
where: {doctorDepartmentId: dd.id},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await prisma.doctorDepartment.deleteMany({
|
|
|
|
|
where: {doctorId: doctor.id},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await prisma.doctor.delete({
|
|
|
|
|
where: {id: doctor.id},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
success: true,
|
2026-04-08 16:40:42 +05:30
|
|
|
message: "Doctor deleted successfully",
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
success: false,
|
|
|
|
|
message: "Failed to delete doctor",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// get doctor timings
|
|
|
|
|
|
|
|
|
|
export const getDoctorTimings = async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const doctors = await prisma.doctor.findMany({
|
|
|
|
|
include: {
|
|
|
|
|
departments: {
|
|
|
|
|
include: {
|
|
|
|
|
timing: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const result = doctors.map((doc) => {
|
2026-04-08 16:40:42 +05:30
|
|
|
const timing = doc.departments[0]?.timing || {};
|
2026-03-13 14:54:47 +05:30
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
Doctor_ID: doc.doctorId,
|
|
|
|
|
Doctor: doc.name,
|
2026-04-08 16:40:42 +05:30
|
|
|
Monday: timing.monday || "",
|
|
|
|
|
Tuesday: timing.tuesday || "",
|
|
|
|
|
Wednesday: timing.wednesday || "",
|
|
|
|
|
Thursday: timing.thursday || "",
|
|
|
|
|
Friday: timing.friday || "",
|
|
|
|
|
Saturday: timing.saturday || "",
|
|
|
|
|
Sunday: timing.sunday || "",
|
|
|
|
|
Additional: timing.additional || "",
|
2026-03-13 14:54:47 +05:30
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
data: result,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
success: false,
|
|
|
|
|
message: "Failed to fetch doctor timings",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
// get doctor timings by id
|
|
|
|
|
|
|
|
|
|
export const getDoctorTimingById = async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const {doctorId} = req.params;
|
|
|
|
|
|
|
|
|
|
const doctor = await prisma.doctor.findUnique({
|
|
|
|
|
where: {doctorId},
|
|
|
|
|
include: {
|
|
|
|
|
departments: {
|
|
|
|
|
include: {
|
2026-03-17 16:22:37 +05:30
|
|
|
department: true,
|
2026-03-13 14:54:47 +05:30
|
|
|
timing: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!doctor) {
|
|
|
|
|
return res.status(404).json({
|
|
|
|
|
success: false,
|
|
|
|
|
message: "Doctor not found",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = {
|
2026-03-17 16:22:37 +05:30
|
|
|
doctorId: doctor.doctorId,
|
|
|
|
|
doctorName: doctor.name,
|
2026-04-08 16:40:42 +05:30
|
|
|
departments: doctor.departments.map((d) => ({
|
|
|
|
|
departmentId: d.department.departmentId,
|
|
|
|
|
departmentName: d.department.name,
|
2026-05-11 00:04:22 +05:30
|
|
|
deptSortOrder: d.sortOrder,
|
2026-04-08 16:40:42 +05:30
|
|
|
timing: d.timing || {},
|
|
|
|
|
})),
|
2026-03-13 14:54:47 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
data: result,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
success: false,
|
|
|
|
|
message: "Failed to fetch doctor timing",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|