Skip to content

Commit

Permalink
map events to enable upsert (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtuchi authored Nov 12, 2024
1 parent 4bfa16a commit e2791e4
Showing 1 changed file with 15 additions and 25 deletions.
40 changes: 15 additions & 25 deletions workflows/wf2/5-get-teis.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ each(
orgUnit: $.orgUnit,
program: $.program,
filter: [`AYbfTPYMNJH:Eq:${$.data.patient.uuid}`],
fields: '*',
fields: '*,enrollments[*],enrollments[events[*]]',
},
{},
async state => {
Expand All @@ -20,7 +20,7 @@ each(
state.TEIs ??= {};
state.TEIs[encounter.patient.uuid] = {
trackedEntity,
enrollments,
events: enrollments[0]?.events,
enrollment: enrollments[0]?.enrollment,
};
}
Expand Down Expand Up @@ -160,22 +160,6 @@ const dataValuesMapping = (data, dataValueMap, optsMap, optionSetKey) => {

// Prepare DHIS2 data model for create events
fn(state => {
const createEvent = (data, state) => {
const { trackedEntity, enrollment } = state.TEIs[data.patient.uuid] || {};

if (!trackedEntity || !enrollment) {
return null;
}

return {
program: state.program,
orgUnit: state.orgUnit,
trackedEntity,
enrollment,
occurredAt: data.encounterDatetime.replace('+0000', ''),
};
};

const handleMissingRecord = (data, state) => {
const { uuid, display } = data.patient;

Expand All @@ -187,23 +171,29 @@ fn(state => {
patient: display,
};

state.missingRecords[uuid].encounters.push(data);
state.missingRecords[uuid].encounters.push(data.uuid);
};

const processEncounter = (data, state) => {
const event = createEvent(data, state);
if (!event) {
handleMissingRecord(data, state);
const form = state.formMaps[data.form.uuid];
if (!form?.dataValueMap) {
return null;
}
const { trackedEntity, enrollment, events } =
state.TEIs[data.patient.uuid] || {};

const form = state.formMaps[data.form.uuid];
if (!form?.dataValueMap) {
if (!trackedEntity || !enrollment) {
handleMissingRecord(data, state);
return null;
}

return {
...event,
event: events.find(e => e.programStage === form.programStage)?.event,
program: state.program,
orgUnit: state.orgUnit,
trackedEntity,
enrollment,
occurredAt: data.encounterDatetime.replace('+0000', ''),
programStage: form.programStage,
dataValues: dataValuesMapping(
data,
Expand Down

0 comments on commit e2791e4

Please sign in to comment.