feat: implement sorting and visibility changes
This commit is contained in:
@@ -4,7 +4,10 @@ import prisma from "../prisma/client.js";
|
||||
|
||||
export const getAllDoctors = async (req, res) => {
|
||||
try {
|
||||
const {admin} = req.query;
|
||||
|
||||
const doctors = await prisma.doctor.findMany({
|
||||
where: admin === "true" ? {} : {isActive: true},
|
||||
include: {
|
||||
departments: {
|
||||
include: {
|
||||
@@ -13,7 +16,7 @@ export const getAllDoctors = async (req, res) => {
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: {name: "asc"},
|
||||
orderBy: [{globalSortOrder: "asc"}, {name: "asc"}],
|
||||
});
|
||||
|
||||
const formatted = doctors.map((doc, index) => ({
|
||||
@@ -24,10 +27,10 @@ export const getAllDoctors = async (req, res) => {
|
||||
designation: doc.designation,
|
||||
workingStatus: doc.workingStatus,
|
||||
qualification: doc.qualification,
|
||||
|
||||
isActive: doc.isActive,
|
||||
globalSortOrder: doc.globalSortOrder,
|
||||
departments: doc.departments.map((d) => {
|
||||
const t = d.timing || {};
|
||||
|
||||
const timingArray = [
|
||||
t.monday && `Monday ${t.monday}`,
|
||||
t.tuesday && `Tuesday ${t.tuesday}`,
|
||||
@@ -43,6 +46,7 @@ export const getAllDoctors = async (req, res) => {
|
||||
departmentId: d.department.departmentId,
|
||||
departmentName: d.department.name,
|
||||
timing: timingArray.join(" & "),
|
||||
deptSortOrder: d.sortOrder,
|
||||
};
|
||||
}),
|
||||
}));
|
||||
@@ -135,17 +139,23 @@ export const getDoctorsByDepartmentId = async (req, res) => {
|
||||
});
|
||||
}
|
||||
|
||||
const doctors = await prisma.doctorDepartment.findMany({
|
||||
where: {departmentId: department.id},
|
||||
const doctorsInDept = await prisma.doctorDepartment.findMany({
|
||||
where: {
|
||||
departmentId: department.id,
|
||||
doctor: {isActive: true},
|
||||
},
|
||||
include: {
|
||||
doctor: true,
|
||||
},
|
||||
orderBy: {sortOrder: "asc"},
|
||||
});
|
||||
|
||||
const result = doctors.map((d) => ({
|
||||
const result = doctorsInDept.map((d) => ({
|
||||
GG_ID: d.doctor.doctorId,
|
||||
Name: d.doctor.name,
|
||||
image: d.doctor.image ?? "",
|
||||
designation: d.doctor.designation,
|
||||
hierarchyOrder: d.sortOrder,
|
||||
}));
|
||||
|
||||
res.status(200).json({
|
||||
@@ -171,6 +181,8 @@ export const createDoctor = async (req, res) => {
|
||||
designation,
|
||||
workingStatus,
|
||||
qualification,
|
||||
isActive,
|
||||
globalSortOrder,
|
||||
departments,
|
||||
} = req.body;
|
||||
|
||||
@@ -182,6 +194,9 @@ export const createDoctor = async (req, res) => {
|
||||
designation,
|
||||
workingStatus,
|
||||
qualification,
|
||||
isActive: isActive !== undefined ? isActive : true,
|
||||
globalSortOrder:
|
||||
globalSortOrder !== undefined ? Number(globalSortOrder) : 0,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -196,6 +211,7 @@ export const createDoctor = async (req, res) => {
|
||||
data: {
|
||||
doctorId: doctor.id,
|
||||
departmentId: department.id,
|
||||
sortOrder: dep.sortOrder !== undefined ? Number(dep.sortOrder) : 0,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -232,22 +248,29 @@ export const updateDoctor = async (req, res) => {
|
||||
image,
|
||||
workingStatus,
|
||||
qualification,
|
||||
isActive,
|
||||
globalSortOrder,
|
||||
departments,
|
||||
} = req.body;
|
||||
|
||||
const doctor = await prisma.doctor.findUnique({
|
||||
where: {doctorId},
|
||||
});
|
||||
|
||||
if (!doctor) {
|
||||
const doctor = await prisma.doctor.findUnique({where: {doctorId}});
|
||||
if (!doctor)
|
||||
return res
|
||||
.status(404)
|
||||
.json({success: false, message: "Doctor not found"});
|
||||
}
|
||||
|
||||
await prisma.doctor.update({
|
||||
where: {id: doctor.id},
|
||||
data: {name, designation, image, workingStatus, qualification},
|
||||
data: {
|
||||
name,
|
||||
designation,
|
||||
image,
|
||||
workingStatus,
|
||||
qualification,
|
||||
isActive,
|
||||
globalSortOrder:
|
||||
globalSortOrder !== undefined ? Number(globalSortOrder) : undefined,
|
||||
},
|
||||
});
|
||||
|
||||
const oldRelations = await prisma.doctorDepartment.findMany({
|
||||
@@ -265,16 +288,16 @@ export const updateDoctor = async (req, res) => {
|
||||
});
|
||||
|
||||
for (const dep of departments) {
|
||||
const department = await prisma.department.findUnique({
|
||||
const targetDept = await prisma.department.findUnique({
|
||||
where: {departmentId: dep.departmentId},
|
||||
});
|
||||
if (!targetDept) continue;
|
||||
|
||||
if (!department) continue;
|
||||
|
||||
const doctorDepartment = await prisma.doctorDepartment.create({
|
||||
const newDD = await prisma.doctorDepartment.create({
|
||||
data: {
|
||||
doctorId: doctor.id,
|
||||
departmentId: department.id,
|
||||
departmentId: targetDept.id,
|
||||
sortOrder: dep.sortOrder !== undefined ? Number(dep.sortOrder) : 0,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -283,10 +306,7 @@ export const updateDoctor = async (req, res) => {
|
||||
dep.timing;
|
||||
|
||||
await prisma.doctorTiming.create({
|
||||
data: {
|
||||
doctorDepartmentId: doctorDepartment.id,
|
||||
...cleanTiming,
|
||||
},
|
||||
data: {doctorDepartmentId: newDD.id, ...cleanTiming},
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -421,6 +441,7 @@ export const getDoctorTimingById = async (req, res) => {
|
||||
departments: doctor.departments.map((d) => ({
|
||||
departmentId: d.department.departmentId,
|
||||
departmentName: d.department.name,
|
||||
deptSortOrder: d.sortOrder,
|
||||
timing: d.timing || {},
|
||||
})),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user