/
2a-getGoDataCases.js
63 lines (58 loc) · 2.08 KB
/
2a-getGoDataCases.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
//Job to fetch Cases to sync to other system
// Fetch cases from Go.Data matching a specific outbreak id
listCases('3b5554d7-2c19-41d0-b9af-475ad25a382b', {}, state => {
function yesterdayDate() {
const date = new Date();
date.setDate(date.getDate() - 1);
date.setHours(0, 0, 0, 0);
return date.toISOString();
}
// set to null if we want to use manualCursor.
// set to yesterDayDate() to use the date of yesterday.
const yesterday = null;
const manualCursor = '2020-07-24T00:00:00.000Z';
const cases = state.data
.filter(report => {
return report.dateOfReporting === (yesterday || manualCursor);
})
.map(report => {
return {
name: `${report.firstName}, ${report.lastName || ''}`,
status: report.classification,
externalId: report.id,
caseId: report.visualId,
age: report.age ? report.age.years : report['age:years'],
phone:
report.addresses && report.addresses[0]
? report.addresses[0].phoneNumber
: report['addresses:phoneNumber'],
country:
report.addresses && report.addresses[0]
? report.addresses[0].country
: report['addresses:country'],
location:
report.addresses && report.addresses[0]
? report.addresses[0].locationId
: report['addresses:locationId'],
};
});
const HMISCases = state.data.filter(report => {
return report.dateOfReporting === (yesterday || manualCursor);
});
console.log('Cases received...');
console.log(cases);
return { ...state, cases, HMISCases };
});
// Bulk post the Go.Data API response to the OpenFn Inbox, where it can be mapped & loaded into the HMIS via another job
alterState(state => {
const { openfnInboxUrl } = state.configuration;
const data = state.cases;
console.log(`Sending to OpenFn Inbox in bulk...`);
return axios({
method: 'POST',
url: 'https://www.openfn.org/inbox/8775c5e5-72c2-4524-b2ba-e422510ba115', //Replace with your unique OpenFn project inbox URL
data,
}).then(response => {
return state;
});
});