Skip to content

Commit

Permalink
fix(#123): improve cht hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
Maria Lorena Rodriguez Viruel committed Dec 5, 2024
1 parent 68dd2f9 commit c8638b7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
20 changes: 12 additions & 8 deletions mediator/test/cht-resource-factories.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { randomUUID } from 'crypto';
import { Factory } from 'rosie';

const PlaceFactory = Factory.define('place')
export const PlaceFactory = Factory.define('place')
.option('placeId', randomUUID())
.attr('name', 'CHP Branch One')
.attr('type', 'district_hospital')
.attr('parent', ['placeId'], function (placeId) {
.attr('placeId', ['placeId'], function (placeId) {
return placeId;
});

Expand All @@ -14,28 +14,32 @@ const ContactFactory = Factory.define('contact')
.attr('phone', '+2868917046');

export const UserFactory = Factory.define('user')
.option('placeId')
.option('parentPlace')
.attr('password', 'Dakar1234')
.attr('username', 'maria')
.attr('type', 'chw')
.attr('place', ['placeId'], function (placeId) {
return PlaceFactory.build({}, { placeId });
.attr('place', ['parentPlace'], function (parentPlace) {
return {
"name": "Mary's Area",
"type": "health_center",
"parent": parentPlace
};
})
.attr('contact', function () {
return ContactFactory.build();
});

export const PatientFactory = Factory.define('patient')
.option('placeId')
.option('place')
.attr('name', 'John Test')
.attr('phone', '+2548277217095')
.attr('date_of_birth', '1980-06-06')
.attr('sex', 'male')
.attr('type', 'person')
.attr('role', 'patient')
.attr('contact_type', 'patient')
.attr('place', ['placeId'], function (placeId) {
return placeId;
.attr('place', ['place'], function (place) {
return place;
});

const DocsFieldsFactory = Factory.define('fields')
Expand Down
38 changes: 26 additions & 12 deletions mediator/test/workflows.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import request from 'supertest';
import { OPENHIM, CHT, FHIR, OPENMRS } from '../config';
import {
UserFactory, PatientFactory, TaskReportFactory
UserFactory, PatientFactory, TaskReportFactory, PlaceFactory
} from './cht-resource-factories';
import {
EndpointFactory as EndpointFactoryBase,
Expand Down Expand Up @@ -70,26 +70,28 @@ const createOpenMRSIdType = async (name: string) => {
}
};

let placeId: string;
const parentPlace = PlaceFactory.build();
let chwUserName: string;
let chwPassword: string;
let contactId: string;
let patientId: string;
let parentPlaceId: string;
let placeId: string;


const configureCHT = async () => {
const createPlaceResponse = await request(CHT.url)
.post('/api/v1/places')
.auth(CHT.username, CHT.password)
.send({ 'name': 'CHP Branch Two', 'type': 'district_hospital' });
.send(parentPlace);

if (createPlaceResponse.status === 200 && createPlaceResponse.body.ok === true) {
placeId = createPlaceResponse.body.id;
parentPlaceId = createPlaceResponse.body.id;
} else {
throw new Error(`CHT place creation failed: Reason ${createPlaceResponse.status}`);
}

const user = UserFactory.build({}, { placeId: placeId });

const user = UserFactory.build({}, { parentPlace: parentPlaceId });
chwUserName = user.username;
chwPassword = user.password;

Expand All @@ -102,6 +104,15 @@ const configureCHT = async () => {
} else {
throw new Error(`CHT user creation failed: Reason ${createUserResponse.status}`);
}

const retrieveChtHealthCenterResponse = await request(CHT.url)
.get('/api/v2/users/maria')
.auth(CHT.username, CHT.password);
if (retrieveChtHealthCenterResponse.status === 200) {
placeId = retrieveChtHealthCenterResponse.body.place[0]._id;
} else {
throw new Error(`CHT health center retrieval failed: Reason ${retrieveChtHealthCenterResponse.status}`);
}
};

describe('Workflows', () => {
Expand All @@ -122,12 +133,13 @@ describe('Workflows', () => {
expect(checkMediatorResponse.status).toBe(200);
expect(checkMediatorResponse.body.status).toBe('success');

const patient = PatientFactory.build({name: 'OpenMRS Patient', phone: '+2548277217095'}, { placeId: placeId });
const patient = PatientFactory.build({name: 'CHTOpenMRS Patient', phone: '+2548277217095'}, { place: placeId });

const createPatientResponse = await request(CHT.url)
.post('/api/v1/people')
.auth(chwUserName, chwPassword)
.send(patient);

expect(createPatientResponse.status).toBe(200);
expect(createPatientResponse.body.ok).toEqual(true);
patientId = createPatientResponse.body.id;
Expand Down Expand Up @@ -168,7 +180,7 @@ describe('Workflows', () => {
);

const searchOpenMrsPatientResponse = await request(OPENMRS.url)
.get(`/Patient/?given=OpenMRS&family=Patient`)
.get(`/Patient/?given=CHTOpenMRS&family=Patient`)
.auth(OPENMRS.username, OPENMRS.password);
expect(searchOpenMrsPatientResponse.status).toBe(200);
expect(searchOpenMrsPatientResponse.body.total).toBe(1);
Expand All @@ -181,7 +193,7 @@ describe('Workflows', () => {
.auth(FHIR.username, FHIR.password);
expect(checkMediatorResponse.status).toBe(200);

const openMrsPatient = OpenMRSPatientFactory.build({}, {placeId});
const openMrsPatient = OpenMRSPatientFactory.build({}, {placeId: parentPlace.placeId});

const createOpenMrsPatientResponse = await request(OPENMRS.url)
.post('/Patient')
Expand All @@ -201,7 +213,7 @@ describe('Workflows', () => {
.send();
expect(triggerOpenMrsSyncPatientResponse.status).toBe(200);

await new Promise((r) => setTimeout(r, 10000));
await new Promise((r) => setTimeout(r, 20000));

const retrieveFhirPatientIdResponse = await request(FHIR.url)
.get('/fhir/Patient/?identifier=' + openMrsPatientId)
Expand All @@ -217,6 +229,8 @@ describe('Workflows', () => {
expect(retrieveChtPatientResponse.status).toBe(200);
});

});

describe('Loss To Follow-Up (LTFU) workflow', () => {
let encounterUrl: string;
let endpointId: string;
Expand Down Expand Up @@ -263,7 +277,7 @@ describe('Workflows', () => {
expect(retrieveOrganizationResponse.status).toBe(200);
expect(retrieveOrganizationResponse.body.total).toBe(1);

const patient = PatientFactory.build({}, { name: 'LTFU patient', placeId: placeId });
const patient = PatientFactory.build({}, { name: 'LTFU patient', place: placeId });

const createPatientResponse = await request(CHT.url)
.post('/api/v1/people')
Expand Down Expand Up @@ -294,7 +308,7 @@ describe('Workflows', () => {
expect(sendMediatorServiceRequestResponse.status).toBe(201);
encounterUrl = sendMediatorServiceRequestResponse.body.criteria;

const taskReport = TaskReportFactory.build({}, { placeId, contactId, patientId });
const taskReport = TaskReportFactory.build({}, { placeId: placeId, contactId, patientId });

const submitChtTaskResponse = await request(CHT.url)
.post('/medic/_bulk_docs')
Expand Down

0 comments on commit c8638b7

Please sign in to comment.