Skip to content

Commit

Permalink
Cleanup workflow codes (#73)
Browse files Browse the repository at this point in the history
* remove unused code

* temporary add collections in job code (#74)

For offline support until we migrate to collections
  • Loading branch information
mtuchi authored Dec 16, 2024
1 parent 9ddcb24 commit 52d608d
Show file tree
Hide file tree
Showing 10 changed files with 45,414 additions and 111 deletions.
22,700 changes: 22,691 additions & 9 deletions workflows/wf1/1-fetch-metadata.js

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions workflows/wf1/2-get-teis-and-locations.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ get(
const teis = state.data.instances.filter(
tei => tei.updatedAt >= state.cursor
);
//for testing
//.filter(tei => tei.createdAt > state.cursor) //for prod
//.slice(0, 1); //to limit 1 for testing

console.log(
'# of TEIs found before filter ::',
Expand Down
7 changes: 1 addition & 6 deletions workflows/wf2/1-get-patients.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
//Here we define the date cursor
//$.cursor at beggining of the project 2023-05-20T06:01:24.000+0000
cursor($.lastRunDateTime || $.manualCursor || '2023-05-20T06:01:24.000+0000');
// Update the lastRunDateTime for the next run

cursor('today', {
key: 'lastRunDateTime',
format: c => dateFns.format(new Date(c), "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"),
});

searchPatient({ q: 'IQ', v: 'full', limit: '100' });
//searchPatient({ q: 'Katrina', v: 'full', limit: '100' });
//Query all patients (q=all) not supported on demo OpenMRS; needs to be configured
//...so we query all Patients with name "Patient" instead

fn(state => {
const { cursor, data, lastRunDateTime } = state;
Expand Down
22,717 changes: 22,701 additions & 16 deletions workflows/wf2/2-mappings.js

Large diffs are not rendered by default.

10 changes: 3 additions & 7 deletions workflows/wf2/2-upsert-teis.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const buildPatientsUpsert = (state, patient, isNewPatient) => {

const findOptsUuid = uuid =>
patient.person.attributes.find(a => a.attributeType.uuid === uuid)?.value
?.uuid || patient.person.attributes.find(a => a.attributeType.uuid === uuid)?.value;
?.uuid ||
patient.person.attributes.find(a => a.attributeType.uuid === uuid)?.value;

const findOptCode = optUuid =>
state.optsMap.find(o => o['value.uuid - External ID'] === optUuid)?.[
Expand Down Expand Up @@ -87,7 +88,6 @@ const buildPatientsUpsert = (state, patient, isNewPatient) => {
},
};

// TODO: AK do we need this log👇🏾?
console.log('mapped dhis2 payloads:: ', JSON.stringify(payload, null, 2));

if (isNewPatient) {
Expand Down Expand Up @@ -129,11 +129,7 @@ each(
// Upsert TEIs to DHIS2
each(
$.patientsUpsert,
upsert('trackedEntityInstances', $.data.query, state => {
// Uncomment👇🏾 for inspecting input payload
// console.log('Upserting', state.data.data);
return state.data.data;
})
upsert('trackedEntityInstances', $.data.query, $.data.data)
);
fn(state => {
const {
Expand Down
1 change: 0 additions & 1 deletion workflows/wf2/3-get-encounters.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ each(
)
);

// Log filtered encounters
fn(state => {
const {
data,
Expand Down
71 changes: 16 additions & 55 deletions workflows/wf2/5-get-teis.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const processAnswer = (
optsMap,
optionSetKey
) => {
// console.log('Has answer', conceptUuid, dataElement);
return typeof answer.value === 'object'
? processObjectAnswer(
answer,
Expand All @@ -58,15 +57,17 @@ const processObjectAnswer = (
optionSetKey
) => {
if (isDiagnosisByPsychologist(conceptUuid, dataElement)) {
console.log('Yes done by psychologist..')
console.log('Yes done by psychologist..');
return '' + answer.value.uuid === '278401ee-3d6f-4c65-9455-f1c16d0a7a98';
}

if (isTrueOnlyQuestion(conceptUuid, dataElement)) {
console.log('True only question detected..', dataElement)
return answer.value.uuid === '681cf0bc-5213-492a-8470-0a0b3cc324dd' ? 'true' : undefined;
console.log('True only question detected..', dataElement);
return answer.value.uuid === '681cf0bc-5213-492a-8470-0a0b3cc324dd'
? 'true'
: undefined;
}
//return 'true';

return findMatchingOption(answer, optsMap, optionSetKey);
};

Expand All @@ -79,44 +80,37 @@ const processOtherAnswer = (answer, conceptUuid, dataElement) => {
};

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, optionSetKey) => {
const optionKey = `${answer.formUuid}-${answer.concept.uuid}`;
const optionKey = `${answer.formUuid}-${answer.concept.uuid}`;

//const matchingOptionSet = optionSetKey[answer.concept.uuid];
const matchingOptionSet = optionSetKey[optionKey];
console.log('optionKey', optionKey);
console.log('conceptUid', answer.concept.uuid);
console.log('value uid', answer.value.uuid);
console.log('value display', answer.value.display);
console.log('matchingOptionSet', matchingOptionSet);

//const answerKey = answerMappingUid

const matchingOption = optsMap.find(
o =>
o['value.uuid - External ID'] === answer.value.uuid &&
o['DHIS2 Option Set UID'] === matchingOptionSet
)?.['DHIS2 Option Code'] || answer.value.display; //TODO: revisit this logic if optionSet not found
const matchingOption =
optsMap.find(
o =>
o['value.uuid - External ID'] === answer.value.uuid &&
o['DHIS2 Option Set UID'] === matchingOptionSet
)?.['DHIS2 Option Code'] || answer.value.display; //TODO: revisit this logic if optionSet not found

console.log('matchingOption value', matchingOption)
console.log('matchingOption value', matchingOption);

// to convert ALL caps to lowercase per DHIS2 api
if (matchingOption === 'FALSE') {
console.log('false option', matchingOption)
return 'false';
}
if (matchingOption === 'TRUE') {
console.log('true option', matchingOption)
return 'true';
}
////=========================================//

return matchingOption || '';
};
Expand All @@ -139,8 +133,7 @@ const isDiagnosisByPsychologist = (conceptUuid, dataElement) =>
const isPhq9Score = (value, conceptUuid, dataElement) =>
typeof value === 'number' &&
(conceptUuid === '5f3d618e-5c89-43bd-8c79-07e4e98c2f23' ||
conceptUuid === '6545b874-f44d-4d18-9ab1-7a8bb21c0a15')

conceptUuid === '6545b874-f44d-4d18-9ab1-7a8bb21c0a15');

const getRangePhq = input => {
if (input >= 20) return '>20';
Expand All @@ -157,7 +150,7 @@ const dataValuesMapping = (data, dataValueMap, optsMap, optionSetKey) => {
const obsAnswer = data.obs.find(o => o.concept.uuid === conceptUuid);
const answer = {
...obsAnswer,
formUuid: data.form.uuid
formUuid: data.form.uuid,
};
const value = answer
? processAnswer(answer, conceptUuid, dataElement, optsMap, optionSetKey)
Expand Down Expand Up @@ -220,35 +213,3 @@ fn(state => {

return state;
});
// const findMatchingOption = (answer, optsMap, optionSetKey) => {
// const answerKeyUid = optionSetKey[answer.concept.uuid];

// const matchingOption = optsMap.find(
// (o) => o["DHIS2 answerKeyUid"] === answerKeyUid
// )?.["DHIS2 Option Code"];

// //TBD if we want this.. TODO: revisit this logic
// if (matchingOption === "no") {
// return "FALSE";
// }
// if (matchingOption === "yes") {
// return "TRUE";
// }
// //======//
// return matchingOption || "";
// };

//=== Original logic modified on Nov 11 =========//
// 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 || '';
// };
3 changes: 1 addition & 2 deletions workflows/wf2/6-create-events.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Create events for each encounter
// Create or update events for each encounter
create(
'tracker',
{
Expand Down Expand Up @@ -41,7 +41,6 @@ fn(state => {
return state;
});

// Return only lastRunDateTime
fnIf(
state => state.genderUpdated.length === 0,
({ lastRunDateTime }) => ({ lastRunDateTime })
Expand Down
2 changes: 1 addition & 1 deletion workflows/wf2/7-update-teis.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ create(
{ trackedEntities: $.teisToUpdate },
{ params: { async: false, importStrategy: 'UPDATE' } }
);
// Return only lastRunDateTime

fn(({ lastRunDateTime }) => ({ lastRunDateTime }));
11 changes: 0 additions & 11 deletions workflows/wf3/2-map-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,26 +137,15 @@ fn(state => {

const optionSetKey = Object.entries(formMaps).reduce(
(acc, [formKey, formValue]) => {
// Iterate over each object in the optionSetMap array
formValue.optionSetMap.forEach(item => {
// Extract the single key-value pair from each object in the array
const [originalKey, originalValue] = Object.entries(item)[0];
// Reverse key-value, adding form prefix
acc[`${formKey}-${originalValue}`] = originalKey;
});
return acc;
},
{}
);

// const optionSetKey = Object.entries(formMaps).reduce((acc, [formKey, formValue]) => {
// // Iterate over each optionSetMap entry and reverse key-value, adding form prefix
// Object.entries(formValue.optionSetMap).forEach(([originalKey, originalValue]) => {
// acc[`${formKey}-${originalValue}`] = originalKey;
// });
// return acc;
// }, {})

return {
formMaps,
formMetadata,
Expand Down

0 comments on commit 52d608d

Please sign in to comment.