feat: department wise timing
This commit is contained in:
@@ -17,37 +17,35 @@ export const getAllDoctors = async (req, res) => {
|
||||
});
|
||||
|
||||
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(" & "),
|
||||
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(" & "),
|
||||
};
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
@@ -89,29 +87,17 @@ export const getDoctorByDoctorId = async (req, res) => {
|
||||
});
|
||||
}
|
||||
|
||||
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 ?? "",
|
||||
},
|
||||
departments: doctor.departments.map((d) => ({
|
||||
departmentId: d.department.departmentId,
|
||||
departmentName: d.department.name,
|
||||
timing: d.timing || {},
|
||||
})),
|
||||
};
|
||||
|
||||
res.status(200).json({
|
||||
@@ -137,7 +123,6 @@ export const createDoctor = async (req, res) => {
|
||||
workingStatus,
|
||||
qualification,
|
||||
departments,
|
||||
timing,
|
||||
} = req.body;
|
||||
|
||||
const doctor = await prisma.doctor.create({
|
||||
@@ -150,11 +135,13 @@ export const createDoctor = async (req, res) => {
|
||||
},
|
||||
});
|
||||
|
||||
for (const depId of departments) {
|
||||
for (const dep of departments) {
|
||||
const department = await prisma.department.findUnique({
|
||||
where: {departmentId: depId},
|
||||
where: {departmentId: dep.departmentId},
|
||||
});
|
||||
|
||||
if (!department) continue;
|
||||
|
||||
const doctorDepartment = await prisma.doctorDepartment.create({
|
||||
data: {
|
||||
doctorId: doctor.id,
|
||||
@@ -162,11 +149,11 @@ export const createDoctor = async (req, res) => {
|
||||
},
|
||||
});
|
||||
|
||||
if (timing) {
|
||||
if (dep.timing) {
|
||||
await prisma.doctorTiming.create({
|
||||
data: {
|
||||
doctorDepartmentId: doctorDepartment.id,
|
||||
...timing,
|
||||
...dep.timing,
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -189,21 +176,8 @@ export const createDoctor = async (req, res) => {
|
||||
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 {name, designation, workingStatus, qualification, departments} =
|
||||
req.body;
|
||||
|
||||
const doctor = await prisma.doctor.findUnique({
|
||||
where: {doctorId},
|
||||
@@ -212,75 +186,61 @@ export const updateDoctor = async (req, res) => {
|
||||
if (!doctor) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: `Doctor with ID ${doctorId} not found`,
|
||||
message: "Doctor not found",
|
||||
});
|
||||
}
|
||||
|
||||
const updatedDoctor = await prisma.doctor.update({
|
||||
await prisma.doctor.update({
|
||||
where: {id: doctor.id},
|
||||
data: {
|
||||
...(name && {name}),
|
||||
...(designation && {designation}),
|
||||
...(workingStatus && {workingStatus}),
|
||||
...(qualification && {qualification}),
|
||||
name,
|
||||
designation,
|
||||
workingStatus,
|
||||
qualification,
|
||||
},
|
||||
});
|
||||
|
||||
if (departments) {
|
||||
const doctorDepartments = await prisma.doctorDepartment.findMany({
|
||||
where: {doctorId: doctor.id},
|
||||
const oldRelations = await prisma.doctorDepartment.findMany({
|
||||
where: {doctorId: doctor.id},
|
||||
});
|
||||
|
||||
for (const rel of oldRelations) {
|
||||
await prisma.doctorTiming.deleteMany({
|
||||
where: {doctorDepartmentId: rel.id},
|
||||
});
|
||||
}
|
||||
|
||||
await prisma.doctorDepartment.deleteMany({
|
||||
where: {doctorId: doctor.id},
|
||||
});
|
||||
|
||||
for (const dep of departments) {
|
||||
const department = await prisma.department.findUnique({
|
||||
where: {departmentId: dep.departmentId},
|
||||
});
|
||||
|
||||
for (const dd of doctorDepartments) {
|
||||
await prisma.doctorTiming.deleteMany({
|
||||
where: {doctorDepartmentId: dd.id},
|
||||
});
|
||||
}
|
||||
if (!department) continue;
|
||||
|
||||
await prisma.doctorDepartment.deleteMany({
|
||||
where: {doctorId: doctor.id},
|
||||
const doctorDepartment = await prisma.doctorDepartment.create({
|
||||
data: {
|
||||
doctorId: doctor.id,
|
||||
departmentId: department.id,
|
||||
},
|
||||
});
|
||||
|
||||
for (const depId of departments) {
|
||||
const department = await prisma.department.findUnique({
|
||||
where: {departmentId: depId},
|
||||
});
|
||||
if (!department) continue;
|
||||
|
||||
const doctorDepartment = await prisma.doctorDepartment.create({
|
||||
if (dep.timing) {
|
||||
await prisma.doctorTiming.create({
|
||||
data: {
|
||||
doctorId: doctor.id,
|
||||
departmentId: department.id,
|
||||
doctorDepartmentId: doctorDepartment.id,
|
||||
...dep.timing,
|
||||
},
|
||||
});
|
||||
|
||||
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,
|
||||
message: "Doctor updated successfully",
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
@@ -403,6 +363,7 @@ export const getDoctorTimingById = async (req, res) => {
|
||||
include: {
|
||||
departments: {
|
||||
include: {
|
||||
department: true,
|
||||
timing: true,
|
||||
},
|
||||
},
|
||||
@@ -416,23 +377,29 @@ export const getDoctorTimingById = async (req, res) => {
|
||||
});
|
||||
}
|
||||
|
||||
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 ?? "",
|
||||
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 || "",
|
||||
},
|
||||
};
|
||||
}),
|
||||
};
|
||||
|
||||
res.status(200).json({
|
||||
|
||||
Reference in New Issue
Block a user