133 lines
4.7 KiB
JavaScript
133 lines
4.7 KiB
JavaScript
/**
|
||
* Apply Designing detailed Dehradun Agenda.xlsx to the Dehradun microsite
|
||
* and dashboard agenda, keeping 19–20 September dates.
|
||
*
|
||
* Usage:
|
||
* node scripts/apply-dehradun-excel-agenda.js [--publish] [--xlsx=path]
|
||
*/
|
||
const path = require('path');
|
||
const fs = require('fs');
|
||
const { spawnSync } = require('child_process');
|
||
|
||
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 DEFAULT_XLSX = '/Volumes/Storage/Downloads/Designing detailed Dehradun Agenda.xlsx';
|
||
const PARSER = path.join(__dirname, 'parse-dehradun-excel-agenda.py');
|
||
|
||
function argValue(name, fallback) {
|
||
const hit = process.argv.find((a) => a.startsWith(`--${name}=`));
|
||
return hit ? hit.split('=').slice(1).join('=').trim() : fallback;
|
||
}
|
||
|
||
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 loadDaysFromExcel(xlsxPath) {
|
||
if (!fs.existsSync(xlsxPath)) {
|
||
throw new Error(`Excel not found: ${xlsxPath}`);
|
||
}
|
||
const env = { ...process.env, DEHRADUN_AGENDA_XLSX: xlsxPath };
|
||
const result = spawnSync('python3', [PARSER], { encoding: 'utf8', env });
|
||
if (result.status !== 0) {
|
||
throw new Error(result.stderr || result.stdout || 'Failed to parse Dehradun Excel');
|
||
}
|
||
const parsed = JSON.parse(result.stdout);
|
||
if (!parsed?.days?.length) throw new Error('Excel parser returned 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,
|
||
}));
|
||
}
|
||
|
||
async function main() {
|
||
const shouldPublish = process.argv.includes('--publish');
|
||
const xlsxPath = argValue('xlsx', DEFAULT_XLSX);
|
||
const days = loadDaysFromExcel(xlsxPath);
|
||
|
||
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(`Excel: ${xlsxPath}`);
|
||
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);
|
||
});
|