Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 92 additions & 40 deletions src/controllers/aptcontrol.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,77 @@
import Appointment from '../models/appointment.js';
import mongoose from 'mongoose';
import Appointment from "../models/appointment.js";
import mongoose from "mongoose";

const DAYS = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];

const validateWorkingHours = (doctorDoc, date, time) => {
if (!doctorDoc.workingHours || doctorDoc.workingHours.length === 0) {
return { valid: true }; // No working hours defined — skip validation
}

const dayName = DAYS[new Date(date).getDay()];
const workingDay = doctorDoc.workingHours.find((d) => d.day === dayName);

if (!workingDay || !workingDay.isAvailable || workingDay.slots.length === 0) {
return {
valid: false,
message: `Dr. ${doctorDoc.name} is not available on ${dayName}.`,
};
}

const withinSlot = workingDay.slots.some(
(slot) => time >= slot.startTime && time < slot.endTime,
);

if (!withinSlot) {
const slotList = workingDay.slots
.map((s) => `${s.startTime}–${s.endTime}`)
.join(", ");
return {
valid: false,
message: `Dr. ${doctorDoc.name} is only available on ${dayName} during: ${slotList}.`,
};
}

return { valid: true };
};

const createAppointment = async (req, res) => {
try {
const { patient, dept, doctor } = req.body;
const { patient, dept, doctor, date, time } = req.body;


const [patientDoc, deptDoc, doctorDoc] = await Promise.all([
mongoose.model('Patient').findById(patient),
mongoose.model('Department').findById(dept),
mongoose.model('User').findOne({ _id: doctor, role: 'doctor' })
mongoose.model("Patient").findById(patient),
mongoose.model("Department").findById(dept),
mongoose.model("User").findOne({ _id: doctor, role: "doctor" }),
]);

if (!patientDoc) return res.status(400).json({ message: 'Invalid patient ID' });
if (!deptDoc) return res.status(400).json({ message: 'Invalid department ID' });
if (!doctorDoc) return res.status(400).json({ message: 'Invalid doctor ID or user is not a doctor' });
if (!patientDoc) return res.status(400).json({ message: "Invalid patient ID" });
if (!deptDoc) return res.status(400).json({ message: "Invalid department ID" });
if (!doctorDoc)
return res
.status(400)
.json({ message: "Invalid doctor ID or user is not a doctor" });

if (date && time) {
const { valid, message } = validateWorkingHours(doctorDoc, date, time);
if (!valid) {
return res.status(400).json({ message });
}
}

const appointment = new Appointment(req.body);
await appointment.save();
res.status(201).json(appointment);
} catch (err) {
console.error('Error creating appointment:', err);
console.error("Error creating appointment:", err);
res.status(400).json({ message: err.message });
}
};
Expand All @@ -30,43 +81,47 @@ const getAppointments = async (req, res) => {
const { id: userId, role } = req.user;

let query = {};
if (role === 'doctor') {
query.doctor = userId;
} else if (role === 'patient') {
const patient = await mongoose.model('Patient').findOne({ email: req.user.email });
if (role === "doctor") {
query.doctor = userId;
} else if (role === "patient") {
const patient = await mongoose.model("Patient").findOne({ email: req.user.email });
if (!patient) {
return res.status(404).json({ message: 'Patient record not found' });
return res.status(404).json({ message: "Patient record not found" });
}
query.patient = patient._id;
}

const appointments = await Appointment.find(query)
.populate('patient', 'name email')
.populate('doctor', 'name spec')
.populate('dept', 'dept')
.populate("patient", "name email")
.populate("doctor", "name spec")
.populate("dept", "dept")
.lean();

res.json(appointments);
} catch (err) {
console.error('Error fetching appointments:', err);
res.status(500).json({ message: 'Server error while fetching appointments', error: err.message });
console.error("Error fetching appointments:", err);
res
.status(500)
.json({ message: "Server error while fetching appointments", error: err.message });
}
};

const getAppointmentById = async (req, res) => {
try {
const appointment = await Appointment.findById(req.params.id)
.populate('patient', 'name email')
.populate('doctor', 'name spec')
.populate('dept', 'dept')
.populate("patient", "name email")
.populate("doctor", "name spec")
.populate("dept", "dept")
.lean();

if (!appointment) return res.status(404).json({ message: 'Appointment not found' });
if (!appointment) return res.status(404).json({ message: "Appointment not found" });

res.json(appointment);
} catch (err) {
console.error('Error fetching appointment by ID:', err);
res.status(500).json({ message: 'Server error while fetching appointment', error: err.message });
console.error("Error fetching appointment by ID:", err);
res
.status(500)
.json({ message: "Server error while fetching appointment", error: err.message });
}
};

Expand All @@ -79,7 +134,7 @@ const updateAppointment = async (req, res, next) => {
if (!appointment) {
return res.status(404).json({
success: false,
message: "Appointment not found"
message: "Appointment not found",
});
}

Expand All @@ -102,32 +157,30 @@ const updateAppointment = async (req, res, next) => {

if (patient || dept || doctor) {
const checks = [];
if (patient) checks.push(mongoose.model('Patient').findById(patient));
if (dept) checks.push(mongoose.model('Department').findById(dept));
if (doctor) checks.push(
mongoose.model('User').findOne({ _id: doctor, role: 'doctor' })
);
if (patient) checks.push(mongoose.model("Patient").findById(patient));
if (dept) checks.push(mongoose.model("Department").findById(dept));
if (doctor)
checks.push(mongoose.model("User").findOne({ _id: doctor, role: "doctor" }));

const results = await Promise.all(checks);

if (patient && !results[0])
return res.status(400).json({ success: false, message: 'Invalid patient ID' });

return res.status(400).json({ success: false, message: "Invalid patient ID" });
}

if (status) {
const validTransitions = {
scheduled: ["completed", "cancelled"],
completed: [],
cancelled: []
cancelled: [],
};

const allowed = validTransitions[appointment.status] || [];

if (!allowed.includes(status)) {
return res.status(400).json({
success: false,
message: "Invalid status transition"
message: "Invalid status transition",
});
}

Expand All @@ -142,9 +195,8 @@ const updateAppointment = async (req, res, next) => {

res.status(200).json({
success: true,
data: appointment
data: appointment,
});

} catch (err) {
next(err);
}
Expand All @@ -169,4 +221,4 @@ export {
getAppointmentById,
updateAppointment,
//deleteAppointment,
};
};
Loading
Loading