Skip to content

Instantly share code, notes, and snippets.

@haftamuk
Last active January 17, 2023 02:27
Show Gist options
  • Save haftamuk/67abbe8f91ee8ef143a72615a2c9fb0a to your computer and use it in GitHub Desktop.
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;
});
@haftamuk
Copy link
Author

I have made a few updates!
Kindly check if that is what you were saying.

@mtuchi
Copy link

mtuchi commented Jan 11, 2023

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

@haftamuk
Copy link
Author

See mapping spec here

Kindly adjust access of the shared excel document!

@mtuchi
Copy link

mtuchi commented Jan 11, 2023

Kindly adjust access of the shared excel document!

Mapping spec

https://gist.github.com/mtuchi/a03657bcf42cc7b1296a05083be1ec0a?permalink_comment_id=4425254#gistcomment-4425254

@haftamuk here is the mappingSpec, I also adjusted the access restriction See the mapping spec

@haftamuk
Copy link
Author

Kindly adjust access of the shared excel document!

Mapping spec

https://gist.github.com/mtuchi/a03657bcf42cc7b1296a05083be1ec0a?permalink_comment_id=4425254#gistcomment-4425254

@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!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment