/** * Restore Dehradun's original programme and dates, using Kochi's nested * session + topics structure on the microsite (without copying Kochi content). * * Usage: * node scripts/restore-dehradun-schedule-structure.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 Session = require('../src/models/Session'); const EventStaticMicrosite = require('../src/models/EventStaticMicrosite'); const User = require('../src/models/User'); const { publishStaticMicrosite } = require('../src/utils/publishStaticMicrosite'); const { eventBuzzCodeFor } = require('../src/utils/eventBuzzCode'); const { resolveDefaultTrackId } = require('../src/utils/importNovelScheduleAgenda'); const SOURCE_EVENT_ID = '6a509c4aaea9db627064ecf1'; // original 14-session programme const TARGET_EVENT_ID = '6a50d3de64775fc391bd92bb'; const TARGET_SUBDOMAIN = 'novel2026-dehradun-19-20-sep'; const SOURCE_DAY1 = Date.parse('2026-08-07T00:00:00.000Z'); const TARGET_DAY1 = Date.parse('2026-09-19T00:00:00.000Z'); const DAY_DATES = ['September 19, 2026', 'September 20, 2026']; const SUBTITLE = 'Scientific programme at Hyatt Regency, Dehradun.'; function deepClone(obj) { return JSON.parse(JSON.stringify(obj)); } function shiftTime(date) { if (!date) return date; return new Date(new Date(date).getTime() + (TARGET_DAY1 - SOURCE_DAY1)); } 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 toKochiSessionStructure(session) { return { time: session.time || '', type: session.type || 'Main programme', title: '', name: session.name || session.title || '', topics: Array.isArray(session.topics) ? session.topics : [], }; } function applyStructuredSchedule(config, sourceDays) { const next = deepClone(config); next.sections = next.sections || {}; next.sections.event_schedule = { ...(next.sections.event_schedule || {}), show: true, heading: next.sections.event_schedule?.heading || 'Event Schedule', subtitle: SUBTITLE, days: (sourceDays || []).map((day, index) => ({ label: day.label || `Day 0${index + 1}`, date: DAY_DATES[index] || day.date, sessions: (day.sessions || []).map(toKochiSessionStructure), })), }; return next; } async function restoreAgenda(sourceSessions) { const trackId = await resolveDefaultTrackId(TARGET_EVENT_ID); const deleted = await Session.deleteMany({ event: TARGET_EVENT_ID }); const payloads = sourceSessions.map((session) => { const payload = { event: TARGET_EVENT_ID, track: trackId, title: session.title, description: session.description || '', type: session.type || 'panel', startTime: shiftTime(session.startTime), endTime: shiftTime(session.endTime), location: session.location || '', speakers: session.speakers || [], keyPersons: session.keyPersons || [], moderator: session.moderator || undefined, status: session.status || 'scheduled', topics: session.topics || [], audienceFeedbackEnabled: session.audienceFeedbackEnabled !== false, audienceRatingsEnabled: session.audienceRatingsEnabled !== false, audienceKeyPersonsEnabled: session.audienceKeyPersonsEnabled !== false, audienceMySessionsEnabled: session.audienceMySessionsEnabled !== false, audienceAgendaEnabled: session.audienceAgendaEnabled !== false, enableQnA: session.enableQnA !== false, enableQnP: session.enableQnP !== false, }; return payload; }); const created = payloads.length ? await Session.insertMany(payloads) : []; await Promise.all( created.map(async (doc) => { const joinCode = eventBuzzCodeFor(String(doc._id), 'session'); await Session.updateOne({ _id: doc._id }, { $set: { joinCode } }); }), ); return { deleted: deleted.deletedCount || 0, created: created.length, trackId: String(trackId) }; } 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({ event: SOURCE_EVENT_ID }).lean(); const sourceDays = sourceSite?.config?.sections?.event_schedule?.days; if (!sourceDays?.length) throw new Error('Original programme days not found'); const sourceSessions = await Session.find({ event: SOURCE_EVENT_ID }).sort({ startTime: 1 }).lean(); if (sourceSessions.length !== 14) { throw new Error(`Expected 14 original sessions, found ${sourceSessions.length}`); } const agenda = await restoreAgenda(sourceSessions); console.log(`Agenda restored: deleted ${agenda.deleted}, created ${agenda.created}`); const targetSite = await EventStaticMicrosite.findOne({ $or: [{ event: TARGET_EVENT_ID }, { subdomain: TARGET_SUBDOMAIN }], }); if (!targetSite) throw new Error(`Dehradun microsite not found: ${TARGET_SUBDOMAIN}`); targetSite.config = applyStructuredSchedule(targetSite.config, sourceDays); targetSite.markModified('config'); const user = shouldPublish ? await findPublishUser() : null; if (user) targetSite.updatedBy = user._id; await targetSite.save(); const days = targetSite.config.sections.event_schedule.days; console.log(`Event: ${targetEvent.name}`); console.log(`Subdomain: ${targetSite.subdomain}`); console.log( 'Schedule:', days.map((d) => `${d.label} ${d.date} sessions=${d.sessions.length} first=${d.sessions[0]?.name}`).join(' | '), ); 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); });