Skip to content

Commit

Permalink
Fix for recursive posting eid results to amrs obs on error and added …
Browse files Browse the repository at this point in the history
…eid missing services
  • Loading branch information
maikofelix47 committed Nov 3, 2021
1 parent d442bd5 commit ad87ad3
Show file tree
Hide file tree
Showing 5 changed files with 381 additions and 26 deletions.
241 changes: 241 additions & 0 deletions app/lab-integration/lab-sync-pre-processor.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
'use strict';
var rp = require('../../request-config');
var config = require('../../conf/config');
var moment = require('moment');
var db = require('../../etl-db');

var definition = {
processLabSyncReqest: processLabSyncReqest,
getRestResource: getRestResource,
isBatchMode: isBatchMode,
getPersonAttributes: getPersonAttributes,
hasVisitStartedToday: hasVisitStartedToday,
getPatientVisits: getPatientVisits,
savePendingLabResults: savePendingLabResults,
getCachedPendingLabResults: getCachedPendingLabResults,
processPendingLabResultRequest: processPendingLabResultRequest,
hasPendingVLOrder: hasPendingVLOrder
};

module.exports = definition;

function processLabSyncReqest(params) {
const hasVisitStartedEnabled =
config.eid.filterOptions.hasVisitStartedEnabled;
return new Promise(function (resolve, reject) {
determineIfTestPatient(params.patientUuId)
.then((result) => {
if (result === false) {
const isBatch = isBatchMode(params);
if (isBatch === true) {
resolve(true);
} else {
if (hasVisitStartedEnabled) {
hasVisitStartedToday(params.patientUuId)
.then((result) => {
if (result === true) {
resolve(true);
} else {
resolve(false);
}
})
.catch((error) => {
reject(error);
});
} else {
resolve(true);
}
}
} else {
reject('ERROR: is a TestPatient');
}
})
.catch((error) => {
reject('ERROR: isTestPatient', error);
});
});
}

function getRestResource(path) {
var link = config.openmrs.host + ':' + config.openmrs.port + path;
if (config.openmrs.https === true) {
link = 'https://' + link;
} else {
link = 'http://' + link;
}
return link;
}

function isBatchMode(params) {
if (params.mode) {
const mode = params.mode;
if (mode === 'batch') {
return true;
}
} else {
return false;
}
}

function getPersonAttributes(patientUuid) {
var uri = getRestResource(
'/' + config.openmrs.applicationName + '/ws/rest/v1/person/' + patientUuid
);
var queryString = {
v: 'custom:(attributes:(uuid,display,value,attributeType:(uuid,display))'
};
return new Promise(function (resolve, reject) {
rp.getRequestPromise(queryString, uri)
.then(function (response) {
resolve(response.attributes);
})
.catch(function (error) {
reject(error);
});
});
}

function hasVisitStartedToday(patientUuId) {
return new Promise(function (resolve, reject) {
getPatientVisits(patientUuId)
.then((visits) => {
var hastodaysVisits = visits.some((visit) => {
return (
moment(visit.startDatetime).format('YYYY-MM-DD') ===
moment().format('YYYY-MM-DD')
);
});
resolve(hastodaysVisits);
})
.catch((error) => {
reject(error);
});
});
}

function getPatientVisits(patientUuid) {
var uri = getRestResource(
'/' + config.openmrs.applicationName + '/ws/rest/v1/visit'
);
var queryString = {
v: 'custom:(uuid,startDatetime,stopDatetime)',
patient: patientUuid
};
return new Promise(function (resolve, reject) {
rp.getRequestPromise(queryString, uri)
.then(function (response) {
resolve(response.results);
})
.catch(function (error) {
reject(error);
});
});
}

function savePendingLabResults(eidResults, personUuid) {
return new Promise((resolve, reject) => {
let queryParts = {};
let sql = '';

sql =
"replace into etl.eid_pending_results (person_uuid,pending_result) values ('" +
personUuid +
"','" +
eidResults +
"');";
queryParts = {
sql: sql
};
db.queryServer(queryParts, function (result) {
result.sql = sql;
resolve(result);
});
});
}

function getCachedPendingLabResults(personUuid) {
return new Promise((resolve, reject) => {
let queryParts = {};
let sql = '';

sql =
"select * from etl.eid_pending_results where person_uuid = '" +
personUuid +
"' AND DATE(date_created) = DATE(CURDATE()) order by date_created limit 1;";
queryParts = {
sql: sql
};
db.queryServer(queryParts, function (result) {
result.sql = sql;
resolve(result);
});
});
}

function processPendingLabResultRequest(patientUuid) {
return new Promise((resolve, reject) => {
determineIfTestPatient(patientUuid)
.then((result) => {
if (result === false) {
getCachedPendingLabResults(patientUuid)
.then((cachedResults) => {
let cachedEidResult = [];
if (cachedResults.size > 0) {
cachedEidResult = JSON.parse(
cachedResults.result[0].pending_result
);
resolve(cachedEidResult);
} else {
resolve(cachedEidResult);
}
})
.catch((error) => {
reject(error);
});
} else {
reject('Is Test Patient');
}
})
.catch((error) => {
reject(error);
});
});
}

