generated from OpenFn/project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenfn-3f789c20-4af7-47aa-81cd-511b22105623-spec.yaml
174 lines (158 loc) · 6.49 KB
/
openfn-3f789c20-4af7-47aa-81cd-511b22105623-spec.yaml
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
name: cht-sandbox
description: null
credentials:
name: DHIS2 2-40-6
owner: [email protected]
name: DHIS2 Dev
owner: [email protected]
workflows:
CHT-webhook:
name: CHT webhook
jobs:
DHIS2-mapping:
name: DHIS2 mapping
adaptor: '@openfn/[email protected]'
credential: null
body: |
//TODO: Remove this once we have orgUnit from report
// const facilities = {
// "712526f6-f826-45a7-a80b-6546ad9fde9d": "DiszpKrYNg8", //corresponding_on_DHIS2,
// };
const convertToBool = (val) => {
if (typeof val === 'boolean') return val;
if (typeof val === 'string') {
val = val.toLowerCase().trim();
return val === '1' || val === 'true' || val === 'yes';
}
return !!val;
};
const safeGet = (obj, path, defaultValue = undefined) => {
return path.split('.').reduce((acc, part) => acc && acc[part], obj) ?? defaultValue;
};
const genderMap = (value) => {
if (!value) return undefined;
return value.toLowerCase().trim().startsWith('f') ? 'Female' : 'Male';
};
const convertToNumeric = (value) => {
if (typeof value === 'number') return value;
if (typeof value === 'string') {
const parsed = parseFloat(value);
return isNaN(parsed) ? undefined : parsed;
}
return undefined;
};
const mapReport = (report) => {
const dataElements = [
// { dataElement: "EuiLnVQP7fz", path: 'fever_detected' },
// { dataElement: "c7DB86yPCPw", path: 'danger_signs' },
// { dataElement: "pUQ3PxCLBGW", path: 'symptoms_severity_level' },
// { dataElement: "UkSorKPBQKp", path: 'd_diarrhoea' },
{ dataElement: "mQfnNz6HAxg", path: "properties.d_disease_severe_malaria_possible", transform: convertToBool },
{ dataElement: "rUiCXLuXe0D", path: "properties.p_id" },
{ dataElement: "L9FyeXeKysL", path: "properties.p_gender", transform: genderMap },
{ dataElement: "sRViHZrNOOi", path: "properties.p_age" },
{ dataElement: "zWAPsDC96SK", path: "properties.p_weight" },
{ dataElement: "nrsvTZKdAxD", path: "properties.p_temp" },
{ dataElement: "f8AiyAoBmQZ", path: "properties.d_pneumonia", transform: convertToBool },
{ dataElement: "nOqH46e5VmP", path: "properties.d_malaria", transform: convertToBool },
{ dataElement: "S6XCvAD6uv6", path: "properties.d_scabies_infected", transform: convertToBool },
{ dataElement: "Vg8aEzNouYb", path: "properties.d_measles", transform: convertToBool },
{ dataElement: "Ofyvm4Ynqh7", path: "properties.ds_amoxicillin", transform: convertToBool },
{ dataElement: "EijcDbR4oWL", path: "properties.ds_paracetamol", transform: convertToNumeric },
{ dataElement: "DsiPa2sGoEe", path: "properties.ds_erythromycin", transform: convertToNumeric },
{ dataElement: "xpWPRlZlN7b", path: "properties.d_deworming_needed", transform: convertToBool },
{ dataElement: "tgUaWBaAIag", path: "properties.dur_amoxicillin" },
];
return {
dataValues: dataElements.map(({ dataElement, path, transform }) => ({
dataElement,
value: transform ? transform(safeGet(report, path)) : safeGet(report, path),
})),
notes: [{
value: safeGet(report, 'data.closed.doc.contact.notes'),
storedBy: safeGet(report, 'data.closed.doc.contact.name'),
storedDate: report.reported_date,
}],
};
};
fn((state) => {
const { report } = state.data;
state.dhis2Mapping = {
orgUnit: "DiszpKrYNg8",
occurredAt: report.reported_date,
program: "wNRmGnc1t11",
status: "COMPLETED",
...mapReport(report)
};
console.log(state.dhis2Mapping);
return state;
});
Create-Event-Program:
name: Create Event Program
adaptor: '@openfn/[email protected]'
credential: [email protected]
body: |
// Create events
create("events", $.dhis2Mapping);
triggers:
webhook:
type: webhook
enabled: true
edges:
webhook->DHIS2-mapping:
source_trigger: webhook
target_job: DHIS2-mapping
condition_type: always
enabled: true
DHIS2-mapping->Create-Event-Program:
source_job: DHIS2-mapping
target_job: Create-Event-Program
condition_type: on_job_success
enabled: true
Sample-Workflow:
name: Sample Workflow
jobs:
Get-Patients:
name: Get Patients
adaptor: '@openfn/language-cht@latest'
credential: null
body: |
// Check out the Job Writing Guide for help getting started:
// https://docs.openfn.org/documentation/jobs/job-writing-guide
Transform-and-map-patients:
name: Transform and map patients
adaptor: '@openfn/language-common@latest'
credential: null
body: |
// Check out the Job Writing Guide for help getting started:
// https://docs.openfn.org/documentation/jobs/job-writing-guide
Load-to-DHIS2:
name: Load to DHIS2
adaptor: '@openfn/language-dhis2@latest'
credential: [email protected]
body: |
// Check out the Job Writing Guide for help getting started:
// https://docs.openfn.org/documentation/jobs/job-writing-guide
triggers:
cron:
type: cron
cron_expression: '0 0 * * *'
enabled: true
edges:
cron->Get-Patients:
source_trigger: cron
target_job: Get-Patients
condition_type: always
enabled: true
Get-Patients->Transform-and-map-patients:
source_job: Get-Patients
target_job: Transform-and-map-patients
condition_type: on_job_success
enabled: true
Transform-and-map-patients->Load-to-DHIS2:
source_job: Transform-and-map-patients
target_job: Load-to-DHIS2
condition_type: on_job_success
enabled: true