Multi-Doctor Consultation Flow
Backend Implementation Details: Multi-Doctor Consultation Flow
Overview
The backend changes focus on removing the "single-session" restriction for patient visits, enabling multiple doctors to manage a patient's records simultaneously while ensuring proper queue synchronization during doctor reassignments.
Core Backend Changes
1. Prescription Modification Logic
File: app/services/patient_consultation_service.py
Modification: Refactored validate_consultation_status_for_prescription_modification
Change: Previously, the system enforced a "cycle number" restriction, allowing only the latest consultation record to be edited. This limitation has been removed to support multi-doctor (double consultation) workflows.
Impact: All consultation records associated with an active patient visit can now be edited by authorized users, enabling concurrent updates across multiple doctors.
2. Queue Specialization Synchronization
File: app/services/patient_consultation_service.py
Function: reassign_doctor_for_consultation
Change: Added logic to dynamically update the queue specialization based on the newly assigned doctor.
Logic:
from app.models.doctor import Doctor
new_doc = db.query(Doctor).filter(Doctor.id == doctor_id).first()
if new_doc and queue_record:
queue_record.specialization = new_doc.specialization.lower()
Impact: When a coordinator reassigns a doctor (e.g., Dentist → Pediatrician), the patient is immediately moved to the correct specialization queue without manual intervention.
3. Queue Deduplication (Unique Patients)
File: app/services/consultation_queue_service.py
Function: get_all_active_queue
Change:
Implemented PostgreSQL DISTINCT ON logic to ensure unique patient entries.
SQL Logic:
query = query.distinct(ConsultationQueue.book_no)
query = query.order_by(ConsultationQueue.book_no, po, ConsultationQueue.queue_number)
Reasoning: In a multi-doctor workflow, a single patient may have multiple consultation records. Without deduplication, dashboards (Counseling, Verification) would show duplicate entries for the same patient.
Impact:
- Ensures one entry per Book Number
- Displays the most relevant/latest queue status
- Improves dashboard clarity and usability
4. Patient History Integrity
Validation Performed:
-
Verified via SQL queries that
patient_consultationsrecords are correctly indexed by:patient_visit_details_iddoctor_id
-
Confirmed consistent retrieval of Book Number through joins with the
patientstable -
Ensured cross-camp history integrity is maintained
Database Schema Relations
| Table | Column | Relation Description |
|---|---|---|
| consultation_queue | specialization | Dynamically synced via patient_consultation_service
|
| patient_consultations | doctor_id | Linked to doctors.id → users.user_name
|
| camp_visits | user_id | Anchor for retrieving patients.book_no
|
Summary
These backend changes:
- Enable true multi-doctor consultation workflows
- Remove restrictive single-cycle editing logic
- Ensure real-time queue synchronization
- Prevent duplicate patient entries in dashboards
- Maintain strong data integrity across visits and consultations
This provides a scalable and realistic workflow aligned with real-world medical camp operations.