Skip to content

Commit

Permalink
Show right visit after DC enrolment through program manager (#1193)
Browse files Browse the repository at this point in the history
Co-authored-by: Kipchumba C. Bett <kbett68@gmail.com>
  • Loading branch information
jecihjoy and corneliouzbett authored Jan 25, 2023
1 parent 9556056 commit 01e9fef
Showing 1 changed file with 40 additions and 8 deletions.
48 changes: 40 additions & 8 deletions service/dc-patient-visit-evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}';`;

Expand Down

0 comments on commit 01e9fef

Please sign in to comment.