Skip to content

Commit

Permalink
Merge pull request #43 from jembi/Enable-patient-update
Browse files Browse the repository at this point in the history
hotfix
  • Loading branch information
bradsawadye authored May 9, 2024
2 parents 9cc1e01 + d13ed92 commit 36e07ca
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/routes/handlers/fetchPatientSummaries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export const fetchAllPatientSummariesByRefs = async (
patientRefs: string[],
queryParams?: object
): Promise<Bundle> => {
// remove duplicates
patientRefs = Array.from(new Set(patientRefs.map(ref => ref?.split('/').pop() || '')));

const patientExternalRefs = patientRefs.map((ref) => {
const params = Object.entries(queryParams ?? {});
let combinedParams = null;
Expand All @@ -29,7 +32,7 @@ export const fetchAllPatientSummariesByRefs = async (
.join('&');
}

const path = `/fhir/${ref}/$summary${combinedParams ? `?${combinedParams}` : ''}`;
const path = `/fhir/Patient/${ref}/$summary${combinedParams ? `?${combinedParams}` : ''}`;

return getData(protocol, host, port, path, {
'Content-Type': 'application/fhir+json',
Expand Down
5 changes: 4 additions & 1 deletion src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { matchAsyncHandler } from './handlers/matchPatientAsync';
import { matchSyncHandler } from './handlers/matchPatientSync';
import { mpiMdmQueryLinksMiddleware } from '../middlewares/mpi-mdm-query-links';
import { validationMiddleware } from '../middlewares/validation';
import { buildOpenhimResponseObject, getData } from '../utils/utils';
import { buildOpenhimResponseObject, getData, patientProjector } from '../utils/utils';
import { fetchEverythingByRef } from './handlers/fetchPatientResources';
import { mpiMdmSummaryMiddleware } from '../middlewares/mpi-mdm-summary';
import { fetchPatientSummaryByRef } from './handlers/fetchPatientSummaries';
Expand Down Expand Up @@ -125,6 +125,9 @@ routes.get('/fhir/Patient/:patientId', async (req, res) => {
const patient = mpiResponse.body as Patient;

patient.id = requestedId;

if (req.query.projection === 'partial') mpiResponse.body = patientProjector(patient);

logger.debug(
`Mapped upstream ID ${upstreamId} to requested ID ${requestedId} in response body`
);
Expand Down
14 changes: 11 additions & 3 deletions src/utils/kafkaFhir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,19 @@ export const processBundle = async (bundle: Bundle): Promise<MpiMediatorResponse
const newPatientMap: NewPatientMap = {};
// transform and send each patient resource and submit to MPI
const promises = patientEntries.map(async (patientEntry) => {
let guttedPatient: ResponseObject;
let id: string;

if (patientEntry.fullUrl) {
// Check if patient already exists and perform update
const guttedPatient = await sendRequest({
guttedPatient = await sendRequest({
...fhirDatastoreRequestDetails,
method: 'GET',
path: `/fhir/Patient/${patientEntry.fullUrl.split('/').pop()}`,
});

if (isHttpStatusOk(guttedPatient.status)) {
const id: string = Object.assign(guttedPatient.body).link[0].other.reference.split('/').pop();
id = Object.assign(guttedPatient.body).link[0].other.reference.split('/').pop();

let mpiPatient = await sendRequest({
...clientRegistryRequestDetails,
Expand Down Expand Up @@ -232,7 +235,12 @@ export const processBundle = async (bundle: Bundle): Promise<MpiMediatorResponse
);
}

return sendRequest(clientRegistryRequestDetails);
return sendRequest(clientRegistryRequestDetails).then(response => {
if (isHttpStatusOk(guttedPatient.status)) {
return {status: response.status, body: {...response.body, id}}
}
return response;
});
});

const clientRegistryResponses = await Promise.all(promises);
Expand Down
15 changes: 15 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ export const transformPatientResourceForMPI = (patient: Patient): MpiTransformRe
export const restorePatientResource = (patientData: PatientData) => {
patientData.restoredPatient = patientData.mpiResponsePatient;

// restore the source uuid of the patient
const id = Object.assign({id: ''}, patientData.mpiTransformResult?.patient).id;
patientData.restoredPatient = Object.assign({}, patientData.restoredPatient, {id});

if (patientData.mpiTransformResult?.extension?.length) {
patientData.restoredPatient = Object.assign({}, patientData.restoredPatient, {
extension: patientData.mpiTransformResult.extension,
Expand Down Expand Up @@ -312,3 +316,14 @@ export const mergeBundles = async (

return bundle;
};

export const patientProjector = (patient: Patient) : Patient => {
return {
resourceType: patient.resourceType,
id: patient.id,
identifier: patient.identifier,
name: patient.name,
birthDate: patient.birthDate,
gender: patient.gender
}
};
2 changes: 1 addition & 1 deletion tests/unit/matchPatientSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ describe('Match Patient Synchronously', (): void => {
},
},
restoredPatient: {
id: 'testPatient',
id: '12333',
resourceType: 'Patient',
},
},
Expand Down
108 changes: 108 additions & 0 deletions tests/unit/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
createHandlerResponseObject,
mergeBundles,
transformPatientResourceForMPI,
patientProjector,
} from '../../src/utils/utils';
import {
MpiMediatorResponseObject,
Expand Down Expand Up @@ -592,6 +593,113 @@ describe('Utils', (): void => {
});
});

describe('*patientProjector', (): void => {
it('should project patient', (): void => {
const patient: Patient = {
resourceType: 'Patient',
id: '642b83d3-a43c-41ef-a578-2b730f276bfb',
extension: [
{
url: 'http://hl7.org/fhir/StructureDefinition/patient-religion',
valueCodeableConcept: {
coding: [
{
system: 'http://terminology.hl7.org/ValueSet/v3-ReligiousAffiliation',
code: '1036',
display: 'Orthodox',
},
],
},
},
{
url: 'http://hl7.org/fhir/StructureDefinition/patient-relatedPerson',
valueReference: {
reference: 'RelatedPerson/f8b7dccb-f1ef-4365-9323-29303479d02bRelatedPerson481',
},
},
{
url: 'http://cdr.aacahb.gov.et/EducationalLevel',
valueCodeableConcept: {
coding: [
{
system: 'http://terminology.hl7.org/CodeSystem/v3-EducationLevel',
code: 'ELEM',
display: 'Elementary School',
},
],
},
},
{
url: 'http://cdr.aacahb.gov.et/Occupation',
valueString: 'Foreman',
},
],
identifier: [
{
system: 'http://cdr.aacahb.gov.et/SmartCareID',
value: '642b83d3-a43c-41ef-a578-2b730f276bfb',
},
{
system: 'http://cdr.aacahb.gov.et/NationalID',
value: 'MRN-642b83d3-a43c-41ef-a578-2b730f476bf9',
},
{
system: 'http://cdr.aacahb.gov.et/UAN',
value: 'UAN-642b83d3-a43c-41ef-a578-2b730f276bfb',
},
],
name: [
{
use: 'official',
family: 'Rodrigues',
given: ['Liniee'],
},
],
telecom: [
{
system: 'phone',
value: '+2519000000',
use: 'home',
},
],
gender: 'female',
birthDate: '1999-06-19',
address: [
{
type: 'physical',
text: 'Urban',
state: 'Addis Ababa',
city: 'Cherkos sub city',
district: '10',
line: ['17', '927/5'],
},
],
maritalStatus: {
coding: [
{
system: 'http://terminology.hl7.org/CodeSystem/v3-MaritalStatus',
code: 'M',
display: 'Married',
},
],
},
managingOrganization: {
reference: 'Organization/009a6a861c1b45778c0cbedadefe52a4',
},
};

const result: Patient = patientProjector(patient);

expect(result).to.haveOwnProperty('birthDate');
expect(result).to.haveOwnProperty('id');
expect(result).to.haveOwnProperty('name');
expect(result).to.haveOwnProperty('resourceType');
expect(result).to.haveOwnProperty('gender');
expect(result).to.haveOwnProperty('identifier');
expect(result).to.not.haveOwnProperty('address')
});
});

describe('*mergeBundles', (): void => {
it('should merge bundles', async (): Promise<void> => {
const clientRegistryPatientRef: string = 'http://client-registry:8080/fhir/Patient/1455';
Expand Down

0 comments on commit 36e07ca

Please sign in to comment.