Skip to content

The open alternative to Salesforce, designed for AI.

Sync calendar webhooks without waiting for the import cron (#24987)

neo773d9cb7cc35f83
diff --git a/packages/twenty-server/src/engine/core-modules/message-queue/message-queue-worker-config.constant.ts b/packages/twenty-server/src/engine/core-modules/message-queue/message-queue-worker-config.constant.ts
index 624b2e96d0c..e31f30cd7cd 100644
--- a/packages/twenty-server/src/engine/core-modules/message-queue/message-queue-worker-config.constant.ts
+++ b/packages/twenty-server/src/engine/core-modules/message-queue/message-queue-worker-config.constant.ts
@@ -76,6 +76,15 @@ export const MESSAGE_QUEUE_WORKER_CONFIG: Record<
boundedShutdownDrain: false,
},
},
+ [MessageQueue.connectedAccountSyncWebhookQueue]: {
+ priority: 1,
+ workerOptions: {
+ concurrency: 5,
+ lockDuration: 30_000,
+ maxStalledCount: 1,
+ boundedShutdownDrain: false,
+ },
+ },
[MessageQueue.contactCreationQueue]: {
priority: 4,
workerOptions: {
diff --git a/packages/twenty-server/src/engine/core-modules/message-queue/message-queue.constants.ts b/packages/twenty-server/src/engine/core-modules/message-queue/message-queue.constants.ts
index 0b9364d886b..d9fb480f76a 100644
--- a/packages/twenty-server/src/engine/core-modules/message-queue/message-queue.constants.ts
+++ b/packages/twenty-server/src/engine/core-modules/message-queue/message-queue.constants.ts
@@ -9,6 +9,7 @@ export enum MessageQueue {
cronQueue = 'cron-queue',
emailQueue = 'email-queue',
calendarQueue = 'calendar-queue',
+ connectedAccountSyncWebhookQueue = 'connected-account-sync-webhook-queue',
contactCreationQueue = 'contact-creation-queue',
billingQueue = 'billing-queue',
workspaceQueue = 'workspace-queue',
diff --git a/packages/twenty-server/src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/calendar-event-webhook-sync.exception.ts b/packages/twenty-server/src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/calendar-event-webhook-sync.exception.ts
new file mode 100644
index 00000000000..eeba2f3b632
--- /dev/null
+++ b/packages/twenty-server/src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/calendar-event-webhook-sync.exception.ts
@@ -0,0 +1,34 @@
+import { type MessageDescriptor } from '@lingui/core';
+import { msg } from '@lingui/core/macro';
+import { assertUnreachable } from 'twenty-shared/utils';
+
+import { CustomException } from 'src/utils/custom-exception';
+
+export enum CalendarEventWebhookSyncExceptionCode {
+ CALENDAR_CHANNEL_SYNC_ALREADY_IN_PROGRESS = 'CALENDAR_CHANNEL_SYNC_ALREADY_IN_PROGRESS',
+}
+
+const getCalendarEventWebhookSyncExceptionUserFriendlyMessage = (
+ code: CalendarEventWebhookSyncExceptionCode,
+) => {
+ switch (code) {
+ case CalendarEventWebhookSyncExceptionCode.CALENDAR_CHANNEL_SYNC_ALREADY_IN_PROGRESS:
+ return msg`The calendar is already syncing.`;
+ default:
+ assertUnreachable(code);
+ }
+};
+
+export class CalendarEventWebhookSyncException extends CustomException<CalendarEventWebhookSyncExceptionCode> {
+ constructor(
+ message: string,
+ code: CalendarEventWebhookSyncExceptionCode,
+ { userFriendlyMessage }: { userFriendlyMessage?: MessageDescriptor } = {},
+ ) {
+ super(message, code, {
+ userFriendlyMessage:
+ userFriendlyMessage ??
+ getCalendarEventWebhookSyncExceptionUserFriendlyMessage(code),
+ });
+ }
+}
diff --git a/packages/twenty-server/src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/calendar-event-webhook-sync.module.ts b/packages/twenty-server/src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/calendar-event-webhook-sync.module.ts
new file mode 100644
index 00000000000..e227a76fc31
--- /dev/null
+++ b/packages/twenty-server/src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/calendar-event-webhook-sync.module.ts
@@ -0,0 +1,16 @@
+import { Module } from '@nestjs/common';
+import { TypeOrmModule } from '@nestjs/typeorm';
+
+import { CalendarChannelEntity } from 'src/engine/metadata-modules/calendar-channel/entities/calendar-channel.entity';
+import { CalendarEventImportManagerModule } from 'src/modules/calendar/calendar-event-import-manager/calendar-event-import-manager.module';
+import { CalendarEventWebhookSyncJob } from 'src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/jobs/calendar-event-webhook-sync.job';
+import { CalendarEventWebhookSyncService } from 'src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/services/calendar-event-webhook-sync.service';
+
+@Module({
+ imports: [
+ CalendarEventImportManagerModule,
+ TypeOrmModule.forFeature([CalendarChannelEntity]),
+ ],
+ providers: [CalendarEventWebhookSyncService, CalendarEventWebhookSyncJob],
+})
+export class CalendarEventWebhookSyncModule {}
diff --git a/packages/twenty-server/src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/constants/calendar-event-webhook-sync-inline-import-max-events.constant.ts b/packages/twenty-server/src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/constants/calendar-event-webhook-sync-inline-import-max-events.constant.ts
new file mode 100644
index 00000000000..7fcd725152a
--- /dev/null
+++ b/packages/twenty-server/src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/constants/calendar-event-webhook-sync-inline-import-max-events.constant.ts
@@ -0,0 +1 @@
+export const CALENDAR_EVENT_WEBHOOK_SYNC_INLINE_IMPORT_MAX_EVENTS = 50;
diff --git a/packages/twenty-server/src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/constants/calendar-event-webhook-sync-retry-initial-delay-ms.constant.ts b/packages/twenty-server/src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/constants/calendar-event-webhook-sync-retry-initial-delay-ms.constant.ts
new file mode 100644
index 00000000000..8d0050f4927
--- /dev/null
+++ b/packages/twenty-server/src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/constants/calendar-event-webhook-sync-retry-initial-delay-ms.constant.ts
@@ -0,0 +1 @@
+export const CALENDAR_EVENT_WEBHOOK_SYNC_RETRY_INITIAL_DELAY_MS = 5000;
diff --git a/packages/twenty-server/src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/constants/calendar-event-webhook-sync-retry-limit.constant.ts b/packages/twenty-server/src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/constants/calendar-event-webhook-sync-retry-limit.constant.ts
new file mode 100644
index 00000000000..03c31f77464
--- /dev/null
+++ b/packages/twenty-server/src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/constants/calendar-event-webhook-sync-retry-limit.constant.ts
@@ -0,0 +1 @@
+export const CALENDAR_EVENT_WEBHOOK_SYNC_RETRY_LIMIT = 3;
diff --git a/packages/twenty-server/src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/jobs/calendar-event-webhook-sync.job.ts b/packages/twenty-server/src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/jobs/calendar-event-webhook-sync.job.ts
new file mode 100644
index 00000000000..da86d4b33b3
--- /dev/null
+++ b/packages/twenty-server/src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/jobs/calendar-event-webhook-sync.job.ts
@@ -0,0 +1,28 @@
+import { Scope } from '@nestjs/common';
+
+import { Process } from 'src/engine/core-modules/message-queue/decorators/process.decorator';
+import { Processor } from 'src/engine/core-modules/message-queue/decorators/processor.decorator';
+import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';
+import { CalendarEventWebhookSyncService } from 'src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/services/calendar-event-webhook-sync.service';
+
+export type CalendarEventWebhookSyncJobData = {
+ calendarChannelId: string;
+ workspaceId: string;
+};
+
+@Processor({
+ queueName: MessageQueue.connectedAccountSyncWebhookQueue,
+ scope: Scope.REQUEST,
+})
+export class CalendarEventWebhookSyncJob {
+ constructor(
+ private readonly calendarEventWebhookSyncService: CalendarEventWebhookSyncService,
+ ) {}
+
+ @Process(CalendarEventWebhookSyncJob.name)
+ async handle(data: CalendarEventWebhookSyncJobData): Promise<void> {
+ await this.calendarEventWebhookSyncService.processCalendarEventWebhookSync(
+ data,
+ );
+ }
+}
diff --git a/packages/twenty-server/src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/services/calendar-event-webhook-sync.service.ts b/packages/twenty-server/src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/services/calendar-event-webhook-sync.service.ts
new file mode 100644
index 00000000000..0dd806e88b9
--- /dev/null
+++ b/packages/twenty-server/src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/services/calendar-event-webhook-sync.service.ts
@@ -0,0 +1,157 @@
+import { Injectable, Logger } from '@nestjs/common';
+import { InjectRepository } from '@nestjs/typeorm';
+
+import { CalendarChannelSyncStage } from 'twenty-shared/types';
+import { isDefined } from 'twenty-shared/utils';
+import { Repository } from 'typeorm';
+
+import { InjectCacheStorage } from 'src/engine/core-modules/cache-storage/decorators/cache-storage.decorator';
+import { CacheStorageService } from 'src/engine/core-modules/cache-storage/services/cache-storage.service';
+import { CacheStorageNamespace } from 'src/engine/core-modules/cache-storage/types/cache-storage-namespace.enum';
+import { CalendarChannelEntity } from 'src/engine/metadata-modules/calendar-channel/entities/calendar-channel.entity';
+import { CalendarEventsImportService } from 'src/modules/calendar/calendar-event-import-manager/services/calendar-events-import.service';
+import { CalendarFetchEventsService } from 'src/modules/calendar/calendar-event-import-manager/services/calendar-fetch-events.service';
+import {
+ CalendarEventWebhookSyncException,
+ CalendarEventWebhookSyncExceptionCode,
+} from 'src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/calendar-event-webhook-sync.exception';
+import { CALENDAR_EVENT_WEBHOOK_SYNC_INLINE_IMPORT_MAX_EVENTS } from 'src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/constants/calendar-event-webhook-sync-inline-import-max-events.constant';
+
+@Injectable()
+export class CalendarEventWebhookSyncService {
+ private readonly logger = new Logger(CalendarEventWebhookSyncService.name);
+
+ constructor(
+ @InjectCacheStorage(CacheStorageNamespace.ModuleCalendar)
+ private readonly cacheStorage: CacheStorageService,
+ @InjectRepository(CalendarChannelEntity)
+ private readonly calendarChannelRepository: Repository<CalendarChannelEntity>,
+ private readonly calendarFetchEventsService: CalendarFetchEventsService,
+ private readonly calendarEventsImportService: CalendarEventsImportService,
+ ) {}
+
+ async processCalendarEventWebhookSync({
+ calendarChannelId,
+ workspaceId,
+ }: {
+ calendarChannelId: string;
+ workspaceId: string;
+ }): Promise<void> {
+ const isCalendarChannelScheduled =
+ await this.markCalendarChannelAsListFetchScheduledIfPending({
+ calendarChannelId,
+ workspaceId,
+ });
+
+ if (!isCalendarChannelScheduled) {
+ throw new CalendarEventWebhookSyncException(
+ `Calendar channel ${calendarChannelId} is not available for a webhook sync`,
+ CalendarEventWebhookSyncExceptionCode.CALENDAR_CHANNEL_SYNC_ALREADY_IN_PROGRESS,
+ );
+ }
+
+ const calendarChannel = await this.findSyncEnabledCalendarChannel({
+ calendarChannelId,
+ workspaceId,
+ });
+
+ if (!isDefined(calendarChannel)) {
+ return;
+ }
+
+ await this.calendarFetchEventsService.fetchCalendarEvents(
+ calendarChannel,
+ calendarChannel.connectedAccount,
+ workspaceId,
+ );
+
+ await this.importFetchedCalendarEvents({ calendarChannelId, workspaceId });
+ }
+
+ private async markCalendarChannelAsListFetchScheduledIfPending({
+ calendarChannelId,
+ workspaceId,
+ }: {
+ calendarChannelId: string;
+ workspaceId: string;
+ }): Promise<boolean> {
+ const updateResult = await this.calendarChannelRepository
+ .createQueryBuilder()
+ .update()
+ .set({
+ syncStage: CalendarChannelSyncStage.CALENDAR_EVENT_LIST_FETCH_SCHEDULED,
+ syncStageStartedAt: new Date(),
+ })
+ .where({
+ id: calendarChannelId,
+ workspaceId,
+ isSyncEnabled: true,
+ syncStage: CalendarChannelSyncStage.CALENDAR_EVENT_LIST_FETCH_PENDING,
+ })
+ .returning('id')
+ .execute();
+
+ return updateResult.raw.length > 0;
+ }
+
+ private async findSyncEnabledCalendarChannel({
+ calendarChannelId,
+ workspaceId,
+ }: {
+ calendarChannelId: string;
+ workspaceId: string;
+ }): Promise<CalendarChannelEntity | null> {
+ return this.calendarChannelRepository.findOne({
+ where: { id: calendarChannelId, workspaceId, isSyncEnabled: true },
+ relations: ['connectedAccount'],
+ });
+ }
+
+ private async importFetchedCalendarEvents({
+ calendarChannelId,
+ workspaceId,
+ }: {
+ calendarChannelId: string;
+ workspaceId: string;
+ }): Promise<void> {
+ const calendarEventsToImportCount = await this.cacheStorage.getSetLength(
+ `calendar-events-to-import:${workspaceId}:${calendarChannelId}`,
+ );
+
+ if (calendarEventsToImportCount === 0) {
+ return;
+ }
+
+ if (this.shouldDeferImportToCron(calendarEventsToImportCount)) {
+ this.logger.log(
+ `Deferring ${calendarEventsToImportCount} calendar events to the import cron for calendar channel ${calendarChannelId}`,
+ );
+
+ return;
+ }
+
+ const calendarChannel = await this.findSyncEnabledCalendarChannel({
+ calendarChannelId,
+ workspaceId,
+ });
+
+ if (!isDefined(calendarChannel)) {
+ return;
+ }
+
+ await this.calendarEventsImportService.processCalendarEventsImport(
+ calendarChannel,
+ calendarChannel.connectedAccount,
+ workspaceId,
+ );
+ }
+
+ private shouldDeferImportToCron(
+ calendarEventsToImportCount: number,
+ ): boolean {
+ return (
+ calendarEventsToImportCount >
+ CALENDAR_EVENT_WEBHOOK_SYNC_INLINE_IMPORT_MAX_EVENTS
+ );
+ }
+}
diff --git a/packages/twenty-server/src/modules/connected-account-sync-webhooks/connected-account-sync-webhooks.module.ts b/packages/twenty-server/src/modules/connected-account-sync-webhooks/connected-account-sync-webhooks.module.ts
index 9e5c1f54dce..e411bb5a596 100644
--- a/packages/twenty-server/src/modules/connected-account-sync-webhooks/connected-account-sync-webhooks.module.ts
+++ b/packages/twenty-server/src/modules/connected-account-sync-webhooks/connected-account-sync-webhooks.module.ts
@@ -1,11 +1,16 @@
import { Module } from '@nestjs/common';
import { ConnectedAccountSyncWebhooksController } from 'src/modules/connected-account-sync-webhooks/connected-account-sync-webhooks.controller';
+import { CalendarEventWebhookSyncModule } from 'src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/calendar-event-webhook-sync.module';
import { GoogleWebhookDriverModule } from 'src/modules/connected-account-sync-webhooks/drivers/google/google-webhook-driver.module';
import { MicrosoftWebhookDriverModule } from 'src/modules/connected-account-sync-webhooks/drivers/microsoft/microsoft-webhook-driver.module';
@Module({
- imports: [GoogleWebhookDriverModule, MicrosoftWebhookDriverModule],
+ imports: [
+ GoogleWebhookDriverModule,
+ MicrosoftWebhookDriverModule,
+ CalendarEventWebhookSyncModule,
+ ],
controllers: [ConnectedAccountSyncWebhooksController],
})
export class ConnectedAccountSyncWebhooksModule {}
diff --git a/packages/twenty-server/src/modules/connected-account/webhook-subscription-manager/services/webhook-sync-trigger.service.ts b/packages/twenty-server/src/modules/connected-account/webhook-subscription-manager/services/webhook-sync-trigger.service.ts
index 625a1063e5d..964c87cbf0f 100644
--- a/packages/twenty-server/src/modules/connected-account/webhook-subscription-manager/services/webhook-sync-trigger.service.ts
+++ b/packages/twenty-server/src/modules/connected-account/webhook-subscription-manager/services/webhook-sync-trigger.service.ts
@@ -1,21 +1,19 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
-import {
- CalendarChannelSyncStage,
- MessageChannelSyncStage,
-} from 'twenty-shared/types';
+import { MessageChannelSyncStage } from 'twenty-shared/types';
import { Repository } from 'typeorm';
import { InjectMessageQueue } from 'src/engine/core-modules/message-queue/decorators/message-queue.decorator';
import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/core-modules/message-queue/services/message-queue.service';
-import { CalendarChannelEntity } from 'src/engine/metadata-modules/calendar-channel/entities/calendar-channel.entity';
import { MessageChannelEntity } from 'src/engine/metadata-modules/message-channel/entities/message-channel.entity';
import {
- CalendarEventListFetchJob,
- type CalendarEventListFetchJobData,
-} from 'src/modules/calendar/calendar-event-import-manager/jobs/calendar-event-list-fetch.job';
+ CalendarEventWebhookSyncJob,
+ type CalendarEventWebhookSyncJobData,
+} from 'src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/jobs/calendar-event-webhook-sync.job';
+import { CALENDAR_EVENT_WEBHOOK_SYNC_RETRY_INITIAL_DELAY_MS } from 'src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/constants/calendar-event-webhook-sync-retry-initial-delay-ms.constant';
+import { CALENDAR_EVENT_WEBHOOK_SYNC_RETRY_LIMIT } from 'src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/constants/calendar-event-webhook-sync-retry-limit.constant';
import {
MessagingMessageListFetchJob,
type MessagingMessageListFetchJobData,
@@ -26,12 +24,10 @@ export class WebhookSyncTriggerService {
constructor(
@InjectMessageQueue(MessageQueue.messagingQueue)
private readonly messagingQueueService: MessageQueueService,
- @InjectMessageQueue(MessageQueue.calendarQueue)
- private readonly calendarQueueService: MessageQueueService,
+ @InjectMessageQueue(MessageQueue.connectedAccountSyncWebhookQueue)
+ private readonly connectedAccountSyncWebhookQueueService: MessageQueueService,
@InjectRepository(MessageChannelEntity)
private readonly messageChannelRepository: Repository<MessageChannelEntity>,
- @InjectRepository(CalendarChannelEntity)
- private readonly calendarChannelRepository: Repository<CalendarChannelEntity>,
) {}
async triggerMessagingSync(
@@ -84,45 +80,18 @@ export class WebhookSyncTriggerService {
calendarChannelId: string,
workspaceId: string,
): Promise<void> {
- const updateResult = await this.calendarChannelRepository
- .createQueryBuilder()
- .update()
- .set({
- syncStage: CalendarChannelSyncStage.CALENDAR_EVENT_LIST_FETCH_SCHEDULED,
- syncStageStartedAt: new Date(),
- })
- .where({
- id: calendarChannelId,
- workspaceId,
- isSyncEnabled: true,
- syncStage: CalendarChannelSyncStage.CALENDAR_EVENT_LIST_FETCH_PENDING,
- })
- .returning('id')
- .execute();
-
- if (updateResult.raw.length === 0) {
- return;
- }
-
- try {
- await this.calendarQueueService.add<CalendarEventListFetchJobData>(
- CalendarEventListFetchJob.name,
- { workspaceId, calendarChannelId },
- );
- } catch (error) {
- await this.calendarChannelRepository
- .createQueryBuilder()
- .update()
- .set({
- syncStage: CalendarChannelSyncStage.CALENDAR_EVENT_LIST_FETCH_PENDING,
- })
- .where({
- id: calendarChannelId,
- workspaceId,
- })
- .execute();
-
- throw error;
- }
+ await this.connectedAccountSyncWebhookQueueService.add<CalendarEventWebhookSyncJobData>(
+ CalendarEventWebhookSyncJob.name,
+ { workspaceId, calendarChannelId },
+ {
+ id: `${CalendarEventWebhookSyncJob.name}-${calendarChannelId}`,
+ retryLimit: CALENDAR_EVENT_WEBHOOK_SYNC_RETRY_LIMIT,
+ backoff: {
+ strategy: 'exponential',
+ initialDelayMilliseconds:
+ CALENDAR_EVENT_WEBHOOK_SYNC_RETRY_INITIAL_DELAY_MS,
+ },
+ },
+ );
}
}
diff --git a/packages/twenty-server/test/integration/google/calendar/webhook-sync.integration-spec.ts b/packages/twenty-server/test/integration/google/calendar/webhook-sync.integration-spec.ts
new file mode 100644
index 00000000000..06808843b9f
--- /dev/null
+++ b/packages/twenty-server/test/integration/google/calendar/webhook-sync.integration-spec.ts
@@ -0,0 +1,127 @@
+import { randomUUID } from 'node:crypto';
+
+import {
+ CalendarChannelSyncStage,
+ ConnectedAccountProvider,
+} from 'twenty-shared/types';
+
+import { CALENDAR_EVENT_WEBHOOK_SYNC_INLINE_IMPORT_MAX_EVENTS } from 'src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/constants/calendar-event-webhook-sync-inline-import-max-events.constant';
+
+import { googleCalendarEvent } from 'test/integration/google/mocks/google-calendar-event.util';
+import { setupGoogleMock } from 'test/integration/google/mocks/setup-google-mock.util';
+import { connectMessagingAccount } from 'test/integration/utils/connect-messaging-account.util';
+import { findImportedCalendarEventTitles } from 'test/integration/utils/find-imported-records.util';
+import { queryCalendarChannel } from 'test/integration/utils/query-messaging.util';
+import { runCalendarChannelEventsImport } from 'test/integration/utils/run-calendar-channel-events-import.util';
+import { runCalendarChannelWebhookSync } from 'test/integration/utils/run-calendar-channel-webhook-sync.util';
+
+const HANDLE = '[email protected]';
+
+describe('Google calendar webhook sync (integration)', () => {
+ const google = setupGoogleMock({ handle: HANDLE });
+
+ let channel: Awaited<ReturnType<typeof connectMessagingAccount>>;
+
+ beforeAll(async () => {
+ channel = await connectMessagingAccount({
+ provider: ConnectedAccountProvider.GOOGLE,
+ handle: HANDLE,
+ });
+ }, 60000);
+
+ afterAll(async () => {
+ await channel?.cleanup().catch(() => undefined);
+ });
+
+ it('imports a new event in a single job without a separate import run', async () => {
+ const eventTitle = `Webhook event ${randomUUID()}`;
+
+ google.serveCalendarEvents([googleCalendarEvent({ summary: eventTitle })], {
+ nextSyncToken: `webhook-sync-token-${randomUUID()}`,
+ });
+
+ await runCalendarChannelWebhookSync(channel.calendarChannelId);
+
+ expect(await findImportedCalendarEventTitles([eventTitle])).toEqual([
+ eventTitle,
+ ]);
+
+ const channelState = await queryCalendarChannel(channel);
+
+ expect(channelState.syncStage).toBe(
+ CalendarChannelSyncStage.CALENDAR_EVENT_LIST_FETCH_PENDING,
+ );
+ }, 60000);
+
+ it('applies an update to an already imported event', async () => {
+ const eventExternalId = `google-calendar-event-${randomUUID()}`;
+ const originalTitle = `Webhook event ${randomUUID()}`;
+ const updatedTitle = `Webhook event updated ${randomUUID()}`;
+
+ google.serveCalendarEvents(
+ [googleCalendarEvent({ id: eventExternalId, summary: originalTitle })],
+ { nextSyncToken: `webhook-sync-token-${randomUUID()}` },
+ );
+
+ await runCalendarChannelWebhookSync(channel.calendarChannelId);
+
+ expect(await findImportedCalendarEventTitles([originalTitle])).toEqual([
+ originalTitle,
+ ]);
+
+ google.serveCalendarEvents(
+ [googleCalendarEvent({ id: eventExternalId, summary: updatedTitle })],
+ { nextSyncToken: `webhook-sync-token-${randomUUID()}` },
+ );
+
+ await runCalendarChannelWebhookSync(channel.calendarChannelId);
+
+ expect(await findImportedCalendarEventTitles([updatedTitle])).toEqual([
+ updatedTitle,
+ ]);
+ expect(await findImportedCalendarEventTitles([originalTitle])).toEqual([]);
+ }, 60000);
+
+ it('leaves a delta larger than the inline cap to the import cron', async () => {
+ const eventTitles = Array.from(
+ { length: CALENDAR_EVENT_WEBHOOK_SYNC_INLINE_IMPORT_MAX_EVENTS + 1 },
+ () => `Webhook bulk event ${randomUUID()}`,
+ );
+
+ google.serveCalendarEvents(
+ eventTitles.map((summary) => googleCalendarEvent({ summary })),
+ { nextSyncToken: `webhook-sync-token-${randomUUID()}` },
+ );
+
+ await runCalendarChannelWebhookSync(channel.calendarChannelId);
+
+ expect(await findImportedCalendarEventTitles(eventTitles)).toEqual([]);
+
+ const channelState = await queryCalendarChannel(channel);
+
+ expect(channelState.syncStage).toBe(
+ CalendarChannelSyncStage.CALENDAR_EVENTS_IMPORT_PENDING,
+ );
+
+ await runCalendarChannelEventsImport(channel.calendarChannelId);
+
+ expect(await findImportedCalendarEventTitles(eventTitles)).toEqual(
+ [...eventTitles].sort(),
+ );
+ }, 120000);
+
+ it('does not sync when the channel is already being synced', async () => {
+ const eventTitle = `Webhook event ${randomUUID()}`;
+
+ google.serveCalendarEvents([googleCalendarEvent({ summary: eventTitle })], {
+ nextSyncToken: `webhook-sync-token-${randomUUID()}`,
+ });
+
+ await runCalendarChannelWebhookSync(
+ channel.calendarChannelId,
+ CalendarChannelSyncStage.CALENDAR_EVENT_LIST_FETCH_ONGOING,
+ );
+
+ expect(await findImportedCalendarEventTitles([eventTitle])).toEqual([]);
+ }, 60000);
+});
diff --git a/packages/twenty-server/test/integration/microsoft/calendar/webhook-sync.integration-spec.ts b/packages/twenty-server/test/integration/microsoft/calendar/webhook-sync.integration-spec.ts
new file mode 100644
index 00000000000..20b30d0ea50
--- /dev/null
+++ b/packages/twenty-server/test/integration/microsoft/calendar/webhook-sync.integration-spec.ts
@@ -0,0 +1,195 @@
+import { randomUUID } from 'node:crypto';
+
+import {
+ CalendarChannelSyncStage,
+ ConnectedAccountProvider,
+} from 'twenty-shared/types';
+
+import { CALENDAR_EVENT_WEBHOOK_SYNC_INLINE_IMPORT_MAX_EVENTS } from 'src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/constants/calendar-event-webhook-sync-inline-import-max-events.constant';
+
+import { microsoftCalendarEvent } from 'test/integration/microsoft/mocks/microsoft-calendar-event.util';
+import { setupMicrosoftMock } from 'test/integration/microsoft/mocks/setup-microsoft-mock.util';
+import { connectMessagingAccount } from 'test/integration/utils/connect-messaging-account.util';
+import { findCalendarEventAssociationChannelIds } from 'test/integration/utils/find-calendar-event-association-channel-ids.util';
+import { findImportedCalendarEventTitles } from 'test/integration/utils/find-imported-records.util';
+import { queryCalendarChannel } from 'test/integration/utils/query-messaging.util';
+import { runCalendarChannelEventsImport } from 'test/integration/utils/run-calendar-channel-events-import.util';
+import { runCalendarChannelWebhookSync } from 'test/integration/utils/run-calendar-channel-webhook-sync.util';
+
+const HANDLE = '[email protected]';
+
+const deltaToken = () => `webhook-sync-delta-token-${randomUUID()}`;
+
+describe('Microsoft calendar webhook sync (integration)', () => {
+ const microsoft = setupMicrosoftMock({ handle: HANDLE });
+
+ let channel: Awaited<ReturnType<typeof connectMessagingAccount>>;
+
+ beforeAll(async () => {
+ channel = await connectMessagingAccount({
+ provider: ConnectedAccountProvider.MICROSOFT,
+ handle: HANDLE,
+ });
+ }, 60000);
+
+ afterAll(async () => {
+ await channel?.cleanup().catch(() => undefined);
+ });
+
+ it('imports a new event in a single job without a separate import run', async () => {
+ const eventTitle = `Webhook event ${randomUUID()}`;
+
+ microsoft.serveCalendarEvents(
+ [microsoftCalendarEvent({ subject: eventTitle })],
+ { deltaToken: deltaToken() },
+ );
+
+ await runCalendarChannelWebhookSync(channel.calendarChannelId);
+
+ expect(await findImportedCalendarEventTitles([eventTitle])).toEqual([
+ eventTitle,
+ ]);
+
+ const channelState = await queryCalendarChannel(channel);
+
+ expect(channelState.syncStage).toBe(
+ CalendarChannelSyncStage.CALENDAR_EVENT_LIST_FETCH_PENDING,
+ );
+ }, 60000);
+
+ it('applies an update to an already imported event', async () => {
+ const eventExternalId = `microsoft-calendar-event-${randomUUID()}`;
+ const originalTitle = `Webhook event ${randomUUID()}`;
+ const updatedTitle = `Webhook event updated ${randomUUID()}`;
+
+ microsoft.serveCalendarEvents(
+ [microsoftCalendarEvent({ id: eventExternalId, subject: originalTitle })],
+ { deltaToken: deltaToken() },
+ );
+
+ await runCalendarChannelWebhookSync(channel.calendarChannelId);
+
+ expect(await findImportedCalendarEventTitles([originalTitle])).toEqual([
+ originalTitle,
+ ]);
+
+ microsoft.serveCalendarEvents(
+ [microsoftCalendarEvent({ id: eventExternalId, subject: updatedTitle })],
+ { deltaToken: deltaToken() },
+ );
+
+ await runCalendarChannelWebhookSync(channel.calendarChannelId);
+
+ expect(await findImportedCalendarEventTitles([updatedTitle])).toEqual([
+ updatedTitle,
+ ]);
+ expect(await findImportedCalendarEventTitles([originalTitle])).toEqual([]);
+ }, 60000);
+
+ it('deletes the calendar event once the delta reports it removed', async () => {
+ const eventExternalId = `microsoft-calendar-event-${randomUUID()}`;
+ const eventTitle = `Webhook removable event ${randomUUID()}`;
+
+ microsoft.serveCalendarEvents(
+ [microsoftCalendarEvent({ id: eventExternalId, subject: eventTitle })],
+ { deltaToken: deltaToken() },
+ );
+
+ await runCalendarChannelWebhookSync(channel.calendarChannelId);
+
+ expect(
+ await findCalendarEventAssociationChannelIds(eventExternalId),
+ ).toEqual([channel.calendarChannelId]);
+
+ microsoft.serveCalendarEvents([], {
+ deltaToken: deltaToken(),
+ removedEventIds: [eventExternalId],
+ });
+
+ await runCalendarChannelWebhookSync(channel.calendarChannelId);
+
+ expect(
+ await findCalendarEventAssociationChannelIds(eventExternalId),
+ ).toEqual([]);
+ expect(await findImportedCalendarEventTitles([eventTitle])).toEqual([]);
+ }, 60000);
+
+ it('keeps the events that the delta did not report removed', async () => {
+ const removedExternalId = `microsoft-calendar-event-${randomUUID()}`;
+ const removedTitle = `Webhook removed ${randomUUID()}`;
+ const survivingTitle = `Webhook surviving ${randomUUID()}`;
+
+ microsoft.serveCalendarEvents(
+ [
+ microsoftCalendarEvent({
+ id: removedExternalId,
+ subject: removedTitle,
+ }),
+ microsoftCalendarEvent({ subject: survivingTitle }),
+ ],
+ { deltaToken: deltaToken() },
+ );
+
+ await runCalendarChannelWebhookSync(channel.calendarChannelId);
+
+ expect(
+ await findImportedCalendarEventTitles([removedTitle, survivingTitle]),
+ ).toEqual([removedTitle, survivingTitle].sort());
+
+ microsoft.serveCalendarEvents([], {
+ deltaToken: deltaToken(),
+ removedEventIds: [removedExternalId],
+ });
+
+ await runCalendarChannelWebhookSync(channel.calendarChannelId);
+
+ expect(await findImportedCalendarEventTitles([removedTitle])).toEqual([]);
+ expect(await findImportedCalendarEventTitles([survivingTitle])).toEqual([
+ survivingTitle,
+ ]);
+ }, 60000);
+
+ it('leaves a delta larger than the inline cap to the import cron', async () => {
+ const eventTitles = Array.from(
+ { length: CALENDAR_EVENT_WEBHOOK_SYNC_INLINE_IMPORT_MAX_EVENTS + 1 },
+ () => `Webhook bulk event ${randomUUID()}`,
+ );
+
+ microsoft.serveCalendarEvents(
+ eventTitles.map((subject) => microsoftCalendarEvent({ subject })),
+ { deltaToken: deltaToken() },
+ );
+
+ await runCalendarChannelWebhookSync(channel.calendarChannelId);
+
+ expect(await findImportedCalendarEventTitles(eventTitles)).toEqual([]);
+
+ const channelState = await queryCalendarChannel(channel);
+
+ expect(channelState.syncStage).toBe(
+ CalendarChannelSyncStage.CALENDAR_EVENTS_IMPORT_PENDING,
+ );
+
+ await runCalendarChannelEventsImport(channel.calendarChannelId);
+
+ expect(await findImportedCalendarEventTitles(eventTitles)).toEqual(
+ [...eventTitles].sort(),
+ );
+ }, 120000);
+
+ it('does not sync when the channel is already being synced', async () => {
+ const eventTitle = `Webhook event ${randomUUID()}`;
+
+ microsoft.serveCalendarEvents(
+ [microsoftCalendarEvent({ subject: eventTitle })],
+ { deltaToken: deltaToken() },
+ );
+
+ await runCalendarChannelWebhookSync(
+ channel.calendarChannelId,
+ CalendarChannelSyncStage.CALENDAR_EVENT_LIST_FETCH_ONGOING,
+ );
+
+ expect(await findImportedCalendarEventTitles([eventTitle])).toEqual([]);
+ }, 60000);
+});
diff --git a/packages/twenty-server/test/integration/microsoft/mocks/microsoft-calendar-events-handlers.util.ts b/packages/twenty-server/test/integration/microsoft/mocks/microsoft-calendar-events-handlers.util.ts
index b017579ee71..fee0a4f43bb 100644
--- a/packages/twenty-server/test/integration/microsoft/mocks/microsoft-calendar-events-handlers.util.ts
+++ b/packages/twenty-server/test/integration/microsoft/mocks/microsoft-calendar-events-handlers.util.ts
@@ -6,10 +6,17 @@ import { type MswHandler } from 'test/integration/utils/http-mock.util';
export const microsoftCalendarEventsHandlers = (
events: Event[],
deltaToken: string,
+ removedEventIds: string[] = [],
): MswHandler[] => [
http.get('*/me/calendar/events/delta', () =>
HttpResponse.json({
- value: events.map((event) => ({ id: event.id })),
+ value: [
+ ...events.map((event) => ({ id: event.id })),
+ ...removedEventIds.map((id) => ({
+ id,
+ '@removed': { reason: 'deleted' },
+ })),
+ ],
'@odata.deltaLink': `https://graph.microsoft.com/beta/me/calendar/events/delta?$deltatoken=${deltaToken}`,
}),
),
diff --git a/packages/twenty-server/test/integration/microsoft/mocks/setup-microsoft-mock.util.ts b/packages/twenty-server/test/integration/microsoft/mocks/setup-microsoft-mock.util.ts
index a14b932044f..34b66dde085 100644
--- a/packages/twenty-server/test/integration/microsoft/mocks/setup-microsoft-mock.util.ts
+++ b/packages/twenty-server/test/integration/microsoft/mocks/setup-microsoft-mock.util.ts
@@ -32,7 +32,7 @@ export type MicrosoftMock = {
createdCalendarEvents: Event[];
serveCalendarEvents: (
events: Event[],
- options?: { deltaToken?: string },
+ options?: { deltaToken?: string; removedEventIds?: string[] },
) => void;
failSubscriptionRenewal: () => void;
failMessageDelta: (failure: MicrosoftGraphFailure) => void;
@@ -154,8 +154,11 @@ export const setupMicrosoftMock = ({
createdCalendarEvents,
serveCalendarEvents: (
events,
- { deltaToken = 'mock-calendar-delta-token' } = {},
- ) => httpMock.use(...microsoftCalendarEventsHandlers(events, deltaToken)),
+ { deltaToken = 'mock-calendar-delta-token', removedEventIds = [] } = {},
+ ) =>
+ httpMock.use(
+ ...microsoftCalendarEventsHandlers(events, deltaToken, removedEventIds),
+ ),
failSubscriptionRenewal: () =>
httpMock.use(
...microsoftWebhookSubscriptionHandlers(subscriptionStore, {
diff --git a/packages/twenty-server/test/integration/utils/find-calendar-event-association-channel-ids.util.ts b/packages/twenty-server/test/integration/utils/find-calendar-event-association-channel-ids.util.ts
new file mode 100644
index 00000000000..456b71e0d1e
--- /dev/null
+++ b/packages/twenty-server/test/integration/utils/find-calendar-event-association-channel-ids.util.ts
@@ -0,0 +1,16 @@
+import { findRecordNodesByFilter } from 'test/integration/utils/find-records-by-filter.util';
+
+export const findCalendarEventAssociationChannelIds = async (
+ eventExternalId: string,
+): Promise<string[]> => {
+ const associations = await findRecordNodesByFilter<{
+ calendarChannelId: string;
+ }>(
+ 'calendarChannelEventAssociation',
+ 'calendarChannelEventAssociations',
+ 'calendarChannelId',
+ { eventExternalId: { eq: eventExternalId } },
+ );
+
+ return associations.map(({ calendarChannelId }) => calendarChannelId).sort();
+};
diff --git a/packages/twenty-server/test/integration/utils/run-calendar-channel-webhook-sync.util.ts b/packages/twenty-server/test/integration/utils/run-calendar-channel-webhook-sync.util.ts
new file mode 100644
index 00000000000..d715077909b
--- /dev/null
+++ b/packages/twenty-server/test/integration/utils/run-calendar-channel-webhook-sync.util.ts
@@ -0,0 +1,25 @@
+import { CalendarChannelSyncStage } from 'twenty-shared/types';
+
+import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';
+import { CalendarChannelEntity } from 'src/engine/metadata-modules/calendar-channel/entities/calendar-channel.entity';
+import { CalendarEventWebhookSyncJob } from 'src/modules/connected-account-sync-webhooks/calendar-event-webhook-sync/jobs/calendar-event-webhook-sync.job';
+
+import { enqueueJobAndDrain } from 'test/integration/utils/enqueue-job-and-drain.util';
+import { scheduleChannelStage } from 'test/integration/utils/schedule-channel-stage.util';
+
+export const runCalendarChannelWebhookSync = async (
+ calendarChannelId: string,
+ startingStage: CalendarChannelSyncStage = CalendarChannelSyncStage.CALENDAR_EVENT_LIST_FETCH_PENDING,
+): Promise<void> => {
+ const workspaceId = await scheduleChannelStage(
+ CalendarChannelEntity,
+ calendarChannelId,
+ startingStage,
+ );
+
+ await enqueueJobAndDrain(
+ MessageQueue.connectedAccountSyncWebhookQueue,
+ CalendarEventWebhookSyncJob.name,
+ { workspaceId, calendarChannelId },
+ );
+};