Skip to content

Commit

Permalink
Family testing dashboard enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
jecihjoy committed Nov 8, 2022
1 parent de08392 commit 3fcd774
Showing 1 changed file with 115 additions and 67 deletions.
182 changes: 115 additions & 67 deletions app/family-history/family-history.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,49 +94,60 @@ export class FamilyTestingService {
}

if (params.elicited_clients == 0) {
//all reviewed clients
//all screened clients
sql = this.allReviewedClientsQuery(params, sql, from);
} else if (params.elicited_clients == 1) {
//Reviewed Without Contacts
sql = this.clientsWithoutContactsQuery(params, sql, from);
} else if (params.elicited_clients == 3) {
//Unreviewed Clients
sql = this.activeNotReviewed(params);
//Screened and reviewed for children
sql = this.screenedAndReviewed(params, sql, from);
} else if (params.elicited_clients == 2) {
//Reviewed With Contacts
where = `${where} and tx.person_id in (select patient_id from etl.flat_family_testing where t3.last_non_transit_location_id in (${params.locations}) ) group by tx.person_id`;
//Screened not reviewed for children
sql = this.screenedNotReviewed(params, sql, from);
} else if (params.elicited_clients == 3) {
//Clients not screened for contacts
sql = this.activeNotScreened(params);
} else if (params.elicited_clients == 4) {
//'Children 19yrs and below'
sql =
sql +
' tx.*, extract(year from (from_days(datediff(now(),tx.birthdate)))) as age, tx.gender as index_gender, 1 as hideContactColumns ' +
columns +
from +
where +
' LIMIT 1000 OFFSET ' +
' and obs_group_id is not null and extract(year from (from_days(datediff(t1.date_elicited,t1.fm_dob)))) < 20 AND t1.relationship_type in ("CHILD","MOTHER","FATHER") AND fm_name is not null LIMIT 2000 OFFSET ' +
params.startIndex;
} else if (params.elicited_clients == 4) {
//'Children 19yrs and below'
} else if (params.elicited_clients == 5) {
//'Sexual partners'
sql =
sql +
columns +
from +
where +
' and obs_group_id is not null and extract(year from (from_days(datediff(t1.date_elicited,t1.fm_dob)))) < 20 LIMIT 1000 OFFSET ' +
' and obs_group_id is not null and t1.relationship_type in ("PARTNER-SPOUSE","PARTNER-OTHER","FELLOW-WIFE") AND fm_name is not null LIMIT 2000 OFFSET ' +
params.startIndex;
} else if (params.elicited_clients == 5) {
//'Contacts above 19 yrs'
} else if (params.elicited_clients == 6) {
//'Siblings'
sql =
sql +
columns +
from +
where +
' and obs_group_id is not null and t1.relationship_type in ("SIBLING") AND fm_name is not null LIMIT 2000 OFFSET ' +
params.startIndex;
} else if (params.elicited_clients == 7) {
//'Uncategorized contacts'
sql =
sql +
columns +
from +
where +
' and obs_group_id is not null and extract(year from (from_days(datediff(t1.date_elicited,t1.fm_dob)))) >= 20 LIMIT 1000 OFFSET ' +
' and obs_group_id is not null and t1.relationship_type not in ("SIBLING","PARTNER-SPOUSE","PARTNER-OTHER","FELLOW-WIFE","CHILD","MOTHER","FATHER") AND fm_name is not null LIMIT 2000 OFFSET ' +
params.startIndex;
} else {
sql =
sql +
columns +
from +
where +
' and obs_group_id is not null LIMIT 1000 OFFSET ' +
' and obs_group_id is not null AND fm_name is not null LIMIT 2000 OFFSET ' +
params.startIndex;
}

Expand Down Expand Up @@ -427,13 +438,49 @@ export class FamilyTestingService {
' tx.*, extract(year from (from_days(datediff(now(),tx.birthdate)))) as age, tx.gender as index_gender, 1 as hideContactColumns ' +
from +
w +
' group by tx.person_id LIMIT 1000 OFFSET ' +
' group by tx.person_id LIMIT 2000 OFFSET ' +
params.startIndex;
return sql;
}

