forked from UKSOURCE/cms.lams
feat: add programme module and blog integration
- 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
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
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");
|
||||
|
||||
/**
|
||||
* Seed / upsert all programmes from data/programmes.json into MongoDB.
|
||||
* Run: node scripts/2026_04_20_150000_seed_programmes.js
|
||||
*/
|
||||
async function migrate() {
|
||||
try {
|
||||
await connectDB();
|
||||
console.log("Connected to MongoDB");
|
||||
|
||||
const jsonPath = path.join(__dirname, "../data/programmes.json");
|
||||
const raw = await fs.readFile(jsonPath, "utf8");
|
||||
const data = JSON.parse(raw);
|
||||
|
||||
if (!Array.isArray(data)) {
|
||||
throw new Error("data/programmes.json must be a JSON array");
|
||||
}
|
||||
|
||||
const results = await Programme.migrateFromJson(data);
|
||||
console.log("Programme migration completed:");
|
||||
results.forEach((r) => console.log(` [${r.action}] id=${r.id}`));
|
||||
|
||||
await mongoose.disconnect();
|
||||
process.exit(0);
|
||||
} catch (error) {
|
||||
console.error("Programme migration error:", error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
migrate();
|
||||
}
|
||||
|
||||
module.exports = { migrate };
|
||||
@@ -0,0 +1,53 @@
|
||||
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();
|
||||
Reference in New Issue
Block a user