forked from UKSOURCE/cms.hailearning.edu.vn
80 lines
2.9 KiB
JavaScript
80 lines
2.9 KiB
JavaScript
const mongoose = require('mongoose');
|
|
const AboutUs = require('../models/aboutUs');
|
|
const fs = require('fs');
|
|
require('dotenv').config();
|
|
|
|
// Load and clean JSON data
|
|
const raw = fs.readFileSync(require('path').join(__dirname, '..', 'data', 'aboutUs.json'), 'utf8');
|
|
let data = JSON.parse(raw || '{}');
|
|
|
|
// Remove _id fields recursively to avoid conflicts
|
|
function stripIds(obj) {
|
|
if (Array.isArray(obj)) return obj.map(i => stripIds(i));
|
|
if (obj && typeof obj === 'object') {
|
|
const out = {};
|
|
for (const k in obj) {
|
|
if (k !== '_id') out[k] = stripIds(obj[k]);
|
|
}
|
|
return out;
|
|
}
|
|
return obj;
|
|
}
|
|
|
|
data = stripIds(data);
|
|
|
|
// Check for --dry-run flag
|
|
const dryRun = process.argv.includes('--dry-run') || process.argv.includes('-n');
|
|
|
|
async function importAboutUs() {
|
|
try {
|
|
const dbUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/ggcamps';
|
|
console.log('📍 Using DB URI:', dbUri);
|
|
|
|
if (dryRun) {
|
|
console.log('\n🔍 === DRY RUN MODE ===');
|
|
console.log('Document to be upserted (preview only, no DB changes):\n');
|
|
console.log(JSON.stringify(data, null, 2));
|
|
console.log('\n=== END DRY RUN ===\n');
|
|
console.log('To actually import, run without --dry-run flag');
|
|
process.exit(0);
|
|
}
|
|
|
|
console.log('🔄 Connecting to database...');
|
|
await mongoose.connect(dbUri);
|
|
console.log('✓ Connected to database');
|
|
|
|
// Safe upsert: update existing doc or create new one
|
|
console.log('📥 Upserting AboutUs document (safe mode)...');
|
|
const result = await AboutUs.findOneAndUpdate({}, data, {
|
|
upsert: true,
|
|
new: true,
|
|
setDefaultsOnInsert: true
|
|
});
|
|
|
|
console.log('✅ Successfully upserted AboutUs data!');
|
|
console.log('📝 Document ID:', result._id.toString());
|
|
console.log('📊 Data structure:');
|
|
console.log(' - Hero:', data.hero ? '✓' : '✗');
|
|
console.log(' - Introduction:', data.introduction ? '✓' : '✗');
|
|
console.log(' - Introduction Services:', data.introduction?.services?.length || 0, 'items');
|
|
console.log(' - Statistics:', data.statistics ? '✓' : '✗');
|
|
console.log(' - Statistics Items:', data.statistics?.items?.length || 0, 'items');
|
|
console.log(' - Accommodation:', data.accommodation ? '✓' : '✗');
|
|
console.log(' - Accommodation Features:', data.accommodation?.features?.length || 0, 'items');
|
|
console.log(' - Activities:', data.activities ? '✓' : '✗');
|
|
console.log(' - Activities Gallery:', data.activities?.gallery?.length || 0, 'items');
|
|
console.log(' - Newsletter:', data.newsletter ? '✓' : '✗');
|
|
console.log(' - Events:', data.events ? '✓' : '✗');
|
|
console.log(' - Events Items:', data.events?.items?.length || 0, 'items');
|
|
|
|
process.exit(0);
|
|
} catch (error) {
|
|
console.error('❌ Error:', error.message);
|
|
console.error(error);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
// Run import
|
|
importAboutUs();
|