From bb06e1a94127e80e3f19dab81bfbe5118cf54c12 Mon Sep 17 00:00:00 2001 From: ashaban Date: Tue, 28 May 2024 16:10:21 +0300 Subject: [PATCH] returning proper FHIR response --- server/lib/routes/fhir.js | 32 ++++++++++++++++++++++++++----- tests/sampleMultiplePatients.json | 4 ++-- tests/sampleSinglePatient.json | 10 +++++----- tests/uploadJSON.js | 1 + ui/package-lock.json | 2 +- ui/package.json | 2 +- 6 files changed, 37 insertions(+), 14 deletions(-) diff --git a/server/lib/routes/fhir.js b/server/lib/routes/fhir.js index e479d764..51463d78 100644 --- a/server/lib/routes/fhir.js +++ b/server/lib/routes/fhir.js @@ -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 { @@ -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); @@ -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({ @@ -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]); @@ -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); @@ -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 = { diff --git a/tests/sampleMultiplePatients.json b/tests/sampleMultiplePatients.json index 41c4a0ea..f4b08239 100644 --- a/tests/sampleMultiplePatients.json +++ b/tests/sampleMultiplePatients.json @@ -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", diff --git a/tests/sampleSinglePatient.json b/tests/sampleSinglePatient.json index 06908455..f1ac036d 100644 --- a/tests/sampleSinglePatient.json +++ b/tests/sampleSinglePatient.json @@ -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" }, @@ -26,9 +26,9 @@ "active": true, "name": [{ "use": "official", - "family": "Joshu", + "family": "Juma", "given": [ - "Emmanuel" + "Peter" ] }], "telecom": [{ @@ -36,5 +36,5 @@ "value": "0678 5616088" }], "gender": "male", - "birthDate": "1972-01-08" + "birthDate": "1986-01-08" } diff --git a/tests/uploadJSON.js b/tests/uploadJSON.js index 386a536e..38714be6 100644 --- a/tests/uploadJSON.js +++ b/tests/uploadJSON.js @@ -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); diff --git a/ui/package-lock.json b/ui/package-lock.json index 4e5622c6..c258e969 100644 --- a/ui/package-lock.json +++ b/ui/package-lock.json @@ -15,7 +15,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", diff --git a/ui/package.json b/ui/package.json index 55c7641f..ef095cce 100644 --- a/ui/package.json +++ b/ui/package.json @@ -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",