@@ -12,6 +12,7 @@ import org.simple.clinic.overdue.Appointment.Status
1212import org.simple.clinic.util.Unicode
1313import java.time.Instant
1414import java.util.UUID
15+ import org.simple.clinic.medicalhistory.Answer as MedicalHistoryAnswer
1516
1617@Parcelize
1718data class RecentPatient (
@@ -30,6 +31,10 @@ data class RecentPatient(
3031 val updatedAt : Instant ,
3132
3233 val eligibleForReassignment : Answer ,
34+
35+ val diagnosedWithHypertension : MedicalHistoryAnswer ? ,
36+
37+ val diagnosedWithDiabetes : MedicalHistoryAnswer ?
3338) : Parcelable {
3439
3540 override fun toString (): String {
@@ -43,51 +48,65 @@ data class RecentPatient(
4348
4449 const val RECENT_PATIENT_QUERY = """
4550 SELECT P.uuid, P.fullName, P.gender, P.dateOfBirth, P.age_value,
46- P.age_updatedAt, P.recordedAt patientRecordedAt, P.eligibleForReassignment,
47- MAX(
48- IFNULL(BP.latestRecordedAt, '0'),
49- IFNULL(PD.latestUpdatedAt, '0'),
50- IFNULL(AP.latestCreatedAt, '0'),
51- IFNULL(BloodSugar.latestRecordedAt, '0')
52- ) updatedAt
51+ P.age_updatedAt, P.recordedAt patientRecordedAt, P.eligibleForReassignment,
52+ (
53+ SELECT diagnosedWithHypertension
54+ FROM MedicalHistory
55+ WHERE patientUuid = P.uuid
56+ ORDER BY updatedAt DESC
57+ LIMIT 1
58+ ) diagnosedWithHypertension,
59+ (
60+ SELECT hasDiabetes
61+ FROM MedicalHistory
62+ WHERE patientUuid = P.uuid
63+ ORDER BY updatedAt DESC
64+ LIMIT 1
65+ ) diagnosedWithDiabetes,
66+ MAX(
67+ IFNULL(BP.latestRecordedAt, '0'),
68+ IFNULL(PD.latestUpdatedAt, '0'),
69+ IFNULL(AP.latestCreatedAt, '0'),
70+ IFNULL(BloodSugar.latestRecordedAt, '0')
71+ ) updatedAt
5372 FROM Patient P
5473 LEFT JOIN (
5574 SELECT MAX(recordedAt) latestRecordedAt, patientUuid, facilityUuid
56- FROM BloodPressureMeasurement
57- WHERE facilityUuid = :facilityUuid
75+ FROM BloodPressureMeasurement
76+ WHERE facilityUuid = :facilityUuid
5877 AND deletedAt IS NULL
59- GROUP BY patientUuid
78+ GROUP BY patientUuid
6079 ) BP ON P.uuid = BP.patientUuid
6180 LEFT JOIN (
6281 SELECT MAX(updatedAt) latestUpdatedAt, patientUuid, facilityUuid
63- FROM PrescribedDrug
64- WHERE facilityUuid = :facilityUuid
82+ FROM PrescribedDrug
83+ WHERE facilityUuid = :facilityUuid
6584 AND deletedAt IS NULL
66- GROUP BY patientUuid
85+ GROUP BY patientUuid
6786 ) PD ON P.uuid = PD.patientUuid
6887 LEFT JOIN (
6988 SELECT MAX(createdAt) latestCreatedAt, uuid, patientUuid, facilityUuid, creationFacilityUuid
70- FROM Appointment
71- WHERE creationFacilityUuid = :facilityUuid
89+ FROM Appointment
90+ WHERE creationFacilityUuid = :facilityUuid
7291 AND deletedAt IS NULL
7392 AND status = :appointmentStatus
7493 AND appointmentType = :appointmentType
75- GROUP BY patientUuid
94+ GROUP BY patientUuid
7695 ) AP ON P.uuid = AP.patientUuid
7796 LEFT JOIN (
7897 SELECT MAX(recordedAt) latestRecordedAt, patientUuid, facilityUuid
79- FROM BloodSugarMeasurements
80- WHERE facilityUuid = :facilityUuid
98+ FROM BloodSugarMeasurements
99+ WHERE facilityUuid = :facilityUuid
81100 AND deletedAt IS NULL
82- GROUP BY patientUuid
101+ GROUP BY patientUuid
83102 ) BloodSugar ON P.uuid = BloodSugar.patientUuid
84103 WHERE (
85104 (
86105 BP.facilityUuid = :facilityUuid OR
87106 PD.facilityUuid = :facilityUuid OR
88107 AP.creationFacilityUuid = :facilityUuid OR
89108 BloodSugar.facilityUuid = :facilityUuid
90- )
109+ )
91110 AND P.deletedAt IS NULL
92111 AND P.status = :patientStatus
93112 )
@@ -96,14 +115,12 @@ data class RecentPatient(
96115 }
97116
98117 /* *
99- Goal: Fetch a list of patients with 10 most recent changes.
100- There are tables like BloodPressureMeasurement (BP), PrescribedDrug (PD), Appointment (AP), etc. Let’s call each table T1, T2, T3, etc.
101-
102- Algo:
103- 1. Get a list of all patients
104- 2. For each patient, from each table T, get the latest change for them. Columns: T1.latestUpdatedAt, T2.latestUpdatedAt, etc.
105- 3. Pick latestUpdatedAt for each patient
106- 4. Order by updatedAt from final list and cap it to 10 entries.
118+ * Goal: Fetch a list of patients with 10 most recent changes.
119+ * Algo:
120+ * 1. Get a list of all patients
121+ * 2. For each patient, from each table T, get the latest change for them.
122+ * 3. Pick latestUpdatedAt for each patient
123+ * 4. Order by updatedAt from final list and cap it to the limit.
107124 */
108125 @Query(" $RECENT_PATIENT_QUERY LIMIT :limit" )
109126 fun recentPatientsWithLimit (
0 commit comments