Skip to content

Commit

Permalink
remove encountersMapping
Browse files Browse the repository at this point in the history
  • Loading branch information
mtuchi committed Nov 11, 2024
1 parent fd34ee3 commit adc681e
Showing 1 changed file with 7 additions and 113 deletions.
120 changes: 7 additions & 113 deletions workflows/wf2/6-create-events.js
Original file line number Diff line number Diff line change
@@ -1,120 +1,14 @@
const processAnswer = (answer, conceptUuid, dataElement, optsMap) => {
// console.log('Has answer', conceptUuid, dataElement);
return typeof answer.value === 'object'
? processObjectAnswer(answer, conceptUuid, dataElement, optsMap)
: processOtherAnswer(answer, conceptUuid, dataElement);
};

const processObjectAnswer = (answer, conceptUuid, dataElement, optsMap) => {
if (isDiagnosisByPsychologist(conceptUuid, dataElement)) {
return '' + answer.value.uuid === '278401ee-3d6f-4c65-9455-f1c16d0a7a98';
}
return findMatchingOption(answer, optsMap);
};

const processOtherAnswer = (answer, conceptUuid, dataElement) => {
if (isPhq9Score(answer.value, conceptUuid, dataElement)) {
return getRangePhq(answer.value);
}
return answer.value;
};

const processNoAnswer = (data, conceptUuid, dataElement) => {
// console.log('No answer', conceptUuid, dataElement);
if (isEncounterDate(conceptUuid, dataElement)) {
return data.encounterDatetime.replace('+0000', '');
}
return '';
};

const findMatchingOption = (answer, optsMap) => {
const matchingOption = optsMap.find(
o => o['value.uuid - External ID'] === answer.value.uuid
)?.['DHIS2 Option Code'];

return matchingOption || '';
};

const isEncounterDate = (conceptUuid, dataElement) => {
return (
conceptUuid === 'encounter-date' &&
['CXS4qAJH2qD', 'I7phgLmRWQq', 'yUT7HyjWurN'].includes(dataElement)
);
};

const isDiagnosisByPsychologist = (conceptUuid, dataElement) =>
conceptUuid === '722dd83a-c1cf-48ad-ac99-45ac131ccc96' &&
dataElement === 'pN4iQH4AEzk';

const isPhq9Score = (value, conceptUuid, dataElement) =>
typeof value === 'number' &&
conceptUuid === '5f3d618e-5c89-43bd-8c79-07e4e98c2f23' &&
dataElement === 'tsFOVnlc6lz';

const getRangePhq = input => {
if (input >= 20) return '>20';
if (input >= 15) return '15_19';
if (input >= 10) return '10_14';
if (input >= 5) return '5_9';
return '0_4';
};

const dataValuesMapping = (data, dataValueMap, optsMap) => {
return Object.keys(dataValueMap)
.map(dataElement => {
const conceptUuid = dataValueMap[dataElement];
const answer = data.obs.find(o => o.concept.uuid === conceptUuid);
const value = answer
? processAnswer(answer, conceptUuid, dataElement, optsMap)
: processNoAnswer(data, conceptUuid, dataElement);

return { dataElement, value };
})
.filter(d => d);
};

// Prepare DHIS2 data model for create events
fn(state => {
state.encountersMapping = state.encounters
.map(data => {
const form = state.formMaps[data.form.uuid];
const occurredAt = data.encounterDatetime.replace('+0000', '');
const { trackedEntity, enrollment } = state.TEIs[data.patient.uuid] || {};

const event = {
program: state.program,
orgUnit: state.orgUnit,
trackedEntity,
enrollment,
occurredAt,
};
if (!form.dataValueMap) {
console.log(data.patient);
}
if (form && form.dataValueMap) {
return {
...event,
programStage: form.programStage,
dataValues: dataValuesMapping(data, form.dataValueMap, state.optsMap),
};
}
})
.filter(e => e);

return state;
});

//For troubleshooting, we need to see the payload sent to DHIS2
fn(state => {
console.log('payload to import:: ', JSON.stringify($.encountersMapping, null, 2));
return state;
})

// Create events for each encounter
create(
'tracker',
{
events: $.encountersMapping,
events: state => {
console.log(
'Creating events for: ',
JSON.stringify(state.encountersMapping, null, 2)
);
return state.encountersMapping;
},
},
{
params: {
Expand Down

0 comments on commit adc681e

Please sign in to comment.