Skip to content

Commit

Permalink
converting rapidpro flow results to fhir QuestionnaireResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
ashaban committed Oct 20, 2021
1 parent 7468404 commit 01520d6
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 30 deletions.
50 changes: 29 additions & 21 deletions server/lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ function appRoutes() {

app.put('/emNutt/optout', (req, res) => {
let processingError = false
let responseBody = []
let globalid = req.query.globalid
let resourceType = req.query.entitytype
if((!globalid || !resourceType) && !Array.isArray(req.body)) {
Expand Down Expand Up @@ -169,7 +170,10 @@ function appRoutes() {
}
})
if(bundle.entry.length >= 200) {
macm.saveResource(bundle, (err) => {
macm.saveResource(bundle, (err, response, body) => {
if(body.entry) {
responseBody = responseBody.concat(body.entry)
}
if(err) {
processingError = true
}
Expand All @@ -181,7 +185,10 @@ function appRoutes() {
}, () => {
let promise = new Promise((resolve) => {
if(bundle.entry.length > 0) {
macm.saveResource(bundle, (err) => {
macm.saveResource(bundle, (err, response, body) => {
if(body.entry) {
responseBody = responseBody.concat(body.entry)
}
if(err) {
processingError = true
}
Expand All @@ -193,9 +200,9 @@ function appRoutes() {
})
promise.then(() => {
if(processingError) {
return res.status(500).send()
return res.status(500).json(responseBody)
}
return res.status(200).send()
return res.status(200).json(responseBody)
})
})
})
Expand All @@ -204,6 +211,7 @@ function appRoutes() {

app.put('/emNutt/undoOptout', (req, res) => {
let processingError = false
let responseBody = []
let globalid = req.query.globalid
let resourceType = req.query.entitytype
if((!globalid || !resourceType) && !Array.isArray(req.body)) {
Expand Down Expand Up @@ -233,16 +241,14 @@ function appRoutes() {
}]
};
async.eachSeries(practitioners, (practitioner, nxt) => {
logger.error(JSON.stringify({
resourceParameters: parameters,
resourceType: practitioner.entitytype,
resourceID: practitioner.globalid
},0,2));
macm["$meta-delete"]({
resourceParameters: parameters,
resourceType: practitioner.entitytype,
resourceID: practitioner.globalid
}).then(() => {
}).then((err, response, body) => {
if(body.entry) {
responseBody = responseBody.concat(body.entry)
}
return nxt();
}).catch((err) => {
logger.error(err);
Expand All @@ -251,9 +257,9 @@ function appRoutes() {
});
}, () => {
if(processingError) {
return res.status(500).send()
return res.status(500).json(responseBody)
}
return res.status(200).send()
return res.status(200).json(responseBody)
})
})

Expand All @@ -263,15 +269,17 @@ function appRoutes() {
if(requestIDs.parentReqId) {
delete processedMessages[requestIDs.parentReqId]
}
for(let requestID of requestIDs.childrenReqIDs) {
logger.info(`Clearing progress data for clientID ${requestID}`);
let data = JSON.stringify({})
redisClient.set(requestID, data, (err, reply) => {
if (err) {
logger.error(err);
logger.error(`An error has occured while clearing progress data for ${type} and requestID ${requestID}`);
}
});
if(Array.isArray(requestIDs.childrenReqIDs)) {
for(let requestID of requestIDs.childrenReqIDs) {
logger.info(`Clearing progress data for clientID ${requestID}`);
let data = JSON.stringify({})
redisClient.set(requestID, data, (err, reply) => {
if (err) {
logger.error(err);
logger.error(`An error has occured while clearing progress data for ${type} and requestID ${requestID}`);
}
});
}
}
});

Expand Down
55 changes: 49 additions & 6 deletions server/lib/macm.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,9 @@ module.exports = function () {
};
request.post(options, (err, res, body) => {
if(err || !res.statusCode || (res.statusCode < 200 && res.statusCode > 299)) {
return reject();
return reject(err, res, body);
}
return resolve();
return resolve(err, res, body);
});
});
},
Expand Down Expand Up @@ -533,6 +533,7 @@ module.exports = function () {
}
});
}
let dialog = []
for (const path of run.path) {
const values = Object.keys(run.values);
const texts = [];
Expand All @@ -554,14 +555,16 @@ module.exports = function () {
}
}
const id = uuid5(path.node, run.uuid);
texts.push({
let text = {
id,
nodeUUID: path.node,
type: 'reply',
inResponseTo,
time: run.values[valueKey].time,
msg: run.values[valueKey].input
});
}
texts.push(text);
dialog.push(text)
}
}
if (texts.length === 0) {
Expand All @@ -572,13 +575,15 @@ module.exports = function () {
for (const action of node.actions) {
if (action.type === 'send_msg') {
id = uuid5(action.uuid, run.uuid);
texts.push({
let text = {
id,
nodeUUID: action.uuid,
type: 'sent',
time: path.time,
msg: action.text
});
}
texts.push(text);
dialog.push(text)
}
}
}
Expand Down Expand Up @@ -625,6 +630,44 @@ module.exports = function () {
});
}
}
//create questionnaire response resource
let qnresponse = {
resourceType: "QuestionnaireResponse",
id: run.uuid,
authored: run.created_on,
item: []
}
if(run.exit_type === 'interrupted' || run.exit_type === 'expired') {
qnresponse.status = 'stopped'
} else if(run.exit_type === 'completed') {
qnresponse.status = 'completed'
} else {
qnresponse.status = 'in-progress'
}
for(let text of dialog) {
if(text.type !== 'sent') {
continue
}
let reply = dialog.find((txt) => {
return txt.type === 'reply' && txt.inResponseTo === text.id
})
let link = {}
link.linkId = text.nodeUUID
link.text = text.msg
if(reply) {
link.answer = [{
valueString: reply.msg
}]
}
qnresponse.item.push(link)
}
bundle.entry.push({
resource: qnresponse,
request: {
method: 'PUT',
url: `${qnresponse.resourceType}/${qnresponse.id}`
}
});
return callback(processingError, bundle);
}
};
Expand Down
3 changes: 3 additions & 0 deletions server/lib/prerequisites.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ const checkDependencies = (callback) => {

function isRunning(dependence, callback) {
logger.info('Checking if ' + dependence.name + ' is running');
if(dependence.name === 'kibana') {
return callback(true)
}
const options = {
url: dependence.url,
auth: dependence.auth
Expand Down
7 changes: 4 additions & 3 deletions server/lib/rapidpro.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const macm = require('./macm')();
const config = require('./config');
const logger = require('./winston');
const mixin = require('./mixin');
const fhirAudit = require('./modules/fhirAudit')
module.exports = function () {
return {
/**
Expand Down Expand Up @@ -536,6 +537,7 @@ module.exports = function () {
resourceType: "Practitioner",
id: contact.uuid,
name: [humanName],
active: true,
identifier: [{
system: "http://app.rapidpro.io/contact-uuid",
value: contact.uuid
Expand Down Expand Up @@ -1965,7 +1967,7 @@ module.exports = function () {
}
redisClient.set(reqID, statusResData);
async.eachSeries(commReqBundles, (bundle, nxtBundle) => {
macm.saveResource(bundle, () => {
macm.saveResource(bundle, (error, response, body) => {
return nxtBundle()
})
}, () => {
Expand Down Expand Up @@ -2104,15 +2106,14 @@ module.exports = function () {
commReq.resource.extension = [];
}
commReq.resource.status = commReqStatus
let extIndex = 0;
let extIndex = 1;
for (const index in commReq.resource.extension) {
const ext = commReq.resource.extension[index];
if (ext.url === extUrl) {
extIndex = index;
break;
}
}

if(rpRunStatus && Object.keys(rpRunStatus).length > 0) {
commReq.resource.extension[extIndex] = {
url: extUrl,
Expand Down

0 comments on commit 01520d6

Please sign in to comment.