Skip to content

The open alternative to Salesforce, designed for AI.

2.4 KB
Raw Blame
import { Injectable } from '@nestjs/common';

import { ConnectedAccountProvider } from 'twenty-shared/types';

import { CalDavGetEventsService } from 'src/modules/calendar/calendar-event-import-manager/drivers/caldav/services/caldav-get-events.service';
import { GoogleCalendarGetEventsService } from 'src/modules/calendar/calendar-event-import-manager/drivers/google-calendar/services/google-calendar-get-events.service';
import { MicrosoftCalendarGetEventsService } from 'src/modules/calendar/calendar-event-import-manager/drivers/microsoft-calendar/services/microsoft-calendar-get-events.service';
import {
  CalendarEventImportException,
  CalendarEventImportExceptionCode,
} from 'src/modules/calendar/calendar-event-import-manager/exceptions/calendar-event-import.exception';
import { type ConnectedAccountEntity } from 'src/engine/metadata-modules/connected-account/entities/connected-account.entity';

export type GetCalendarEventsResponse = {
  calendarEventIds: string[];
  calendarEventIdsToDelete: string[];
  nextSyncCursor: string;
};

@Injectable()
export class CalendarGetCalendarEventsService {
  constructor(
    private readonly googleCalendarGetEventsService: GoogleCalendarGetEventsService,
    private readonly microsoftCalendarGetEventsService: MicrosoftCalendarGetEventsService,
    private readonly caldavCalendarGetEventsService: CalDavGetEventsService,
  ) {}

  public async getCalendarEvents(
    connectedAccount: Pick<
      ConnectedAccountEntity,
      | 'provider'
      | 'accessToken'
      | 'refreshToken'
      | 'id'
      | 'connectionParameters'
      | 'handle'
      | 'workspaceId'
    >,
    syncCursor?: string,
  ): Promise<GetCalendarEventsResponse> {
    switch (connectedAccount.provider) {
      case ConnectedAccountProvider.GOOGLE:
        return this.googleCalendarGetEventsService.getCalendarEvents(
          connectedAccount,
          syncCursor,
        );
      case ConnectedAccountProvider.MICROSOFT:
        return this.microsoftCalendarGetEventsService.getCalendarEvents(
          connectedAccount,
          syncCursor,
        );
      case ConnectedAccountProvider.IMAP_SMTP_CALDAV:
        return this.caldavCalendarGetEventsService.getCalendarEvents(
          connectedAccount.id,
          syncCursor,
        );
      default:
        throw new CalendarEventImportException(
          `Provider ${connectedAccount.provider} is not supported`,
          CalendarEventImportExceptionCode.PROVIDER_NOT_SUPPORTED,
        );
    }
  }
}