2026-03-16 10:16:27 +05:30
|
|
|
import prisma from "../prisma/client.js";
|
2026-04-24 11:40:14 +05:30
|
|
|
import { sendEmail } from "../utils/sendEmail.js";
|
|
|
|
|
import { getEmailsByType } from "../utils/getEmailByTypes.js";
|
2026-03-16 10:16:27 +05:30
|
|
|
|
|
|
|
|
export const createAppointment = async (req, res) => {
|
|
|
|
|
try {
|
2026-04-24 11:40:14 +05:30
|
|
|
const { name, mobileNumber, email, message, date, doctorId, departmentId } =
|
2026-03-16 10:16:27 +05:30
|
|
|
req.body;
|
|
|
|
|
|
|
|
|
|
if (!name || !mobileNumber || !doctorId || !departmentId || !date) {
|
|
|
|
|
return res.status(400).json({
|
|
|
|
|
success: false,
|
|
|
|
|
message: "Required fields missing",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const appointment = await prisma.appointment.create({
|
|
|
|
|
data: {
|
|
|
|
|
name,
|
|
|
|
|
mobileNumber,
|
|
|
|
|
email,
|
|
|
|
|
message,
|
|
|
|
|
date: new Date(date),
|
|
|
|
|
doctorId,
|
|
|
|
|
departmentId,
|
|
|
|
|
},
|
|
|
|
|
include: {
|
|
|
|
|
doctor: true,
|
|
|
|
|
department: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2026-03-19 13:12:04 +05:30
|
|
|
try {
|
|
|
|
|
const emailList = await getEmailsByType("APPOINTMENT");
|
|
|
|
|
|
|
|
|
|
if (emailList) {
|
|
|
|
|
await sendEmail({
|
|
|
|
|
to: emailList,
|
|
|
|
|
subject: "New Appointment Booked",
|
|
|
|
|
html: `
|
2026-04-27 17:29:33 +05:30
|
|
|
<div style="font-family: Arial, sans-serif; background-color: #f4f6f8; padding: 20px;">
|
|
|
|
|
|
|
|
|
|
<div style="max-width: 600px; margin: auto; background: #ffffff; border-radius: 10px; overflow: hidden; box-shadow: 0 4px 10px rgba(0,0,0,0.05);">
|
|
|
|
|
|
|
|
|
|
<!-- Header -->
|
|
|
|
|
<div style="background-color: #0d6efd; color: #ffffff; padding: 20px;">
|
|
|
|
|
<h2 style="margin: 0;">GG Hospital</h2>
|
|
|
|
|
<p style="margin: 5px 0 0; font-size: 14px;">
|
|
|
|
|
New Appointment Booked
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<!-- Body -->
|
|
|
|
|
<div style="padding: 20px; color: #333;">
|
|
|
|
|
|
|
|
|
|
<h3 style="margin-top: 0;">Patient Details</h3>
|
|
|
|
|
|
|
|
|
|
<table style="width: 100%; border-collapse: collapse;">
|
|
|
|
|
<tr>
|
|
|
|
|
<td style="padding: 8px 0;"><b>Name:</b></td>
|
|
|
|
|
<td style="padding: 8px 0;">${name}</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td style="padding: 8px 0;"><b>Phone:</b></td>
|
|
|
|
|
<td style="padding: 8px 0;">${mobileNumber}</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td style="padding: 8px 0;"><b>Email:</b></td>
|
|
|
|
|
<td style="padding: 8px 0;">${email || "-"}</td>
|
|
|
|
|
</tr>
|
|
|
|
|
</table>
|
|
|
|
|
|
|
|
|
|
<h3 style="margin-top: 20px;">Appointment Details</h3>
|
|
|
|
|
|
|
|
|
|
<table style="width: 100%; border-collapse: collapse;">
|
|
|
|
|
<tr>
|
|
|
|
|
<td style="padding: 8px 0;"><b>Doctor:</b></td>
|
|
|
|
|
<td style="padding: 8px 0;">${appointment.doctor?.name || "-"}</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td style="padding: 8px 0;"><b>Department:</b></td>
|
|
|
|
|
<td style="padding: 8px 0;">${appointment.department?.name || "-"}</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
|
<td style="padding: 8px 0;"><b>Date:</b></td>
|
|
|
|
|
<td style="padding: 8px 0;">
|
|
|
|
|
${new Date(date).toLocaleDateString()}
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
</table>
|
|
|
|
|
|
|
|
|
|
<!-- Message Box -->
|
|
|
|
|
<div style="margin-top: 20px;">
|
|
|
|
|
<h3>Message</h3>
|
|
|
|
|
<div style="
|
|
|
|
|
background: #f8f9fa;
|
|
|
|
|
padding: 15px;
|
|
|
|
|
border-radius: 6px;
|
|
|
|
|
line-height: 1.6;
|
|
|
|
|
white-space: pre-wrap;
|
|
|
|
|
word-break: break-word;
|
|
|
|
|
overflow-wrap: anywhere;
|
|
|
|
|
">
|
|
|
|
|
${message ? message.replace(/\n/g, "<br/>") : "-"}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<!-- Footer -->
|
|
|
|
|
<div style="background: #f1f1f1; padding: 15px; text-align: center; font-size: 12px; color: #666;">
|
|
|
|
|
This appointment was booked via the GG Hospital website.
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
`,
|
2026-03-19 13:12:04 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error("Email failed:", err);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 10:16:27 +05:30
|
|
|
res.status(201).json({
|
|
|
|
|
success: true,
|
|
|
|
|
message: "Appointment booked successfully",
|
|
|
|
|
data: appointment,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
success: false,
|
|
|
|
|
message: "Failed to create appointment",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// GET ALL APPOINTMENTS
|
|
|
|
|
|
|
|
|
|
export const getAppointments = async (req, res) => {
|
|
|
|
|
try {
|
2026-04-24 11:40:14 +05:30
|
|
|
const page = parseInt(req.query.page) || 1;
|
|
|
|
|
const limit = parseInt(req.query.limit) || 10;
|
|
|
|
|
const skip = (page - 1) * limit;
|
|
|
|
|
const { date, search } = req.query;
|
|
|
|
|
|
|
|
|
|
const where = {};
|
|
|
|
|
|
|
|
|
|
if (date) {
|
|
|
|
|
const start = new Date(date);
|
|
|
|
|
const end = new Date(date);
|
|
|
|
|
end.setDate(end.getDate() + 1);
|
|
|
|
|
where.date = { gte: start, lt: end };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (search) {
|
|
|
|
|
where.OR = [
|
|
|
|
|
{ name: { contains: search, mode: "insensitive" } },
|
|
|
|
|
{ mobileNumber: { contains: search } },
|
|
|
|
|
{ email: { contains: search, mode: "insensitive" } },
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const [appointments, total] = await Promise.all([
|
|
|
|
|
prisma.appointment.findMany({
|
|
|
|
|
where,
|
|
|
|
|
include: { doctor: true, department: true },
|
|
|
|
|
orderBy: { createdAt: "desc" },
|
|
|
|
|
skip,
|
|
|
|
|
take: limit,
|
|
|
|
|
}),
|
|
|
|
|
prisma.appointment.count({ where }),
|
|
|
|
|
]);
|
2026-03-16 10:16:27 +05:30
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
data: appointments,
|
2026-04-24 11:40:14 +05:30
|
|
|
pagination: { total, page, limit, totalPages: Math.ceil(total / limit) },
|
2026-03-16 10:16:27 +05:30
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
2026-04-24 11:40:14 +05:30
|
|
|
res
|
|
|
|
|
.status(500)
|
|
|
|
|
.json({ success: false, message: "Failed to fetch appointments" });
|
2026-03-16 10:16:27 +05:30
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// GET SINGLE APPOINTMENT
|
|
|
|
|
|
|
|
|
|
export const getAppointment = async (req, res) => {
|
|
|
|
|
try {
|
2026-04-24 11:40:14 +05:30
|
|
|
const { id } = req.params;
|
2026-03-16 10:16:27 +05:30
|
|
|
|
|
|
|
|
const appointment = await prisma.appointment.findUnique({
|
|
|
|
|
where: {
|
|
|
|
|
id: Number(id),
|
|
|
|
|
},
|
|
|
|
|
include: {
|
|
|
|
|
doctor: true,
|
|
|
|
|
department: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!appointment) {
|
|
|
|
|
return res.status(404).json({
|
|
|
|
|
success: false,
|
|
|
|
|
message: "Appointment not found",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
data: appointment,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
success: false,
|
|
|
|
|
message: "Failed to fetch appointment",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// GET APPOINTMENTS BY DOCTOR
|
|
|
|
|
|
|
|
|
|
export const getAppointmentsByDoctor = async (req, res) => {
|
|
|
|
|
try {
|
2026-04-24 11:40:14 +05:30
|
|
|
const { doctorId } = req.params;
|
2026-03-16 10:16:27 +05:30
|
|
|
|
|
|
|
|
const appointments = await prisma.appointment.findMany({
|
|
|
|
|
where: {
|
|
|
|
|
doctorId,
|
|
|
|
|
},
|
|
|
|
|
include: {
|
|
|
|
|
doctor: true,
|
|
|
|
|
department: true,
|
|
|
|
|
},
|
|
|
|
|
orderBy: {
|
|
|
|
|
date: "asc",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
data: appointments,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
success: false,
|
|
|
|
|
message: "Failed to fetch doctor appointments",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// GET APPOINTMENTS BY DEPARTMENT
|
|
|
|
|
|
|
|
|
|
export const getAppointmentsByDepartment = async (req, res) => {
|
|
|
|
|
try {
|
2026-04-24 11:40:14 +05:30
|
|
|
const { departmentId } = req.params;
|
2026-03-16 10:16:27 +05:30
|
|
|
|
|
|
|
|
const appointments = await prisma.appointment.findMany({
|
|
|
|
|
where: {
|
|
|
|
|
departmentId,
|
|
|
|
|
},
|
|
|
|
|
include: {
|
|
|
|
|
doctor: true,
|
|
|
|
|
department: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
data: appointments,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
success: false,
|
|
|
|
|
message: "Failed to fetch department appointments",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// UPDATE APPOINTMENT
|
|
|
|
|
|
|
|
|
|
export const updateAppointment = async (req, res) => {
|
|
|
|
|
try {
|
2026-04-24 11:40:14 +05:30
|
|
|
const { id } = req.params;
|
2026-03-16 10:16:27 +05:30
|
|
|
|
|
|
|
|
const appointment = await prisma.appointment.update({
|
|
|
|
|
where: {
|
|
|
|
|
id: Number(id),
|
|
|
|
|
},
|
|
|
|
|
data: req.body,
|
|
|
|
|
include: {
|
|
|
|
|
doctor: true,
|
|
|
|
|
department: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
message: "Appointment updated successfully",
|
|
|
|
|
data: appointment,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
success: false,
|
|
|
|
|
message: "Failed to update appointment",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//DELETE APPOINTMENT
|
|
|
|
|
|
|
|
|
|
export const deleteAppointment = async (req, res) => {
|
|
|
|
|
try {
|
2026-04-24 11:40:14 +05:30
|
|
|
const { id } = req.params;
|
2026-03-16 10:16:27 +05:30
|
|
|
|
|
|
|
|
await prisma.appointment.delete({
|
|
|
|
|
where: {
|
|
|
|
|
id: Number(id),
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
success: true,
|
|
|
|
|
message: "Appointment deleted successfully",
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
res.status(500).json({
|
|
|
|
|
success: false,
|
|
|
|
|
message: "Failed to delete appointment",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|