Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patient verification report summary #1201

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions app/routes/patient-verification-report.routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { PatientVerificationReport } from '../../service/patient-verification-report';
const routes = [
{
method: 'GET',
path: '/etl/verification-aggregates',
config: {
handler: function (request, reply) {
let service = new PatientVerificationReport();
service
.generateAggregates(request.query)
.then((res) => reply(res))
.catch((err) => {
console.log('ERROR l13', err);
reply(err);
});
},
description: 'Get all verification aggregates ',
notes: 'Returns all verification aggregates',
tags: ['api'],
validate: {
options: {
allowUnknown: true
},
params: {}
}
}
},
{
method: 'GET',
path: '/etl/unverified-clients',
config: {
handler: function (request, reply) {
let service = new PatientVerificationReport();
service
.generateUnverifiedClients(request.query)
.then((res) => reply(res))
.catch((err) => {
console.log('ERROR l38', err);
reply(err);
});
},
description: 'Get all unverified clients ',
notes: 'Returns all unverified clients',
tags: ['api'],
validate: {
options: {
allowUnknown: true
},
params: {}
}
}
}
];
exports.routes = (server) => server.route(routes);
132 changes: 132 additions & 0 deletions service/patient-verification-report.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
var db = require('../etl-db');
const Promise = require('bluebird');

export class PatientVerificationReport {
generateAggregates(params) {
const that = this;
return new Promise(function (resolve, reject) {
const sql = that.aggregateQuery(params.locationUuid, params.curDate);
const queryParts = {
sql: sql
};
return db.queryServer(queryParts, function (result) {
resolve(result);
});
});
}

generateUnverifiedClients(params) {
const self = this;
return new Promise(function (resolve, reject) {
const queryParts = {
sql: self.unverifiedQuery(params.locationUuid)
};
return db.queryServer(queryParts, function (result) {
resolve(result);
});
});
}

aggregateQuery(location, curDate) {
return `SELECT
SUM(verified) AS total_verified,
SUM(unverified) AS total_unverified,
SUM(on_art) AS total_on_art,
SUM(verified_today) AS total_verified_today
FROM
(SELECT
hmsd.person_id,
upi.identifier,
hmsd.location_id,
CASE
WHEN upi.patient_id IS NOT NULL THEN 1
ELSE 0
END AS verified,
CASE
WHEN upi.patient_id IS NULL THEN 1
ELSE 0
END AS unverified,
CASE
WHEN hmsd.person_id IS NOT NULL THEN 1
ELSE 0
END AS on_art,
CASE
WHEN DATE(upi.date_created) = DATE('${curDate}') THEN 1
ELSE 0
END AS verified_today
FROM
etl.hiv_monthly_report_dataset_v1_2 hmsd
LEFT JOIN amrs.patient_identifier upi ON (upi.patient_id = hmsd.person_id
AND upi.identifier_type = 45
AND upi.voided = 0)
LEFT JOIN etl.moh_731_last_release_month m ON (hmsd.endDate = m.last_released_month)
left join amrs.location l on (l.location_id = hmsd.location_id)
WHERE
(DATE(endDate) = DATE(m.last_released_month))
AND l.uuid = '${location}'
AND (hmsd.on_art_this_month = 1)
GROUP BY hmsd.person_id) in_h;`;
}

unverifiedQuery(locationUuid) {
return `SELECT
hmsd.gender AS gender,
hmsd.birthdate AS birthdate,
hmsd.age AS age,
cc.identifier AS 'CCC',
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(fh.rtc_date, '%d-%m-%Y') AS next_appointment_date,
CONCAT(COALESCE(DATE_FORMAT(fh.encounter_datetime, '%Y-%m-%d'),''),' ',COALESCE(et.name, '')) as last_appoointment
FROM
etl.hiv_monthly_report_dataset_v1_2 hmsd
INNER JOIN
amrs.location l ON (l.location_id = hmsd.location_id)
INNER JOIN
amrs.person t1 ON (hmsd.person_id = t1.person_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_hiv_summary_v15b fh ON (t1.person_id = fh.person_id
AND fh.next_clinical_datetime_hiv IS NULL
AND fh.is_clinical_encounter = 1)
LEFT JOIN
amrs.encounter_type et ON (fh.encounter_type = et.encounter_type_id)
LEFT JOIN
amrs.person_address pa ON (t1.person_id = pa.person_id)
LEFT JOIN
amrs.patient_identifier cc ON (fh.person_id = cc.patient_id
AND cc.identifier_type = 28
AND cc.voided = 0)
LEFT JOIN
amrs.patient_identifier upi ON (upi.patient_id = fh.person_id
AND upi.identifier_type = 45
AND upi.voided = 0)
LEFT JOIN
etl.moh_731_last_release_month m ON (hmsd.endDate = m.last_released_month)
WHERE
(DATE(endDate) = DATE(m.last_released_month))
AND l.uuid = '${locationUuid}'
AND (hmsd.on_art_this_month = 1)
AND upi.patient_id is null
GROUP BY hmsd.person_id;`;
}
}