Created
April 13, 2023 10:44
-
-
Save aleksa-krolls/fc46446dfdd2391578df30a7d57360d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Log size of initial state | |
fn(state => { | |
const { data, year } = state; | |
console.log( | |
`Initial state for Job 3 contains ${data.results.length} aggregate records`, | |
`from SLUHIS for ${year}.` | |
); | |
return state; | |
}); | |
// Build dhis2 dataValues | |
fn(state => { | |
// Extract orgUnitGlobals, dataElementGlobals and results from state object | |
const { dataElementGlobals, categoryOptionCombos, data } = state; | |
const { results } = data; | |
const getDataElement = (code, label) => { | |
if (!dataElementGlobals[code]) { | |
console.log(`⚠ ATC Code '${code}' not found in dataElementGlobals.`); | |
return null; | |
} | |
if (!dataElementGlobals[code][label]) { | |
console.log(`⚠ Dose Label '${label}' not found for ATC Code '${code}'.`); | |
return null; | |
} | |
return dataElementGlobals[code][label]; | |
}; | |
const getCategoryOptionCombo = (genderChar, internal, underOne) => { | |
if (!categoryOptionCombos[genderChar]) { | |
console.log( | |
`⚠ Gender character '${genderChar}' not found in categoryOptionCombos.` | |
); | |
return null; | |
} | |
if (!categoryOptionCombos[genderChar][internal]) { | |
console.log(`⚠ Internal/External missing!.`); | |
return null; | |
} | |
if (!categoryOptionCombos[genderChar][internal][underOne]) { | |
console.log(`⚠ Under One assignment missing!.`); | |
return null; | |
} | |
return categoryOptionCombos[genderChar][internal][underOne]; | |
}; | |
const dataValues = results | |
.map((result, index) => { | |
const { | |
count, | |
orgUnit, | |
atcCode, | |
doseLabel, | |
period, | |
underOne, | |
internal, | |
gender, | |
} = result; | |
if (!orgUnit) | |
console.log(`⚠ WARNING! orgUnit for record #${index} was not found!.`); | |
const dataElement = getDataElement(atcCode, doseLabel); | |
const categoryOptionCombo = getCategoryOptionCombo( | |
gender, | |
internal, | |
underOne | |
); | |
return { | |
dataElement: dataElement, | |
period, | |
orgUnit: orgUnit, | |
categoryOptionCombo, | |
value: count, | |
}; | |
}) | |
.filter(obj => Object.values(obj).every(val => val !== null)); | |
console.log('Aggregate records received from SLUHIS:', results.length); | |
console.log('Valid records to send to DHIS2:', dataValues.length); | |
const nullOrgUnits = results.filter(result => !result.orgUnit).length; | |
console.log(`WARNING! THERE WERE ${nullOrgUnits} null orgUnits!`); | |
return { ...state, dataValues }; | |
}); | |
// fn(state => { | |
// console.log('for DHIS2', state.dataValues); | |
// return state; | |
// }); | |
// Load datavalue sets using dhis2's version of "UPSERT" (see `importStrategy`) | |
create('dataValueSets', state => ({ dataValues: state.dataValues }), { | |
importStrategy: 'CREATE_AND_UPDATE', | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment