diff --git a/openfn-cd92dd57-9a3c-4318-bdcb-f57a386cf811-spec.yaml b/openfn-cd92dd57-9a3c-4318-bdcb-f57a386cf811-spec.yaml index ee6fd6a..b9cfab9 100644 --- a/openfn-cd92dd57-9a3c-4318-bdcb-f57a386cf811-spec.yaml +++ b/openfn-cd92dd57-9a3c-4318-bdcb-f57a386cf811-spec.yaml @@ -182,6 +182,13 @@ workflows: credential: mtuchi@openfn.org-MSF-DHIS2-UAT body: path: workflows/wf2/6-create-events.js + + Update-TEIs: + name: Update TEIs + adaptor: '@openfn/language-dhis2@5.0.1' + credential: mtuchi@openfn.org-MSF-DHIS2-UAT + body: + path: workflows/wf2/7-update-teis.js Mappings: name: Mappings @@ -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 diff --git a/workflows/wf2/4-get-options-map.js b/workflows/wf2/4-get-options-map.js index e44967e..bbe56f4 100644 --- a/workflows/wf2/4-get-options-map.js +++ b/workflows/wf2/4-get-options-map.js @@ -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 diff --git a/workflows/wf2/6-create-events.js b/workflows/wf2/6-create-events.js index c307c70..4135bc8 100644 --- a/workflows/wf2/6-create-events.js +++ b/workflows/wf2/6-create-events.js @@ -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 }) +); diff --git a/workflows/wf2/7-update-teis.js b/workflows/wf2/7-update-teis.js new file mode 100644 index 0000000..42a06ed --- /dev/null +++ b/workflows/wf2/7-update-teis.js @@ -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 })); diff --git a/workflows/wf2/workflow.json b/workflows/wf2/workflow.json index e0cc270..7052f5a 100644 --- a/workflows/wf2/workflow.json +++ b/workflows/wf2/workflow.json @@ -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": { @@ -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" } ] }