diff --git a/app/family-history/family-history.service.js b/app/family-history/family-history.service.js index a074b5671..eb489d0bd 100644 --- a/app/family-history/family-history.service.js +++ b/app/family-history/family-history.service.js @@ -48,12 +48,23 @@ export class FamilyTestingService { FROM etl.flat_family_testing WHERE - location_uuid = '${params.locationUuid}' - GROUP BY patient_id) t2 ON (t1.patient_id = t2.patient_id) `; + location_id in (${params.locations}) + GROUP BY patient_id) t2 ON (t1.patient_id = t2.patient_id) + inner join (SELECT + location_id, person_id,is_clinical_encounter + FROM + etl.flat_hiv_summary_v15b t4 + WHERE + is_clinical_encounter = 1 + AND t4.location_id in (${params.locations}) + AND date(encounter_datetime) > '2020-07-30' + group by t4.person_id + ORDER BY encounter_datetime DESC + ) t3 on (t3.person_id = tx.person_id) `; where = ` WHERE - tx.location_uuid = '${params.locationUuid}' `; + t3.location_id in (${params.locations}) `; if (params.start_date != null && params.end_date != null) { where = @@ -92,21 +103,11 @@ 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`; + where = `${where} and tx.person_id in (select patient_id from etl.flat_family_testing where t3.location_id in (${params.locations}) ) group by tx.person_id`; sql = sql + ' tx.*, extract(year from (from_days(datediff(now(),tx.birthdate)))) as age, tx.gender as index_gender ' + @@ -368,4 +369,66 @@ export class FamilyTestingService { }); }); }; + + allReviewedClientsQuery(params, sql, from) { + let w = ` WHERE t3.location_id in (${params.locations}) `; + 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}')) + OR (DATE(tx.encounter_datetime) BETWEEN date('${params.start_date}') and date('${params.end_date}')))`; + } 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}'))) `; + } + + 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 t3.location_id in (${params.locations}) `; + 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; + } } diff --git a/app/routes/family-history.route.js b/app/routes/family-history.route.js index 4d0a6d2b3..aab4e387f 100644 --- a/app/routes/family-history.route.js +++ b/app/routes/family-history.route.js @@ -1,6 +1,8 @@ -var authorizer = require('../../authorization/etl-authorizer'); import { FamilyTestingService } from '../family-history/family-history.service'; +var authorizer = require('../../authorization/etl-authorizer'); var privileges = authorizer.getAllPrivileges(); +var etlHelpers = require('../../etl-helpers'); +var preRequest = require('../../pre-request-processing'); const routes = [ { @@ -13,23 +15,35 @@ const routes = [ } }, handler: function (request, reply) { - let familyTestingService = new FamilyTestingService(); - let params = { - locationUuid: request.query.locationUuid, - eligible: request.query.eligible, - start_date: request.query.start_date, - end_date: request.query.end_date, - programs: request.query.program_type, - child_status: request.query.child_status, - elicited_clients: request.query.elicited_clients - }; + preRequest.resolveLocationIdsToLocationUuids(request, function () { + let requestParams = Object.assign({}, request.query, request.params); + + let requestCopy = _.cloneDeep(requestParams); + let reportParams = etlHelpers.getReportParams( + '', + ['startDate', 'endDate', 'locationUuid', 'locations'], + requestParams + ); + + requestCopy.locationUuids = reportParams.requestParams.locationUuids; + let familyTestingService = new FamilyTestingService(); + let params = { + locations: reportParams.requestParams.locations, + eligible: request.query.eligible, + start_date: request.query.start_date, + end_date: request.query.end_date, + programs: request.query.program_type, + child_status: request.query.child_status, + elicited_clients: request.query.elicited_clients + }; - familyTestingService.getPatientList(params).then((result) => { - if (result.error) { - reply(result); - } else { - reply(result); - } + familyTestingService.getPatientList(params).then((result) => { + if (result.error) { + reply(result); + } else { + reply(result); + } + }); }); }, description: 'Family testing patient list',