Files
gg-backend/backend/src/controllers/doctor.controller.js
T

704 lines
15 KiB
JavaScript
Raw Normal View History

2026-03-13 14:54:47 +05:30
import prisma from "../prisma/client.js";
// get doctors
export const getAllDoctors = async (req, res) => {
try {
const {admin} = req.query;
2026-03-13 14:54:47 +05:30
const doctors = await prisma.doctor.findMany({
where: admin === "true" ? {} : {isActive: true},
2026-03-13 14:54:47 +05:30
include: {
2026-05-20 10:15:53 +05:30
seo: true,
2026-03-13 14:54:47 +05:30
departments: {
include: {
department: true,
timing: true,
},
},
2026-05-20 10:15:53 +05:30
specializations: {
orderBy: {
createdAt: "asc",
},
},
2026-03-13 14:54:47 +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,
isActive: doc.isActive,
2026-05-20 10:15:53 +05:30
experience: doc.experience,
professionalSummary: doc.professionalSummary,
globalSortOrder: doc.globalSortOrder,
2026-05-20 10:15:53 +05:30
specializations: doc.specializations.map((item) => ({
id: item.id,
name: item.name,
description: item.description,
})),
seo: {
seoTitle: doc.seo?.seoTitle ?? "",
metaDescription: doc.seo?.metaDescription ?? "",
focusKeyphrase: doc.seo?.focusKeyphrase ?? "",
slug: doc.seo?.slug ?? "",
tags: doc.seo?.tags ?? [],
ogTitle: doc.seo?.ogTitle ?? "",
ogDescription: doc.seo?.ogDescription ?? "",
ogImage: doc.seo?.ogImage ?? "",
},
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(" & "),
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: {
2026-05-20 10:15:53 +05:30
seo: true,
specializations: true,
2026-03-13 14:54:47 +05:30
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-05-20 10:15:53 +05:30
experience: doctor.experience,
professionalSummary: doctor.professionalSummary,
seo: {
seoTitle: doctor.seo?.seoTitle ?? "",
metaDescription: doctor.seo?.metaDescription ?? "",
focusKeyphrase: doctor.seo?.focusKeyphrase ?? "",
slug: doctor.seo?.slug ?? "",
tags: doctor.seo?.tags ?? [],
ogTitle: doctor.seo?.ogTitle ?? "",
ogDescription: doctor.seo?.ogDescription ?? "",
ogImage: doctor.seo?.ogImage ?? "",
},
specializations:
doctor.specializations?.map((item) => ({
id: item.id,
name: item.name,
description: item.description,
})) ?? [],
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",
});
}
const doctorsInDept = await prisma.doctorDepartment.findMany({
where: {
departmentId: department.id,
doctor: {isActive: true},
},
2026-04-08 16:40:42 +05:30
include: {
2026-05-25 12:04:14 +05:30
doctor: {
include: {
seo: {
select: {
slug: true,
},
},
},
},
2026-04-08 16:40:42 +05:30
},
orderBy: {sortOrder: "asc"},
2026-04-08 16:40:42 +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 ?? "",
designation: d.doctor.designation,
hierarchyOrder: d.sortOrder,
2026-05-25 12:04:14 +05:30
slug: d.doctor.seo?.slug ?? "",
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,
isActive,
globalSortOrder,
2026-03-13 14:54:47 +05:30
departments,
2026-05-20 10:15:53 +05:30
experience,
professionalSummary,
seoTitle,
metaDescription,
focusKeyphrase,
slug,
tags,
specializations,
ogTitle,
ogDescription,
ogImage,
2026-03-13 14:54:47 +05:30
} = req.body;
2026-05-21 11:20:09 +05:30
const messages = [];
if (!doctorId) messages.push("Doctor ID is required");
if (!name?.trim()) messages.push("Doctor name is required");
if (!designation?.trim()) messages.push("Designation is required");
if (!qualification?.trim()) messages.push("Qualification is required");
if (!departments || departments.length === 0) {
messages.push("At least one department is required");
}
if (messages.length > 0) {
return res.status(400).json({
success: false,
message: messages.join(", "),
});
}
2026-05-20 10:15:53 +05:30
const seo = await prisma.seo.create({
data: {
seoTitle,
metaDescription,
focusKeyphrase,
slug: slug ? slug : null,
tags: tags || [],
// Open Graph
ogTitle,
ogDescription,
ogImage,
},
});
2026-03-13 14:54:47 +05:30
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-20 10:15:53 +05:30
experience: experience ? Number(experience) : null,
professionalSummary,
seoId: seo.id,
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,
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
},
});
}
}
2026-05-20 10:15:53 +05:30
if (specializations?.length) {
await prisma.doctorSpecialization.createMany({
data: specializations
.filter((item) => item.name?.trim())
.map((item) => ({
name: item.name.trim(),
description: item.description?.trim() || null,
doctorId: doctor.id,
})),
});
}
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 {
2026-05-22 16:34:37 +05:30
const {doctorId, action} = req.params;
2026-04-14 17:33:21 +05:30
const {
name,
designation,
image,
workingStatus,
qualification,
isActive,
globalSortOrder,
2026-04-14 17:33:21 +05:30
departments,
2026-05-20 10:15:53 +05:30
experience,
professionalSummary,
seoTitle,
metaDescription,
2026-05-26 12:33:51 +05:30
ogTitle,
ogDescription,
2026-05-20 10:15:53 +05:30
focusKeyphrase,
slug,
tags,
2026-05-26 11:52:29 +05:30
ogImage,
2026-05-20 10:15:53 +05:30
specializations,
2026-04-14 17:33:21 +05:30
} = req.body;
2026-05-26 11:52:29 +05:30
2026-05-22 16:34:37 +05:30
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 not found"});
if (action === "toggleStatus") {
await prisma.doctor.update({
where: {id: doctor.id},
data: {
isActive: !doctor.isActive,
},
});
return res.status(200).json({
success: true,
message: `Doctor has been ${
doctor.isActive ? "deactivated" : "activated"
} successfully`,
});
}
2026-05-21 11:20:09 +05:30
const messages = [];
if (!doctorId) messages.push("Doctor ID is required");
if (!name?.trim()) messages.push("Doctor name is required");
if (!qualification?.trim()) messages.push("Qualification is required");
if (!designation?.trim()) messages.push("Designation is required");
2026-03-13 14:54:47 +05:30
2026-05-21 11:20:09 +05:30
if (!departments || departments.length === 0) {
messages.push("At least one department is required");
}
if (messages.length > 0) {
return res.status(400).json({
success: false,
message: messages.join(", "),
});
}
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: {
name,
designation,
image,
workingStatus,
qualification,
isActive,
2026-05-20 10:15:53 +05:30
experience: experience ? Number(experience) : null,
professionalSummary,
globalSortOrder:
globalSortOrder !== undefined ? Number(globalSortOrder) : undefined,
},
2026-03-13 14:54:47 +05:30
});
2026-05-20 10:43:05 +05:30
2026-05-20 10:15:53 +05:30
if (doctor.seoId) {
await prisma.seo.update({
where: {
id: doctor.seoId,
},
data: {
seoTitle,
metaDescription,
2026-05-26 12:33:51 +05:30
ogTitle,
ogDescription,
2026-05-26 11:52:29 +05:30
ogImage,
2026-05-20 10:15:53 +05:30
focusKeyphrase,
slug: slug ? slug : null,
tags: tags || [],
},
});
} else {
const seo = await prisma.seo.create({
data: {
2026-05-26 11:52:29 +05:30
ogImage,
2026-05-20 10:15:53 +05:30
seoTitle,
metaDescription,
2026-05-26 12:33:51 +05:30
metaDescription,
ogTitle,
2026-05-20 10:15:53 +05:30
focusKeyphrase,
slug: slug ? slug : null,
tags: tags || [],
},
});
await prisma.doctor.update({
where: {
id: doctor.id,
},
data: {
seoId: seo.id,
},
});
}
2026-03-13 14:54:47 +05:30
2026-05-22 16:34:37 +05:30
// Update Departments & Timings
if (Array.isArray(departments)) {
const oldRelations = await prisma.doctorDepartment.findMany({
2026-05-20 10:15:53 +05:30
where: {
doctorId: doctor.id,
},
2026-05-22 16:34:37 +05:30
include: {
timing: true,
},
2026-05-20 10:15:53 +05:30
});
2026-05-22 16:34:37 +05:30
// Delete old timings
2026-05-13 14:19:42 +05:30
for (const rel of oldRelations) {
2026-05-22 16:34:37 +05:30
if (rel.timing) {
await prisma.doctorTiming.deleteMany({
where: {
doctorDepartmentId: rel.id,
},
});
}
2026-05-13 14:19:42 +05:30
}
2026-03-13 14:54:47 +05:30
2026-05-22 16:34:37 +05:30
// Delete old departments
2026-05-13 14:19:42 +05:30
await prisma.doctorDepartment.deleteMany({
2026-05-22 16:34:37 +05:30
where: {
doctorId: doctor.id,
},
2026-03-13 14:54:47 +05:30
});
2026-05-22 16:34:37 +05:30
// Recreate departments + timings
2026-05-13 14:19:42 +05:30
for (const dep of departments) {
2026-05-22 16:34:37 +05:30
const department = await prisma.department.findUnique({
where: {
departmentId: dep.departmentId,
},
2026-05-13 14:19:42 +05:30
});
2026-04-08 16:40:42 +05:30
2026-05-22 16:34:37 +05:30
if (!department) continue;
const doctorDepartment = await prisma.doctorDepartment.create({
2026-05-13 14:19:42 +05:30
data: {
doctorId: doctor.id,
2026-05-22 16:34:37 +05:30
departmentId: department.id,
2026-05-13 14:19:42 +05:30
sortOrder: dep.sortOrder !== undefined ? Number(dep.sortOrder) : 0,
},
2026-03-13 14:54:47 +05:30
});
2026-05-13 14:19:42 +05:30
2026-05-22 16:34:37 +05:30
if (dep.timing && Object.keys(dep.timing).length > 0) {
2026-05-13 14:19:42 +05:30
const {id, doctorDepartmentId, createdAt, updatedAt, ...cleanTiming} =
dep.timing;
2026-05-22 16:34:37 +05:30
2026-05-13 14:19:42 +05:30
await prisma.doctorTiming.create({
2026-05-22 16:34:37 +05:30
data: {
doctorDepartmentId: doctorDepartment.id,
...cleanTiming,
},
2026-05-13 14:19:42 +05:30
});
}
2026-03-13 14:54:47 +05:30
}
}
2026-05-22 16:34:37 +05:30
// Update Specializations
if (Array.isArray(specializations)) {
await prisma.doctorSpecialization.deleteMany({
where: {
doctorId: doctor.id,
},
});
if (specializations.length) {
await prisma.doctorSpecialization.createMany({
data: specializations
.filter((item) => item.name?.trim())
.map((item) => ({
name: item.name.trim(),
description: item.description?.trim() || null,
doctorId: doctor.id,
})),
});
}
}
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,
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",
});
}
};