clientsWithoutContactsQuery(params, sql, from) {
let w = ` WHERE t3.last_non_transit_location_id in (${params.locations}) `;
screenedNotReviewed(params, sql, from) {
let w = ` WHERE t3.last_non_transit_location_id in (${params.locations})
AND tx.child_status IS NULL `;
if (params.start_date != null && params.end_date != null) {
w =
w +
` and date(tx.encounter_datetime) between date('${params.start_date}') and date('${params.end_date}')`;
} else if (params.end_date != null) {
w =
w + ` and date(tx.encounter_datetime) <= date('${params.end_date}') `;
}

switch (params.child_status) {
case '1':
w = w + ` and tx.child_status_reason = 11890`;
break;
case '0':
w = w + ` and tx.child_status_reason = 11891`;
break;
}

sql =
sql +
' tx.*, extract(year from (from_days(datediff(now(),tx.birthdate)))) as age, tx.gender as index_gender, 1 as hideContactColumns ' +
from +
w +
' group by tx.person_id LIMIT 2000 OFFSET ' +
params.startIndex;

return sql;
}

screenedAndReviewed(params, sql, from) {
let w = ` WHERE t3.last_non_transit_location_id in (${params.locations})
AND (IF(tx.child_status IS NOT NULL or (obs_group_id IS NOT NULL
AND EXTRACT(YEAR FROM (FROM_DAYS(DATEDIFF(t1.date_elicited, t1.fm_dob)))) < 20
AND t1.relationship_type IN ('CHILD' , 'MOTHER', 'FATHER')
AND fm_name IS NOT NULL) ,1,0) = 1) `;
if (params.start_date != null && params.end_date != null) {
w =
w +
Expand All @@ -457,57 +504,58 @@ export class FamilyTestingService {
' tx.*, extract(year from (from_days(datediff(now(),tx.birthdate)))) as age, tx.gender as index_gender, 1 as hideContactColumns ' +
from +
w +
' and obs_group_id is null LIMIT 1000 OFFSET ' +
' group by tx.person_id LIMIT 2000 OFFSET ' +
params.startIndex;

return sql;
}

activeNotReviewed(params) {
return `SELECT Extract(year FROM ( From_days(Datediff(Now(), t2.birthdate)) ))
AS
age,
t2.gender
AS index_gender,
Concat(Coalesce(person_name.given_name, ''), ' ', Coalesce(
person_name.middle_name, ''), ' ', Coalesce(person_name.family_name, ''))
AS
person_name,
Group_concat(DISTINCT id.identifier SEPARATOR ', ')
AS identifiers,
Group_concat(DISTINCT contacts.value SEPARATOR ', ')
AS phone_number,
Date_format(t1.arv_first_regimen_start_date, "%d-%m-%y")
AS
arv_first_regimen_start_date,
pr.name
AS patient_program_name, 1 as hideContactColumns
FROM etl.flat_hiv_summary_v15b t1
INNER JOIN amrs.person t2
ON ( t2.person_id = t1.person_id )
LEFT JOIN amrs.patient_program pp
ON ( t1.person_id = pp.patient_id )
LEFT JOIN amrs.program pr
ON ( pr.program_id = pp.program_id )
LEFT JOIN amrs.person_name person_name
ON ( t1.person_id = person_name.person_id
AND ( person_name.voided IS NULL
|| person_name.voided = 0 )
AND person_name.preferred = 1 )
LEFT JOIN amrs.patient_identifier id
ON ( t1.person_id = id.patient_id
AND ( id.voided IS NULL
|| id.voided = 0 ) )
LEFT JOIN amrs.person_attribute contacts
ON ( t1.person_id = contacts.person_id
AND ( contacts.voided IS NULL
|| contacts.voided = 0 )
AND contacts.person_attribute_type_id IN ( 10, 48 ) )
Left join etl.flat_family_testing_index ft on (t1.person_id = ft.person_id)
WHERE t1.last_non_transit_location_id IN ( ${params.locations} )
AND t1.next_clinical_datetime_hiv IS NULL
AND is_clinical_encounter = 1
AND Timestampdiff(day, t1.rtc_date, Now()) < 30 AND ft.person_id is null
GROUP BY t1.person_id LIMIT 300 OFFSET ${params.startIndex} ;`;
activeNotScreened(params) {
return `SELECT
EXTRACT(YEAR FROM (FROM_DAYS(DATEDIFF(NOW(), t2.birthdate)))) AS age,
t2.gender AS index_gender,
CONCAT(COALESCE(person_name.given_name, ''),
' ',
COALESCE(person_name.middle_name, ''),
' ',
COALESCE(person_name.family_name, '')) AS person_name,
GROUP_CONCAT(DISTINCT id.identifier
SEPARATOR ', ') AS identifiers,
GROUP_CONCAT(DISTINCT contacts.value
SEPARATOR ', ') AS phone_number,
DATE_FORMAT(t1.arv_first_regimen_start_date,
'%d-%m-%y') AS arv_first_regimen_start_date,
pr.name AS patient_program_name,
1 AS hideContactColumns
FROM
amrs.person t2
INNER JOIN
etl.flat_hiv_summary_v15b t1 ON (t1.person_id = t2.person_id
AND t1.last_non_transit_location_id IN ( ${params.locations} )
AND t1.next_clinical_datetime_hiv IS NULL
AND is_clinical_encounter = 1
AND TIMESTAMPDIFF(DAY, t1.rtc_date, NOW()) < 30)
LEFT JOIN
amrs.patient_program pp ON (t1.person_id = pp.patient_id)
LEFT JOIN
amrs.program pr ON (pr.program_id = pp.program_id)
LEFT JOIN
amrs.person_name person_name ON (t1.person_id = person_name.person_id
AND (person_name.voided IS NULL
|| person_name.voided = 0)
AND person_name.preferred = 1)
LEFT JOIN
amrs.patient_identifier id ON (t1.person_id = id.patient_id
AND (id.voided IS NULL || id.voided = 0))
LEFT JOIN
amrs.person_attribute contacts ON (t1.person_id = contacts.person_id
AND (contacts.voided IS NULL
|| contacts.voided = 0)
AND contacts.person_attribute_type_id IN (10 , 48))
LEFT JOIN
etl.flat_family_testing_index ft ON (t1.person_id = ft.person_id)
WHERE
ft.person_id IS NULL
GROUP BY t1.person_id LIMIT 300 OFFSET ${params.startIndex};`;
}
}

0 comments on commit 3fcd774

Please sign in to comment.