-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPullKoboGrievanceData.js
64 lines (59 loc) · 2.2 KB
/
PullKoboGrievanceData.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
fn(state => {
console.log('Current cursor value:', state.lastEnd);
// IF YOU CLEAR STATE...
// Set this manual cursor to the earliest submission date you want fetch.
const manualCursor = '2023-04-11T11:01:18.729Z';
state.data = {
surveys: [
//==== KOBO FORMS ===============//
{
uid: 'kobo-form-id', // E.g., 'aYnnCn9Pi4m8M7Fakihs381' from Kobo form id
formName: 'Kobo-form-Name', // E.g., 'Mozambique Grievances
projectid: 'Asana-Proj-ID' // E.g., '1344128806220991' from Asana Project URL
},
].map(survey => ({
...survey,
formId: survey.uid,
url: `https://kf.kobotoolbox.org/api/v2/assets/${survey.uid}/data/?format=json`,
query: `&query={"_submission_time":{"$gte":"${
state.lastEnd || manualCursor
}"}}`,
})),
};
return state;
});
each(dataPath('surveys[*]'), state => {
const { url, query, tag, formId, formType, formName, owner, projectid } = state.data;
return get(`${url}${query}`, {}, state => {
state.data.submissions = state.data.results.map(submission => {
return {
// Here we append the tags defined above to the Kobo form submission data
projectid,
formName,
formOwner: owner,
formType,
body: submission,
};
});
const count = state.data.submissions.length;
console.log(`Fetched ${count} submissions from ${formName}.`);
//Once we fetch the data, we want to post each individual Kobo survey
//back to the OpenFn inbox to run through the jobs =========================
return each(dataPath('submissions[*]'), state => {
console.log(`Posting 1 of ${count}...`);
return post(state.configuration.openfnInboxUrl, {
body: state => state.data,
})(state);
})(state);
// =========================================================================
})(state);
});
fn(state => {
let lastEnd = state.references
.filter(item => item.body)
.map(s => s.body.end)
.sort((a, b) => new Date(b.date) - new Date(a.date))[0];
lastEnd = new Date(lastEnd) > new Date() ? lastEnd : new Date().toISOString();
console.log('New cursor value:', lastEnd);
return { ...state, data: {}, references: [], lastEnd };
});