-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanup.js
More file actions
72 lines (63 loc) · 1.91 KB
/
cleanup.js
File metadata and controls
72 lines (63 loc) · 1.91 KB
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
const Airtable = require("airtable");
const base = new Airtable({ apiKey: "key0aVfVmWLnBm0dC" }).base(
"appUt5CHVK6Q1kZFR"
);
const brandsTable = base("Brands");
const videosTable = base("Videos");
const getRecords = (table) => {
return new Promise((resolve, reject) => {
const records = [];
table.select().eachPage(
(recs, fetchNextPage) => {
records.push(...recs);
fetchNextPage();
},
(err) => {
if (err) {
reject(err);
}
resolve(records);
}
);
});
};
const run = async () => {
console.log("start");
try {
const brands = await getRecords(brandsTable);
for (const brand of brands) {
const videoIds = brand.get("Videos"); // This will get array of Video IDs linked to the brand
const brandId = brand.get("uniqueId");
console.log(`=== ${brandId} ===`);
if (!videoIds) {
console.log(`No videos for brand ${brand.id}`);
continue;
}
let filteredVideoIds = [...videoIds]; // Create a copy of videoIds to modify
for (const videoId of videoIds) {
const videoRecord = await videosTable.find(videoId); // Fetch Video record using ID
console.log(`Video ID: ${videoRecord.id}`); // Logs the Video ID
const author = videoRecord.get("author");
if (brandId !== author) {
filteredVideoIds = filteredVideoIds.filter((id) => id !== videoId); // Filter out this videoId
console.log("filtered out the video");
}
}
// If any videos were unlinked, update the brand
if (filteredVideoIds.length !== videoIds.length) {
await brandsTable.update([
{
id: brand.id,
fields: {
Videos: filteredVideoIds,
},
},
]);
console.log(`Updated brand ${brand.id} with new video links`);
}
}
} catch (err) {
console.error(err);
}
};
run();