Files
calendar/packages/features/calendar-subscription/lib/sync/CalendarSyncService.ts
T
Benny JooandGitHub ff38d6c7db refactor: Remove circular deps between @calcom/lib and @calcom/features [1] (#24399)
* add eslint config

* migrate autoLock to features

* migrate teamService to features

* migrate userCreationService

* migrate insights services to features

* migrate ProfileRepository

* update imports

* migrate filter segmen tests

* migrate filter segment repository

* migrate getBusyTimes

* migrate getLocaleFromRequest

* refactor csvUtils

* make filename clearer

* migrate getLuckyUser integration test

* migrate autoLock test to features

* wip

* refactors

* migrate useBookerUrl

* migrate more

* wip

* Migrate eventTypeRepository

* membership repository

* update imports

* update imports

* migrate

* move organization repository

* update imports

* update imports

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix tests

* fix type checks

* fix

* fix

* migrate

* update imports

* fix tests

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix

* fix
2025-10-13 12:01:02 -03:00

99 lines
2.7 KiB
TypeScript

import type { CalendarSubscriptionEventItem } from "@calcom/features/calendar-subscription/lib/CalendarSubscriptionPort.interface";
import logger from "@calcom/lib/logger";
import { BookingRepository } from "@calcom/features/bookings/repositories/BookingRepository";
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
}
}