EventBuzz_Backend/scripts/apply-dehradun-printed-agen...

117 lines
4.0 KiB
JavaScript

/**
* Apply the printed Dehradun scientific programme to the event agenda
* and microsite, then optionally publish.
*
* Usage:
* node scripts/apply-dehradun-printed-agenda.js [--publish]
*/
const path = require('path');
const fs = require('fs');
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 TARGET_EVENT_ID = '6a50d3de64775fc391bd92bb';
const TARGET_SUBDOMAIN = 'novel2026-dehradun-19-20-sep';
const SUBTITLE = 'Scientific programme at Hyatt Regency, Dehradun.';
const DATE_KEYS = ['2026-09-19', '2026-09-20'];
const AGENDA_JSON = path.join(__dirname, 'data', 'dehradun-printed-agenda.json');
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 loadDays() {
const parsed = JSON.parse(fs.readFileSync(AGENDA_JSON, 'utf8'));
if (!parsed?.days?.length) throw new Error('Printed agenda JSON has no days');
return parsed.days;
}
function applyScheduleToConfig(config, days) {
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,
};
return next;
}
function summarize(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),
first: day.sessions?.[0]?.name,
last: day.sessions?.[day.sessions.length - 1]?.name,
}));
}
async function main() {
const shouldPublish = process.argv.includes('--publish');
const days = loadDays();
await connectDB();
const targetEvent = await Event.findById(TARGET_EVENT_ID).lean();
if (!targetEvent) throw new Error(`Target event not found: ${TARGET_EVENT_ID}`);
const agenda = await importNovelScheduleToAgenda(TARGET_EVENT_ID, days, { dateKeys: DATE_KEYS });
console.log(`Agenda: deleted ${agenda.deleted}, created ${agenda.created} session(s)`);
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 = applyScheduleToConfig(targetSite.config, days);
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(summarize(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);
});