Skip to content

Commit

Permalink
returning proper FHIR response
Browse files Browse the repository at this point in the history
  • Loading branch information
ashaban committed May 28, 2024
1 parent 2d9fffc commit bb06e1a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 14 deletions.
32 changes: 27 additions & 5 deletions server/lib/routes/fhir.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ router.get('/:resource?/:id?', (req, res) => {
pixmRequest({
req
}, (resourceData, statusCode) => {
logger.error(JSON.stringify(resourceData,0,2));
res.status(statusCode).send(resourceData);
});
} else {
Expand Down Expand Up @@ -255,7 +254,7 @@ router.post('/', (req, res) => {
filteredResponseBundle.push(entry);
} else {
let replaceIndex = filteredResponseBundle.findIndex((fil) => {
return parseInt(fil.response.etag) < parseInt(entry.response.etag) && fil.response.location.startsWith(entry.response.location.split('/_history')[0]);
return parseInt(fil.response.etag) > parseInt(entry.response.etag) && fil.response.location.startsWith(entry.response.location.split('/_history')[0]);
});
if(replaceIndex !== -1) {
filteredResponseBundle.splice(replaceIndex, 1);
Expand All @@ -265,7 +264,16 @@ router.post('/', (req, res) => {
}
res.setHeader('Location', JSON.stringify(results.patients.responseHeaders.patientID));
res.setHeader('LocationCRUID', JSON.stringify(results.patients.responseHeaders.CRUID));
res.status(code).json(filteredResponseBundle);
let type = resource.type + "-response"
if(!resource.type) {
type = "batch-response"
}
let responseBundle = {
resourceType: "Bundle",
type,
entry: filteredResponseBundle
}
res.status(code).json(responseBundle);

const auditBundle = matchMixin.createAddPatientAudEvent(results.patients.operationSummary, req);
fhirWrapper.saveResource({
Expand Down Expand Up @@ -369,6 +377,7 @@ function saveResource(req, res) {
}, () => {
logger.info('Audit saved successfully');
let filteredResponseBundle = [];
logger.error(JSON.stringify(responseBundle, 0, 2));
for(let entry of responseBundle.entry) {
let exists = filteredResponseBundle.findIndex((fil) => {
return fil.response && entry.response && entry.response.location && fil.response.location.startsWith(entry.response.location.split('/_history')[0]);
Expand All @@ -377,7 +386,7 @@ function saveResource(req, res) {
filteredResponseBundle.push(entry);
} else {
let replaceIndex = filteredResponseBundle.findIndex((fil) => {
return parseInt(fil.response.etag) < parseInt(entry.response.etag) && fil.response.location.startsWith(entry.response.location.split('/_history')[0]);
return parseInt(fil.response.etag) > parseInt(entry.response.etag) && fil.response.location.startsWith(entry.response.location.split('/_history')[0]);
});
if(replaceIndex !== -1) {
filteredResponseBundle.splice(replaceIndex, 1);
Expand All @@ -388,9 +397,22 @@ function saveResource(req, res) {
if (error) {
res.status(500).json(filteredResponseBundle);
} else {
let response = filteredResponseBundle.find((bnd) => {
return bnd.response.location.startsWith(responseHeaders.patientID[0]);
})?.response;
let status = response.status.split(" ")[0];
resource.id = responseHeaders.patientID[0];
if(!resource.meta) {
resource.meta = {};
}
resource.meta.versionId = response.etag;
resource.meta.lastUpdated = response.lastModified;
if(status !== 200 && status !== 201) {
status = 200;
}
res.setHeader('Location', responseHeaders.patientID[0]);
res.setHeader('LocationCRUID', responseHeaders.CRUID[0]);
res.status(200).json(filteredResponseBundle);
res.status(status).json(resource);
}

let csvUploadAuditBundle = {
Expand Down
4 changes: 2 additions & 2 deletions tests/sampleMultiplePatients.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"resourceType": "Bundle",
"type": "batch",
"type": "transaction",
"entry": [{
"resource": {
"resourceType": "Patient",
"identifier": [{
"system": "http://clientregistry.org/openmrs",
"value": "patient1"
"value": "patient3256"
},
{
"system": "http://health.go.ug/cr/nationalid",
Expand Down
10 changes: 5 additions & 5 deletions tests/sampleSinglePatient.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
},
"identifier": [{
"system": "http://clientregistry.org/openmrs",
"value": "sourceid27"
"value": "sourceid38"
}, {
"system": "http://clientregistry.org/nationalid",
"value": ""
}, {
"system": "http://system1.org",
"value": "12351",
"value": "12352",
"period": {
"start": "2001-05-07"
},
Expand All @@ -26,15 +26,15 @@
"active": true,
"name": [{
"use": "official",
"family": "Joshu",
"family": "Juma",
"given": [
"Emmanuel"
"Peter"
]
}],
"telecom": [{
"system": "phone",
"value": "0678 5616088"
}],
"gender": "male",
"birthDate": "1972-01-08"
"birthDate": "1986-01-08"
}
1 change: 1 addition & 0 deletions tests/uploadJSON.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ async.eachOfSeries(bundle.entry, (entry, index, nxtEntry) => {
json: entry.resource,
};
request.post(options, (err, res, body) => {
console.error(JSON.stringify(body, 0, 2));
if (err) {
logger.error('An error has occured');
logger.error(err);
Expand Down
2 changes: 1 addition & 1 deletion ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"vue-axios": "^2.1.5",
"vue-cookies": "^1.7.3",
"vue-flag-icon": "^2.1.0",
"vue-i18n": "^8.16.0",
"vue-i18n": "^8.28.2",
"vue-moment": "^4.1.0",
"vue-router": "^3.4.3",
"vuelidate": "^0.7.5",
Expand Down

0 comments on commit bb06e1a

Please sign in to comment.