forked from UKSOURCE/cms.hailearning.edu.vn
first commit
This commit is contained in:
105
scripts/2025_12_09_131838_camp_location.js
Normal file
105
scripts/2025_12_09_131838_camp_location.js
Normal file
@@ -0,0 +1,105 @@
|
||||
require('dotenv').config();
|
||||
const fs = require('fs').promises;
|
||||
const path = require('path');
|
||||
const connectDB = require('../config/database');
|
||||
const CampLocation = require('../models/campLocation');
|
||||
|
||||
async function validateCampLocationData(data) {
|
||||
const requiredFields = [
|
||||
'metadata',
|
||||
'hero',
|
||||
'camps',
|
||||
'locations',
|
||||
'intro',
|
||||
'faq',
|
||||
'welcomeQuote',
|
||||
'securityConcept'
|
||||
];
|
||||
|
||||
const missingFields = requiredFields.filter(field => !data[field]);
|
||||
if (missingFields.length > 0) {
|
||||
throw new Error(`Missing required fields: ${missingFields.join(', ')}`);
|
||||
}
|
||||
|
||||
// Validate camps array
|
||||
if (!Array.isArray(data.camps)) {
|
||||
throw new Error('Camps must be an array');
|
||||
}
|
||||
|
||||
// Validate each camp has required fields
|
||||
data.camps.forEach((camp, index) => {
|
||||
if (!camp.id) {
|
||||
throw new Error(`Camp at index ${index} is missing required field: id`);
|
||||
}
|
||||
if (!camp.title) {
|
||||
throw new Error(`Camp at index ${index} is missing required field: title`);
|
||||
}
|
||||
});
|
||||
|
||||
// Validate locations array
|
||||
if (!Array.isArray(data.locations) || data.locations.length === 0) {
|
||||
throw new Error('Locations must be a non-empty array');
|
||||
}
|
||||
|
||||
// Validate FAQ array
|
||||
if (!Array.isArray(data.faq) || data.faq.length === 0) {
|
||||
throw new Error('FAQ must be a non-empty array');
|
||||
}
|
||||
|
||||
// Validate security concept items
|
||||
if (!Array.isArray(data.securityConcept.items) || data.securityConcept.items.length === 0) {
|
||||
throw new Error('Security concept items must be a non-empty array');
|
||||
}
|
||||
|
||||
console.log('✓ Data validation passed');
|
||||
}
|
||||
|
||||
/**
|
||||
* Migration: camp_location
|
||||
* Created: 13:18:38 9/12/2025
|
||||
* Imports camp location data including camps, locations, FAQ, and security information
|
||||
*/
|
||||
async function migrate() {
|
||||
try {
|
||||
// Kết nối database
|
||||
await connectDB();
|
||||
console.log('Starting migration: camp_location...');
|
||||
|
||||
// Delete existing data
|
||||
const deleteResult = await CampLocation.deleteMany({});
|
||||
console.log(`✓ Deleted ${deleteResult.deletedCount} existing records`);
|
||||
|
||||
// Read JSON file
|
||||
const campLocationData = JSON.parse(
|
||||
await fs.readFile(path.join(__dirname, '../data/camp-location.json'), 'utf8')
|
||||
);
|
||||
console.log('✓ Loaded camp-location.json');
|
||||
|
||||
// Validate data
|
||||
await validateCampLocationData(campLocationData);
|
||||
|
||||
// Create new record
|
||||
const result = await CampLocation.create(campLocationData);
|
||||
console.log('✓ Created camp location record');
|
||||
console.log(` - ${result.camps.length} camps`);
|
||||
console.log(` - ${result.locations.length} locations`);
|
||||
console.log(` - ${result.faq.length} FAQ items`);
|
||||
console.log(` - ${result.securityConcept.items.length} security measures`);
|
||||
|
||||
console.log('Migration camp_location completed successfully!');
|
||||
|
||||
const mongoose = require('mongoose');
|
||||
await mongoose.disconnect();
|
||||
process.exit(0);
|
||||
} catch (error) {
|
||||
console.error('Migration error:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Chạy migration nếu được gọi trực tiếp
|
||||
if (require.main === module) {
|
||||
migrate();
|
||||
}
|
||||
|
||||
module.exports = { migrate };
|
||||
Reference in New Issue
Block a user