function hasPendingVLOrder(personUuid) {
return new Promise((resolve, reject) => {
let queryParts = {};
let sql = '';

sql =
"SELECT t3.uuid FROM amrs.orders t1 INNER JOIN amrs.person t3 ON t3.person_id = t1.patient_id LEFT OUTER JOIN amrs.obs t2 ON t1.order_id = t2.order_id where t2.order_id IS NULL AND t1.date_activated >= DATE('2020-01-01') AND t3.uuid = '" +
personUuid +
"';";
queryParts = {
sql: sql
};
db.queryServer(queryParts, function (result) {
result.sql = sql;
resolve(result);
});
});
}

function determineIfTestPatient(personUuid) {
var testPatientAttributeTypeUuid = '1e38f1ca-4257-4a03-ad5d-f4d972074e69';
return new Promise((resolve, reject) => {
getPersonAttributes(personUuid)
.then((result) => {
const isTestPatient = result.some((attribute) => {
return (
attribute.attributeType.uuid === testPatientAttributeTypeUuid &&
attribute.value === true
);
});
resolve(isTestPatient);
})
.catch((error) => {
reject(error);
});
});
}
69 changes: 47 additions & 22 deletions app/lab-integration/lab-sync-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,39 @@ import { VLAdapter } from './adapters/vl-adapter';
import { DNAPCRAdapter } from './adapters/dnapcr-adpater';
import { CD4Adapter } from './adapters/cd4-adapter';
import { EidCompareOperator } from './utils/eid-compare-operator';
import { PatientLastOrderLocationService } from '../../service/eid/eid-patient-last-order-location.service';

export class LabSyncService {
syncAllLabsByPatientUuid(patientUuid, reply) {
let tasks = [];
Object.keys(config.hivLabSystem).forEach((labLocation) => {
tasks.push((cb) => {
// delay alupe for a few ms
cb(
null,
this.syncLabsByPatientUuid(
patientUuid,
labLocation,
labLocation === 'alupe' ? 50 : 0
).then((result) => {
return result;
})
);
const service = new PatientLastOrderLocationService();
service
.isPatientLastOrderLocationAffliatedToAlupe(patientUuid)
.then((isAffliated) => {
Object.keys(config.hivLabSystem).forEach((labLocation) => {
tasks.push((cb) => {
// delay alupe for a few ms
cb(
null,
this.syncLabsByPatientUuid(
patientUuid,
labLocation,
labLocation === 'alupe' ? 50 : 0
)
.then((result) => {
return result;
})
.catch((error) => {
return error;
})
);
});
});
this.syncLabsParallel(tasks, reply);
});
});
}

syncLabsParallel(tasks, reply) {
async.parallel(async.reflectAll(tasks), (err, results) => {
// currently we have duplicate data in db. Try to remove here
Promise.all(results.map((result) => result.value))
Expand All @@ -44,7 +58,6 @@ export class LabSyncService {
reply(_lab_data);
})
.catch((err) => {
console.log('sync service error', err);
reply(Boom.notFound('Sorry, sync service temporarily unavailable.'));
});
});
Expand Down Expand Up @@ -73,15 +86,15 @@ export class LabSyncService {
};
})
.catch((error) => {
console.log('ERROR', error);
return Promise.reject(error);
});
} else {
return this.syncAndGetPatientLabResults(patientUuid, labLocation)
.then((result) => {
return this.syncLabsByPatientUuid(patientUuid, labLocation);
})
.catch((err) => {
console.log('ERROR Getting results', err);
return Promise.reject(err);
});
}
} else {
Expand All @@ -90,12 +103,13 @@ export class LabSyncService {
return this.syncLabsByPatientUuid(patientUuid, labLocation);
})
.catch((error) => {
console.log('ERROR', error);
return Promise.reject(error);
});
}
})
.catch((error) => {
console.error('getLabSyncLog error', error);
return Promise.reject(error);
});
}

Expand Down Expand Up @@ -240,19 +254,30 @@ export class LabSyncService {
return eidService
.saveEidSyncLog(table, fields, savedObs)
.catch((error) => {
console.log('ERROR saving logs', error);
return Promise.reject(error);
});
})
.catch((error) => {
console.error('ERROR : combineObsPostPromises', error);
return new Promise((resolve, reject) => {
fields[0].status = 1;
fields[0].message = error.toString();
eidService
.saveEidSyncLog(table, fields, [])
.then((result) => {
reject(error);
})
.catch((error) => {
reject(error);
});
});
});
})
.catch((error) => {
console.error('ERROR : labResultsPromises', error);
return Promise.reject(error);
});
})
.catch((error) => {
console.error('ERROR : Fetching results', error);
return Promise.reject(error);
});
});
}
Expand Down
Loading

0 comments on commit ad87ad3

Please sign in to comment.