Last active
August 16, 2024 06:09
-
-
Save samsoft00/614a84e49b1f341ebc58120c3d4a52ad to your computer and use it in GitHub Desktop.
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
/** | |
* Google App Script to Sync and Update Task on Motion on Monday.com | |
*/ | |
function getEnv(key) { | |
return PropertiesService.getScriptProperties().getProperty(key); | |
} | |
function doGet(e){ | |
var BOARD_ID = e.parameter.q; | |
const ZERO = 0; | |
const LIMIT = 10; | |
// const BOARD_ID = 4630352851; | |
const MONDAY_URL = `https://api.monday.com/v2`; | |
const ACCESS_TOKEN = getEnv("MONDAY_TOKEN"); | |
let query = `{boards(ids: ${BOARD_ID}) {items_page(limit: ${LIMIT}, query_params: {order_by: [{column_id: "date", direction: desc}], rules: [{column_id: "text", compare_value: [""], operator: is_not_empty}, {column_id: "status2", compare_value: [1], operator: not_any_of}], operator: and}) { cursor items {id column_values {id value}}}}}`; | |
/* | |
if(CURSOR_ID.length > 5) { | |
query = `{boards(ids: ${BOARD_ID}) {items_page(limit: ${LIMIT}, cursor: "${CURSOR_ID}") { cursor items {id column_values {id value}}}}}`; | |
} | |
*/ | |
var options = { | |
'method':'post', | |
'contentType':'application/json', | |
'payload' : JSON.stringify({'query' : query}), | |
'muteHttpExceptions': true, | |
'headers': { | |
'Authorization':`${ACCESS_TOKEN}`, | |
}, | |
} | |
try{ | |
var response = UrlFetchApp.fetch(MONDAY_URL, options); | |
var result = JSON.parse(response.getContentText()); | |
var { items } = result.data.boards[0].items_page; | |
processMotionItems(items, BOARD_ID, ZERO); | |
var data = {'processed': true, 'board': BOARD_ID }; | |
return ContentService.createTextOutput(JSON.stringify(data)).setMimeType(ContentService.MimeType.JSON); | |
}catch(err){ | |
Logger.log(err); | |
var data = {'processed': false, 'board': BOARD_ID, message: err.message }; | |
return ContentService.createTextOutput(JSON.stringify(data)).setMimeType(ContentService.MimeType.JSON); | |
} | |
} | |
async function processMotionItems(items, boardId, index = 0) { | |
let processed = 0; | |
if (index >= items.length) { | |
Logger.log(`Total Processed => ${processed}`); | |
return; | |
}; | |
var item = items[index]; | |
var { value:taskId } = item.column_values.filter(({id}) => Object.is(id,"text"))[0]; | |
var MONDAY_ITEM_ID = item.id; | |
const MOTION_KEY = getEnv("MOTION_TOKEN"); | |
var options = { | |
'method':'get', | |
'contentType':'application/json', | |
'headers': { | |
'X-API-Key' : `${MOTION_KEY}` | |
}, | |
} | |
var TASK_ID = taskId.replace(/"/g, ''); | |
var response = UrlFetchApp.fetch('https://api.usemotion.com/v1/tasks/'+TASK_ID, options); | |
var result = JSON.parse(response.getContentText()); | |
// Handle Status Error ==> | |
if(Object.is(result.status.name, "Completed")) { | |
// update monday status column | |
var query = `mutation{change_multiple_column_values(board_id:${boardId},item_id:${MONDAY_ITEM_ID},column_values:"{\\\"status2\\\" : {\\\"index\\\" : \\\"1\\\"}}"){id}}`; | |
var mondayResp = UrlFetchApp.fetch('https://api.monday.com/v2', { | |
'method':'post', | |
'contentType':'application/json', | |
'payload' : JSON.stringify({'query' : query}), | |
'muteHttpExceptions': true, | |
'headers': { | |
'Authorization':`${getEnv("MONDAY_TOKEN")}`, | |
}, | |
}); | |
var mondayRes = JSON.parse(mondayResp.getContentText()); | |
Logger.log({ | |
motion_status: result.status.name, | |
monday_res: mondayRes.data.change_multiple_column_values | |
}); | |
processed = processed + 1; | |
} | |
processMotionItems(items, boardId, index + 1); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment