generated from OpenFn/project
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wf2-5-createEvents.js
129 lines (110 loc) · 3.87 KB
/
wf2-5-createEvents.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
fn(state => {
const TEIs = {};
return { ...state, TEIs };
});
// Fetch TEI's for each patient
// each(
// 'encounters[*]',
// get(
// 'trackedEntityInstances',
// state => ({
// ou: 'l22DQq4iV3G',
// filter: [`jGNhqEeXy2L:Eq:${state.data.patient.uuid}`],
// }),
// {},
// state => {
// const encounter = state.references[0];
// console.log(encounter.patient.uuid, 'patient uuid')
// console.log(state.data.trackedEntityInstances)
// state.TEIs[encounter.patient.uuid] =
// state.data.trackedEntityInstances[0].trackedEntityInstance;
// return state;
// }
// )
// );
fn(async state => {
const { encounters } = state;
const getTEI = async encounter => {
await new Promise(resolve => setTimeout(resolve, 2000), 'OCL Mappings');
await get(
'trackedEntityInstances',
{
ou: 'l22DQq4iV3G',
filter: [`jGNhqEeXy2L:Eq:${encounter.patient.uuid}`],
},
{},
state => {
console.log(encounter.patient.uuid, 'Encounter patient uuid');
state.TEIs[encounter.patient.uuid] =
state.data.trackedEntityInstances[0].trackedEntityInstance;
return state;
}
)(state);
};
for (const encounter of encounters) {
await getTEI(encounter);
}
return state;
});
// Prepare DHIS2 data model for create events
fn(state => {
const { oclMappings, TEIs } = state;
//console.log(JSON.stringify(oclMappings, null, 2));
const encountersMapping = state.encounters.map(data => {
const encounterDate = data.encounterDatetime.replace('+0000', '');
const pluckObs = arg => data.obs.find(ob => ob.concept.uuid === arg);
//console.log('Observation ::', pluckObs);
// const pluckOcl = arg =>
// oclMappings.find(ocl => ocl.from_concept_name_resolved === arg); //TODO: map using concept uid, not name
const pluckOcl = arg =>
oclMappings.find(ocl => ocl.from_concept_code === arg);
//console.log('OCL code match ::', pluckOcl);
const obs1 = pluckObs('da33d74e-33b3-495a-9d7c-aa00a-aa0160');
const obs2 = pluckObs('da33d74e-33b3-495a-9d7c-aa00a-aa0177');
// const oclMap1 = obs1 && pluckOcl(obs1.value.display);
// const oclMap2 = obs2 && pluckOcl(obs2.value.display);
const cleanedObs1 = obs1.value.uuid.split('-').pop().toUpperCase();
const cleanedObs2 = obs2.value.uuid.split('-').pop().toUpperCase();
console.log('cleanedObs1 ', cleanedObs1);
console.log('cleanedObs2 ', cleanedObs2);
const oclMap1 = obs1 && pluckOcl(cleanedObs1);
const oclMap2 = obs2 && pluckOcl(cleanedObs2);
console.log('oclMapping for Obs1 ', JSON.stringify(oclMap1, null, 2));
console.log('oclMapping for Obs2 ', JSON.stringify(oclMap2, null, 2));
// const valueForEncounter1 = oclMap1 ? oclMap1.to_concept_name_resolved : '';
// const valueForEncounter2 = oclMap2 ? oclMap2.to_concept_name_resolved : '';
const valueForEncounter1 = oclMap1
? oclMap1.to_concept.extras.dhis2_option_code
: '';
const valueForEncounter2 = oclMap2
? oclMap2.to_concept.extras.dhis2_option_code
: '';
console.log('valueForEncounter1', valueForEncounter1);
console.log('valueForEncounter2', valueForEncounter2);
return {
program: 'uGHvY5HFoLG',
orgUnit: 'l22DQq4iV3G',
programStage: 'hfKSeo6nZK0',
trackedEntityInstance: TEIs[data.patient.uuid],
eventDate: encounterDate,
dataValues: [
{
dataElement: 'ZTSBtZKc8Ff', //diagnosis
value: valueForEncounter1,
},
{
dataElement: 'vqGFXhDM1XG', //entry triage color
value: valueForEncounter2,
},
],
};
});
return { ...state, encountersMapping };
});
// Create events fore each encounter
each(
'encountersMapping[*]',
create('events', state => state.data)
);
// Clean up state
fn(state => ({ ...state, data: {}, references: [] }));