| | 1 | = UseCase11 - Create Referral = |
| | 2 | |
| | 3 | == Initiating Actor - `Doctor` == |
| | 4 | |
| | 5 | == Description == |
| | 6 | A doctor creates a referral for a patient to another doctor for specialist opinion or a consultation. The system creates the referral record and automatically schedules an appointment with the referred doctor. |
| | 7 | |
| | 8 | == Scenario == |
| | 9 | |
| | 10 | 1. Doctor opens a patient's medical record. |
| | 11 | |
| | 12 | {{{ |
| | 13 | #!sql |
| | 14 | SELECT |
| | 15 | mr.record_id, |
| | 16 | mr.patient_id |
| | 17 | FROM medical_records mr |
| | 18 | WHERE mr.patient_id = ( |
| | 19 | SELECT patient_id FROM users WHERE username = 'maja.veljanova' |
| | 20 | ); |
| | 21 | }}} |
| | 22 | |
| | 23 | 2. System retrieves the list of available specialist doctors. |
| | 24 | |
| | 25 | {{{ |
| | 26 | #!sql |
| | 27 | SELECT |
| | 28 | d.doctor_id, |
| | 29 | u_d.first_name, |
| | 30 | u_d.last_name, |
| | 31 | ds.specialization_name, |
| | 32 | d.department_id |
| | 33 | FROM doctors d |
| | 34 | JOIN doctor_specialization ds ON d.specialization_id = ds.specialization_id |
| | 35 | JOIN users u_d ON d.doctor_id = u_d.doctor_id |
| | 36 | ORDER BY ds.specialization_name, u_d.last_name; |
| | 37 | }}} |
| | 38 | |
| | 39 | 3. Doctor selects the receiving doctor, enters the referral reason, and specifies the appointment date and time. |
| | 40 | |
| | 41 | {{{ |
| | 42 | #!sql |
| | 43 | INSERT INTO referrals (record_id, from_doctor_id, to_doctor_id, reason, referral_date, appointment_date, appointment_time) |
| | 44 | VALUES ( |
| | 45 | (SELECT record_id FROM medical_records WHERE patient_id = (SELECT patient_id FROM users WHERE username = 'maja.veljanova')), |
| | 46 | (SELECT doctor_id FROM users WHERE username = 'elena.kirova'), |
| | 47 | (SELECT doctor_id FROM users WHERE username = 'nikola.trajkov'), |
| | 48 | 'Cardiology consultation for hypertension management', |
| | 49 | '2026-05-30', |
| | 50 | '2026-06-15', |
| | 51 | '14:00' |
| | 52 | ) |
| | 53 | RETURNING referral_id; |
| | 54 | }}} |
| | 55 | |
| | 56 | 4. System automatically creates an appointment with the receiving doctor. |
| | 57 | |
| | 58 | {{{ |
| | 59 | #!sql |
| | 60 | INSERT INTO appointments (patient_id, doctor_id, appointment_date, appointment_time, status) |
| | 61 | VALUES ( |
| | 62 | (SELECT patient_id FROM users WHERE username = 'maja.veljanova'), |
| | 63 | (SELECT doctor_id FROM users WHERE username = 'nikola.trajkov'), |
| | 64 | '2026-06-15', |
| | 65 | '14:00', |
| | 66 | 'SCHEDULED' |
| | 67 | ) |
| | 68 | RETURNING appointment_id; |
| | 69 | }}} |
| | 70 | |
| | 71 | 5. System displays the confirmation with full referral and appointment details. |
| | 72 | |
| | 73 | {{{ |
| | 74 | #!sql |
| | 75 | SELECT |
| | 76 | r.referral_id, |
| | 77 | r.reason, |
| | 78 | r.referral_date, |
| | 79 | r.appointment_date, |
| | 80 | r.appointment_time, |
| | 81 | u_from.first_name AS from_doctor_first_name, |
| | 82 | u_from.last_name AS from_doctor_last_name, |
| | 83 | u_to.first_name AS to_doctor_first_name, |
| | 84 | u_to.last_name AS to_doctor_last_name |
| | 85 | FROM referrals r |
| | 86 | JOIN doctors d_from ON r.from_doctor_id = d_from.doctor_id |
| | 87 | JOIN users u_from ON d_from.doctor_id = u_from.doctor_id |
| | 88 | JOIN doctors d_to ON r.to_doctor_id = d_to.doctor_id |
| | 89 | JOIN users u_to ON d_to.doctor_id = u_to.doctor_id |
| | 90 | WHERE r.referral_id = (SELECT MAX(referral_id) FROM referrals); |
| | 91 | }}} |
| | 92 | |
| | 93 | 6. System notifies the receiving doctor by displaying their incoming referrals. |
| | 94 | |
| | 95 | {{{ |
| | 96 | #!sql |
| | 97 | SELECT |
| | 98 | r.referral_id, |
| | 99 | r.reason, |
| | 100 | r.referral_date, |
| | 101 | r.appointment_date, |
| | 102 | u_p.first_name AS patient_first_name, |
| | 103 | u_p.last_name AS patient_last_name |
| | 104 | FROM referrals r |
| | 105 | JOIN medical_records mr ON r.record_id = mr.record_id |
| | 106 | JOIN patients p ON mr.patient_id = p.patient_id |
| | 107 | JOIN users u_p ON p.patient_id = u_p.patient_id |
| | 108 | WHERE r.to_doctor_id = (SELECT doctor_id FROM users WHERE username = 'nikola.trajkov') |
| | 109 | ORDER BY r.referral_date DESC; |
| | 110 | }}} |