diff --git a/app/family-history/family-history.service.js b/app/family-history/family-history.service.js index a074b5671..82a3bf912 100644 --- a/app/family-history/family-history.service.js +++ b/app/family-history/family-history.service.js @@ -92,19 +92,9 @@ export class FamilyTestingService { } if (params.elicited_clients == 0) { - where = `${where} group by tx.person_id`; - sql = - sql + - ' tx.*, extract(year from (from_days(datediff(now(),tx.birthdate)))) as age, tx.gender as index_gender ' + - from + - where; + sql = this.allReviewedClientsQuery(params, sql, from); } else if (params.elicited_clients < 0) { - where = `${where} and obs_group_id is null `; - sql = - sql + - ' tx.*, extract(year from (from_days(datediff(now(),tx.birthdate)))) as age, tx.gender as index_gender ' + - from + - where; + sql = this.clientsWithoutContactsQuery(params, sql, from); } else if (params.elicited_clients > 0) { where = `${where} and tx.person_id in (select patient_id from etl.flat_family_testing where location_uuid = '${params.locationUuid}' ) group by tx.person_id`; sql = @@ -368,4 +358,66 @@ export class FamilyTestingService { }); }); }; + + allReviewedClientsQuery(params, sql, from) { + let w = ` WHERE tx.location_uuid = '${params.locationUuid}' `; + if (params.start_date != null && params.end_date != null) { + w = + w + + ` AND ((DATE(t1.date_elicited) BETWEEN date('${params.start_date}') and date('${params.end_date}') AND obs_group_id IS NOT NULL) + OR (DATE(tx.encounter_datetime) BETWEEN date('${params.start_date}') and date('${params.end_date}') and obs_group_id IS NULL))`; + } else if (params.end_date != null) { + w = + w + + ` and (date(t1.date_elicited) <= date('${params.end_date}') or (date(tx.encounter_datetime) <= date('${params.end_date}') and obs_group_id IS NULL)) `; + } + + 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 ' + + from + + w + + ' group by tx.person_id'; + + return sql; + } + + clientsWithoutContactsQuery(params, sql, from) { + let w = ` WHERE tx.location_uuid = '${params.locationUuid}' `; + 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 ' + + from + + w + + ' and obs_group_id is null'; + + return sql; + } }