/** * Copy Kochi NOVEL microsite scientific programme onto Dehradun, retitle days, and publish. * * Usage: * node scripts/copy-kochi-schedule-to-dehradun.js [--publish] */ const path = require('path'); require('dotenv').config({ path: path.join(__dirname, '..', '.env') }); const mongoose = require('mongoose'); const { connectDB } = require('../src/config/database'); const Event = require('../src/models/Event'); const EventStaticMicrosite = require('../src/models/EventStaticMicrosite'); const User = require('../src/models/User'); const { publishStaticMicrosite } = require('../src/utils/publishStaticMicrosite'); const { importNovelScheduleToAgenda } = require('../src/utils/importNovelScheduleAgenda'); const SOURCE_SUBDOMAIN = 'novel2026-kochi-22-23-aug'; const TARGET_SUBDOMAIN = 'novel2026-dehradun-19-20-sep'; const TARGET_EVENT_ID = '6a50d3de64775fc391bd92bb'; const TARGET_SCHEDULE = { subtitle: 'Scientific programme at Hyatt Regency, Dehradun.', day1: 'September 19, 2026', day2: 'September 20, 2026', dateKeys: ['2026-09-19', '2026-09-20'], }; function deepClone(obj) { return JSON.parse(JSON.stringify(obj)); } async function findPublishUser() { const platform = await User.findOne({ platform_admin: 'yes', isActive: { $ne: false } }).sort({ createdAt: 1 }); if (platform) return platform; const superAdmin = await User.findOne({ role: 'super_admin' }).sort({ createdAt: 1 }); if (superAdmin) return superAdmin; throw new Error('No user found for publish'); } function scheduleFromConfig(config) { return config?.sections?.event_schedule || config?.components?.event_schedule || null; } function applyKochiSchedule(targetConfig, sourceSchedule) { const next = deepClone(targetConfig); next.sections = next.sections || {}; const days = deepClone(sourceSchedule.days || []); if (days[0]) { days[0].label = days[0].label || 'Day 01'; days[0].date = TARGET_SCHEDULE.day1; } if (days[1]) { days[1].label = days[1].label || 'Day 02'; days[1].date = TARGET_SCHEDULE.day2; } next.sections.event_schedule = { ...(next.sections.event_schedule || {}), show: true, heading: sourceSchedule.heading || next.sections.event_schedule?.heading || 'Event Schedule', subtitle: TARGET_SCHEDULE.subtitle, days, }; return next; } function summarizeDays(days) { return (days || []).map((day) => ({ label: day.label, date: day.date, sessions: (day.sessions || []).length, topics: (day.sessions || []).reduce((n, s) => n + ((s.topics || []).length), 0), })); } async function main() { const shouldPublish = process.argv.includes('--publish'); await connectDB(); const targetEvent = await Event.findById(TARGET_EVENT_ID).lean(); if (!targetEvent) throw new Error(`Target event not found: ${TARGET_EVENT_ID}`); const sourceSite = await EventStaticMicrosite.findOne({ subdomain: SOURCE_SUBDOMAIN }).lean(); if (!sourceSite) throw new Error(`Source microsite not found: ${SOURCE_SUBDOMAIN}`); const sourceSchedule = scheduleFromConfig(sourceSite.config); if (!sourceSchedule?.days?.length) { throw new Error('Kochi event_schedule is missing days'); } const targetSite = await EventStaticMicrosite.findOne({ $or: [{ event: TARGET_EVENT_ID }, { subdomain: TARGET_SUBDOMAIN }], }); if (!targetSite) throw new Error(`Dehradun microsite not found: ${TARGET_SUBDOMAIN}`); const days = applyKochiSchedule({ sections: {} }, sourceSchedule).sections.event_schedule.days; const agendaResult = await importNovelScheduleToAgenda(TARGET_EVENT_ID, days, { dateKeys: TARGET_SCHEDULE.dateKeys, }); console.log( `Agenda: deleted ${agendaResult.deleted}, created ${agendaResult.created} session(s) on track ${agendaResult.trackId}`, ); targetSite.config = applyKochiSchedule(targetSite.config, sourceSchedule); targetSite.markModified('config'); const user = shouldPublish ? await findPublishUser() : null; if (user) targetSite.updatedBy = user._id; await targetSite.save(); console.log(`Event: ${targetEvent.name}`); console.log(`Subdomain: ${targetSite.subdomain}`); console.log('Schedule:', JSON.stringify(summarizeDays(days), null, 2)); if (shouldPublish) { const populated = await EventStaticMicrosite.findById(targetSite._id).populate('template'); const result = await publishStaticMicrosite({ staticMicrosite: populated, template: populated.template, userId: user._id, }); Object.assign(populated, result); await populated.save(); console.log(`Published: ${result.publicUrl}`); } else { console.log('Saved as draft. Re-run with --publish to publish.'); } await mongoose.disconnect(); } main().catch((err) => { console.error(err); process.exit(1); });