Skip to content

Commit

Permalink
HIV-705: family history report bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jecihjoy committed Feb 4, 2022
1 parent 9bcc95a commit f6dad27
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 33 deletions.
95 changes: 79 additions & 16 deletions app/family-history/family-history.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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 ' +
Expand Down Expand Up @@ -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;
}
}
48 changes: 31 additions & 17 deletions app/routes/family-history.route.js
Original file line number Diff line number Diff line change
@@ -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 = [
{
Expand All @@ -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',
Expand Down

0 comments on commit f6dad27

Please sign in to comment.