EventBuzz_Backend/scripts/import-novel-schedule-agend...

59 lines
1.9 KiB
JavaScript

#!/usr/bin/env node
/**
* Import NOVEL schedule CSV into event agenda (Session documents).
*
* Usage:
* node scripts/import-novel-schedule-agenda.js [--csv=path] [--event-id=id]
*/
const path = require('path');
require('dotenv').config({ path: path.join(__dirname, '..', '.env') });
const fs = require('fs');
const mongoose = require('mongoose');
const { connectDB } = require('../src/config/database');
const Event = require('../src/models/Event');
const { parseNovelScheduleCsv } = require('../src/utils/parseNovelScheduleCsv');
const { importNovelScheduleToAgenda } = require('../src/utils/importNovelScheduleAgenda');
const DEFAULT_EVENT_ID = '6a43e05fdc940a0d6e057f7b';
const DEFAULT_CSV = path.join(__dirname, '..', '..', 'novel2026-goa-7-8-aug.csv');
function argValue(name, fallback) {
const hit = process.argv.find((a) => a.startsWith(`--${name}=`));
return hit ? hit.split('=').slice(1).join('=').trim() : fallback;
}
async function main() {
const eventId = argValue('event-id', DEFAULT_EVENT_ID);
const csvPath = argValue('csv', DEFAULT_CSV);
if (!mongoose.Types.ObjectId.isValid(eventId)) {
throw new Error(`Invalid event id: ${eventId}`);
}
if (!fs.existsSync(csvPath)) {
throw new Error(`CSV not found: ${csvPath}`);
}
await connectDB();
const event = await Event.findById(eventId).lean();
if (!event) throw new Error(`Event not found: ${eventId}`);
const text = fs.readFileSync(csvPath, 'utf8');
const { days } = parseNovelScheduleCsv(text, {
dayDates: ['August 7, 2026', 'August 8, 2026'],
});
const result = await importNovelScheduleToAgenda(eventId, days);
console.log(`Deleted ${result.deleted} existing session(s) for event ${eventId}`);
console.log(`Created ${result.created} session(s) for "${event.name}" (track ${result.trackId})`);
console.log(`CSV: ${csvPath}`);
await mongoose.disconnect();
}
main().catch((err) => {
console.error(err);
process.exit(1);
});