-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaptcontrol.js
More file actions
172 lines (139 loc) · 4.82 KB
/
aptcontrol.js
File metadata and controls
172 lines (139 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import Appointment from '../models/appointment.js';
import mongoose from 'mongoose';
const createAppointment = async (req, res) => {
try {
const { patient, dept, doctor } = 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' })
]);
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' });
const appointment = new Appointment(req.body);
await appointment.save();
res.status(201).json(appointment);
} catch (err) {
console.error('Error creating appointment:', err);
res.status(400).json({ message: err.message });
}
};
const getAppointments = async (req, res) => {
try {
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 (!patient) {
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')
.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 });
}
};
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')
.lean();
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 });
}
};
const updateAppointment = async (req, res, next) => {
try {
const { patient, dept, doctor, status } = req.body;
const appointment = await Appointment.findById(req.params.id);
if (!appointment) {
return res.status(404).json({
success: false,
message: "Appointment not found"
});
}
if (
req.user.role === "doctor" &&
appointment.doctor.toString() !== req.user.id.toString()
) {
return res.status(403).json({
success: false,
message: "Not authorized to modify this appointment",
});
}
if (req.user.role === "doctor" && (doctor || dept)) {
return res.status(403).json({
success: false,
message: "Doctor cannot reassign appointment",
});
}
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' })
);
const results = await Promise.all(checks);
if (patient && !results[0])
return res.status(400).json({ success: false, message: 'Invalid patient ID' });
}
if (status) {
const validTransitions = {
scheduled: ["completed", "cancelled"],
completed: [],
cancelled: []
};
const allowed = validTransitions[appointment.status] || [];
if (!allowed.includes(status)) {
return res.status(400).json({
success: false,
message: "Invalid status transition"
});
}
appointment.status = status;
}
if (patient) appointment.patient = patient;
if (dept) appointment.dept = dept;
if (doctor) appointment.doctor = doctor;
await appointment.save();
res.status(200).json({
success: true,
data: appointment
});
} catch (err) {
next(err);
}
};
/*const deleteAppointment = async (req, res) => {
try {
const deletedAppointment = await Appointment.findByIdAndDelete(req.params.id);
if (!deletedAppointment) return res.status(404).json({ message: 'Appointment not found' });
res.json({ message: 'Appointment deleted successfully' });
} catch (err) {
console.error('Error deleting appointment:', err);
res.status(500).json({ message: err.message });
}
};*/
export {
createAppointment,
getAppointments,
getAppointmentById,
updateAppointment,
//deleteAppointment,
};