Skip to content

Commit

Permalink
updating end points. updating openapi
Browse files Browse the repository at this point in the history
  • Loading branch information
ashaban committed Aug 14, 2020
1 parent 987d8f5 commit 69b7570
Show file tree
Hide file tree
Showing 7 changed files with 381 additions and 28 deletions.
122 changes: 122 additions & 0 deletions server/lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,127 @@ function appRoutes() {
};
}

app.post('/fhir', (req, res) => {
const resource = req.body;
if (!resource.resourceType ||
(resource.resourceType && resource.resourceType !== 'Bundle') ||
!resource.entry || (resource.entry && resource.entry.length === 0)) {
return res.status(400).json({
resourceType: "OperationOutcome",
issue: [{
severity: "error",
code: "processing",
diagnostics: "Invalid bundle submitted"
}],
response: {
status: 400
}
});
}
let patients = [];
for(let index in resource.entry) {
let entry = resource.entry[index];
if(entry.resource && entry.resource.resourceType === "Patient") {
patients.push(entry);
resource.entry.splice(index, 1);
}
}
async.parallel({
otherResources: (callback) => {
if(resource.entry.length === 0) {
return callback(null, {});
}
fhirWrapper.create(resource, (code, err, response, body) => {
return callback(null, {code, err, response, body});
});
},
patients: (callback) => {
if(patients.length === 0) {
return callback(null, {});
}
let patientsBundle = {
entry: patients
};
let clientID;
if (config.get('mediator:register')) {
clientID = req.headers['x-openhim-clientid'];
} else {
const cert = req.connection.getPeerCertificate();
clientID = cert.subject.CN;
}
addPatient(clientID, patientsBundle, (err, response, operationSummary) => {
if (err) {
return callback(null, {code: 500, err, response, body: operationSummary});
}
return callback(null, {code: 201, err, response, body: operationSummary});
});
}
}, (err, results) => {
let code;
if(results.patients.code > results.otherResources.code) {
code = results.patients.code;
} else {
code = results.otherResources.code;
}
if(!code) {
code = 500;
}
return res.status(code).json([results.patients.body, results.patients.body]);
});
});

app.post('/fhir/:resourceType', (req, res) => {
let resource = req.body;
let resourceType = req.params.resourceType;
logger.info('Received a request to add resource type ' + resourceType);
if(resourceType !== "Patient") {
fhirWrapper.create(resource, (code, err, response, body) => {
return res.status(code).send(body);
});
} else {
if(!resource.resourceType || !resource.identifier || (resource.identifier && resource.identifier.length === 0)) {
return res.status(400).json({
resourceType: "OperationOutcome",
issue: [{
severity: "error",
code: "processing",
diagnostics: "Invalid patient resource submitted"
}],
response: {
status: 400
}
});
}
const patientsBundle = {
entry: [{
resource
}]
};
let clientID;
if (config.get('mediator:register')) {
clientID = req.headers['x-openhim-clientid'];
} else {
const cert = req.connection.getPeerCertificate();
clientID = cert.subject.CN;
}
addPatient(clientID, patientsBundle, (err, response, operationSummary) => {
const auditBundle = createAddPatientAudEvent(operationSummary, req);
fhirWrapper.saveResource({
resourceData: auditBundle
}, () => {
logger.info('Audit saved successfully');
if (err) {
res.status(500).send();
} else {
res.setHeader('location', response.entry[0].response.location);
res.status(201).send();
}
});
});
}
});

//this API is deprecated, use /fhir/Patient
app.post('/Patient', (req, res) => {
logger.info('Received a request to add new patient');
const patient = req.body;
Expand Down Expand Up @@ -317,6 +438,7 @@ function appRoutes() {
});
});

