Skip to content

Commit

Permalink
Add mappings for placeOfLivingMap and auto-generate `state.genderOp…
Browse files Browse the repository at this point in the history
…tions` (#71)

* remove placeOfliving mapping

* add placeOfliving mapping

and format existing code
  • Loading branch information
mtuchi authored Nov 27, 2024
1 parent 5d1b953 commit b3cd8b6
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 39 deletions.
14 changes: 3 additions & 11 deletions workflows/wf2/2-mappings.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,10 @@ fn(state => {
});

fn(state => {
//TODO: Update dynamically like placeOfLivingMap?
state.genderOptions = {
M: 'male',
F: 'female',
U: 'unknown',
O: 'prefer_not_to_answer',
};

state.placeOflivingMap = state.optsMap
.filter(o => o['OptionSet name'] === 'Place of Living')
state.genderOptions = state.optsMap
.filter(o => o['OptionSet name'] === 'Sex - Patient')
.reduce((acc, value) => {
acc[value['value.display - Answers']] = value['DHIS2 Option Code'];
acc[value['value.uuid - External ID']] = value['DHIS2 Option Code'];
return acc;
}, {});

Expand Down
2 changes: 1 addition & 1 deletion workflows/wf3/1-fetch-metadata.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn(state => {
state.sheets = ['OptionSets', 'identifiers'];
state.sheets = ['OptionSets', 'identifiers', 'Places of living'];
state.siteId =
'openfnorg.sharepoint.com,4724a499-afbc-4ded-a371-34ae40bf5d8d,1d20a7d4-a6f1-407c-aa77-76bd47bb0f32';
return state;
Expand Down
82 changes: 57 additions & 25 deletions workflows/wf3/2-map-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,40 @@ const questionKeyValuePairs = arr => {
}
const mappedArr = arr.slice(2).map(item => mapArrayToObject(item, arr[1]));
try {
return mappedArr.filter(
o => isValidValue(o['External ID']) && isValidValue(o['DHIS2 Option Set UID'])
)
.map(value => ({
[value['DHIS2 Option Set UID']]: value['External ID']
}));
return mappedArr
.filter(
o =>
isValidValue(o['External ID']) &&
isValidValue(o['DHIS2 Option Set UID'])
)
.map(value => ({
[value['DHIS2 Option Set UID']]: value['External ID'],
}));
} catch (error) {
console.error(`Error processing ${arr}:`, error);
return arr; // Return original value if processing fails
}
};

fn(state => {
state.placeOflivingMap = state['Places of living']
.slice(2)
.map(item => mapArrayToObject(item, state['Places of living'][1]))
.filter(
o =>
(isValidValue(o['External ID']) &&
isValidValue(o['DHIS2 DE full name'])) ||
(isValidValue(o['value.display - Answers']) &&
isValidValue(o['DHIS2 Option code']))
)
.reduce((acc, value) => {
acc[value['Answers']] = value['DHIS2 Option code'];
return acc;
}, {});

return state;
});

fn(state => {
const { OptionSets, identifiers } = state;
const keys = OptionSets[1];
Expand All @@ -60,9 +82,12 @@ fn(state => {
isValidValue(o['DHIS2 Option code']))
)
.map(o => {
const optionSetUid = o['DHIS2 Option Set UID']=='NA' ? '' : `-${o['DHIS2 Option Set UID']}`;
//then build an answerKeyUid = DEuid + OptionSetUid
const answerKeyUid = `${o['External ID']}${optionSetUid}`;
const optionSetUid =
o['DHIS2 Option Set UID'] == 'NA'
? ''
: `-${o['DHIS2 Option Set UID']}`;
//then build an answerKeyUid = DEuid + OptionSetUid
const answerKeyUid = `${o['External ID']}${optionSetUid}`;

return {
'DHIS2 Option Set UID': o['DHIS2 Option Set UID'],
Expand All @@ -71,7 +96,7 @@ fn(state => {
'DHIS2 Option Code': o['DHIS2 Option code'],
'value.display - Answers': o['Answers'],
'value.uuid - External ID': o['External ID'],
'answerMappingUid': answerKeyUid,
answerMappingUid: answerKeyUid,
'DHIS2 DE full name': o['DHIS2 DE full name'],
'DHIS2 DE UID': o['DHIS2 DE UID'],
'OptionSet name': o['OptionSet name'],
Expand All @@ -93,9 +118,8 @@ fn(state => {
return state;
});


fn(state => {
const { formMetadata, optsMap, identifiers } = state;
const { formMetadata, optsMap, identifiers, placeOflivingMap } = state;

const formMaps = formMetadata.reduce((acc, form) => {
const formName = form['OMRS form sheet name'];
Expand All @@ -111,17 +135,19 @@ fn(state => {
return acc;
}, {});

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 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
Expand All @@ -131,6 +157,12 @@ fn(state => {
// return acc;
// }, {})


return { formMaps, formMetadata, optsMap, optionSetKey, identifiers };
return {
formMaps,
formMetadata,
optsMap,
optionSetKey,
identifiers,
placeOflivingMap,
};
});
12 changes: 10 additions & 2 deletions workflows/wf3/3-save-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ get(metadataPath, {
});

fn(state => {
const { formMaps, formMetadata, optsMap, data, identifiers, optionSetKey } =
state;
const {
formMaps,
formMetadata,
optsMap,
data,
identifiers,
optionSetKey,
placeOflivingMap,
} = state;

state.body = {
message: 'Update metadata content',
Expand All @@ -27,6 +34,7 @@ fn(state => {
formMaps,
identifiers,
formMetadata,
placeOflivingMap,
})
),
sha: data.sha,
Expand Down

0 comments on commit b3cd8b6

Please sign in to comment.