From 01e9fefec5abecb652442e0cefba387d6ab8d804 Mon Sep 17 00:00:00 2001 From: Jacinta Gichuhi Date: Wed, 25 Jan 2023 11:59:33 +0300 Subject: [PATCH] Show right visit after DC enrolment through program manager (#1193) Co-authored-by: Kipchumba C. Bett --- service/dc-patient-visit-evaluator.js | 48 ++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/service/dc-patient-visit-evaluator.js b/service/dc-patient-visit-evaluator.js index 065a8b935..bf7f1a4fa 100644 --- a/service/dc-patient-visit-evaluator.js +++ b/service/dc-patient-visit-evaluator.js @@ -13,25 +13,41 @@ function getPatientQualifiedDcVisits(patientUuid) { latest_standard_visit.*, latest_dc_enrolment_visit.*, CASE - WHEN latest_dc_enrolment_visit.latest_dc_enrolment_visit is null THEN @recent_dc_enrolment:=0 + WHEN latest_dc_enrolment_visit.latest_dc_enrolment_visit is null THEN @recent_dc_enrolment:=0 + WHEN date(latest_dc_enrolment_visit.latest_dc_enrolment_visit) < date(latest_new_dc_enrollment_date.latest_new_dc_enrollment_date) THEN @recent_dc_enrolment:=0 WHEN (latest_dc_enrolment_visit.latest_dc_enrolment_visit > latest_medication_visit.latest_medication_visit - AND latest_dc_enrolment_visit.latest_dc_enrolment_visit > latest_standard_visit.latest_standard_visit) THEN @recent_dc_enrolment:=1 - WHEN latest_dc_enrolment_visit.latest_dc_enrolment_visit > latest_medication_visit.latest_medication_visit - AND latest_standard_visit.latest_standard_visit is null THEN @recent_dc_enrolment:=1 - WHEN latest_dc_enrolment_visit.latest_dc_enrolment_visit > latest_standard_visit.latest_standard_visit - AND latest_medication_visit.latest_medication_visit is null THEN @recent_dc_enrolment:=1 + AND latest_dc_enrolment_visit.latest_dc_enrolment_visit > latest_standard_visit.latest_standard_visit) THEN @recent_dc_enrolment:=1 + WHEN latest_dc_enrolment_visit.latest_dc_enrolment_visit > latest_medication_visit.latest_medication_visit + AND latest_standard_visit.latest_standard_visit is null THEN @recent_dc_enrolment:=1 + WHEN latest_dc_enrolment_visit.latest_dc_enrolment_visit > latest_standard_visit.latest_standard_visit + AND latest_medication_visit.latest_medication_visit is null THEN @recent_dc_enrolment:=1 ELSE @recent_dc_enrolment:=0 END AS 'recent_dc_enrolment', @recent_dc_enrolment, CASE - WHEN @recent_dc_enrolment = 1 THEN 1 + WHEN latest_new_dc_enrollment_date.latest_new_dc_enrollment_date is null THEN @recent_dc_enrolment_program_manager:=0 + WHEN (latest_new_dc_enrollment_date.latest_new_dc_enrollment_date > latest_medication_visit.latest_medication_visit + AND latest_new_dc_enrollment_date.latest_new_dc_enrollment_date > latest_standard_visit.latest_standard_visit) THEN @recent_dc_enrolment_program_manager:=1 + WHEN latest_new_dc_enrollment_date.latest_new_dc_enrollment_date > latest_medication_visit.latest_medication_visit + AND latest_standard_visit.latest_standard_visit is null THEN @recent_dc_enrolment_program_manager:=1 + WHEN latest_new_dc_enrollment_date.latest_new_dc_enrollment_date > latest_standard_visit.latest_standard_visit + AND latest_medication_visit.latest_medication_visit is null THEN @recent_dc_enrolment_program_manager:=1 + WHEN latest_new_dc_enrollment_date.latest_new_dc_enrollment_date is not null and latest_medication_visit.latest_medication_visit is null + AND latest_standard_visit.latest_standard_visit is null then @recent_dc_enrolment_program_manager:=1 + ELSE @recent_dc_enrolment_program_manager:=0 + END AS 'recent_dc_enrolment_program_manager', + @recent_dc_enrolment_program_manager, + CASE + WHEN @recent_dc_enrolment = 1 THEN 1 + WHEN @recent_dc_enrolment_program_manager = 1 THEN 0 WHEN latest_standard_visit.latest_standard_visit > latest_medication_visit.latest_medication_visit THEN 1 WHEN latest_standard_visit.latest_standard_visit is not null AND latest_medication_visit.latest_medication_visit is NULL THEN 1 WHEN latest_standard_visit.latest_standard_visit is null AND latest_medication_visit.latest_medication_visit is NULL THEN 1 ELSE 0 END AS 'qualifies_for_medication_refill', CASE - WHEN @recent_dc_enrolment = 1 THEN 0 + WHEN @recent_dc_enrolment = 1 THEN 0 + WHEN @recent_dc_enrolment_program_manager = 1 THEN 1 WHEN latest_medication_visit.latest_medication_visit > latest_standard_visit.latest_standard_visit THEN 1 WHEN latest_medication_visit.latest_medication_visit IS NOT NULL AND latest_standard_visit.latest_standard_visit is NULL THEN 1 ELSE 0 @@ -81,6 +97,22 @@ FROM AND pp.program_id is not null AND p.uuid = '${patientUuid}' order by encounter_datetime desc limit 1) latest_dc_enrolment_visit on (latest_dc_enrolment_visit.patient_id = p.person_id) + LEFT JOIN ( + SELECT + p.person_id, + pp.date_enrolled as latest_new_dc_enrollment_date +FROM + amrs.person p + INNER JOIN + amrs.patient_program pp ON (pp.patient_id = p.person_id + AND pp.program_id = 9 + AND date_completed IS NULL + AND pp.voided = 0) + WHERE + p.uuid = '${patientUuid}' + ORDER BY program_id DESC + LIMIT 1 + ) latest_new_dc_enrollment_date on (latest_new_dc_enrollment_date.person_id = p.person_id) WHERE p.voided = 0 AND p.uuid = '${patientUuid}';`;