| | 1 | = UseCase10 - Schedule Appointment = |
| | 2 | |
| | 3 | == Initiating Actor - `Doctor or System Administrator` == |
| | 4 | |
| | 5 | == Description == |
| | 6 | An appointment is scheduled for a patient with a specific doctor on a specific date and time. The system checks for conflicts, creates the appointment record, and makes it visible to both the patient and doctor. |
| | 7 | |
| | 8 | == Scenario == |
| | 9 | |
| | 10 | 1. User selects a patient and a doctor to schedule an appointment for. |
| | 11 | |
| | 12 | {{{ |
| | 13 | #!sql |
| | 14 | SELECT |
| | 15 | p.patient_id, |
| | 16 | u_p.first_name, |
| | 17 | u_p.last_name, |
| | 18 | p.embg |
| | 19 | FROM patients p |
| | 20 | JOIN users u_p ON p.patient_id = u_p.patient_id |
| | 21 | WHERE u_p.username = 'maja.veljanova'; |
| | 22 | }}} |
| | 23 | |
| | 24 | 2. System retrieves the selected doctor details. |
| | 25 | |
| | 26 | {{{ |
| | 27 | #!sql |
| | 28 | SELECT |
| | 29 | d.doctor_id, |
| | 30 | u_d.first_name, |
| | 31 | u_d.last_name, |
| | 32 | ds.specialization_name, |
| | 33 | d.department_id |
| | 34 | FROM doctors d |
| | 35 | JOIN doctor_specialization ds |
| | 36 | ON d.specialization_id = ds.specialization_id |
| | 37 | JOIN users u_d |
| | 38 | ON d.doctor_id = u_d.doctor_id |
| | 39 | WHERE u_d.username = 'elena.kirova'; |
| | 40 | }}} |
| | 41 | |
| | 42 | 3. System checks which time slots are already taken for the doctor on the selected date. |
| | 43 | |
| | 44 | {{{ |
| | 45 | #!sql |
| | 46 | SELECT a.appointment_time |
| | 47 | FROM appointments a |
| | 48 | WHERE a.doctor_id = ( |
| | 49 | SELECT doctor_id |
| | 50 | FROM users |
| | 51 | WHERE username = 'elena.kirova') |
| | 52 | AND a.appointment_date = '2026-06-12' |
| | 53 | AND a.status != 'CANCELLED'; |
| | 54 | }}} |
| | 55 | |
| | 56 | 4. System verifies the patient does not already have an appointment with this doctor at the chosen time. |
| | 57 | |
| | 58 | {{{ |
| | 59 | #!sql |
| | 60 | SELECT 1 |
| | 61 | FROM appointments a |
| | 62 | WHERE a.patient_id = (SELECT patient_id FROM users WHERE username = 'maja.veljanova') |
| | 63 | AND a.doctor_id = (SELECT doctor_id FROM users WHERE username = 'elena.kirova') |
| | 64 | AND a.appointment_date = '2026-06-12' |
| | 65 | AND a.appointment_time = '10:00' |
| | 66 | AND a.status != 'CANCELLED'; |
| | 67 | }}} |
| | 68 | |
| | 69 | 5. System creates the appointment record. |
| | 70 | |
| | 71 | {{{ |
| | 72 | #!sql |
| | 73 | INSERT INTO appointments (patient_id, doctor_id, appointment_date, appointment_time, status) |
| | 74 | VALUES ( |
| | 75 | (SELECT patient_id FROM users WHERE username = 'maja.veljanova'), |
| | 76 | (SELECT doctor_id FROM users WHERE username = 'elena.kirova'), |
| | 77 | '2026-06-12', |
| | 78 | '10:00', |
| | 79 | 'SCHEDULED' |
| | 80 | ) |
| | 81 | RETURNING appointment_id; |
| | 82 | }}} |
| | 83 | |
| | 84 | 6. System displays the confirmation with full appointment details. |
| | 85 | |
| | 86 | {{{ |
| | 87 | #!sql |
| | 88 | SELECT |
| | 89 | a.appointment_id, |
| | 90 | a.appointment_date, |
| | 91 | a.appointment_time, |
| | 92 | a.status, |
| | 93 | u_d.first_name AS doctor_first_name, |
| | 94 | u_d.last_name AS doctor_last_name, |
| | 95 | u_p.first_name AS patient_first_name, |
| | 96 | u_p.last_name AS patient_last_name |
| | 97 | FROM appointments a |
| | 98 | JOIN doctors d ON a.doctor_id = d.doctor_id |
| | 99 | JOIN users u_d ON d.doctor_id = u_d.doctor_id |
| | 100 | JOIN patients p ON a.patient_id = p.patient_id |
| | 101 | JOIN users u_p ON p.patient_id = u_p.patient_id |
| | 102 | WHERE a.appointment_id = (SELECT MAX(appointment_id) FROM appointments); |
| | 103 | }}} |
| | 104 | |
| | 105 | 7. System displays all upcoming appointments for the patient. |
| | 106 | |
| | 107 | {{{ |
| | 108 | #!sql |
| | 109 | SELECT |
| | 110 | a.appointment_id, |
| | 111 | a.appointment_date, |
| | 112 | a.appointment_time, |
| | 113 | a.status, |
| | 114 | u_d.first_name AS doctor_first_name, |
| | 115 | u_d.last_name AS doctor_last_name |
| | 116 | FROM appointments a |
| | 117 | JOIN doctors d ON a.doctor_id = d.doctor_id |
| | 118 | JOIN users u_d ON d.doctor_id = u_d.doctor_id |
| | 119 | WHERE a.patient_id = ( |
| | 120 | SELECT patient_id |
| | 121 | FROM users |
| | 122 | WHERE username = 'maja.veljanova') |
| | 123 | AND a.appointment_date >= CURRENT_DATE |
| | 124 | ORDER BY a.appointment_date ASC, a.appointment_time ASC; |
| | 125 | }}} |