@@ -64,35 +64,83 @@ const getAppointmentById = async (req, res) => {
6464 }
6565} ;
6666
67- const updateAppointment = async ( req , res ) => {
67+ const updateAppointment = async ( req , res , next ) => {
6868 try {
69- const { patient, dept, doctor } = req . body ;
69+ const { patient, dept, doctor, status } = req . body ;
70+
71+ const appointment = await Appointment . findById ( req . params . id ) ;
72+
73+ if ( ! appointment ) {
74+ return res . status ( 404 ) . json ( {
75+ success : false ,
76+ message : "Appointment not found"
77+ } ) ;
78+ }
79+
80+ if (
81+ req . user . role === "doctor" &&
82+ appointment . doctor . toString ( ) !== req . user . id
83+ ) {
84+ return res . status ( 403 ) . json ( {
85+ success : false ,
86+ message : "Not authorized to modify this appointment" ,
87+ } ) ;
88+ }
89+
90+ if ( req . user . role === "doctor" && ( doctor || dept ) ) {
91+ return res . status ( 403 ) . json ( {
92+ success : false ,
93+ message : "Doctor cannot reassign appointment" ,
94+ } ) ;
95+ }
7096
7197 if ( patient || dept || doctor ) {
7298 const checks = [ ] ;
7399 if ( patient ) checks . push ( mongoose . model ( 'Patient' ) . findById ( patient ) ) ;
74100 if ( dept ) checks . push ( mongoose . model ( 'Department' ) . findById ( dept ) ) ;
75- if ( doctor ) checks . push ( mongoose . model ( 'User' ) . findOne ( { _id : doctor , role : 'doctor' } ) ) ;
101+ if ( doctor ) checks . push (
102+ mongoose . model ( 'User' ) . findOne ( { _id : doctor , role : 'doctor' } )
103+ ) ;
104+
105+ const results = await Promise . all ( checks ) ;
76106
77- const [ patientDoc , deptDoc , doctorDoc ] = await Promise . all ( checks ) ;
107+ if ( patient && ! results [ 0 ] )
108+ return res . status ( 400 ) . json ( { success : false , message : 'Invalid patient ID' } ) ;
78109
79- if ( patient && ! patientDoc ) return res . status ( 400 ) . json ( { message : 'Invalid patient ID' } ) ;
80- if ( dept && ! deptDoc ) return res . status ( 400 ) . json ( { message : 'Invalid department ID' } ) ;
81- if ( doctor && ! doctorDoc ) return res . status ( 400 ) . json ( { message : 'Invalid doctor ID or user is not a doctor' } ) ;
82110 }
83111
84- const updatedAppointment = await Appointment . findByIdAndUpdate (
85- req . params . id ,
86- req . body ,
87- { new : true , runValidators : true }
88- ) ;
112+ if ( status ) {
113+ const validTransitions = {
114+ scheduled : [ "completed" , "cancelled" ] ,
115+ completed : [ ] ,
116+ cancelled : [ ]
117+ } ;
89118
90- if ( ! updatedAppointment ) return res . status ( 404 ) . json ( { message : 'Appointment not found' } ) ;
119+ const allowed = validTransitions [ appointment . status ] || [ ] ;
120+
121+ if ( ! allowed . includes ( status ) ) {
122+ return res . status ( 400 ) . json ( {
123+ success : false ,
124+ message : "Invalid status transition"
125+ } ) ;
126+ }
127+
128+ appointment . status = status ;
129+ }
130+
131+ if ( patient ) appointment . patient = patient ;
132+ if ( dept ) appointment . dept = dept ;
133+ if ( doctor ) appointment . doctor = doctor ;
134+
135+ await appointment . save ( ) ;
136+
137+ res . status ( 200 ) . json ( {
138+ success : true ,
139+ data : appointment
140+ } ) ;
91141
92- res . json ( updatedAppointment ) ;
93142 } catch ( err ) {
94- console . error ( 'Error updating appointment:' , err ) ;
95- res . status ( 400 ) . json ( { message : err . message } ) ;
143+ next ( err ) ;
96144 }
97145} ;
98146
0 commit comments