Skip to content

Commit

Permalink
Add update trackedEntities step (#44)
Browse files Browse the repository at this point in the history
* update gist url

* add gender updated mappings and update TEIs to address #38 

* update lastRunDateTime

* add update-teis step

* commented out G0hLyxqgcO7

* add log when creating an event

* update optionsMap gist link

latest optionSets

---------

Co-authored-by: Aleksa Krolls <[email protected]>
  • Loading branch information
mtuchi and aleksa-krolls authored Oct 3, 2024
1 parent 2cbe86a commit 10a6f83
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 14 deletions.
14 changes: 14 additions & 0 deletions openfn-cd92dd57-9a3c-4318-bdcb-f57a386cf811-spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ workflows:
credential: [email protected]
body:
path: workflows/wf2/6-create-events.js

Update-TEIs:
name: Update TEIs
adaptor: '@openfn/[email protected]'
credential: [email protected]
body:
path: workflows/wf2/7-update-teis.js

Mappings:
name: Mappings
Expand Down Expand Up @@ -227,6 +234,13 @@ workflows:
condition_label: has-teis
condition_expression: state.TEIs && !state.errors
enabled: true
Create-Events->Update-TEIs:
source_job: Create-Events
target_job: Update-TEIs
condition_type: js_expression
condition_label: has-gender-updated
condition_expression: state.genderUpdated.length > 0 && !state.errors
enabled: true
Mappings->Upsert-TEIs:
source_job: Mappings
target_job: Upsert-TEIs
Expand Down
2 changes: 1 addition & 1 deletion workflows/wf2/4-get-options-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const mhpssFollowup = {
rGS6S4jyKu3: '08cd4b4a-4b0b-4391-987b-b5b3d770d30f', //Location of intervention - If Mobile Clinic, specify
L6Jj7Tppr5q: 'e08d532b-e56c-43dc-b831-af705654d2dc', //Location of intervention - If other, specify
vWWl7izQpqd: '82978311-bef9-46f9-9a9a-cc62254b00a6', //Type of intervention
G0hLyxqgcO7: '54e8c1b6-6397-4822-89a4-cf81fbc68ce9', //The patient did not come
// G0hLyxqgcO7: '54e8c1b6-6397-4822-89a4-cf81fbc68ce9', //The patient did not come TODO @aleksa this is causing an error when creating an event
tloy0Bd9qDf: 'd7410cd3-29be-4f8b-93d6-eb4de005db29', //Number of appointments missed
AZUFlZzPN6V: '278d3d1e-c02a-4db1-8ab3-8db3b82eb9b5', //Patient rescheduled
HNZHetAtowR: 'd34d5e93-03d6-494e-8f4c-2d7221227162', //Reason for missed appointment
Expand Down
41 changes: 30 additions & 11 deletions workflows/wf2/6-create-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,42 @@ fn(state => {
}
});

console.log(
'dhis2 events to import:: ',
JSON.stringify(state.encountersMapping, null, 2)
);

return state;
});

//Create events for each encounter
// Create events for each encounter
each(
'$.encountersMapping[*]',
create('events', $.data, {
params: {
dataElementIdScheme: 'UID',
create(
'events',
state => {
console.log('dhis2 event to import:: ', state.data);
return state.data;
},
})
{
params: {
dataElementIdScheme: 'UID',
},
}
)
);

fn(state => {
state.genderUpdated = state.encounters.reduce((acc, e) => {
const answer = e.obs.find(
o => o.concept.uuid === 'ec42d68d-3e23-43de-b8c5-a03bb538e7c7'
);
if (answer) {
acc.push(answer);
}
return acc;
}, []);

return state;
});

// Return only lastRunDateTime
fn(({ lastRunDateTime }) => ({ lastRunDateTime }));
fnIf(
state => state.genderUpdated.length === 0,
({ lastRunDateTime }) => ({ lastRunDateTime })
);
39 changes: 39 additions & 0 deletions workflows/wf2/7-update-teis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
fn(state => {
const { optsMap, genderUpdated, TEIs } = state;
const genderMap = optsMap
.filter(o => o['DHIS2 DE UID'] === 'qptKDiv9uPl')
.reduce((acc, obj) => {
acc[obj['value.display - Answers']] = obj['DHIS2 Option Code'];
return acc;
}, {});

state.teisToUpdate = genderUpdated.map(answer => {
const { trackedEntity } = TEIs[answer.person.uuid];
return {
trackedEntity,
program: 'w9MSPn5oSqp',
orgUnit: 'OPjuJMZFLop',
trackedEntityType: 'cHlzCA2MuEF',
attributes: [
{
attribute: 'qptKDiv9uPl', //gender
value: genderMap[answer.value.display],
},
{
attribute: 'AYbfTPYMNJH', //OpenMRS Patient UID to use to upsert TEI
value: answer.person.uuid,
},
],
};
});
return state;
});

// Update TEIs
create(
'tracker',
{ trackedEntities: $.teisToUpdate },
{ params: { async: false, importStrategy: 'UPDATE' } }
);
// Return only lastRunDateTime
fn(({ lastRunDateTime }) => ({ lastRunDateTime }));
13 changes: 11 additions & 2 deletions workflows/wf2/workflow.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"adaptor": "openmrs",
"configuration": "../tmp/openmrs-creds.json",
"state": {
"lastRunDateTime": "2024-09-30T12:00:00.000Z"
"lastRunDateTime": "2024-09-30T07:00:00.000Z"
},
"expression": "1-get-patients.js",
"next": {
Expand Down Expand Up @@ -60,7 +60,16 @@
"id": "create-events",
"adaptor": "dhis2",
"configuration": "../tmp/dhis2-creds.json",
"expression": "6-create-events.js"
"expression": "6-create-events.js",
"next": {
"update-teis": "state.genderUpdated.length > 0 && !state.errors"
}
},
{
"id": "update-teis",
"adaptor": "dhis2",
"configuration": "../tmp/dhis2-creds.json",
"expression": "7-update-teis.js"
}
]
}
Expand Down

0 comments on commit 10a6f83

Please sign in to comment.