diff --git a/app/reporting-framework/base-mysql.report.js b/app/reporting-framework/base-mysql.report.js index dcb3f3803..b61654633 100755 --- a/app/reporting-framework/base-mysql.report.js +++ b/app/reporting-framework/base-mysql.report.js @@ -221,6 +221,10 @@ import * as hiv_cervical_cancer_screening_monthly_main_dataset_base from './json import * as hiv_cervical_cancer_monthly_summary_lesions_base from './json-reports/hiv-cervical-cancer-monthly-summary-lesions-base.json'; import * as hiv_cervical_cancer_monthly_summary_lesions_aggregate from './json-reports/hiv-cervical-cancer-monthly-summary-lesions-aggregate.json'; import * as hiv_cervical_cancer_monthly_summary_lesions_pcf_aggregate from './json-reports/hiv-cervical-cancer-monthly-summary-lesions-pcf-aggregate.json'; + +import * as defaulter_list_aggregate from './json-reports/defaulter-list-aggregate.json'; +import * as defaulter_list_base from './json-reports/defaulter-list-base.json'; + export class BaseMysqlReport { constructor(reportName, params) { this.reportName = reportName; @@ -1183,6 +1187,11 @@ export class BaseMysqlReport { hiv_cervical_cancer_monthly_summary_lesions_base ) }); + case 'defaulterListAggregate': + resolve({ + main: this.cloneJsonSchema(defaulter_list_aggregate), + defaulterListBase: this.cloneJsonSchema(defaulter_list_base) + }); default: reject('Unknown report ', reportName); break; diff --git a/app/reporting-framework/json-reports/defaulter-list-aggregate.json b/app/reporting-framework/json-reports/defaulter-list-aggregate.json new file mode 100644 index 000000000..9f3fbbe94 --- /dev/null +++ b/app/reporting-framework/json-reports/defaulter-list-aggregate.json @@ -0,0 +1,50 @@ +{ + "name": "defaulterListAggregate", + "version": "1.0", + "tag": "", + "description": "", + "uses": [ + { + "name": "defaulterListBase", + "version": "1.0", + "type": "dataset_def" + } + ], + "sources": [ + { + "dataSet": "defaulterListBase", + "alias": "t2" + } + ], + "columns": [ + { + "type": "simple_column", + "alias": "person_id", + "column": "t2.person_id" + }, + { + "type": "simple_column", + "alias": "total_defaulted", + "column": "count(distinct t2.person_id)" + } + ], + "groupBy": { + "groupParam": "groupByParam", + "columns": ["person_id"], + "excludeParam": "excludeParam" + }, + "dynamicJsonQueryGenerationDirectives": { + "patientListGenerator": { + "useTemplate": "patient-list-template", + "useTemplateVersion": "1.0", + "generatingDirectives": { + "joinDirectives": { + "joinType": "INNER", + "joinCondition": "<> = <>", + "baseColumn": "person_id", + "templateColumn": "person_id" + } + } + } + } +} diff --git a/app/reporting-framework/json-reports/defaulter-list-base.json b/app/reporting-framework/json-reports/defaulter-list-base.json new file mode 100644 index 000000000..c67e437ad --- /dev/null +++ b/app/reporting-framework/json-reports/defaulter-list-base.json @@ -0,0 +1,84 @@ +{ + "name": "defaulterListBase", + "version": "1.0", + "tag": "", + "description": "", + "uses": [], + "sources": [ + { + "table": "etl.flat_defaulters", + "alias": "fd" + }, + { + "table": "etl.flat_hiv_summary_v15b", + "alias": "de", + "join": { + "type": "LEFT", + "joinCondition": "de.encounter_id = fd.encounter_id" + } + }, + { + "table": "etl.program_visit_map", + "alias": "pm", + "join": { + "type": "LEFT", + "joinCondition": "de.visit_type = pm.visit_type_id" + } + }, + { + "table": "amrs.program", + "alias": "p", + "join": { + "type": "LEFT", + "joinCondition": "p.program_id = pm.program_type_id" + } + } + ], + "columns": [ + { + "type": "simple_column", + "alias": "person_id", + "column": "fd.person_id" + }, + { + "type": "simple_column", + "alias": "filed_id", + "column": "fd.filed_id" + }, + { + "type": "simple_column", + "alias": "days_since_rtc", + "column": "fd.days_since_rtc" + }, + { + "type": "simple_column", + "alias": "program", + "column": "p.name" + }, + { + "type": "simple_column", + "alias": "rtc_date", + "column": "DATE_FORMAT(fd.rtc_date,'%Y-%m-%d')" + } + ], + "filters": { + "conditionJoinOperator": "and", + "conditions": [ + { + "filterType": "tableColumns", + "conditionExpression": "fd.days_since_rtc >= ?", + "parameterName": "defaulterPeriod" + }, + { + "filterType": "tableColumns", + "conditionExpression": "fd.days_since_rtc <= ?", + "parameterName": "maxDefaultPeriod" + }, + { + "filterType": "tableColumns", + "conditionExpression": "fd.location_id in (?)", + "parameterName": "locations" + } + ] + } +} diff --git a/etl-routes.js b/etl-routes.js index 502bc9cd0..bc73f1593 100755 --- a/etl-routes.js +++ b/etl-routes.js @@ -74,6 +74,7 @@ import { PrepReminderService } from './service/prep-reminder/prep-reminder.servi import { HIVGainsAndLossesService } from './service/gains-and-losses/hiv-gains-losses-service'; const cervicalCancerScreeningService = require('./service/cervical-cancer-screening-service'); import { MOH412Service } from './service/moh-412/moh-412'; +import { DefaulterListService } from './service/defaulter-list-service'; module.exports = (function () { var routes = [ @@ -709,7 +710,37 @@ module.exports = (function () { config: { auth: 'simple', handler: function (request, reply) { - dao.getDefaulterList(request, reply); + if (request.query.locationUuids) { + request.query.reportName = 'defaulter-list'; + preRequest.resolveLocationIdsToLocationUuids(request, function () { + let requestParams = Object.assign( + {}, + request.query, + request.params + ); + + let requestCopy = _.cloneDeep(requestParams); + let reportParams = etlHelpers.getReportParams( + request.query.reportName, + ['locationUuids', 'locations'], + requestParams + ); + + const defaulterService = new DefaulterListService(); + + defaulterService + .getPatientListReport(requestParams) + .then((results) => { + _.each(results.result, (item) => { + item.cur_meds = etlHelpers.getARVNames(item.cur_meds); + }); + reply(results); + }) + .catch((err) => { + reply(Boom.internal('An error occured', err)); + }); + }); + } }, plugins: { hapiAuthorization: { diff --git a/service/defaulter-list-service.js b/service/defaulter-list-service.js new file mode 100644 index 000000000..e135ddb0f --- /dev/null +++ b/service/defaulter-list-service.js @@ -0,0 +1,52 @@ +const dao = require('../etl-dao'); +const Promise = require('bluebird'); +const _ = require('lodash'); +import { BaseMysqlReport } from '../app/reporting-framework/base-mysql.report'; +import { PatientlistMysqlReport } from '../app/reporting-framework/patientlist-mysql.report'; + +export class DefaulterListService { + getAggregateReport(reportParams) { + return new Promise(function (resolve, reject) { + let report; + + report = new BaseMysqlReport( + 'defaulterListAggregate', + reportParams.requestParams + ); + + Promise.join(report.generateReport(), (results) => { + let result = results.results.results; + results.size = result ? result.length : 0; + results.result = result; + delete results['results']; + resolve(results); + // TODO Do some post processing + }).catch((errors) => { + reject(errors); + }); + }); + } + + getPatientListReport(reportParams) { + let indicators = reportParams.indicators + ? reportParams.indicators.split(',') + : []; + delete reportParams['gender']; + + let report = new PatientlistMysqlReport( + 'defaulterListAggregate', + reportParams + ); + + return new Promise(function (resolve, reject) { + Promise.join(report.generatePatientListReport(indicators), (results) => { + results['result'] = results.results.results; + delete results['results']; + resolve(results); + }).catch((errors) => { + console.log('Error', errors); + reject(errors); + }); + }); + } +}