import { BookingRepository } from "@calcom/features/bookings/repositories/BookingRepository"; import type { CalendarSubscriptionEventItem } from "@calcom/features/calendar-subscription/lib/CalendarSubscriptionPort.interface"; import logger from "@calcom/lib/logger"; import type { SelectedCalendar } from "@calcom/prisma/client"; const log = logger.getSubLogger({ prefix: ["CalendarSyncService"] }); /** * Service to handle synchronization of calendar events. */ export class CalendarSyncService { constructor( private deps: { bookingRepository: BookingRepository; } ) {} /** * Handles synchronization of calendar events * * @param selectedCalendar calendar to process * @param calendarSubscriptionEvents events to process * @returns */ async handleEvents( selectedCalendar: SelectedCalendar, calendarSubscriptionEvents: CalendarSubscriptionEventItem[] ) { log.debug("handleEvents", { externalId: selectedCalendar.externalId, countEvents: calendarSubscriptionEvents.length, }); // only process cal.com calendar events const calEvents = calendarSubscriptionEvents.filter((e) => e.iCalUID?.toLowerCase()?.endsWith("@cal.com") ); if (calEvents.length === 0) { log.debug("handleEvents: no calendar events to process"); return; } log.debug("handleEvents: processing calendar events", { count: calEvents.length }); await Promise.all( calEvents.map((e) => { if (e.status === "cancelled") { return this.cancelBooking(e); } else { return this.rescheduleBooking(e); } }) ); } /** * Cancels a booking * @param event * @returns */ async cancelBooking(event: CalendarSubscriptionEventItem) { log.debug("cancelBooking", { event }); const [bookingUid] = event.iCalUID?.split("@") ?? [undefined]; if (!bookingUid) { log.debug("Unable to sync, booking not found"); return; } const booking = await this.deps.bookingRepository.findBookingByUidWithEventType({ bookingUid }); if (!booking) { log.debug("Unable to sync, booking not found"); return; } // todo handle cancel booking } /** * Reschedule a booking * @param event */ async rescheduleBooking(event: CalendarSubscriptionEventItem) { log.debug("rescheduleBooking", { event }); const [bookingUid] = event.iCalUID?.split("@") ?? [undefined]; if (!bookingUid) { log.debug("Unable to sync, booking not found"); return; } const booking = await this.deps.bookingRepository.findBookingByUidWithEventType({ bookingUid }); if (!booking) { log.debug("Unable to sync, booking not found"); return; } // todo handle update booking } }