1+ const sql = require ( 'mssql' ) ;
2+
3+ // Create new appointment
4+ const createAppointment = async ( req , res ) => {
5+ const {
6+ patient_id,
7+ doctor_id,
8+ appointment_date,
9+ reason
10+ } = req . body ;
11+
12+ try {
13+ const request = new sql . Request ( ) ;
14+ request . input ( 'patient_id' , sql . Int , patient_id ) ;
15+ request . input ( 'doctor_id' , sql . Int , doctor_id ) ;
16+ request . input ( 'appointment_date' , sql . DateTime , appointment_date ) ;
17+ request . input ( 'status' , sql . VarChar ( 20 ) , 'Scheduled' ) ;
18+
19+ const result = await request . query ( `
20+ INSERT INTO Appointments (
21+ patient_id, doctor_id, appointment_date, status
22+ )
23+ OUTPUT INSERTED.appointment_id
24+ VALUES (
25+ @patient_id, @doctor_id, @appointment_date, @status
26+ )
27+ ` ) ;
28+
29+ const appointmentId = result . recordset [ 0 ] . appointment_id ;
30+
31+ // Create feedback placeholder
32+ await request . query ( `
33+ INSERT INTO Feedback (appointment_id, patient_id)
34+ VALUES (@appointmentId, @patient_id)
35+ ` , { appointmentId } ) ;
36+
37+ res . status ( 201 ) . json ( {
38+ message : 'Appointment created successfully' ,
39+ appointmentId : appointmentId
40+ } ) ;
41+ } catch ( err ) {
42+ console . error ( 'Database error:' , err ) ;
43+ res . status ( 500 ) . json ( { message : 'Error creating appointment' , error : err . message } ) ;
44+ }
45+ } ;
46+
47+ // Get appointments by doctor ID
48+ const getAppointmentsByDoctor = async ( req , res ) => {
49+ const { doctorId } = req . params ;
50+
51+ try {
52+ const request = new sql . Request ( ) ;
53+ request . input ( 'doctorId' , sql . Int , doctorId ) ;
54+
55+ const result = await request . query ( `
56+ SELECT
57+ a.appointment_id,
58+ p.patient_id,
59+ p.full_name as patient_name,
60+ a.appointment_date,
61+ a.status,
62+ f.feedback_id,
63+ f.comments,
64+ f.rating
65+ FROM Appointments a
66+ JOIN Patients p ON a.patient_id = p.patient_id
67+ LEFT JOIN Feedback f ON a.appointment_id = f.appointment_id
68+ WHERE a.doctor_id = @doctorId
69+ ORDER BY a.appointment_date DESC
70+ ` ) ;
71+
72+ res . json ( result . recordset ) ;
73+ } catch ( err ) {
74+ console . error ( 'Database error:' , err ) ;
75+ res . status ( 500 ) . json ( { message : 'Error fetching appointments' , error : err . message } ) ;
76+ }
77+ } ;
78+
79+ // Get appointments by patient ID
80+ const getAppointmentsByPatient = async ( req , res ) => {
81+ const { patientId } = req . params ;
82+
83+ try {
84+ const request = new sql . Request ( ) ;
85+ request . input ( 'patientId' , sql . Int , patientId ) ;
86+
87+ const result = await request . query ( `
88+ SELECT
89+ a.appointment_id,
90+ d.doctor_id,
91+ u.full_name as doctor_name,
92+ d.specialization,
93+ a.appointment_date,
94+ a.status,
95+ f.feedback_id,
96+ f.comments,
97+ f.rating
98+ FROM Appointments a
99+ JOIN Doctors d ON a.doctor_id = d.doctor_id
100+ JOIN Users u ON d.user_id = u.user_id
101+ LEFT JOIN Feedback f ON a.appointment_id = f.appointment_id
102+ WHERE a.patient_id = @patientId
103+ ORDER BY a.appointment_date DESC
104+ ` ) ;
105+
106+ res . json ( result . recordset ) ;
107+ } catch ( err ) {
108+ console . error ( 'Database error:' , err ) ;
109+ res . status ( 500 ) . json ( { message : 'Error fetching appointments' , error : err . message } ) ;
110+ }
111+ } ;
112+
113+ // Update appointment status
114+ const updateAppointmentStatus = async ( req , res ) => {
115+ const { id } = req . params ;
116+ const { status } = req . body ;
117+
118+ try {
119+ const request = new sql . Request ( ) ;
120+ request . input ( 'id' , sql . Int , id ) ;
121+ request . input ( 'status' , sql . VarChar ( 20 ) , status ) ;
122+
123+ await request . query ( `
124+ UPDATE Appointments
125+ SET status = @status
126+ WHERE appointment_id = @id
127+ ` ) ;
128+
129+ res . json ( { message : 'Appointment status updated successfully' } ) ;
130+ } catch ( err ) {
131+ console . error ( 'Database error:' , err ) ;
132+ res . status ( 500 ) . json ( { message : 'Error updating appointment' , error : err . message } ) ;
133+ }
134+ } ;
135+
136+ // Submit feedback for appointment
137+ const submitFeedback = async ( req , res ) => {
138+ const { id } = req . params ;
139+ const { comments, rating } = req . body ;
140+
141+ try {
142+ const request = new sql . Request ( ) ;
143+ request . input ( 'id' , sql . Int , id ) ;
144+ request . input ( 'comments' , sql . Text , comments ) ;
145+ request . input ( 'rating' , sql . Float , rating ) ;
146+
147+ await request . query ( `
148+ UPDATE Feedback
149+ SET comments = @comments,
150+ rating = @rating,
151+ submitted_date = GETDATE()
152+ WHERE appointment_id = @id
153+ ` ) ;
154+
155+ // Update doctor's feedback score
156+ await request . query ( `
157+ UPDATE d
158+ SET d.feedback_score = (
159+ SELECT AVG(CAST(f.rating AS FLOAT))
160+ FROM Feedback f
161+ JOIN Appointments a ON f.appointment_id = a.appointment_id
162+ WHERE a.doctor_id = d.doctor_id
163+ AND f.rating IS NOT NULL
164+ )
165+ FROM Doctors d
166+ JOIN Appointments a ON d.doctor_id = a.doctor_id
167+ WHERE a.appointment_id = @id
168+ ` ) ;
169+
170+ res . json ( { message : 'Feedback submitted successfully' } ) ;
171+ } catch ( err ) {
172+ console . error ( 'Database error:' , err ) ;
173+ res . status ( 500 ) . json ( { message : 'Error submitting feedback' , error : err . message } ) ;
174+ }
175+ } ;
176+
177+ module . exports = {
178+ createAppointment,
179+ getAppointmentsByDoctor,
180+ getAppointmentsByPatient,
181+ updateAppointmentStatus,
182+ submitFeedback
183+ } ;
0 commit comments