-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy path20-03-25_00-00-migrate-courses-link-to-products-link.js
More file actions
80 lines (70 loc) · 2.39 KB
/
20-03-25_00-00-migrate-courses-link-to-products-link.js
File metadata and controls
80 lines (70 loc) · 2.39 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
73
74
75
76
77
78
79
80
import mongoose from "mongoose";
mongoose.connect(process.env.DB_CONNECTION_STRING, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const DomainSchema = new mongoose.Schema({
name: { type: String, required: true, unique: true },
sharedWidgets: {
type: mongoose.Schema.Types.Mixed,
default: {},
},
});
const Domain = mongoose.model("Domain", DomainSchema);
const updateLinks = async () => {
const domains = await Domain.find({});
let updatedCount = 0;
for (const domain of domains) {
let needsUpdate = false;
const sharedWidgets = domain.sharedWidgets || {};
// Update header widget links
if (sharedWidgets.header?.settings?.links) {
sharedWidgets.header.settings.links =
sharedWidgets.header.settings.links.map((link) => {
if (link.href === "/courses") {
needsUpdate = true;
return {
...link,
href: "/products",
label: "Products",
};
}
return link;
});
}
// Update any other widgets that might have /courses links
for (const widgetKey in sharedWidgets) {
const widget = sharedWidgets[widgetKey];
if (widget?.settings?.links) {
widget.settings.links = widget.settings.links.map((link) => {
if (link.href === "/courses") {
needsUpdate = true;
return {
...link,
href: "/products",
label: "Products",
};
}
return link;
});
}
}
if (needsUpdate) {
domain.markModified("sharedWidgets");
await domain.save();
updatedCount++;
console.log(`Updated links for domain: ${domain.name}`);
}
}
console.log(`Migration completed. Updated ${updatedCount} domains.`);
};
(async () => {
try {
await updateLinks();
console.log("Migration completed successfully");
} catch (error) {
console.error("Migration failed:", error);
} finally {
mongoose.connection.close();
}
})();