forked from UKSOURCE/cms.lams
Refactor Homepage, About, Footer (Controller,Model, View, Data), Update: Dashboard, Delete old file
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
require('dotenv').config();
|
||||
const fs = require('fs').promises;
|
||||
const path = require('path');
|
||||
const connectDB = require('../config/database');
|
||||
const Footer = require('../models/footer');
|
||||
|
||||
async function validateFooterData(data) {
|
||||
if (!data || typeof data !== 'object') {
|
||||
throw new Error('Footer data must be a valid object');
|
||||
}
|
||||
|
||||
const required = ['brand', 'explore', 'contact', 'newsletter', 'bottom'];
|
||||
for (const field of required) {
|
||||
if (!data[field]) {
|
||||
throw new Error(`Missing required field: ${field}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function migrateFooterData() {
|
||||
try {
|
||||
await connectDB();
|
||||
console.log('Đã kết nối MongoDB...');
|
||||
|
||||
// Xóa dữ liệu cũ
|
||||
await Footer.deleteMany({});
|
||||
console.log('Đã xóa dữ liệu Footer cũ');
|
||||
|
||||
// Đọc file JSON
|
||||
const footerData = JSON.parse(
|
||||
await fs.readFile(
|
||||
path.join(__dirname, '../data/new/footer.json'),
|
||||
'utf8'
|
||||
)
|
||||
);
|
||||
|
||||
// Validate
|
||||
await validateFooterData(footerData);
|
||||
|
||||
// Transform
|
||||
const finalData = {
|
||||
...footerData,
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
// Insert
|
||||
await Footer.create(finalData);
|
||||
|
||||
console.log('✓ Migrate Footer thành công!');
|
||||
process.exit(0);
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Lỗi:', error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
migrateFooterData();
|
||||
Reference in New Issue
Block a user