Skip to content

Commit

Permalink
Merge pull request #217 from OpenFn/214-deployed-to-archived
Browse files Browse the repository at this point in the history
Update sheet `status` and `date_modified` column
  • Loading branch information
aleksa-krolls authored Jun 11, 2024
2 parents 6c6b2b3 + f05e06c commit d46b83e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 48 deletions.
7 changes: 6 additions & 1 deletion form-sharing/1-getNewKoboForms.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ getForms({}, state => {
return keywords.some(keyword => name.toLowerCase().includes(keyword));
};

state.koboForms = state.data.results
state.activeForms = state.data.results
.filter(form => checkForKeyWords(form.name))
.filter(form => form.deployment__active);

state.archivedForms = state.data.results
.filter(form => checkForKeyWords(form.name))
.filter(form => !form.deployment__active);

state.data = {};
state.references = [];
return state;
Expand Down
37 changes: 27 additions & 10 deletions form-sharing/2-getSheetsList.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
getValues(
"1s7K3kxzm5AlpwiALattyc7D9_aIyqWmo2ubcQIUlqlY", //googlesheet id
"wcs-bns-test!A:L" //range of columns in sheet
'1s7K3kxzm5AlpwiALattyc7D9_aIyqWmo2ubcQIUlqlY', //googlesheet id
'wcs-bns-test!A:N' //range of columns in sheet
);

fn((state) => {
const { koboForms, data } = state;
fn(state => {
const { activeForms, archivedForms, data } = state;
const [headers, ...sheetsData] = data.values;
const sheetsUids = sheetsData.map((row) => row[0]);
console.log("Ignoring headers", headers);
const sheetsUids = sheetsData.map(row => row[0]);
console.log('Ignoring headers', headers);

state.filteredKoboFormsData = koboForms.filter(
(form) => !sheetsUids.includes(form.uid)
state.formsToCreate = activeForms.filter(
form => !sheetsUids.includes(form.uid)
);
state.data = {};
state.references = [];

state.formsToUpdate = archivedForms
.filter(form => sheetsUids.includes(form.uid))
.map(form => {
const rowIndex = sheetsData.findIndex(row => {
return row[0] === form.uid;
});
if (rowIndex !== -1) {
return { ...form, rowIndex };
}
console.log(form.uid, 'Could not be found in google sheet');
});

return state;
});

fn(state => {
const { data, references, response, ...remainingState } = state;

return remainingState;
});
70 changes: 38 additions & 32 deletions form-sharing/3-updateSheetsList.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//Compare new forms in Kobo with GoogleSheet list to see if new forms were shared in Kobo
fn(state => {
const { filteredKoboFormsData } = state;
const { formsToCreate, formsToUpdate } = state;
const keywords = ['price', 'prix', 'bns', 'nrgt', 'grm', 'feedback'];

const tagMapping = {
Expand All @@ -25,10 +25,10 @@ fn(state => {
};

const containsGRMFeedback = name =>
name.toLowerCase().includes('grm', 'feedback');
!name.toLowerCase().includes('grm', 'feedback');

const instance = name =>
containsGRMFeedback(name) ? '' : 'ADD MANUALLY @Admin!';
containsGRMFeedback(name) ? 'ADD MANUALLY @Admin!' : '';

const projectId = name =>
containsGRMFeedback(name) ? 'ADD MANUALLY @Admin!' : '';
Expand All @@ -38,40 +38,46 @@ fn(state => {
const workspaceName = name =>
containsGRMFeedback(name) ? 'Grievances' : 'ConSoSci';

const status = 'deployed'; //if we assume only deployed forms will be fetched

const currentDateTime = new Date().toISOString();
const sheetRowMap = form => [
form.uid,
form.name,
createTagName(form.name),
form.owner__username,
instance(form.name),
//projectId(form.name), //for GRM only
//grmID(form.name), //for GRM only
form.deployment__active ? 'deployed' : 'archived', //deployment status //if we assume only deployed forms will be fetched
workspaceName(form.name), //openfn project space
form.url,
form.date_modified, //kobo_form_date_modified
form.date_created, //kobo_form_date_created
new Date().toISOString(), //row_date_modified
false, //auto_sync checkbox
];

const autoSync = false;
state.rowValuesToCreate = formsToCreate.map(form => sheetRowMap(form));
state.rowValuesToUpdate = formsToUpdate.map(form => ({
range: `wcs-bns-test!A${form.rowIndex + 2}:N${form.rowIndex + 2}`,
values: [sheetRowMap(form)],
}));

state.sheetsData = filteredKoboFormsData.map(form => {
const formName = form.name;
return [
form.uid,
form.name,
createTagName(formName),
form.owner__username,
instance(formName),
//projectId(formName), //for GRM only
//grmID(formName), //for GRM only
status, //deployment status
workspaceName(formName), //openfn project space
form.url,
form.date_modified, //kobo_form_date_modified
form.date_created, //kobo_form_date_created
currentDateTime, //row_date_modified
autoSync, //auto_sync checkbox
];
});

console.log('# of new forms detected:: ', state.sheetsData.length);
console.log('Forms to add to the master sheet:: ', state.sheetsData);
console.log('# of new forms detected:: ', state.rowValuesToCreate.length);
console.log('Forms to add to the master sheet:: ', state.rowValuesToCreate);
return state;
});

//if new Kobo form shared, adding to the Google Sheet...
//if new Kobo form shared, adding to the Google Sheet...
appendValues({
spreadsheetId: '1s7K3kxzm5AlpwiALattyc7D9_aIyqWmo2ubcQIUlqlY', //sheet id
range: 'wcs-bns-test!A:L', //range of columns in sheet
values: state => state.sheetsData,
range: 'wcs-bns-test!A:N', //range of columns in sheet
values: state => state.rowValuesToCreate,
});

each(
'$.rowValuesToUpdate[*]',
batchUpdateValues({
spreadsheetId: '1s7K3kxzm5AlpwiALattyc7D9_aIyqWmo2ubcQIUlqlY', //sheet id
range: state => state.data.range, //range of columns in sheet
values: state => state.data.values,
})
);
11 changes: 6 additions & 5 deletions form-sharing/4-upsertAsanaTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fn(state => {
.toISOString()
.split('T')[0];

state.asanaTasks = state.filteredKoboFormsData.map(form => {
state.asanaTasks = state.activeForms.map(form => {
return {
name: `New form added to OpenFn: ${form.name}`,
approval_status: 'pending',
Expand All @@ -15,16 +15,17 @@ fn(state => {
notes: `New form added to OpenFn: ${form.name}. Please review the Google Sheet and look for cells where it says "ADD MANUALLY" to add any values missing (e.g., "Instance" the form belongs to): https://docs.google.com/spreadsheets/d/1s7K3kxzm5AlpwiALattyc7D9_aIyqWmo2ubcQIUlqlY/edit#gid=1559623602`,
};
});
console.log('# of Asana Tasks to add:: ', state.asanaTasks.length);
console.log('Tasks to upsert:: ', state.asanaTasks);

console.log('# of Asana Tasks to add:: ', state.asanaTasks.length);
console.log('Tasks to upsert:: ', state.asanaTasks);
return state;
});

//upsert Asana task if new form shared notification needed
each(
'$.asanaTasks[*]',
upsertTask('1198901998266253', { //project_id
upsertTask('1198901998266253', {
//project_id
externalId: 'name',
data: state => state.data,
})
Expand Down

0 comments on commit d46b83e

Please sign in to comment.