-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy path30-03-25_00-40-migrate-hero-video.js
More file actions
126 lines (116 loc) · 3.93 KB
/
30-03-25_00-40-migrate-hero-video.js
File metadata and controls
126 lines (116 loc) · 3.93 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
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
import mongoose from "mongoose";
import { nanoid } from "nanoid";
function generateUniqueId() {
return nanoid();
}
mongoose.connect(process.env.DB_CONNECTION_STRING, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const WidgetSchema = new mongoose.Schema({
widgetId: { type: String, required: true, default: generateUniqueId },
name: { type: String, required: true },
deleteable: { type: Boolean, required: true, default: true },
shared: { type: Boolean, required: true, default: false },
settings: mongoose.Schema.Types.Mixed,
});
const MediaSchema = new mongoose.Schema({
mediaId: { type: String, required: true },
originalFileName: { type: String, required: true },
mimeType: { type: String, required: true },
size: { type: Number, required: true },
access: { type: String, required: true, enum: ["public", "private"] },
thumbnail: String,
caption: String,
file: String,
});
const PageSchema = new mongoose.Schema(
{
domain: { type: mongoose.Schema.Types.ObjectId, required: true },
pageId: { type: String, required: true },
type: {
type: String,
required: true,
enum: ["product", "site", "blog", "community"],
default: "product",
},
creatorId: { type: String, required: true },
name: { type: String, required: true },
layout: { type: [WidgetSchema], default: [] },
draftLayout: { type: [WidgetSchema], default: [] },
entityId: { type: String },
deleteable: { type: Boolean, required: true, default: false },
title: { type: String },
description: String,
socialImage: MediaSchema,
robotsAllowed: { type: Boolean, default: true },
draftTitle: String,
draftDescription: String,
draftSocialImage: MediaSchema,
draftRobotsAllowed: Boolean,
deleted: { type: Boolean, default: false },
},
{
timestamps: true,
},
);
PageSchema.index(
{
domain: 1,
pageId: 1,
},
{ unique: true },
);
const Page = mongoose.model("Page", PageSchema);
const updateHeroVideo = async (page) => {
console.log(`Updating homepage for domain: ${page.domain}`);
const heroWidgets = page.layout.filter((widget) => widget.name === "hero");
for (const heroWidget of heroWidgets) {
heroWidget.settings.style = "normal";
heroWidget.settings.mediaRadius = 2;
if (
heroWidget &&
heroWidget.settings.youtubeLink &&
!heroWidget.settings.youtubeLink.startsWith(
"https://www.youtube.com/watch?v=",
)
) {
heroWidget.settings.youtubeLink = `https://www.youtube.com/watch?v=${heroWidget.settings.youtubeLink}`;
}
}
const heroWidgetsDraft = page.draftLayout.filter(
(widget) => widget.name === "hero",
);
for (const heroWidget of heroWidgetsDraft) {
heroWidget.settings.style = "normal";
heroWidget.settings.mediaRadius = 2;
if (
heroWidget &&
heroWidget.settings.youtubeLink &&
!heroWidget.settings.youtubeLink.startsWith(
"https://www.youtube.com/watch?v=",
)
) {
heroWidget.settings.youtubeLink = `https://www.youtube.com/watch?v=${heroWidget.settings.youtubeLink}`;
}
}
page.markModified("layout");
page.markModified("draftLayout");
await page.save();
console.log(`Updated homepage for domain: ${page.domain}\n`);
};
const migrateHeroVideo = async () => {
const pages = await Page.find({ pageId: "homepage" });
for (const page of pages) {
try {
await updateHeroVideo(page);
} catch (error) {
console.error(`Error updating homepage for domain: ${page.domain}`);
console.error(error);
}
}
};
(async () => {
await migrateHeroVideo();
mongoose.connection.close();
})();