-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #226 from OpenFn/form-sharing-updates
Form sharing updates
- Loading branch information
Showing
4 changed files
with
119 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
//== Job to be used for getting a list of "archived" Kobo forms from sheets to auto-sync ==// | ||
// This can be run on-demand at any time by clicking "run" // | ||
getValues( | ||
'1s7K3kxzm5AlpwiALattyc7D9_aIyqWmo2ubcQIUlqlY', | ||
'wcs-bns-ARCHIVED!A:O', //get Deployed forms list from Sheet | ||
state => { | ||
const [headers, ...values] = state.data.values; | ||
|
||
const mapHeaderToValue = value => { | ||
return headers.reduce((obj, header) => { | ||
obj[header] = value[headers.indexOf(header)]; | ||
return obj; | ||
}, {}); | ||
}; | ||
|
||
state.sheetsData = values | ||
.filter( | ||
item => item.includes('TRUE') //return forms where auto-sync = TRUE | ||
//&& item.includes('bns_survey', 'nrgt_current') | ||
) | ||
.map(item => mapHeaderToValue(item)); | ||
|
||
return state; | ||
} | ||
); | ||
|
||
fn(state => { | ||
const { sheetsData } = state; | ||
|
||
// Set a manual cursor if you'd like to only fetch data after this date... | ||
//e.g., '2023-01-01T23:51:45.491+01:00' | ||
// const manualCursor = ''; //lastUsed: 2024-04-01T00:00:00.000Z | ||
// console.log('manualCursor defined?', manualCursor); | ||
//...otherwise the job will use this dynamicCursor | ||
// const dynamicCursor = getTodayISODate(); | ||
|
||
// function getTodayISODate() { | ||
// const today = new Date(); | ||
// today.setUTCHours(0, 0, 0, 0); // Set hours, minutes, seconds, and milliseconds to 0 | ||
// return today.toISOString(); // Convert to ISO string | ||
// } | ||
|
||
// const cursorValue = manualCursor || dynamicCursor; | ||
// console.log('Cursor value to use in query:', cursorValue); | ||
|
||
const formsList = sheetsData.map(survey => ({ | ||
formId: survey.uid, | ||
tag: survey.tag, | ||
name: survey.name, | ||
})); | ||
|
||
console.log('# of archived forms detected in Sheet:: ', formsList.length); | ||
console.log( | ||
'List of forms to re-sync:: ', | ||
JSON.stringify(formsList, null, 2) | ||
); | ||
|
||
state.data = { | ||
surveys: sheetsData.map(survey => ({ | ||
formId: survey.uid, | ||
tag: survey.tag, | ||
name: survey.name, | ||
owner: survey.owner, | ||
url: `https://kf.kobotoolbox.org/api/v2/assets/${survey.uid}/data/?format=json`, | ||
//query: `&query={"end":{"$gte":"${cursorValue}"}}`, //get ALL forms for historical job | ||
})), | ||
}; | ||
return state; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Here we fetch submissions for all "Archived" forms in GoogleSheet | ||
// NOTE: See linked job "[BNS-1B] 1.Get FormsList (Historical)" for GoogleSheet query logic | ||
//**********************************************************// | ||
each(dataPath('surveys[*]'), state => { | ||
const { url, query, tag, formId, name, owner } = state.data; | ||
return get(`${url}${query}`, {}, state => { | ||
state.data.submissions = state.data.results.map((submission, i) => { | ||
return { | ||
i, | ||
// Here we append the tags defined above to the Kobo form submission data | ||
form: tag, | ||
formName: name, | ||
formOwner: owner, | ||
body: submission, | ||
}; | ||
}); | ||
const count = state.data.submissions.length; | ||
console.log('Finding historical forms to resync...'); | ||
console.log(`Fetched ${count} submissions from ${formId} (${tag}).`); | ||
//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 ${state.data.i + 1} of ${count}...`); | ||
return post(state.configuration.openfnInboxUrl, { | ||
body: state => state.data, | ||
})(state); | ||
})(state); | ||
})(state); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters