@@ -113,43 +113,48 @@ With this OpenFn job snippet we fetch submission data from a list of surveys,
113
113
indicated by their IDs.
114
114
115
115
``` js
116
+ // set the cursor to use for this run
117
+ cursor ($ .lastEnd || $ .manualCursor || ' 2020-11-20T14:32:43.325+01:00' );
118
+
119
+ // set the cursor for the next run
120
+ cursor (' now' , {
121
+ key: ' lastEnd' ,
122
+ format : c => dateFns (c, ' YYYY-MM-DD:HH:mm:ss' ),
123
+ });
124
+
116
125
fn (state => {
117
- console .log (' Current cursor value:' , state .lastEnd );
118
- // Set a manual cursor if you'd like to only fetch data after this date.
119
- const manualCursor = ' 2020-11-20T14:32:43.325+01:00' ;
120
- state .data = {
121
- surveys: [
122
- // ** Specify new forms to fetch here **//
123
- {
124
- id: ' aVdh90L9979L945lb02' ,
125
- name: ' Initial Data Collection' ,
126
- },
127
- {
128
- id: ' bkgIF96fK7v9n7Hfj2' ,
129
- name: ' Follow-up' ,
130
- },
131
- ].map (survey => ({
132
- formId: survey .id ,
133
- name: survey .name ,
134
- url: ` https://kf.kobotoolbox.org/api/v2/assets/${ survey .id } /data/?format=json` ,
135
- query: ` &query={"end":{"$gte":"${ state .lastEnd || manualCursor} "}}` ,
136
- })),
137
- };
126
+ console .log (' Current cursor value:' , state .cursor );
127
+
128
+ state .surveys = [
129
+ // ** Specify new forms to fetch here **//
130
+ {
131
+ formId: ' aVdh90L9979L945lb02' ,
132
+ name: ' Initial Data Collection' ,
133
+ },
134
+ {
135
+ formId: ' bkgIF96fK7v9n7Hfj2' ,
136
+ name: ' Follow-up' ,
137
+ },
138
+ ];
138
139
return state;
139
140
});
140
141
141
- each (dataPath (' surveys[*]' ), state => {
142
- const { url , query , formId , name } = state .data ;
143
- return get (` ${ url}${ query} ` , {}, state => {
144
- state .data .submissions = state .data .results .map ((submission , i ) => {
145
- return {
146
- i,
147
- // Here we append the names defined above to the Kobo form submission data
148
- formName: name,
149
- };
150
- });
151
- });
152
- });
142
+ each (
143
+ $ .surveys ,
144
+ getSubmissions ($ .data .formId , {
145
+ query: { end: { $gte: ` ${ $ .cursor } ` } },
146
+ }).then (state => {
147
+ // Lookup each form's id from the previous state
148
+ const { name , formId } = state .references .at (- 1 );
149
+
150
+ state .submissions [formId] = {
151
+ name,
152
+ submissions: state .data ,
153
+ };
154
+
155
+ return state;
156
+ })
157
+ );
153
158
```
154
159
155
160
Check out some of our
0 commit comments