-
Notifications
You must be signed in to change notification settings - Fork 1
/
getForms.js
146 lines (137 loc) · 4.95 KB
/
getForms.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
143
144
145
146
alterState(state => {
// Month prior ====================================================
const date = new Date();
const month = date.getMonth();
let month_prior_start = new Date(date.getFullYear(), month - 1, 1);
month_prior_start = month_prior_start.toISOString().split('T')[0];
let month_prior_end = new Date(date.getFullYear(), month, 0);
month_prior_end = month_prior_end.toISOString().split('T')[0];
// ================================================================
// Manual cursors =================================================
const received_on_start = '2021-01-01'; // start date
const received_on_end = '2021-01-31'; // start date
// ================================================================
const cursorStart = month_prior_start || received_on_start; // Remove 'month_prior_start ||' to use manualCursor
// const cursorStart = received_on_start;
const cursorEnd = month_prior_end || received_on_end; // Remove 'month_prior_end ||' to use manualCursor
// const cursorEnd = received_on_end;
return {
...state,
cursorStart,
cursorEnd,
};
});
alterState(state => {
return get(
'https://www.commcarehq.org/a/miraclefeet/api/v0.5/form/',
{
query: {
limit: 1000, // To update for fetching a specific number of forms
offset:
state.meta && state.meta.next
? state.meta.limit + state.meta.offset
: 0,
xmlns:
'http://openrosa.org/formdesigner/34532e2a938003aae150198e2613ed2b54c410fb', // Register New Patient
received_on_start: state.cursorStart,
received_on_end: state.cursorEnd,
},
},
state => {
const { meta, objects } = state.data;
const { openfnInboxUrl } = state.configuration;
state.meta = meta;
console.log('Metadata in CommCare response for Register New Patient:');
console.log(meta);
const body = objects.filter(form => {
const { received_on, server_modified_on } = form;
const receivedOnDate = new Date(received_on);
const serverModifiedOnDate = new Date(server_modified_on);
receivedOnDate.setHours(
receivedOnDate.getHours(),
receivedOnDate.getMinutes(),
0,
0
);
serverModifiedOnDate.setHours(
serverModifiedOnDate.getHours(),
serverModifiedOnDate.getMinutes(),
0,
0
);
return (
receivedOnDate.toISOString() !== serverModifiedOnDate.toISOString()
);
});
console.log(body.length, 'cleaned forms fetched...');
console.log(`Posting to OpenFn Inbox...${JSON.stringify(body, null, 2)}`);
return each(
body,
alterState(state => {
return post(
`${openfnInboxUrl}`,
{ body: state => state.data, authentication: {username: state.configuration.openfnUsername, password: state.configuration.openfnPassword} },
state => ({ ...state, data: {}, references: [] })
)(state);
})
)(state);
}
)(state);
});
alterState(state => {
return get(
'https://www.commcarehq.org/a/miraclefeet/api/v0.5/form/',
{
query: {
limit: 1000, // To update for fetching a specific number of forms
offset:
state.meta && state.meta.next
? state.meta.limit + state.meta.offset
: 0,
xmlns:
'http://openrosa.org/formdesigner/8FAE7619-BCEA-4C28-B2F4-6989BA4AF3CF', // Log Visit Details
received_on_start: state.cursorStart,
received_on_end: state.cursorEnd,
},
},
state => {
const { meta, objects } = state.data;
const { openfnInboxUrl } = state.configuration;
state.meta = meta;
console.log('Metadata in CommCare response for Log Visit Details:');
console.log(meta);
const body = objects.filter(form => {
const { received_on, server_modified_on } = form;
const receivedOnDate = new Date(received_on);
const serverModifiedOnDate = new Date(server_modified_on);
receivedOnDate.setHours(
receivedOnDate.getHours(),
receivedOnDate.getMinutes(),
0,
0
);
serverModifiedOnDate.setHours(
serverModifiedOnDate.getHours(),
serverModifiedOnDate.getMinutes(),
0,
0
);
return (
receivedOnDate.toISOString() !== serverModifiedOnDate.toISOString()
);
});
console.log(body.length, 'cleaned forms fetched...');
console.log(`Posting to OpenFn Inbox...${JSON.stringify(body, null, 2)}`);
return each(
body,
alterState(state => {
return post(
`${openfnInboxUrl}`,
{ body: state => state.data, authentication: {username: state.configuration.openfnUsername, password: state.configuration.openfnPassword} },
state => ({ ...state, data: {}, references: [] })
)(state);
})
)(state);
}
)(state);
});