2026-05-26 15:48:01 +05:30
|
|
|
import prisma from '../prisma/client.js';
|
2026-03-13 14:54:47 +05:30
|
|
|
|
|
|
|
|
// get doctors
|
|
|
|
|
|
|
|
|
|
export const getAllDoctors = async (req, res) => {
|
|
|
|
|
try {
|
2026-05-26 15:48:01 +05:30
|
|
|
const { admin } = req.query;
|
2026-05-11 00:04:22 +05:30
|
|
|
|
2026-03-13 14:54:47 +05:30
|
|
|
const doctors = await prisma.doctor.findMany({
|
2026-05-26 15:48:01 +05:30
|
|
|
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: {
|
2026-05-26 15:48:01 +05:30
|
|
|
createdAt: 'asc',
|
2026-05-20 10:15:53 +05:30
|
|
|
},
|
|
|
|
|
},
|
2026-03-13 14:54:47 +05:30
|
|
|
},
|
2026-05-26 15:48:01 +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-05-26 15:48:01 +05:30
|
|
|
image: doc.image ?? '',
|
2026-04-08 16:40:42 +05:30
|
|
|
designation: doc.designation,
|
|
|
|
|
workingStatus: doc.workingStatus,
|
|
|
|
|
qualification: doc.qualification,
|
2026-05-11 00:04:22 +05:30
|
|
|
isActive: doc.isActive,
|
2026-05-20 10:15:53 +05:30
|
|
|
experience: doc.experience,
|
|
|
|
|
professionalSummary: doc.professionalSummary,
|
2026-05-11 00:04:22 +05:30
|
|
|
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: {
|
2026-05-26 15:48:01 +05:30
|
|
|
seoTitle: doc.seo?.seoTitle ?? '',
|
|
|
|
|
metaDescription: doc.seo?.metaDescription ?? '',
|
|
|
|
|
focusKeyphrase: doc.seo?.focusKeyphrase ?? '',
|
|
|
|
|
slug: doc.seo?.slug ?? '',
|
2026-05-20 10:15:53 +05:30
|
|
|
tags: doc.seo?.tags ?? [],
|
|
|
|
|
|
2026-05-26 15:48:01 +05:30
|
|
|
ogTitle: doc.seo?.ogTitle ?? '',
|
|
|
|
|
ogDescription: doc.seo?.ogDescription ?? '',
|
|
|
|
|
ogImage: doc.seo?.ogImage ?? '',
|
2026-05-20 10:15:53 +05:30
|
|
|
},
|
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,
|
2026-05-26 15:48:01 +05:30
|
|
|
timing: timingArray.join(' & '),
|
2026-05-11 00:04:22 +05:30
|
|
|
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,
|
2026-05-26 15:48:01 +05:30
|
|
|
message: 'Failed to fetch doctors',
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// get doctor by id
|
|
|
|
|
|
|
|
|
|
export const getDoctorByDoctorId = async (req, res) => {
|
|
|
|
|
try {
|
2026-05-26 15:48:01 +05:30
|
|
|
const { doctorId } = req.params;
|
2026-06-11 11:25:49 +05:30
|
|
|
const { admin } = req.query;
|
2026-03-13 14:54:47 +05:30
|
|
|
|
2026-06-11 11:25:49 +05:30
|
|
|
const doctor = await prisma.doctor.findFirst({
|
|
|
|
|
where: {
|
|
|
|
|
doctorId,
|
|
|
|
|
...(admin === 'true' ? {} : { isActive: true }),
|
|
|
|
|
},
|
2026-03-13 14:54:47 +05:30
|
|
|
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,
|
2026-05-26 15:48:01 +05:30
|
|
|
message: 'Doctor not found',
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const response = {
|
|
|
|
|
doctorId: doctor.doctorId,
|
|
|
|
|
name: doctor.name,
|
2026-05-26 15:48:01 +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,
|
2026-06-11 11:25:49 +05:30
|
|
|
isActive: doctor.isActive,
|
2026-05-20 10:15:53 +05:30
|
|
|
seo: {
|
2026-05-26 15:48:01 +05:30
|
|
|
seoTitle: doctor.seo?.seoTitle ?? '',
|
|
|
|
|
metaDescription: doctor.seo?.metaDescription ?? '',
|
|
|
|
|
focusKeyphrase: doctor.seo?.focusKeyphrase ?? '',
|
|
|
|
|
slug: doctor.seo?.slug ?? '',
|
2026-05-20 10:15:53 +05:30
|
|
|
tags: doctor.seo?.tags ?? [],
|
2026-05-26 15:48:01 +05:30
|
|
|
ogTitle: doctor.seo?.ogTitle ?? '',
|
|
|
|
|
ogDescription: doctor.seo?.ogDescription ?? '',
|
|
|
|
|
ogImage: doctor.seo?.ogImage ?? '',
|
2026-05-20 10:15:53 +05:30
|
|
|
},
|
|
|
|
|
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,
|
2026-05-26 15:48:01 +05:30
|
|
|
message: 'Failed to fetch doctor',
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-08 16:40:42 +05:30
|
|
|
// get doctors by department
|
|
|
|
|
export const getDoctorsByDepartmentId = async (req, res) => {
|
|
|
|
|
try {
|
2026-05-26 15:48:01 +05:30
|
|
|
const { Department_ID } = req.query;
|
2026-04-08 16:40:42 +05:30
|
|
|
|
|
|
|
|
if (!Department_ID) {
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
success: false,
|
2026-05-26 15:48:01 +05:30
|
|
|
message: 'Department_ID is required',
|
2026-04-08 16:40:42 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const department = await prisma.department.findUnique({
|
2026-05-26 15:48:01 +05:30
|
|
|
where: { departmentId: Department_ID },
|
2026-04-08 16:40:42 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!department) {
|
|
|
|
|
return res.status(404).json({
|
|
|
|
|
success: false,
|
2026-05-26 15:48:01 +05:30
|
|
|
message: 'Department not found',
|
2026-04-08 16:40:42 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-11 00:04:22 +05:30
|
|
|
const doctorsInDept = await prisma.doctorDepartment.findMany({
|
|
|
|
|
where: {
|
|
|
|
|
departmentId: department.id,
|
2026-05-26 15:48:01 +05:30
|
|
|
doctor: { isActive: true },
|
2026-05-11 00:04:22 +05:30
|
|
|
},
|
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
|
|
|
},
|
2026-05-26 15:48:01 +05:30
|
|
|
orderBy: { sortOrder: 'asc' },
|
2026-04-08 16:40:42 +05:30
|
|
|
});
|
|
|
|
|
|
2026-05-11 00:04:22 +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-05-26 15:48:01 +05:30
|
|
|
image: d.doctor.image ?? '',
|
2026-05-11 00:04:22 +05:30
|
|
|
designation: d.doctor.designation,
|
|
|
|
|
hierarchyOrder: d.sortOrder,
|
2026-05-26 15:48:01 +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,
|
2026-05-26 15:48:01 +05:30
|
|
|
message: 'Failed to fetch doctors',
|
2026-04-08 16:40:42 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
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,
|
2026-05-11 00:04:22 +05:30
|
|
|
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 = [];
|
|
|
|
|
|
2026-05-26 15:48:01 +05:30
|
|
|
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');
|
2026-05-21 11:20:09 +05:30
|
|
|
if (!departments || departments.length === 0) {
|
2026-05-26 15:48:01 +05:30
|
|
|
messages.push('At least one department is required');
|
2026-05-21 11:20:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (messages.length > 0) {
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
success: false,
|
2026-05-26 15:48:01 +05:30
|
|
|
message: messages.join(', '),
|
2026-05-21 11:20:09 +05:30
|
|
|
});
|
|
|
|
|
}
|
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,
|
2026-05-11 00:04:22 +05:30
|
|
|
isActive: isActive !== undefined ? isActive : true,
|
2026-05-26 15:48:01 +05:30
|
|
|
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-05-26 15:48:01 +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-05-11 00:04:22 +05:30
|
|
|
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,
|
2026-05-26 15:48:01 +05:30
|
|
|
message: 'Doctor created successfully',
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
success: false,
|
2026-05-26 15:48:01 +05:30
|
|
|
message: 'Failed to create doctor',
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//update doctors
|
|
|
|
|
export const updateDoctor = async (req, res) => {
|
|
|
|
|
try {
|
2026-05-26 15:48:01 +05:30
|
|
|
const { doctorId, action } = req.params;
|
2026-04-14 17:33:21 +05:30
|
|
|
const {
|
|
|
|
|
name,
|
|
|
|
|
designation,
|
|
|
|
|
image,
|
|
|
|
|
workingStatus,
|
|
|
|
|
qualification,
|
2026-05-11 00:04:22 +05:30
|
|
|
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,
|
2026-05-26 15:48:01 +05:30
|
|
|
message: 'Doctor ID is required',
|
2026-05-22 16:34:37 +05:30
|
|
|
});
|
|
|
|
|
}
|
2026-05-26 15:48:01 +05:30
|
|
|
const doctor = await prisma.doctor.findUnique({ where: { doctorId } });
|
|
|
|
|
if (!doctor) return res.status(404).json({ success: false, message: 'Doctor not found' });
|
|
|
|
|
if (action === 'toggleStatus') {
|
2026-05-22 16:34:37 +05:30
|
|
|
await prisma.doctor.update({
|
2026-05-26 15:48:01 +05:30
|
|
|
where: { id: doctor.id },
|
2026-05-22 16:34:37 +05:30
|
|
|
data: {
|
|
|
|
|
isActive: !doctor.isActive,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return res.status(200).json({
|
|
|
|
|
success: true,
|
2026-05-26 15:48:01 +05:30
|
|
|
message: `Doctor has been ${doctor.isActive ? 'deactivated' : 'activated'} successfully`,
|
2026-05-22 16:34:37 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-21 11:20:09 +05:30
|
|
|
const messages = [];
|
2026-05-26 15:48:01 +05:30
|
|
|
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) {
|
2026-05-26 15:48:01 +05:30
|
|
|
messages.push('At least one department is required');
|
2026-05-21 11:20:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (messages.length > 0) {
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
success: false,
|
2026-05-26 15:48:01 +05:30
|
|
|
message: messages.join(', '),
|
2026-05-21 11:20:09 +05:30
|
|
|
});
|
|
|
|
|
}
|
2026-03-13 14:54:47 +05:30
|
|
|
|
2026-03-17 16:22:37 +05:30
|
|
|
await prisma.doctor.update({
|
2026-05-26 15:48:01 +05:30
|
|
|
where: { id: doctor.id },
|
2026-05-11 00:04:22 +05:30
|
|
|
data: {
|
|
|
|
|
name,
|
|
|
|
|
designation,
|
|
|
|
|
image,
|
|
|
|
|
workingStatus,
|
|
|
|
|
qualification,
|
|
|
|
|
isActive,
|
2026-05-20 10:15:53 +05:30
|
|
|
experience: experience ? Number(experience) : null,
|
|
|
|
|
professionalSummary,
|
2026-05-26 15:48:01 +05:30
|
|
|
globalSortOrder: globalSortOrder !== undefined ? Number(globalSortOrder) : undefined,
|
2026-05-11 00:04:22 +05:30
|
|
|
},
|
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-26 12:33:51 +05:30
|
|
|
metaDescription,
|
2026-05-26 12:36:11 +05:30
|
|
|
seoTitle,
|
|
|
|
|
ogDescription,
|
2026-05-26 12:33:51 +05:30
|
|
|
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-26 15:48:01 +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-05-26 15:48:01 +05:30
|
|
|
res.status(200).json({ success: true, message: 'Doctor updated successfully' });
|
2026-03-13 14:54:47 +05:30
|
|
|
} catch (error) {
|
2026-05-26 15:48:01 +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 {
|
2026-05-26 15:48:01 +05:30
|
|
|
const { doctorId } = req.params;
|
2026-03-13 14:54:47 +05:30
|
|
|
|
|
|
|
|
const doctor = await prisma.doctor.findUnique({
|
2026-05-26 15:48:01 +05:30
|
|
|
where: { doctorId },
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!doctor) {
|
|
|
|
|
return res.status(404).json({
|
|
|
|
|
success: false,
|
2026-05-26 15:48:01 +05:30
|
|
|
message: 'Doctor not found',
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const doctorDepartments = await prisma.doctorDepartment.findMany({
|
2026-05-26 15:48:01 +05:30
|
|
|
where: { doctorId: doctor.id },
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for (const dd of doctorDepartments) {
|
|
|
|
|
await prisma.doctorTiming.deleteMany({
|
2026-05-26 15:48:01 +05:30
|
|
|
where: { doctorDepartmentId: dd.id },
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await prisma.doctorDepartment.deleteMany({
|
2026-05-26 15:48:01 +05:30
|
|
|
where: { doctorId: doctor.id },
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await prisma.doctor.delete({
|
2026-05-26 15:48:01 +05:30
|
|
|
where: { id: doctor.id },
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
success: true,
|
2026-05-26 15:48:01 +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,
|
2026-05-26 15:48:01 +05:30
|
|
|
message: 'Failed to delete doctor',
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 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-05-26 15:48:01 +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,
|
2026-05-26 15:48:01 +05:30
|
|
|
message: 'Failed to fetch doctor timings',
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
// get doctor timings by id
|
|
|
|
|
|
|
|
|
|
export const getDoctorTimingById = async (req, res) => {
|
|
|
|
|
try {
|
2026-05-26 15:48:01 +05:30
|
|
|
const { doctorId } = req.params;
|
2026-06-11 11:25:49 +05:30
|
|
|
const { admin } = req.query;
|
2026-03-13 14:54:47 +05:30
|
|
|
|
2026-06-11 11:25:49 +05:30
|
|
|
const doctor = await prisma.doctor.findFirst({
|
|
|
|
|
where: {
|
|
|
|
|
doctorId,
|
|
|
|
|
...(admin === 'true' ? {} : { isActive: true }),
|
|
|
|
|
},
|
2026-03-13 14:54:47 +05:30
|
|
|
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,
|
2026-05-26 15:48:01 +05:30
|
|
|
message: 'Doctor not found',
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
2026-05-11 00:04:22 +05:30
|
|
|
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,
|
2026-05-26 15:48:01 +05:30
|
|
|
message: 'Failed to fetch doctor timing',
|
2026-03-13 14:54:47 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|