const { createEvent } = require('ics'); // Event.startDate/endDate are stored as true UTC instants (the admin event form's // datetime-local input is parsed in the browser's local time before being sent as an // ISO string), so serializing them as UTC here requires no timezone math and lets every // viewer's calendar app localize correctly to *their own* timezone. function toUtcArray(date) { const d = new Date(date); return [d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes()]; } /** * Build an RFC 5545 .ics file (as a string) for a single event. * * @param {object} event - Prisma Event row: title, description?, startDate, endDate, location? * @param {string} eventUrl - absolute URL to the event's public page * @returns {string} */ function buildEventIcs(event, eventUrl) { const { error, value } = createEvent({ title: event.title, start: toUtcArray(event.startDate), end: toUtcArray(event.endDate), startInputType: 'utc', endInputType: 'utc', startOutputType: 'utc', endOutputType: 'utc', location: event.location || undefined, description: event.description || undefined, url: eventUrl, }); if (error) throw error; return value; } module.exports = { buildEventIcs };