2026-03-13 14:54:47 +05:30
|
|
|
import prisma from "../prisma/client.js";
|
|
|
|
|
|
|
|
|
|
// get doctors
|
|
|
|
|
|
|
|
|
|
export const getAllDoctors = async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const doctors = await prisma.doctor.findMany({
|
|
|
|
|
include: {
|
|
|
|
|
departments: {
|
|
|
|
|
include: {
|
|
|
|
|
department: true,
|
|
|
|
|
timing: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
orderBy: {name: "asc"},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const formatted = doctors.map((doc, index) => {
|
|
|
|
|
return {
|
|
|
|
|
SL_NO: String(index + 1),
|
2026-03-17 16:22:37 +05:30
|
|
|
doctorId: doc.doctorId,
|
|
|
|
|
name: doc.name,
|
|
|
|
|
designation: doc.designation,
|
|
|
|
|
workingStatus: doc.workingStatus,
|
|
|
|
|
qualification: doc.qualification,
|
|
|
|
|
|
|
|
|
|
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-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,
|
|
|
|
|
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",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// add doctors
|
|
|
|
|
export const createDoctor = async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const {
|
|
|
|
|
doctorId,
|
|
|
|
|
name,
|
|
|
|
|
designation,
|
|
|
|
|
workingStatus,
|
|
|
|
|
qualification,
|
|
|
|
|
departments,
|
|
|
|
|
} = req.body;
|
|
|
|
|
|
|
|
|
|
const doctor = await prisma.doctor.create({
|
|
|
|
|
data: {
|
|
|
|
|
doctorId,
|
|
|
|
|
name,
|
|
|
|
|
designation,
|
|
|
|
|
workingStatus,
|
|
|
|
|
qualification,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
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-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-03-17 16:22:37 +05:30
|
|
|
const {name, designation, workingStatus, qualification, departments} =
|
|
|
|
|
req.body;
|
2026-03-13 14:54:47 +05:30
|
|
|
|
|
|
|
|
const doctor = await prisma.doctor.findUnique({
|
|
|
|
|
where: {doctorId},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!doctor) {
|
|
|
|
|
return res.status(404).json({
|
|
|
|
|
success: false,
|
2026-03-17 16:22:37 +05:30
|
|
|
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},
|
|
|
|
|
data: {
|
2026-03-17 16:22:37 +05:30
|
|
|
name,
|
|
|
|
|
designation,
|
|
|
|
|
workingStatus,
|
|
|
|
|
qualification,
|
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) {
|
|
|
|
|
const department = await prisma.department.findUnique({
|
|
|
|
|
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
|
|
|
|
2026-03-17 16:22:37 +05:30
|
|
|
const doctorDepartment = await prisma.doctorDepartment.create({
|
|
|
|
|
data: {
|
|
|
|
|
doctorId: doctor.id,
|
|
|
|
|
departmentId: department.id,
|
|
|
|
|
},
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
|
|
|
|
|
2026-03-17 16:22:37 +05:30
|
|
|
if (dep.timing) {
|
|
|
|
|
await prisma.doctorTiming.create({
|
|
|
|
|
data: {
|
|
|
|
|
doctorDepartmentId: doctorDepartment.id,
|
|
|
|
|
...dep.timing,
|
|
|
|
|
},
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
success: true,
|
2026-03-17 16:22:37 +05:30
|
|
|
message: "Doctor updated successfully",
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
success: false,
|
|
|
|
|
message: "Failed to update doctor",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
//delete doctor
|
|
|
|
|
|
|
|
|
|
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},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!doctor) {
|
|
|
|
|
return res.status(404).json({
|
|
|
|
|
success: false,
|
|
|
|
|
message: `Doctor with ID ${doctorId} not found`,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
message: `Doctor ${doctorId} deleted successfully`,
|
|
|
|
|
});
|
|
|
|
|
} 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) => {
|
|
|
|
|
let timing = {};
|
|
|
|
|
|
|
|
|
|
if (doc.departments.length > 0) {
|
|
|
|
|
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 ?? "",
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
|
|
|
|
|
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 || "",
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}),
|
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",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|