-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfull_synchronisation.js
188 lines (174 loc) · 6.31 KB
/
full_synchronisation.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Get the query backend
let backend_query = await base.getTable("Variables").selectRecordsAsync({
fields: ['Name', 'Value'],
})
const SF_API_URL = backend_query.records.filter(record => record.name === 'FEEDBACK_API_URL')[0].getCellValue('Value')
// Tables
const tables = [
'Items for review / reviewed',
'Reviews / Fact-checks',
'Appearances',
'Reviewers',
'Authors',
'Outlets',
'Social Media influent.',
'Editors'
]
function return_record_data(table, record) {
if (table == 'Items for review / reviewed') {
return {
"Claim checked (or Headline if no main claim)": record.getCellValue('Claim checked (or Headline if no main claim)'),
"airtableId": record.id,
"lastModified": record.getCellValue('Last modified time')
}
}
else if (table == 'Reviews / Fact-checks') {
return {
"Review editor(s)": record.getCellValue('Review editor(s)').map(editor => editor.id),
"Items reviewed": record.getCellValue('Items reviewed').map(claim => claim.id),
"Review url": record.getCellValue('Review url'),
"Date of publication": record.getCellValue('Date of publication'),
"Post type": record.getCellValue('Post type').name,
"Review headline": record.getCellValue('Review headline'),
"airtableId": record.id,
"lastModified": record.getCellValue('Last modified time')
}
}
else if (table == 'Appearances') {
if (record.getCellValue('Item reviewed') == null) {
console.log('No Item reviews for appearance with id : ', record.id)
return null
}
return {
"airtableId": record.id,
"url": record.getCellValue('url'),
"Item reviewed": record.getCellValue('Item reviewed').map(claim => claim.id),
"Authors": record.getCellValue('Authors')?.map(author => author.id),
"Outlet": record.getCellValue('Outlet')?.map(outlet => outlet.id),
"Verified by": record.getCellValue('Verified by')?.map(testifier => testifier.id),
"lastModified": record.getCellValue('Last modified time')
}
}
else if (table == 'Reviewers') {
return {
"First name": record.getCellValue('First name'),
"Last name": record.getCellValue('Last name'),
"Email": record.getCellValue('Email'),
"airtableId": record.id,
"index": record.getCellValue('index'),
"lastModified": record.getCellValue('Last modified time')
}
}
else if (table == 'Authors') {
return {
"Name": record.getCellValue('Name'),
"airtableId": record.id,
"index": record.getCellValue('index'),
"lastModified": record.getCellValue('Last modified time')
}
}
else if (table == 'Outlets') {
return {
"Name": record.getCellValue('Name'),
"airtableId": record.id,
"lastModified": record.getCellValue('Last modified time')
}
}
else if (table == 'Social Media influent.') {
return {
"url": record.getCellValue('url'),
"Name": record.getCellValue('Name'),
"airtableId": record.id,
"lastModified": record.getCellValue('Last modified time')
}
}
else if (table == 'Editors') {
return {
"Name": record.getCellValue('Name'),
"airtableId": record.id,
"index": record.getCellValue('index'),
"lastModified": record.getCellValue('Last modified time')
}
}
}
function return_table_url(table) {
if (table == 'Items for review / reviewed') {
return `${SF_API_URL}/webhooks/claim`
}
else if (table == 'Reviews / Fact-checks') {
return `${SF_API_URL}/webhooks/verdict`
}
else if (table == 'Appearances') {
return `${SF_API_URL}/webhooks/link`
}
else if (table == 'Reviewers') {
return `${SF_API_URL}/webhooks/reviewer`
}
else if (table == 'Authors') {
return `${SF_API_URL}/webhooks/author`
}
else if (table == 'Outlets') {
return `${SF_API_URL}/webhooks/outlet`
}
else if (table == 'Social Media influent.') {
return `${SF_API_URL}/webhooks/social`
}
else if (table == 'Editors') {
return `${SF_API_URL}/webhooks/editor`
}
}
const syncTable = async (table) => {
let chosen_table = base.getTable(table)
let query = await chosen_table.selectRecordsAsync()
let notSyncQuery = query.records.filter(record => record.getCellValueAsString('Sync status') != "🟢 Synced")
console.log("Number of rows to synchronize : ", notSyncQuery.length)
let nb_rows_to_sync = 0
let nb_rows_synced = 0
let nb_rows_error = 0
let nb_query_to_pass = 0
console.log('Synchronised rows for : ', table)
for (let i=0;i<notSyncQuery.length;i+=1) {
let record = notSyncQuery[i]
nb_rows_to_sync = nb_rows_to_sync + 1
let data = return_record_data(table, record)
if (data == null) {
nb_query_to_pass -= 1
continue
}
let url = return_table_url(table)
let response = await fetch(url, {
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json',
},
})
const current_time = new Date().toISOString()
if (response.status === 200 || response.status === 201) {
await chosen_table.updateRecordAsync(record, {
'Synced time input': current_time
})
nb_rows_synced = nb_rows_synced + 1
} else {
await chosen_table.updateRecordAsync(record, {
'Synced time input': 'Error'
})
nb_rows_error = nb_rows_error + 1
}
}
console.log('Number of rows synchronized : ', nb_rows_synced)
console.log('Number of errors : ', nb_rows_error)
console.log('Number of rows to pass : ', nb_query_to_pass)
return query
}
let choices = tables.concat(['Cancel'])
let tableChoice = input.buttonsAsync(
'Which table do you want to sync?',
choices
)
const res = await tableChoice
if (res == 'Cancel') {
console.log('Action cancelled')
} else {
console.log(await syncTable(res))
}