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: {
|
|
|
|
|
departments: {
|
|
|
|
|
include: {
|
|
|
|
|
department: true,
|
|
|
|
|
timing: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
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,
|
|
|
|
|
globalSortOrder: doc.globalSortOrder,
|
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: {
|
|
|
|
|
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-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,
|
|
|
|
|
} = req.body;
|
|
|
|
|
|
|
|
|
|
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-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
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
} = 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,
|
|
|
|
|
globalSortOrder:
|
|
|
|
|
globalSortOrder !== undefined ? Number(globalSortOrder) : undefined,
|
|
|
|
|
},
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
|
|
|
|
|
2026-03-17 16:22:37 +05:30
|
|
|
const oldRelations = await prisma.doctorDepartment.findMany({
|
|
|
|
|
where: {doctorId: doctor.id},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for (const rel of oldRelations) {
|
|
|
|
|
await prisma.doctorTiming.deleteMany({
|
|
|
|
|
where: {doctorDepartmentId: rel.id},
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
2026-03-17 16:22:37 +05:30
|
|
|
}
|
2026-03-13 14:54:47 +05:30
|
|
|
|
2026-03-17 16:22:37 +05:30
|
|
|
await prisma.doctorDepartment.deleteMany({
|
|
|
|
|
where: {doctorId: doctor.id},
|
|
|
|
|
});
|
2026-03-13 14:54:47 +05:30
|
|
|
|
2026-03-17 16:22:37 +05:30
|
|
|
for (const dep of departments) {
|
2026-05-11 00:04:22 +05:30
|
|
|
const targetDept = 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-05-11 00:04:22 +05:30
|
|
|
if (!targetDept) continue;
|
2026-03-13 14:54:47 +05:30
|
|
|
|
2026-05-11 00:04:22 +05:30
|
|
|
const newDD = await prisma.doctorDepartment.create({
|
2026-03-17 16:22:37 +05:30
|
|
|
data: {
|
|
|
|
|
doctorId: doctor.id,
|
2026-05-11 00:04:22 +05:30
|
|
|
departmentId: targetDept.id,
|
|
|
|
|
sortOrder: dep.sortOrder !== undefined ? Number(dep.sortOrder) : 0,
|
2026-03-17 16:22:37 +05:30
|
|
|
},
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
|
|
|
|
|
2026-03-17 16:22:37 +05:30
|
|
|
if (dep.timing) {
|
2026-04-08 16:40:42 +05:30
|
|
|
const {id, doctorDepartmentId, createdAt, updatedAt, ...cleanTiming} =
|
|
|
|
|
dep.timing;
|
|
|
|
|
|
2026-03-17 16:22:37 +05:30
|
|
|
await prisma.doctorTiming.create({
|
2026-05-11 00:04:22 +05:30
|
|
|
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",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|