450 lines
9.1 KiB
JavaScript
450 lines
9.1 KiB
JavaScript
|
|
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) => {
|
||
|
|
const departmentIds = doc.departments.map(
|
||
|
|
(d) => d.department.departmentId,
|
||
|
|
);
|
||
|
|
|
||
|
|
let timings = [];
|
||
|
|
|
||
|
|
doc.departments.forEach((d) => {
|
||
|
|
const t = d.timing;
|
||
|
|
|
||
|
|
if (!t) return;
|
||
|
|
|
||
|
|
if (t.monday) timings.push(`Monday ${t.monday}`);
|
||
|
|
if (t.tuesday) timings.push(`Tuesday ${t.tuesday}`);
|
||
|
|
if (t.wednesday) timings.push(`Wednesday ${t.wednesday}`);
|
||
|
|
if (t.thursday) timings.push(`Thursday ${t.thursday}`);
|
||
|
|
if (t.friday) timings.push(`Friday ${t.friday}`);
|
||
|
|
if (t.saturday) timings.push(`Saturday ${t.saturday}`);
|
||
|
|
if (t.sunday) timings.push(`Sunday ${t.sunday}`);
|
||
|
|
|
||
|
|
if (t.additional) timings.push(t.additional);
|
||
|
|
});
|
||
|
|
|
||
|
|
return {
|
||
|
|
SL_NO: String(index + 1),
|
||
|
|
Doctor_ID: doc.doctorId,
|
||
|
|
Department_ID: departmentIds,
|
||
|
|
Name: doc.name,
|
||
|
|
Designation: doc.designation,
|
||
|
|
"Working Status": doc.workingStatus,
|
||
|
|
Qualification: doc.qualification,
|
||
|
|
Timing: timings.join(" & "),
|
||
|
|
};
|
||
|
|
});
|
||
|
|
|
||
|
|
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 departments = doctor.departments.map(
|
||
|
|
(d) => d.department.departmentId,
|
||
|
|
);
|
||
|
|
|
||
|
|
const timing = doctor.departments[0]?.timing ?? {};
|
||
|
|
|
||
|
|
const response = {
|
||
|
|
doctorId: doctor.doctorId,
|
||
|
|
name: doctor.name,
|
||
|
|
designation: doctor.designation,
|
||
|
|
workingStatus: doctor.workingStatus,
|
||
|
|
qualification: doctor.qualification,
|
||
|
|
departments,
|
||
|
|
timing: {
|
||
|
|
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: 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,
|
||
|
|
timing,
|
||
|
|
} = req.body;
|
||
|
|
|
||
|
|
const doctor = await prisma.doctor.create({
|
||
|
|
data: {
|
||
|
|
doctorId,
|
||
|
|
name,
|
||
|
|
designation,
|
||
|
|
workingStatus,
|
||
|
|
qualification,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
for (const depId of departments) {
|
||
|
|
const department = await prisma.department.findUnique({
|
||
|
|
where: {departmentId: depId},
|
||
|
|
});
|
||
|
|
|
||
|
|
const doctorDepartment = await prisma.doctorDepartment.create({
|
||
|
|
data: {
|
||
|
|
doctorId: doctor.id,
|
||
|
|
departmentId: department.id,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
if (timing) {
|
||
|
|
await prisma.doctorTiming.create({
|
||
|
|
data: {
|
||
|
|
doctorDepartmentId: doctorDepartment.id,
|
||
|
|
...timing,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
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;
|
||
|
|
const {
|
||
|
|
name,
|
||
|
|
designation,
|
||
|
|
workingStatus,
|
||
|
|
qualification,
|
||
|
|
departments,
|
||
|
|
timing,
|
||
|
|
} = 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 with ID ${doctorId} not found`,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
const updatedDoctor = await prisma.doctor.update({
|
||
|
|
where: {id: doctor.id},
|
||
|
|
data: {
|
||
|
|
...(name && {name}),
|
||
|
|
...(designation && {designation}),
|
||
|
|
...(workingStatus && {workingStatus}),
|
||
|
|
...(qualification && {qualification}),
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
if (departments) {
|
||
|
|
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},
|
||
|
|
});
|
||
|
|
|
||
|
|
for (const depId of departments) {
|
||
|
|
const department = await prisma.department.findUnique({
|
||
|
|
where: {departmentId: depId},
|
||
|
|
});
|
||
|
|
if (!department) continue;
|
||
|
|
|
||
|
|
const doctorDepartment = await prisma.doctorDepartment.create({
|
||
|
|
data: {
|
||
|
|
doctorId: doctor.id,
|
||
|
|
departmentId: department.id,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
if (timing) {
|
||
|
|
await prisma.doctorTiming.create({
|
||
|
|
data: {
|
||
|
|
doctorDepartmentId: doctorDepartment.id,
|
||
|
|
...timing,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} else if (timing) {
|
||
|
|
const doctorDepartments = await prisma.doctorDepartment.findMany({
|
||
|
|
where: {doctorId: doctor.id},
|
||
|
|
});
|
||
|
|
|
||
|
|
for (const dd of doctorDepartments) {
|
||
|
|
await prisma.doctorTiming.upsert({
|
||
|
|
where: {doctorDepartmentId: dd.id},
|
||
|
|
update: {...timing},
|
||
|
|
create: {doctorDepartmentId: dd.id, ...timing},
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
res.status(200).json({
|
||
|
|
success: true,
|
||
|
|
message: `Doctor ${doctorId} updated successfully`,
|
||
|
|
data: updatedDoctor,
|
||
|
|
});
|
||
|
|
} 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: {
|
||
|
|
timing: true,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!doctor) {
|
||
|
|
return res.status(404).json({
|
||
|
|
success: false,
|
||
|
|
message: "Doctor not found",
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
let timing = {};
|
||
|
|
|
||
|
|
if (doctor.departments.length > 0) {
|
||
|
|
timing = doctor.departments[0].timing ?? {};
|
||
|
|
}
|
||
|
|
|
||
|
|
const result = {
|
||
|
|
Doctor_ID: doctor.doctorId,
|
||
|
|
Doctor: doctor.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 timing",
|
||
|
|
});
|
||
|
|
}
|
||
|
|
};
|