|
// Code by Zapier (JavaScript) |
|
const ASANA_API_KEY = inputData.asana_api_key; |
|
const TASK_ID = inputData.task_id; |
|
const TAG_WON_PAID = '1210036399704824'; // trigger won paid |
|
const TAG_WON_UNPAID = '1210038487410190'; // trigger won unpaid |
|
|
|
async function run() { |
|
// 1) Fetch the task’s tags (both GID and name) |
|
const resp = await fetch( |
|
`https://app.asana.com/api/1.0/tasks/${TASK_ID}?opt_fields=tags.gid,tags.name`, |
|
{ |
|
headers: { 'Authorization': `Bearer ${ASANA_API_KEY}` } |
|
} |
|
); |
|
if (!resp.ok) throw new Error(`Asana API error: ${resp.status} ${resp.statusText}`); |
|
const { data } = await resp.json(); |
|
|
|
// 2) Extract GIDs and names |
|
const tags = data.tags || []; |
|
const tagGids = tags.map(t => t.gid); |
|
const tagNames = tags.map(t => t.name); |
|
|
|
// 3) Determine status |
|
const hasPaidTag = tagGids.includes(TAG_WON_PAID); |
|
const hasUnpaidTag = tagGids.includes(TAG_WON_UNPAID); |
|
let status = ''; |
|
if (hasPaidTag && hasUnpaidTag) { |
|
status = 'Both Unpaid and Paid???'; |
|
} else if (hasPaidTag) { |
|
status = 'Won Paid'; |
|
} else if (hasUnpaidTag) { |
|
status = 'Won Unpaid'; |
|
} else { |
|
status = 'Unknown'; |
|
} |
|
|
|
// 4) Build timestamp if “Won Paid” tag is present |
|
let timestamp = ''; |
|
if (hasPaidTag) { |
|
const now = new Date(); |
|
const opts = { |
|
timeZone: 'America/New_York', |
|
year: 'numeric', month: '2-digit', day: '2-digit', |
|
hour: '2-digit', minute: '2-digit', second: '2-digit', |
|
hour12: false |
|
}; |
|
const parts = new Intl.DateTimeFormat('en-US', opts).formatToParts(now); |
|
const p = type => parts.find(x => x.type === type).value; |
|
timestamp = `${p('month')}/${p('day')}/${p('year')} ${p('hour')}:${p('minute')}:${p('second')}`; |
|
} |
|
|
|
// 5) Return for Zap debugging & downstream use |
|
return { |
|
status, |
|
timestamp, |
|
tags_found_gids: tagGids.join(', '), |
|
tags_found_names: tagNames.join(', ') |
|
}; |
|
} |
|
|
|
return run(); |