Skip to content

Commit

Permalink
Merge pull request #9 from OpenFn/wf2-improvements
Browse files Browse the repository at this point in the history
Wf2 mapping improvements
  • Loading branch information
aleksa-krolls authored Sep 11, 2024
2 parents 2e0fbba + 0cd4e16 commit 4bcf231
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 65 deletions.
91 changes: 79 additions & 12 deletions workflows/wf2/2-upsert-teis.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,36 @@ fn(state => {
O: 'prefer_not_to_answer',
};

const DHIS2_PATIENT_NUMBER = '8d79403a-c2cc-11de-8d13-0010c6dffd0f';
const OPENMRS_AUTO_ID = '05a29f94-c0ed-11e2-94be-8c13b969e334';
const DHIS2_PATIENT_NUMBER = '8d79403a-c2cc-11de-8d13-0010c6dffd0f'; //DHIS2 ID or DHIS2 Patient Number
const OPENMRS_AUTO_ID = '05a29f94-c0ed-11e2-94be-8c13b969e334'; //MSF ID or OpenMRS Patient Number
const patientsUpsert = [];

const buildPatientsUpsert = (patient, isNewPatient) => {
const dateCreated = patient.auditInfo.dateCreated.substring(0, 10);

const { identifier } =
patient.identifiers.find(
i => i.identifierType.uuid === DHIS2_PATIENT_NUMBER
) ||
patient.identifiers.find(i => i.identifierType.uuid === OPENMRS_AUTO_ID);
function findIdentifierByUuid(identifiers, targetUuid) {
// Use the `find` method to locate the matching identifier
const matchingIdentifier = identifiers.find(
identifier => identifier.identifierType.uuid === targetUuid
);

// Return the `identifier` value if a match is found; otherwise, return null
return matchingIdentifier ? matchingIdentifier.identifier : undefined;
}

const calculateDOB = age => {
const currentDate = new Date();
const currentYear = currentDate.getFullYear();
const birthYear = currentYear - age;

const birthday = new Date(
birthYear,
currentDate.getMonth(),
currentDate.getDay()
);

return birthday.toISOString().replace(/\.\d+Z$/, '+0000');
};

const enrollments = [
{
Expand All @@ -32,33 +50,82 @@ fn(state => {
query: {
ou: 'OPjuJMZFLop',
program: 'w9MSPn5oSqp',
filter: [`AYbfTPYMNJH:Eq:${patient.uuid}`],
filter: [`AYbfTPYMNJH:Eq:${patient.uuid}`], //upsert on omrs.patient.uid
},
data: {
program: 'w9MSPn5oSqp',
orgUnit: 'OPjuJMZFLop',
trackedEntityType: 'cHlzCA2MuEF',
attributes: [
{
attribute: 'P4wdYGkldeG',
value: identifier,
attribute: 'fa7uwpCKIwa',
value: patient.person.names[0].givenName,
},
{
attribute: 'Jt9BhFZkvP2',
value: patient.person.names[0].familyName,
},
{
attribute: 'P4wdYGkldeG', //DHIS2 ID ==> "Patient Number"
value: findIdentifierByUuid(
patient.identifiers,
DHIS2_PATIENT_NUMBER
),
},
{
attribute: 'ZBoxuExmxcZ', //MSF ID ==> "OpenMRS Patient Number"
value: findIdentifierByUuid(patient.identifiers, OPENMRS_AUTO_ID),
},
{
attribute: 'AYbfTPYMNJH',
attribute: 'AYbfTPYMNJH', //"OpenMRS Patient UID"
value: patient.uuid,
},
{
attribute: 'qptKDiv9uPl',
value: genderOptions[patient.person.gender],
},
{
attribute: 'T1iX2NuPyqS',
attribute: 'Rv8WM2mTuS5',
value: patient.person.age,
},
{
attribute: 'WDp4nVor9Z7',
value: patient.person.birthdate,
},
// {
// attribute: 'rBtrjV1Mqkz', //Place of living
// value: patient.person.address,
// },
// {
// attribute: 'Xvzc9e0JJmp', //nationality
// value: patient.person.attributes[x].value, //input.attributeType = "24d1fa23-9778-4a8e-9f7b-93f694fc25e2"
// },
// {
// attribute: 'YUIQIA2ClN6', //current status
// value: patient.person.attributes[x].value, //input.attributeType = "e0b6ed99-72c4-4847-a442-e9929eac4a0f"
// },
// {
// attribute: 'Qq6xQ2s6LO8', //legal status
// value: patient.person.attributes[x].value, //input.attributeType = "a9b2c642-097f-43f8-b96b-4d2f50ffd9b1"
// },
// {
// attribute: 'FpuGAOu6itZ', //marital status
// value: patient.person.attributes[x].value, //input.attributeType = "3884dc76-c271-4bcb-8df8-81c6fb897f53"
// },
// {
// attribute: 'v7k4OcXrWR8', //employment status
// value: patient.person.attributes[x].value, //input.attributeType = "dd1f7f0f-ccea-4228-9aa8-a8c3b0ea4c3e"
// },
// {
// attribute: 'SVoT2cVLd5O', //employment status
// value: patient.person.attributes[x].value, //input.attributeType = "e363161a-9d5c-4331-8463-238938f018ed"
// },
],
},
};

console.log('mapped dhis2 payloads:: ', JSON.stringify(payload, null, 2));

if (isNewPatient) {
console.log('create enrollment');
payload.data.enrollments = enrollments;
Expand Down
84 changes: 42 additions & 42 deletions workflows/wf2/4-get-options-map.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
get(
'https://gist.githubusercontent.com/aleksa-krolls/b22987f7569bc069e963973401832349/raw/ccc21979aab33e8b5caa931d648698753516011b/msf_mhBaseline_optionsMap.json'
'https://gist.githubusercontent.com/aleksa-krolls/b22987f7569bc069e963973401832349/raw/94ae98714c85a6e871fd2fb292c9bff5e324d3a1/msf_mhBaseline_optionsMap.json'
);

fn(state => {
Expand All @@ -13,47 +13,47 @@ fn(state => {

fn(state => {
state.mhpssMap = {
dfdv3SkeXKe: 'a6c5188c-29f0-4d3d-8cf5-7852998df86f',
hWMBCCA2yy1: 'abede172-ba87-4ebe-8054-3afadb181ea3',
TWuCY5r2wx7: 'ccc4f06c-b76a-440d-9b7e-c48ba2c4a0ab',
QHrIUMhjZlO: 'd516de07-979b-411c-b7e4-bd09cf7d9d91',
H1fMCaOzr8F: '3e97c2d0-15c1-4cfd-884f-7a4721079217',
yCwuZ0htrlH: '5f6e245c-83fc-421b-8d46-061ac773ae71',
RiiH9A53rvG: '6d3876be-0a27-466d-ad58-92edcc8c31fb',
pN4iQH4AEzk: '722dd83a-c1cf-48ad-ac99-45ac131ccc96',
qgfKPlIHjcD: 'd8c84af2-bd9b-4bf3-a815-81652cb0b0bc',
rSIazMFEBjD: '4dae5b12-070f-4153-b1ca-fbec906106e1',
qptKDiv9uPl: 'ec42d68d-3e23-43de-b8c5-a03bb538e7c7',
KSBMR1BDGwx: '1a8bf24f-4f36-4971-aad9-ae77f3525738',
WDY6MkQWyHb: '722dd83a-c1cf-48ad-ac99-45ac131ccc96',
AuDPJg6gZE7: '82978311-bef9-46f9-9a9a-cc62254b00a6',
KeyiEPc4pII: '82978311-bef9-46f9-9a9a-cc62254b00a6',
qfYPXP76j8g: 'c3c86c1b-07be-4506-ab25-8f35f4389b19',
PCGI7EnvCQS: '45b39cbf-0fb2-4682-8544-8aaf3e07a744',
RnbiVrrSFdm: 'ee1b7973-e931-494e-a9cb-22b814b4d8ed',
CUdI1BJ5W8G: '92a92f62-3ff6-4944-9ea9-a7af23949bad',
YfcNA5bvkxT: '9a8204ca-d908-4157-9285-7c970dbb5287',
vC3bg9NwJ78: '3edcfddb-7988-4ce5-97a0-d4c46b267a04',
RqsvaPH9vHt: '22809b19-54ca-4d88-8d26-9577637c184e',
qacGXlyyQOS: 'a1a75011-0fef-460a-b666-dda2d171f39b',
S22iy8o0iLg: 'aae000c3-5242-4e3c-bd1f-7e922a6d3d34',
v0qFX0qv1tX: 'd5e3d927-f7ce-4fdd-ac4e-6ad0b510b608',
SsQqwDBGxjh: '54a9b20e-bce5-4d4a-8c9c-e0248a182586',
FLIlRjAwn4G: 'e0d4e006-85b5-41cb-8a21-e013b1978b8b',
JUabDHhT1wJ: 'c1a3ed2d-6d9a-453d-9d93-749164a76413',
DlqJSA5VApl: '8fb3bb7d-c935-4b57-8444-1b953470e109',
DMaLm9u4GCq: 'b87a93ff-a4a1-4601-b35d-1e42bfa7e194',
CLGnlnFqqnk: '0a0c70d2-2ba5-4cb3-941f-b4a9a4a7ec6d',
f64XCwzJW02: '41e68dee-a2a3-4e6c-9d96-53def5caff52',
YeaUNruqmca: '08cd4b4a-4b0b-4391-987b-b5b3d770d30f',
KjgDauY9v4J: 'e08d532b-e56c-43dc-b831-af705654d2dc',
pj5hIE6iyAR: 'e08d532b-e56c-43dc-b831-af705654d2dc',
pj5hIE6iyAR: 'e08d532b-e56c-43dc-b831-af705654d2dc',
W7cPAi8iXLZ: '819f79e7-b9af-4afd-85d4-2ab677223113',
MF3RML0HLbP: 'b2c5b6e0-66f0-4b9d-8576-b6f48e0a06df',
m8qis4iUOTo: '790b41ce-e1e7-11e8-b02f-0242ac130002',
OZViJk8FPVd: 'c2664992-8a5a-4a6d-9238-5df591307d55',
// tsFOVnlc6lz: '5f3d618e-5c89-43bd-8c79-07e4e98c2f23', //phq9 score
dfdv3SkeXKe: 'a6c5188c-29f0-4d3d-8cf5-7852998df86f', //Has the patient attempted suicide or tried to kill themselves?
hWMBCCA2yy1: 'abede172-ba87-4ebe-8054-3afadb181ea3', //Is the patient currently at risk of hurting himself/herself or attempting suicide?
TWuCY5r2wx7: 'ccc4f06c-b76a-440d-9b7e-c48ba2c4a0ab', //Is the patient currently at risk of hurting others?
QHrIUMhjZlO: 'd516de07-979b-411c-b7e4-bd09cf7d9d91', //Does the patient regularly use alcohol / substances to become intoxicated?
H1fMCaOzr8F: '3e97c2d0-15c1-4cfd-884f-7a4721079217', //Has the patient experienced an act of aggression or violence?
yCwuZ0htrlH: '5f6e245c-83fc-421b-8d46-061ac773ae71', //Follow up session required?
RiiH9A53rvG: '6d3876be-0a27-466d-ad58-92edcc8c31fb', //Referral done
OZViJk8FPVd: 'c2664992-8a5a-4a6d-9238-5df591307d55', //Has the patient had thoughts of death or suicide?
qgfKPlIHjcD: 'd8c84af2-bd9b-4bf3-a815-81652cb0b0bc', //Patient experienced an act of aggression or violence - What type(s) of violence?
rSIazMFEBjD: '4dae5b12-070f-4153-b1ca-fbec906106e1', //Admission type
KSBMR1BDGwx: '1a8bf24f-4f36-4971-aad9-ae77f3525738', //Type of consultation
WDY6MkQWyHb: '722dd83a-c1cf-48ad-ac99-45ac131ccc96', //Consultation done by
AuDPJg6gZE7: '82978311-bef9-46f9-9a9a-cc62254b00a6', //Location of intervention
KeyiEPc4pII: '82978311-bef9-46f9-9a9a-cc62254b00a6', //Type of intervention
qfYPXP76j8g: 'c3c86c1b-07be-4506-ab25-8f35f4389b19', //Patient referred by
PCGI7EnvCQS: '45b39cbf-0fb2-4682-8544-8aaf3e07a744', //Current symptoms or complaints 1
RnbiVrrSFdm: 'ee1b7973-e931-494e-a9cb-22b814b4d8ed', //Current symptoms or complaints 2
CUdI1BJ5W8G: '92a92f62-3ff6-4944-9ea9-a7af23949bad', //Current symptoms or complaints 3
YfcNA5bvkxT: '9a8204ca-d908-4157-9285-7c970dbb5287', //Main category of symptoms
vC3bg9NwJ78: '3edcfddb-7988-4ce5-97a0-d4c46b267a04', //Duration of the main symptom
RqsvaPH9vHt: '22809b19-54ca-4d88-8d26-9577637c184e', //Clinical diagnosis
qacGXlyyQOS: 'a1a75011-0fef-460a-b666-dda2d171f39b', //CGI-S score
S22iy8o0iLg: 'aae000c3-5242-4e3c-bd1f-7e922a6d3d34', //Patient experienced an act of aggression or violence - Time between violence event and consultation
v0qFX0qv1tX: 'd5e3d927-f7ce-4fdd-ac4e-6ad0b510b608', //Main past or precipitating events - 1
SsQqwDBGxjh: '54a9b20e-bce5-4d4a-8c9c-e0248a182586', //Main past or precipitating events - 2
FLIlRjAwn4G: 'e0d4e006-85b5-41cb-8a21-e013b1978b8b', //Main past or precipitating events - 3
JUabDHhT1wJ: 'c1a3ed2d-6d9a-453d-9d93-749164a76413', //Main category of precipitating event
DlqJSA5VApl: '8fb3bb7d-c935-4b57-8444-1b953470e109', //Type of referral
DMaLm9u4GCq: 'b87a93ff-a4a1-4601-b35d-1e42bfa7e194', //Total number of beneficiaries in family consultation
CLGnlnFqqnk: '0a0c70d2-2ba5-4cb3-941f-b4a9a4a7ec6d', //Location of intervention - If Health Facility, specify
f64XCwzJW02: '41e68dee-a2a3-4e6c-9d96-53def5caff52', //Location of intervention - If MSF Health Facility, specify
YeaUNruqmca: '08cd4b4a-4b0b-4391-987b-b5b3d770d30f', //Location of intervention - If Mobile Clinic, specify
KjgDauY9v4J: 'e08d532b-e56c-43dc-b831-af705654d2dc', //Location of intervention - If other, specify
pj5hIE6iyAR: 'e08d532b-e56c-43dc-b831-af705654d2dc', //Current symptoms or complaints - If other, specify
pj5hIE6iyAR: 'e08d532b-e56c-43dc-b831-af705654d2dc', //Current symptoms or complaints - If other, specify
W7cPAi8iXLZ: '819f79e7-b9af-4afd-85d4-2ab677223113', //Clinical diagnosis - If other, specify
MF3RML0HLbP: 'b2c5b6e0-66f0-4b9d-8576-b6f48e0a06df', //MHOS score
m8qis4iUOTo: '790b41ce-e1e7-11e8-b02f-0242ac130002', //Past / Precipitating Events - If other, specify
//pN4iQH4AEzk: '722dd83a-c1cf-48ad-ac99-45ac131ccc96', //Consultation done by //TODO: Return BOOLEAN
//qptKDiv9uPl: 'ec42d68d-3e23-43de-b8c5-a03bb538e7c7', //Sex //TODO: Remove TEA?
//tsFOVnlc6lz: '5f3d618e-5c89-43bd-8c79-07e4e98c2f23', //PHQ9 Score //TODO: ass logic
};

return state;
Expand Down
26 changes: 15 additions & 11 deletions workflows/wf2/6-create-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@ fn(state => {
value = answer.value;
}
if (typeof answer.value === 'object') {
if (
//TODO: Ask mtuchi about this mapping
answer.value.uuid === '278401ee-3d6f-4c65-9455-f1c16d0a7a98' &&
conceptUuid === '722dd83a-c1cf-48ad-ac99-45ac131ccc96'
) {
value = 'TRUE';
} else {
value = optsMap.find(
o => o['value.uuid - External ID'] == answer?.value?.uuid
)?.['DHIS2 Option Code']; //Changed from 'DHIS2 Option UID'
}
value = optsMap.find(
o => o['value.uuid - External ID'] == answer?.value?.uuid
)?.['DHIS2 Option Code']; //Changed from 'DHIS2 Option UID
//**** TODO: this is only true if DE = pN4iQH4AEzk' *************
// if (
// //TODO: this is only true if DE = pN4iQH4AEzk
// answer.value.uuid === '278401ee-3d6f-4c65-9455-f1c16d0a7a98' &&
// conceptUuid === '1a8bf24f-4f36-4971-aad9-ae77f3525738'
// ) {
// value = 'TRUE';
// } else {
// value = optsMap.find(
// o => o['value.uuid - External ID'] == answer?.value?.uuid
// )?.['DHIS2 Option Code']; //Changed from 'DHIS2 Option UID'
// }
}
}
if (!answer) {
Expand Down

0 comments on commit 4bcf231

Please sign in to comment.