Skip to content

Commit

Permalink
HIV-705: Family testing feature enhancements (#1128)
Browse files Browse the repository at this point in the history
  • Loading branch information
jecihjoy authored Dec 22, 2021
1 parent 1a58a69 commit 5eb42ea
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 38 deletions.
110 changes: 88 additions & 22 deletions app/family-history/family-history.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export class FamilyTestingService {
return new Promise((resolve, reject) => {
let queryParts = {};
let where = '';
let sql = `SELECT
t1.*, t2.contacts_count,p.gender as index_gender,
let sql = 'SELECT ';
const columns = `t1.*,tx.*, tx.encounter_datetime as last_date_elicited, t2.contacts_count,tx.gender as index_gender,
case
when eligible_for_testing = 1065 then 'YES'
when eligible_for_testing = 1066 then 'No'
Expand All @@ -34,28 +34,29 @@ export class FamilyTestingService {
else fm_status
end as modified_fm_status,
date_format(current_test_date,"%d-%m-%Y") as modified_current_test_date,
extract(year from (from_days(datediff(now(),p.birthdate)))) as age
FROM
etl.flat_family_testing t1
INNER JOIN
(SELECT
patient_id, COUNT(*) AS 'contacts_count'
FROM
etl.flat_family_testing
WHERE
location_uuid = '${params.locationUuid}'
GROUP BY patient_id) t2 ON (t1.patient_id = t2.patient_id)
INNER JOIN amrs.person p on (t1.patient_id = p.person_id)
`;
extract(year from (from_days(datediff(now(),tx.birthdate)))) as age `;

const from = `FROM
etl.flat_family_testing_index tx
LEFT JOIN
etl.flat_family_testing t1 on (tx.person_id = t1.patient_id)
LEFT JOIN
(SELECT
patient_id, COUNT(*) AS 'contacts_count'
FROM
etl.flat_family_testing
WHERE
location_uuid = '${params.locationUuid}'
GROUP BY patient_id) t2 ON (t1.patient_id = t2.patient_id) `;

where = `
WHERE
location_uuid = '${params.locationUuid}'`;
tx.location_uuid = '${params.locationUuid}' `;

if (params.start_date != null && params.end_date != null) {
where =
where +
` and date(date_elicited) between date('${params.start_date}') and date('${params.end_date}')`;
` and date(tx.encounter_datetime) between date('${params.start_date}') and date('${params.end_date}')`;
}

if (params.eligible != null) {
Expand All @@ -73,11 +74,33 @@ export class FamilyTestingService {
}
}

where = where + ` and patient_program_uuid in (${program})`;
where = where + ` and tx.patient_program_uuid in (${program})`;
}

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

if (params.elicited_clients == 0) {
where = `${where} group by tx.person_id`;
sql = sql + ' tx.* ' + from + where;
} else if (params.elicited_clients < 0) {
where = `${where} and obs_group_id is null `;
sql = sql + ' tx.* ' + from + where;
} 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 = sql + ' tx.* ' + from + where;
} else {
sql = sql + columns + from + where + ' and obs_group_id is not null ';
}

queryParts = {
sql: sql + where
sql: sql
};
return db.queryServer(queryParts, function (result) {
result.sql = sql;
Expand All @@ -89,7 +112,29 @@ export class FamilyTestingService {
getPatientContacts = (params) => {
return new Promise((resolve, reject) => {
let queryParts = {};
let sql = `select *,
let sql = `select t1.*,
case
when children_testing_consent_given = 1065 then 'YES'
when children_testing_consent_given = 1066 then 'NO'
end as children_testing_consent_given,
case
when child_status = 1065 then 'YES'
when child_status = 1066 then 'NO'
when child_status = 1175 then 'Not Applicable'
end as child_status,
case
when child_status_reason = 11890 then 'Children above 19yrs'
when child_status_reason = 11891 then 'No child'
end as child_status_reason,
case
when children_elicited_by_partner = 1065 then 'YES'
when children_elicited_by_partner = 1066 then 'NO'
end as children_elicited_by_partner,
case
when female_partner_status = 10901 then 'unknown HIV status'
when female_partner_status = 9584 then 'Not in care'
when female_partner_status = 159 then 'Deceased'
end as female_partner_status,
case
when fm_uuid is not null then true
when fm_status = 'POSITIVE' or test_result = 703 then false
Expand Down Expand Up @@ -126,8 +171,29 @@ export class FamilyTestingService {
when fm_status is null then 'UNKNOWN'
else fm_status
end as modified_fm_status,
date_format(current_test_date,"%d-%m-%Y") as modified_current_test_date
from etl.flat_family_testing where patient_uuid = '${params.patientUuid}'`;
date_format(current_test_date,"%d-%m-%Y") as modified_current_test_date,
case
when children_count > 0 and child_status is null then 'YES'
when children_count is null and child_status is null then 'NO'
end as children_elicited,
tx.encounter_datetime,
t1.date_elicited,
tx.updated_elicitation_date,
tx.updated_elicitation_date_alert
FROM
etl.flat_family_testing_index tx
LEFT JOIN
etl.flat_family_testing t1 on (tx.person_id = t1.patient_id)
LEFT JOIN
(SELECT patient_id, COUNT(*) AS 'children_count'
FROM
etl.flat_family_testing
WHERE
patient_uuid = '${params.patientUuid}'
and fm_age < 20) t2
ON (t1.patient_id = t2.patient_id)
where tx.patient_uuid = '${params.patientUuid}'`;
/*
1.eligible_for_tracing = 0, not eligible for testing
2.eligible_for_tracing = 1, traced and tested
Expand Down
4 changes: 3 additions & 1 deletion app/routes/family-history.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ const routes = [
eligible: request.query.eligible,
start_date: request.query.start_date,
end_date: request.query.end_date,
programs: request.query.program_type
programs: request.query.program_type,
child_status: request.query.child_status,
elicited_clients: request.query.elicited_clients
};

familyTestingService.getPatientList(params).then((result) => {
Expand Down
41 changes: 28 additions & 13 deletions service/patient-reminder.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var config = require('../conf/config.json');
var encounter_service = require('./openmrs-rest/encounter');
var program_service = require('./openmrs-rest/program.service');
const cervicalCancerScreeningService = require('./cervical-cancer-screening-service');
import { FamilyTestingService } from './../app/family-history/family-history.service';

var serviceDef = {
generateReminders: generateReminders,
Expand Down Expand Up @@ -516,8 +517,8 @@ function getFamilyTestingReminder(data) {
if (res.results.length === 0) {
reminders.push({
message:
'No contact tracing has been done for this index, please fill the contact tracing form',
title: 'Contact Tracing Reminder',
'No elicitation has been done for this index, please elicit for contacts',
title: 'Contact Elicitation Reminder',
type: 'warning',
display: {
banner: true,
Expand All @@ -527,18 +528,20 @@ function getFamilyTestingReminder(data) {
addContacts: true
});
return reminders;
} else if (res.results.length > 0) {
let months = 0;
if (res.results[0].auditInfo.dateChanged != null) {
months = Moment().diff(res.results[0].auditInfo.dateChanged, 'months');
} else {
months = Moment().diff(res.results[0].encounterDatetime, 'months');
}
if (months > 6) {
}
let params = {
patientUuid: data[0].person_uuid
};
const service = new FamilyTestingService();
return service.getPatientContacts(params).then((r) => {
const maxDate = getMaxElicitationDate(r.result);
const months = Moment().diff(Moment(maxDate), 'months');

if (months > 12) {
reminders.push({
message:
"It's been six months since the patient's contacts were last updated, click update to add more contacts",
title: 'Contact Tracing Reminder',
'It has been one year since last elicitation. Do you have more contacts to add?',
title: 'Contact Elicitation Reminder',
type: 'info',
display: {
banner: true,
Expand All @@ -549,10 +552,22 @@ function getFamilyTestingReminder(data) {
});
}
return reminders;
}
});
});
}

function getMaxElicitationDate(contacts) {
const dates = [];
_.each(contacts, (c) => {
dates.push(new Date(c.encounter_datetime));
dates.push(new Date(c.date_elicited));
dates.push(new Date(c.updated_elicitation_date));
dates.push(new Date(c.updated_elicitation_date_alert));
});

return new Date(Math.max.apply(null, dates));
}

function ovcUnenrollmentReminder(data) {
let reminders = [];
return getPatientPrograms(data.person_uuid, {
Expand Down
2 changes: 1 addition & 1 deletion service/surge-reports/surge-report.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export class SurgeService extends SurgeMultiDatasetPatientlistReport {

determineSurgeReportSourceTables(yearWeek) {
const self = this;
if (yearWeek.slice(4) >= moment().week() - 1) {
if (yearWeek >= moment().year() + '' + moment().week() - 1) {
return (self.params.surgeWeeklyDatasetSource =
'etl.surge_weekly_report_dataset');
} else {
Expand Down
3 changes: 2 additions & 1 deletion service/surge-reports/surge-weeks.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const Promise = require('bluebird');
const surgeWeeksDao = {
getSurgeWeeks: () => {
return new Promise((resolve, reject) => {
let sql = 'select * from etl.surge_week order by start_date desc';
let sql =
'select date_format(start_date,"%Y-%m-%d") as start_date , date_format(end_date,"%Y-%m-%d") as end_date, week, formatted_week from etl.surge_week_prod order by start_date desc';
let queryParts = {
sql: sql
};
Expand Down

0 comments on commit 5eb42ea

Please sign in to comment.