|
/* |
|
|
|
Name: Clearing all tables from content and reinserting all the entry_ids from affinity listid |
|
Trigger: At Scheduled time trigger once a day |
|
When creating this automation, create input vars on the left side: |
|
listid = YOURLISTID from affinity |
|
apikey = 'APIKEYASKFROMAMAZINGDEV' |
|
|
|
*/ |
|
var inputs = input.config(); |
|
|
|
var listid = inputs.listid; |
|
var apikey = inputs.apikey; |
|
|
|
//Delete EVERYTHING!!! |
|
|
|
const Tables = function(){ |
|
// ** DEFINE ALL OF YOUR TABLE NAMES HERE ** |
|
// ** WITH A COMMA AFTER ALL BUT THE LAST ** |
|
this.names = [ |
|
"Main", |
|
"People", |
|
"Organizations" |
|
]; |
|
|
|
this.numberOfTablesDefined = this.names.length; |
|
}; |
|
|
|
// **************************************************************** |
|
// ******* NOTHING BELOW HERE SHOULD NEED TO BE CHANGED ******* |
|
// **************************************************************** |
|
|
|
let tables = new Tables(); |
|
|
|
const tablesToClear = [] |
|
let totalRecordsToDelete = 0; |
|
let tablesQueried = 0; |
|
|
|
// Get records from each table and add to array of tables to clear |
|
while (tablesQueried < tables.numberOfTablesDefined) { |
|
let tableName = tables.names[tablesQueried] |
|
let tableToAdd = base.getTable(tableName); |
|
let queryToAdd = await tableToAdd.selectRecordsAsync(); |
|
let recordsToAdd = queryToAdd.records; |
|
|
|
if (recordsToAdd.length > 0) { |
|
totalRecordsToDelete += recordsToAdd.length; |
|
const table = {table: tableToAdd, records: recordsToAdd}; |
|
tablesToClear.push(table); |
|
} |
|
|
|
tablesQueried++ |
|
} |
|
|
|
let totalRecordsDeleted = 0; |
|
|
|
for (let table of tablesToClear) { |
|
let recordsToDelete = table.records.map(record => record.id); |
|
let recordsDeleted = await batchAnd('Delete', table.table, recordsToDelete); |
|
|
|
if (recordsDeleted !== null) { |
|
totalRecordsDeleted += recordsDeleted; |
|
} |
|
}; |
|
|
|
//Get Data from affinity |
|
|
|
let response = await fetch('https://api.affinity.co/lists/'+listid+'/list-entries', { |
|
method: 'GET', |
|
headers: { |
|
'Content-Type': 'application/json', |
|
"Authorization": "Basic "+apikey |
|
}, |
|
}); |
|
var entries = await response.json(); |
|
console.log(entries); |
|
//Lets create only the entry_id in main table |
|
const mainTable = base.getTable('Main'); |
|
|
|
for (let i = 0; i < entries.length; i++) { |
|
const entry = entries[i]; |
|
var newentry = await mainTable.createRecordAsync({"entity_id":entry.entity.id}); |
|
} |
|
|
|
|
|
// *************************************************** |
|
// ****************** FUNCTIONS ****************** |
|
// *************************************************** |
|
async function batchAnd(action, table, records) { |
|
let recordsActedOn = records.length; |
|
|
|
switch (action) { |
|
case 'Update': |
|
while (records.length > 0) { |
|
await table.updateRecordsAsync(records.slice(0, 50)); |
|
records = records.slice(50); |
|
}; |
|
break; |
|
|
|
case 'Create': |
|
while (records.length > 0) { |
|
await table.createRecordsAsync(records.slice(0, 50)); |
|
records = records.slice(50); |
|
}; |
|
break; |
|
|
|
case 'Delete': |
|
while (records.length > 0) { |
|
await table.deleteRecordsAsync(records.slice(0, 50)); |
|
records = records.slice(50); |
|
} |
|
break; |
|
|
|
default: |
|
recordsActedOn = null; |
|
} |
|
return recordsActedOn; |
|
} |