//this API is deprecated, use /fhir
app.post('/', (req, res) => {
logger.info('Received a request to add new patients from a bundle');
const patientsBundle = req.body;
Expand Down
60 changes: 60 additions & 0 deletions server/lib/fhir.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@ const isJSON = require('is-json');
const logger = require('./winston');
const config = require('./config');

class InvalidRequestError extends Error {
constructor (message, status) {
super( message );
this.response = {
status: status || 400,
body: {
resourceType: "OperationOutcome",
issue: [
{
severity: "error",
code: "required",
diagnostics: message
}
]
}
};
}
}

module.exports = () => ({
/**
*
Expand Down Expand Up @@ -187,6 +206,47 @@ module.exports = () => ({
});
},

create(resource, callback) {
let err;
if ( resource === undefined ) {
err = new InvalidRequestError( "resource must be defined" );
err.response = { status: 400 };
return callback(400, err);
}
let url = URI(config.get('fhirServer:baseURL'));
if ( resource.resourceType !== "Bundle" ) {
url = url.segment(resource.resourceType);
} else {
if ( !( resource.type === "transaction" || resource.type === "batch" ) ) {
err = new InvalidRequestError( "Bundles must of type 'transaction' or 'batch'" );
err.response = { status: 400 };
return callback(400, err);
}
}
url = url.toString();
const options = {
url,
headers: {
'Content-Type': 'application/json',
},
withCredentials: true,
auth: {
username: config.get('fhirServer:username'),
password: config.get('fhirServer:password'),
},
json: resource,
};
request.post(options, (err, res, body) => {
let code;
if(res && res.statusCode) {
code = res.statusCode;
} else {
code = 500;
}
return callback(code, err, res, body);
});
},

createGoldenRecord() {
const goldenRecord = {
id: uuid4(),
Expand Down
84 changes: 81 additions & 3 deletions server/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,89 @@
}
}
},
"/Patient": {
"/fhir": {
"post": {
"tags": [
"Patients Bundle"
],
"summary": "Submit a bundle of patients and/or other resources into openCR",
"operationId": "submitPatientsBundle",
"consumes": [
"application/fhir+json",
"application/json+fhir"
],
"produces": [
"application/fhir+json",
"application/json+fhir"
],
"parameters": [{
"in": "body",
"name": "body",
"description": "A bundle of resources",
"schema": {
"type": "object"
}
}],
"responses": {
"201": {
"description": "successful operation"
},
"400": {
"description":"Bad request"
},
"500": {
"description": "Internal error"
}
}
}
},
"/fhir/{ResourceType}": {
"post": {
"tags": [
"Single Patient"
],
"summary": "Submit patient or other resources into openCR",
"operationId": "fhirSubmitResource",
"consumes": [
"application/fhir+json",
"application/json+fhir"
],
"produces": [
"application/fhir+json",
"application/json+fhir"
],
"parameters": [{
"name": "ResourceType",
"in": "path",
"description": "Resource Type to be saved",
"required": true,
"type": "string"
}, {
"in": "body",
"name": "body",
"description": "Resource details",
"schema": {
"type": "object"
}
}],
"responses": {
"201": {
"description": "successful operation"
},
"400": {
"description":"Bad request"
},
"500": {
"description": "Internal error"
}
}
}
},
"/Patient": {
"post": {
"tags": [
"[Deprecated] Single Patient"
],
"summary": "Submit patient information into the client registry",
"operationId": "submitPatient",
"consumes": [
Expand Down Expand Up @@ -91,7 +169,7 @@
"/": {
"post": {
"tags": [
"Patients Bundle"
"[Deprecated] Patients Bundle"
],
"summary": "Submit a bundle of patients into the client registry",
"operationId": "submitPatientsBundle",
Expand Down Expand Up @@ -218,6 +296,6 @@
}
}
}
},
}
}
}
47 changes: 29 additions & 18 deletions tests/sampleMultiplePatients.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"resourceType": "Bundle",
"type": "batch",
"entry": [{
"resource": {
"resourceType": "Patient",
"id": "efa9af55-ed82-49e1-9c58-7a3b36155ffe",
"meta": {
"versionId": "2",
"lastUpdated": "2020-02-25T17:48:18.028+03:00",
Expand Down Expand Up @@ -34,27 +34,38 @@
"telecom": [{
"system": "phone",
"value": "774 232423"
}],
"link": [{
"other": {
"reference": "Patient/c20a8c2e-2e59-4f55-b0e3-78a43981aec0"
}]
}
}, {
"resource": {
"resourceType": "Practitioner",
"identifier": [{
"type": {
"coding": [{
"system": "http://terminology.hl7.org/CodeSystem/v2-0203",
"code": "PT",
"display": "Patient External Identifier"
}]
},
"type": "refer"
"system": "http://clientregistry.org/openmrs",
"value": "1110PPPYYJJHHNN"
}],
"name": [{
"use": "official",
"family": "Ally",
"given": ["Muawiya"]
}],
"gender": "male",
"birthDate": "2020-01-02",
"telecom": [{
"system": "phone",
"value": "774 232423"
}]
}
}, {
"resource": {
"resourceType": "Patient",
"id": "efa9af55-ed82-49e1-9c58-7a3b36155ffe",
"meta": {
"versionId": "2",
"lastUpdated": "2020-02-25T17:48:18.028+03:00",
"source": "#5YskPxdp2NQaRit7",
"tag": [{
"code": "clientid",
"display": "openmrs"
}]
},
"identifier": [{
"type": {
"coding": [{
Expand All @@ -64,12 +75,12 @@
}]
},
"system": "http://clientregistry.org/openmrs",
"value": "111TTTTZZJJMMHH"
"value": "45sdfs2324534"
}],
"name": [{
"use": "official",
"family": "Joshua",
"given": ["Emanuel"]
"family": "Ally",
"given": ["Kawthar"]
}],
"gender": "male",
"birthDate": "2020-01-02",
Expand Down
Loading

0 comments on commit 69b7570

Please sign in to comment.