generated from OpenFn/project
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwf2-2-upsertTEIs.js
142 lines (124 loc) · 3.26 KB
/
wf2-2-upsertTEIs.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
130
131
132
133
134
135
136
137
138
139
140
141
142
fn(state => {
const genderOptions = {
M: 'male',
F: 'female',
U: 'unknown',
O: 'prefer_not_to_answer',
};
const DHIS2_PATIENT_NUMBER = '8d79403a-c2cc-11de-8d13-0010c6dffd0f';
const OPENMRS_AUTO_ID = '05a29f94-c0ed-11e2-94be-8c13b969e334';
const patientsUpsert = [];
const buildPatientsUpsert = (patient, isNewPatient) => {
const dateCreated = patient.auditInfo.dateCreated.substring(0, 10);
const { identifier } =
patient.identifiers.find(
i => i.identifierType.uuid === DHIS2_PATIENT_NUMBER
) ||
patient.identifiers.find(i => i.identifierType.uuid === OPENMRS_AUTO_ID);
const enrollments = [
{
orgUnit: 'l22DQq4iV3G',
program: 'uGHvY5HFoLG',
programStage: 'hfKSeo6nZK0',
enrollmentDate: dateCreated,
},
];
const payload = {
query: {
ou: 'l22DQq4iV3G',
filter: [`jGNhqEeXy2L:Eq:${patient.uuid}`],
},
data: {
program: 'uGHvY5HFoLG',
orgUnit: 'l22DQq4iV3G',
trackedEntityType: 'cHlzCA2MuEF',
attributes: [
{
attribute: 'P4wdYGkldeG',
value: identifier,
},
{
attribute: 'jGNhqEeXy2L',
value: patient.uuid,
},
{
attribute: 'qptKDiv9uPl',
value: genderOptions[patient.person.gender],
},
{
attribute: 'T1iX2NuPyqS',
value: patient.person.age,
},
],
},
};
if (isNewPatient) {
console.log('create enrollmenet');
payload.data.enrollments = enrollments;
}
return patientsUpsert.push(payload);
};
return {
...state,
genderOptions,
patientsUpsert,
buildPatientsUpsert,
};
});
fn(async state => {
const { buildPatientsUpsert, patients } = state;
const getPatient = async patient => {
await new Promise(resolve => setTimeout(resolve, 2000));
await get(
'trackedEntityInstances',
{
ou: 'l22DQq4iV3G',
filter: [`jGNhqEeXy2L:Eq:${patient.uuid}`],
},
{},
state => {
const { trackedEntityInstances } = state.data;
const isNewPatient = trackedEntityInstances.length === 0;
buildPatientsUpsert(patient, isNewPatient);
return state;
}
)(state);
};
for (const patient of patients) {
console.log(patient.uuid, 'patient uuid');
await getPatient(patient);
}
return state;
});
// Prepare DHIS2 data model for patients
// each(
// 'patients[*]',
// get(
// 'trackedEntityInstances',
// state => ({
// ou: 'l22DQq4iV3G',
// filter: [`jGNhqEeXy2L:Eq:${state.data.uuid}`],
// }),
// {},
// state => {
// const { buildPatientsUpsert, references, data } = state;
// const { trackedEntityInstances } = data;
// const patient = references[0];
// console.log(patient.uuid);
// const isNewPatient = trackedEntityInstances.length === 0;
// buildPatientsUpsert(patient, isNewPatient);
// return state;
// }
// )
// );
// Upsert TEIs to DHIS2
each(
'patientsUpsert[*]',
upsert(
'trackedEntityInstances',
state => state.data.query,
state => state.data.data
)
);
// Clean up state
fn(state => ({ ...state, data: {} }));