Skip to content

Commit

Permalink
add encountersMapping
Browse files Browse the repository at this point in the history
  • Loading branch information
mtuchi committed Nov 11, 2024
1 parent 9d80fa1 commit fd34ee3
Showing 1 changed file with 139 additions and 0 deletions.
139 changes: 139 additions & 0 deletions workflows/wf2/5-get-teis.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,142 @@ each(
}
)
);

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'];

if (matchingOption === 'no') {
return 'FALSE';
}
if (matchingOption === 'yes') {
return 'TRUE';
}
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 => {
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;

console.log(uuid, 'Patient is missing trackedEntity && enrollment');

state.missingRecords ??= {};
state.missingRecords[uuid] ??= {
encounters: [],
patient: display,
};

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

const processEncounter = (data, state) => {
const event = createEvent(data, state);
if (!event) {
handleMissingRecord(data, state);
return null;
}

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

return {
...event,
programStage: form.programStage,
dataValues: dataValuesMapping(data, form.dataValueMap, state.optsMap),
};
};

state.encountersMapping = state.encounters
.map(data => processEncounter(data, state))
.filter(Boolean);

return state;
});

0 comments on commit fd34ee3

Please sign in to comment.