import { Injectable } from '@nestjs/common'; import { v4 as uuidv4 } from 'uuid'; import * as moment from 'moment'; @Injectable() export class IcsMeetingService { private formatUtc(dateIso: string | Date): string { return moment.utc(dateIso).format('YYYYMMDDTHHmmss[Z]'); } async generateIcs(dto: any, configJson?: any): Promise { const uid = `${uuidv4()}@etherapp.in`; const now = this.formatUtc(new Date()); const dtStart = this.formatUtc(dto.startTime); const dtEnd = this.formatUtc(new Date(dto.endTime)); const organizerEmail = (configJson.email || configJson.fromEmail) ?? 'yash.joshi@rezolut.in'; const organizerName = dto.organizerName ?? 'Organizer'; const organizer = `ORGANIZER;CN=${organizerName}:mailto:${organizerEmail}`; const attendees = dto.attendees ?.map( (a) => `ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;CN=${a.name ?? a.email};X-NUM-GUESTS=0:mailto:${a.email}`, ) .join('\r\n'); const icsContent = [ 'BEGIN:VCALENDAR', 'PRODID:-//Google Inc//Google Calendar 70.9054//EN', 'VERSION:2.0', 'CALSCALE:GREGORIAN', 'METHOD:REQUEST', 'BEGIN:VEVENT', `DTSTART:${dtStart}`, `DTEND:${dtEnd}`, `DTSTAMP:${now}`, organizer, `UID:${uid}`, attendees ?? '', `CREATED:${now}`, `DESCRIPTION:${dto.description ?? ''}`, `LAST-MODIFIED:${now}`, `LOCATION:${dto.location ?? 'Google Meet'}`, 'SEQUENCE:0', 'STATUS:CONFIRMED', `SUMMARY:${dto.title}`, 'TRANSP:OPAQUE', 'END:VEVENT', 'END:VCALENDAR', ] .filter(Boolean) .join('\r\n'); return Buffer.from(icsContent, 'utf-8').toString('base64'); } }