forked from UKSOURCE/cms.lams
- Add Programme Mongoose model with sub-schemas and migrateFromJson static method - Add programme CRUD controller with audit logging and icon sanitization - Add programme admin views (index, edit) with tabbed form and dynamic arrays - Add seed migration script for programmes - Add /api/programmes and /api/programmes/:id public API endpoints - Register programme routes in admin and public router - Update dashboard with Programmes card and API endpoint entries - Update admin sidebar layout to include Programmes nav link
54 lines
1.5 KiB
JavaScript
54 lines
1.5 KiB
JavaScript
require('dotenv').config();
|
|
const fs = require('fs').promises;
|
|
const path = require('path');
|
|
const mongoose = require('mongoose');
|
|
const connectDB = require('../config/database');
|
|
const Programme = require('../models/programme');
|
|
|
|
async function validateProgrammeData(dataArray) {
|
|
if (!Array.isArray(dataArray)) {
|
|
throw new Error('Data must be an array of programme objects');
|
|
}
|
|
|
|
if (dataArray.length === 0) {
|
|
throw new Error('Programme array cannot be empty');
|
|
}
|
|
|
|
for (const item of dataArray) {
|
|
if (!item.id || !item.title || !item.level) {
|
|
throw new Error(`Programme is missing required fields (id, title, level). Error at id: ${item.id}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
async function migrateProgrammeData() {
|
|
try {
|
|
await connectDB();
|
|
console.log('Đã kết nối đến MongoDB...');
|
|
|
|
await Programme.deleteMany({});
|
|
console.log('Đã xóa dữ liệu Programme cũ');
|
|
|
|
const programmesData = JSON.parse(
|
|
await fs.readFile(path.join(__dirname, '../data/programmes.json'), 'utf8')
|
|
);
|
|
|
|
await validateProgrammeData(programmesData);
|
|
|
|
const dataWithTimestamps = programmesData.map(item => ({
|
|
...item,
|
|
updatedAt: new Date()
|
|
}));
|
|
|
|
await Programme.insertMany(dataWithTimestamps);
|
|
console.log(`✓ Migrate dữ liệu Programmes thành công (${dataWithTimestamps.length} items)!`);
|
|
process.exit(0);
|
|
|
|
} catch (error) {
|
|
console.error('Lỗi:', error.message);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
migrateProgrammeData();
|