generated from OpenFn/project
/
3-request-next-batch.js
50 lines (40 loc) · 1.25 KB
/
3-request-next-batch.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
// post to openfnInboxUrl pageNextRoundPayload
fn(state => {
const { metadataForAgeRequest, metadataForTypeofCaseRequest } = state;
// decide if next round is important
const pageNextRoundDecision = metadata => {
const { total, per, page } = metadata;
const pageDataRequested = per * page;
if (total > pageDataRequested) {
metadata['page'] = metadata['page'] + 1;
return metadata;
}
metadata['getcases'] = false;
return metadata;
};
const metaForAgeReq = pageNextRoundDecision(metadataForAgeRequest);
const metaForTypeOfCaseReq = pageNextRoundDecision(
metadataForTypeofCaseRequest
);
const shouldTrigger = () => {
return metaForAgeReq.getcases || metaForTypeOfCaseReq.getcases
? 'Job 0/3 Succeeds'
: null;
};
const pageNextRoundPayload = {
trigger: shouldTrigger(),
metadataForAgeRequest: metaForAgeReq,
metadataForTypeofCaseRequest: metaForTypeOfCaseReq,
};
if (shouldTrigger() !== null) {
console.log(
'Next round of getCases metadata',
JSON.stringify(pageNextRoundPayload, null, 2)
);
return post(`${state.configuration.inboxUrl}`, {
body: pageNextRoundPayload,
})(state);
}
console.log('No more cases to query');
return state;
});