-
-
Save haftamuk/67abbe8f91ee8ef143a72615a2c9fb0a to your computer and use it in GitHub Desktop.
This job fetches patients record from an EMR and transforms records to DHIS2 TrackedEntityInstances and writes the output in to output.json. | |
Run the job with the following command | |
openfn .\getEMRPatientData.js -a http --no-strict-output | |
Look in to the output.json (scroll down till you see userByUserId as a key). |
// Extract data from an EMR API | |
get('https://jsonplaceholder.typicode.com/users'); | |
// Initialize a mapping variable | |
// Iterate over the extracted EMR data and map data to new data elements | |
// User by userId | |
fn(state => { | |
const users = state.data; | |
// Collect user by userId | |
// r is an accumulator, a is a currentValue | |
const patientsRecord = []; | |
const mappedPatientsData = users.reduce((r, a) => { | |
//Initialize TrackedEntiryRecord and map attributes | |
const trackedEntity = {}; | |
// Set static ID values of DHIS2 trackedEntityInstances(Puting such information in state.json would be good practice!) | |
trackedEntity["orgUnit"] = "g8upMTyEZGZ"; | |
trackedEntity["trackedEntityType"] = "nEenWmSyUEp"; | |
// This is the dynamic part of the trackedEntityInstances record | |
trackedEntity["attributes"] = []; | |
// Initialize array entries for each Tracked entity attributes(Static attribute ID and dynamic value extracted from patient data) | |
// This is were we do the attribute mapping | |
// In this demonstration only Name and Email are used | |
var trackedEntityGenderAttributeValue = | |
{ attribute: "cejWyOfXge6", value: "Male" }; | |
var trackedEntityUniqueIdAttributeValue = | |
{ attribute: "lZGmxYbs97q", value: a.phone }; | |
var trackedEntityFisrtNameAttributeValue = | |
{ attribute: "w75KJ2mc4zz", value: a.name }; | |
var trackedEntityLastNameAttributeValue = | |
{ attribute: "zDhUuAYrxNC", value: a.name }; | |
trackedEntity["attributes"].push(trackedEntityGenderAttributeValue); | |
trackedEntity["attributes"].push(trackedEntityUniqueIdAttributeValue); | |
trackedEntity["attributes"].push(trackedEntityFisrtNameAttributeValue); | |
trackedEntity["attributes"].push(trackedEntityLastNameAttributeValue); | |
patientsRecord.push(trackedEntity); | |
return patientsRecord; | |
}, {}); | |
return { ...state, mappedPatientsData }; | |
}); |
// This job recieves output.json from previous job as an input | |
// data elements mapping and transformation tookplace in the previous job. | |
// In order to run this job makesure DHIS2 credentials are set properly | |
each("mappedPatientsData[*]", state => { | |
// state.data will contain values of each iteration | |
create('trackedEntityInstances', state.data); | |
return state; | |
}); |
I have made a few updates!
Kindly check if that is what you were saying.
I have made a few updates! Kindly check if that is what you were saying.
Yes that's perfect, now you should update the mapping for orgUnit
and trackedEntityType
See mapping spec here
For Gender
Mapping just leave to Male
for all users
See mapping spec here
Kindly adjust access of the shared excel document!
Kindly adjust access of the shared excel document!
Mapping spec
@haftamuk here is the mappingSpec
, I also adjusted the access restriction See the mapping spec
Kindly adjust access of the shared excel document!
Mapping spec
@haftamuk here is the
mappingSpec
, I also adjusted the access restriction See the mapping spec
Thanks I have updated the mapping information of the first job.
I have also added the second job(tiny code!)
@haftamuk thank you for sharing the first job from the homework assignment
If I could make a few suggestions,
Is regarding the naming convention of variables. It's important all variables used have a clear name with context to what you're trying to achieve so for example instead of
userByUserId
you can rename it tomappedPatientsData
, instead ofusers
it can bepatients
.The final state of this job
userByUserIdmappedPatientsData
, can you group this as an array so that it's easy to manipulate in your next job. Eg:This will make it easy to manipulate in the next job