feat(header): add admin UI and APIs for header management

This commit is contained in:
2026-02-04 10:06:22 +07:00
parent 00f93cefab
commit befe6b30aa
12 changed files with 2359 additions and 1622 deletions

73
scripts/migrate-header.js Normal file
View File

@@ -0,0 +1,73 @@
const mongoose = require("mongoose");
const path = require("path");
require("dotenv").config({ path: path.join(__dirname, "../.env") });
const Header = require("../models/header");
const headerData = require("../data/header.json");
const migrateHeader = async () => {
try {
const mongoUri = process.env.MONGODB_URI;
if (!mongoUri) {
throw new Error("MONGODB_URI not found in environment variables");
}
await mongoose.connect(mongoUri);
console.log("Connected to MongoDB");
// Delete existing header
await Header.deleteMany({});
console.log("Cleared existing headers");
// Transform and insert data
const headerDocument = {
top: {
phone: headerData.top.phone,
email: headerData.top.email,
location: headerData.top.location,
socialLinks: headerData.top.socialLinks.map((link, idx) => ({
...link,
order: idx,
})),
languages: headerData.top.languages,
},
offcanvas: headerData.offcanvas,
menu: headerData.menu.map((item, idx) => ({
...item,
order: idx,
children:
item.children?.map((child, childIdx) => ({
...child,
order: childIdx,
children:
child.children?.map((subchild, subIdx) => ({
...subchild,
order: subIdx,
})) || [],
})) || [],
})),
logo: {
light: "/assets/img/logo/white-logo.svg",
dark: "/assets/img/logo/black-logo.svg",
alt: "Hai Learning",
},
ctaButton: {
label: "Get Started",
href: "/contact",
style: "primary",
},
status: "active",
order: 1,
};
const result = await Header.create(headerDocument);
console.log("Header migrated successfully:", result._id);
await mongoose.connection.close();
process.exit(0);
} catch (error) {
console.error("Migration error:", error);
process.exit(1);
}
};
migrateHeader();

View File

@@ -0,0 +1,106 @@
const mongoose = require("mongoose");
const path = require("path");
require("dotenv").config({ path: path.join(__dirname, "../.env") });
const Header = require("../models/header");
async function updateHeaderData() {
try {
// Connect to MongoDB
await mongoose.connect(process.env.MONGODB_URI || "mongodb://localhost:27017/hailearning");
console.log("Connected to MongoDB");
// Find the first header
let header = await Header.findOne().sort({ order: 1 });
if (!header) {
console.log("No header found, creating new one...");
header = new Header({
top: {
phone: "+09 378 357 5222",
email: "info@hailearning.edu.vn",
location: "69 Street, 5th Avenue LA, United States",
socialLinks: [
{
platform: "linkedin",
url: "https://linkedin.com",
icon: "fa-brands fa-linkedin",
},
{
platform: "twitter",
url: "https://twitter.com",
icon: "fa-brands fa-twitter",
},
{
platform: "instagram",
url: "https://instagram.com",
icon: "fa-brands fa-instagram",
},
{
platform: "youtube",
url: "https://youtube.com",
icon: "fa-brands fa-youtube",
},
],
languages: [
{ name: "English", value: "1" },
{ name: "Bangla", value: "2" },
{ name: "Hindi", value: "3" },
],
},
status: "active",
order: 1,
});
} else {
console.log("Header found, updating...");
// Update existing header
header.top = {
phone: header.top?.phone || "+09 378 357 5222",
email: header.top?.email || "info@hailearning.edu.vn",
location: header.top?.location || "69 Street, 5th Avenue LA, United States",
socialLinks:
header.top?.socialLinks?.length > 0
? header.top.socialLinks
: [
{
platform: "linkedin",
url: "https://linkedin.com",
icon: "fa-brands fa-linkedin",
},
{
platform: "twitter",
url: "https://twitter.com",
icon: "fa-brands fa-twitter",
},
{
platform: "instagram",
url: "https://instagram.com",
icon: "fa-brands fa-instagram",
},
{
platform: "youtube",
url: "https://youtube.com",
icon: "fa-brands fa-youtube",
},
],
languages: header.top?.languages || [
{ name: "English", value: "1" },
{ name: "Bangla", value: "2" },
{ name: "Hindi", value: "3" },
],
};
}
await header.save();
console.log("Header updated successfully!");
console.log("Header data:", JSON.stringify(header, null, 2));
await mongoose.connection.close();
console.log("Database connection closed");
} catch (error) {
console.error("Error updating header:", error);
process.exit(1);
}
}
updateHeaderData();