forked from UKSOURCE/cms.hailearning.edu.vn
feat/huy-05022026-cms-add-footer-api-management
This commit is contained in:
68
scripts/2026_02_05_120000_footer.js
Normal file
68
scripts/2026_02_05_120000_footer.js
Normal file
@@ -0,0 +1,68 @@
|
||||
const mongoose = require("mongoose");
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
|
||||
// Import model
|
||||
const Footer = require("../models/footer");
|
||||
|
||||
/**
|
||||
* Migration script để import dữ liệu footer từ JSON
|
||||
*/
|
||||
async function up() {
|
||||
try {
|
||||
console.log("Starting footer migration...");
|
||||
|
||||
// Đọc dữ liệu từ file JSON
|
||||
const jsonPath = path.join(__dirname, "../data/footer.json");
|
||||
|
||||
if (!fs.existsSync(jsonPath)) {
|
||||
throw new Error("Footer JSON file not found");
|
||||
}
|
||||
|
||||
const footerData = JSON.parse(fs.readFileSync(jsonPath, "utf8"));
|
||||
|
||||
// Sử dụng static method từ model để migrate
|
||||
const result = await Footer.migrateFromJson(footerData);
|
||||
|
||||
console.log("Footer migration completed successfully");
|
||||
return result;
|
||||
} catch (error) {
|
||||
console.error("Footer migration failed:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Rollback migration
|
||||
*/
|
||||
async function down() {
|
||||
try {
|
||||
console.log("Rolling back footer migration...");
|
||||
|
||||
// Xóa footer data
|
||||
await Footer.deleteMany({});
|
||||
|
||||
console.log("Footer rollback completed");
|
||||
} catch (error) {
|
||||
console.error("Footer rollback failed:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { up, down };
|
||||
|
||||
// Chạy migration nếu file được gọi trực tiếp
|
||||
if (require.main === module) {
|
||||
const connectDB = require("../config/database");
|
||||
|
||||
connectDB()
|
||||
.then(() => up())
|
||||
.then(() => {
|
||||
console.log("Migration completed successfully");
|
||||
process.exit(0);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Migration failed:", error);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
90
scripts/2026_02_05_130000_add_footer_menu_order.js
Normal file
90
scripts/2026_02_05_130000_add_footer_menu_order.js
Normal file
@@ -0,0 +1,90 @@
|
||||
const mongoose = require("mongoose");
|
||||
const Footer = require("../models/footer");
|
||||
const footerData = require("../data/footer.json");
|
||||
|
||||
async function addFooterMenuOrder() {
|
||||
try {
|
||||
console.log("=== Adding order field to Footer Menu Links ===");
|
||||
|
||||
// Connect to database
|
||||
await mongoose.connect(process.env.MONGODB_URI || "mongodb://localhost:27017/hailearning");
|
||||
console.log("✓ Connected to MongoDB");
|
||||
|
||||
// Get existing footer or create from JSON
|
||||
let footer = await Footer.findOne();
|
||||
|
||||
if (!footer) {
|
||||
console.log("No existing footer found, creating from JSON data...");
|
||||
|
||||
// Add order to bottom menu links
|
||||
if (footerData.bottom && footerData.bottom.menuLinks) {
|
||||
footerData.bottom.menuLinks = footerData.bottom.menuLinks.map((link, index) => ({
|
||||
...link,
|
||||
order: index + 1,
|
||||
}));
|
||||
}
|
||||
|
||||
// Add order to top menu links
|
||||
if (footerData.top && footerData.top.menuLinks) {
|
||||
footerData.top.menuLinks = footerData.top.menuLinks.map((link, index) => ({
|
||||
...link,
|
||||
order: index + 1,
|
||||
}));
|
||||
}
|
||||
|
||||
footer = await Footer.create(footerData);
|
||||
console.log("✓ Footer created with order fields");
|
||||
} else {
|
||||
console.log("Found existing footer, adding order fields...");
|
||||
|
||||
// Add order to bottom menu links
|
||||
if (footer.bottom && footer.bottom.menuLinks) {
|
||||
footer.bottom.menuLinks = footer.bottom.menuLinks.map((link, index) => ({
|
||||
label: link.label,
|
||||
href: link.href,
|
||||
order: link.order || index + 1,
|
||||
}));
|
||||
}
|
||||
|
||||
// Add order to top menu links
|
||||
if (footer.top && footer.top.menuLinks) {
|
||||
footer.top.menuLinks = footer.top.menuLinks.map((link, index) => ({
|
||||
label: link.label,
|
||||
href: link.href,
|
||||
order: link.order || index + 1,
|
||||
}));
|
||||
}
|
||||
|
||||
await footer.save();
|
||||
console.log("✓ Footer updated with order fields");
|
||||
}
|
||||
|
||||
console.log("Bottom Menu Links with order:");
|
||||
footer.bottom.menuLinks.forEach((link, index) => {
|
||||
console.log(` ${index + 1}. ${link.label} (order: ${link.order}) -> ${link.href}`);
|
||||
});
|
||||
|
||||
console.log("=== Footer Menu Order Migration Completed ===");
|
||||
} catch (error) {
|
||||
console.error("✗ Migration failed:", error);
|
||||
throw error;
|
||||
} finally {
|
||||
await mongoose.disconnect();
|
||||
console.log("✓ Disconnected from MongoDB");
|
||||
}
|
||||
}
|
||||
|
||||
// Run migration if called directly
|
||||
if (require.main === module) {
|
||||
addFooterMenuOrder()
|
||||
.then(() => {
|
||||
console.log("Migration completed successfully");
|
||||
process.exit(0);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Migration failed:", error);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = addFooterMenuOrder;
|
||||
Reference in New Issue
Block a user