-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreset_field_script.js
62 lines (49 loc) · 1.8 KB
/
reset_field_script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const tables = [
'Items for review / reviewed',
'Reviews / Fact-checks',
'Appearances',
'Reviewers',
'Authors',
'Outlets',
'Social Media influent.',
'Editors'
]
const fieldToReset = 'Synced time input'
const resetValue = null
const resetSyncStatus = async (tables, fieldToReset, resetValue) => {
let tablesToReset = tables.map(table => base.getTable(table))
let records = {}
await Promise.all(tablesToReset.map(async (table) => {
let query = await table.selectRecordsAsync()
records[table.name] = []
query.records.forEach((record, index) => {
if (record.getCellValue(fieldToReset) !== null) {
let updated = {id: record.id, fields: {[fieldToReset]: resetValue}}
records[table.name].push(updated)
}
})
}))
await Promise.all(tablesToReset.map(async (table) => {
let nb_rows_updated = 0
console.log('Number of rows to update for table : ', table.name, ' - ', records[table.name].length)
for (let i=0;i<records[table.name].length;i+=30) {
await base.getTable(table.name).updateRecordsAsync(records[table.name].slice(i, i+30))
nb_rows_updated = nb_rows_updated + records[table.name].slice(i, i+30).length
}
console.log('Number of rows updated for table : ', table.name, ' - ', nb_rows_updated)
}))
return records
}
let choices = tables.concat(['All', 'Cancel'])
let tableChoice = input.buttonsAsync(
'Which table do you want to reset?',
choices
)
const res = await tableChoice
if (res == 'All') {
console.log(await resetSyncStatus(tables, fieldToReset, resetValue))
} else if (res == 'Cancel') {
console.log('Action cancelled')
} else {
console.log(await resetSyncStatus([res], fieldToReset, resetValue))
}