generated from OpenFn/project
-
Notifications
You must be signed in to change notification settings - Fork 2
/
wf1-1-getTEIs.js
48 lines (41 loc) · 1.23 KB
/
wf1-1-getTEIs.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
fn(state => {
const manualCursor = '2023-06-20T17:00:00.00';
const cursor =
state.lastRunDateTime != null && state.lastRunDateTime != ''
? state.lastRunDateTime
: manualCursor;
console.log('Date cursor to filter TEI extract ::', cursor);
return { ...state, cursor };
});
// Get trackedEntityInstances that are "active" in the target program
get(
'trackedEntityInstances',
{
ou: 'l22DQq4iV3G',
program: 'uGHvY5HFoLG',
programStatus: 'ACTIVE',
},
{},
state => {
const trackedEntityInstances = state.data.trackedEntityInstances.filter(
tei => tei.created > state.cursor
);
const offset = 2; // GMT+2 (Geneva time)
const currentDateTime = new Date();
currentDateTime.setHours(currentDateTime.getHours() + offset);
const lastRunDateTime = currentDateTime.toISOString().replace('Z', '');
console.log('# of TEIs extracted ::', trackedEntityInstances.length);
console.log(
'trackedEntityInstance IDs ::',
trackedEntityInstances.map(tei => tei.trackedEntityInstance)
);
console.log('Next sync start date:', lastRunDateTime);
return {
...state,
data: {},
references: [],
trackedEntityInstances,
lastRunDateTime,
};
}
);