From 92b553ffd87c780df02d0b24f5a554616c2975d4 Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Wed, 3 Apr 2024 14:21:38 +0200 Subject: [PATCH 01/33] renaming --- .../src/engine/integrations/message-queue/jobs.module.ts | 4 ++-- ...ll-sync.command.ts => google-calendar-sync.command.ts} | 6 +++--- .../commands/workspace-calendar-sync-commands.module.ts | 4 ++-- .../calendar/jobs/google-calendar-full-sync.job.ts | 4 ++-- ...full-sync.module.ts => google-calendar-sync.module.ts} | 8 ++++---- ...ll-sync.service.ts => google-calendar-sync.service.ts} | 6 +++--- 6 files changed, 16 insertions(+), 16 deletions(-) rename packages/twenty-server/src/modules/calendar/commands/{google-calendar-full-sync.command.ts => google-calendar-sync.command.ts} (93%) rename packages/twenty-server/src/modules/calendar/services/{google-calendar-full-sync.module.ts => google-calendar-sync.module.ts} (89%) rename packages/twenty-server/src/modules/calendar/services/{google-calendar-full-sync.service.ts => google-calendar-sync.service.ts} (98%) diff --git a/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts b/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts index 821cc082f092..1175998ceb57 100644 --- a/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts +++ b/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts @@ -35,7 +35,7 @@ import { CalendarEventCleanerModule } from 'src/modules/calendar/services/calend import { RecordPositionBackfillJob } from 'src/engine/api/graphql/workspace-query-runner/jobs/record-position-backfill.job'; import { RecordPositionBackfillModule } from 'src/engine/api/graphql/workspace-query-runner/services/record-position-backfill-module'; import { DeleteConnectedAccountAssociatedCalendarDataJob } from 'src/modules/messaging/jobs/delete-connected-account-associated-calendar-data.job'; -import { GoogleCalendarFullSyncModule } from 'src/modules/calendar/services/google-calendar-full-sync.module'; +import { GoogleCalendarSyncModule } from 'src/modules/calendar/services/google-calendar-sync.module'; import { GoogleAPIRefreshAccessTokenModule } from 'src/modules/connected-account/services/google-api-refresh-access-token/google-api-refresh-access-token.module'; import { GmailFullSyncModule } from 'src/modules/messaging/services/gmail-full-sync/gmail-full-sync.module'; import { GmailPartialSyncModule } from 'src/modules/messaging/services/gmail-partial-sync/gmail-partial-sync.module'; @@ -63,7 +63,7 @@ import { GmailPartialSyncV2Module } from 'src/modules/messaging/services/gmail-p DataSeedDemoWorkspaceModule, EnvironmentModule, HttpModule, - GoogleCalendarFullSyncModule, + GoogleCalendarSyncModule, ObjectMetadataModule, StripeModule, ThreadCleanerModule, diff --git a/packages/twenty-server/src/modules/calendar/commands/google-calendar-full-sync.command.ts b/packages/twenty-server/src/modules/calendar/commands/google-calendar-sync.command.ts similarity index 93% rename from packages/twenty-server/src/modules/calendar/commands/google-calendar-full-sync.command.ts rename to packages/twenty-server/src/modules/calendar/commands/google-calendar-sync.command.ts index d3eb8df0a5a6..20036bb5dde5 100644 --- a/packages/twenty-server/src/modules/calendar/commands/google-calendar-full-sync.command.ts +++ b/packages/twenty-server/src/modules/calendar/commands/google-calendar-sync.command.ts @@ -19,11 +19,11 @@ interface GoogleCalendarFullSyncOptions { } @Command({ - name: 'workspace:google-calendar-full-sync', + name: 'workspace:google-calendar-sync', description: - 'Start google calendar full-sync for all workspaceMembers in a workspace.', + 'Start google calendar sync for all workspaceMembers in a workspace.', }) -export class GoogleCalendarFullSyncCommand extends CommandRunner { +export class GoogleCalendarSyncCommand extends CommandRunner { constructor( @Inject(MessageQueue.messagingQueue) private readonly messageQueueService: MessageQueueService, diff --git a/packages/twenty-server/src/modules/calendar/commands/workspace-calendar-sync-commands.module.ts b/packages/twenty-server/src/modules/calendar/commands/workspace-calendar-sync-commands.module.ts index 18e1b88691ca..42bf05fb22ba 100644 --- a/packages/twenty-server/src/modules/calendar/commands/workspace-calendar-sync-commands.module.ts +++ b/packages/twenty-server/src/modules/calendar/commands/workspace-calendar-sync-commands.module.ts @@ -1,6 +1,6 @@ import { Module } from '@nestjs/common'; -import { GoogleCalendarFullSyncCommand } from 'src/modules/calendar/commands/google-calendar-full-sync.command'; +import { GoogleCalendarSyncCommand } from 'src/modules/calendar/commands/google-calendar-sync.command'; import { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.module'; import { ConnectedAccountObjectMetadata } from 'src/modules/connected-account/standard-objects/connected-account.object-metadata'; @@ -8,6 +8,6 @@ import { ConnectedAccountObjectMetadata } from 'src/modules/connected-account/st imports: [ ObjectMetadataRepositoryModule.forFeature([ConnectedAccountObjectMetadata]), ], - providers: [GoogleCalendarFullSyncCommand], + providers: [GoogleCalendarSyncCommand], }) export class WorkspaceCalendarSyncCommandsModule {} diff --git a/packages/twenty-server/src/modules/calendar/jobs/google-calendar-full-sync.job.ts b/packages/twenty-server/src/modules/calendar/jobs/google-calendar-full-sync.job.ts index 6408e72b6dba..a1e9f5682a04 100644 --- a/packages/twenty-server/src/modules/calendar/jobs/google-calendar-full-sync.job.ts +++ b/packages/twenty-server/src/modules/calendar/jobs/google-calendar-full-sync.job.ts @@ -3,7 +3,7 @@ import { Injectable, Logger } from '@nestjs/common'; import { MessageQueueJob } from 'src/engine/integrations/message-queue/interfaces/message-queue-job.interface'; import { GoogleAPIRefreshAccessTokenService } from 'src/modules/connected-account/services/google-api-refresh-access-token/google-api-refresh-access-token.service'; -import { GoogleCalendarFullSyncService } from 'src/modules/calendar/services/google-calendar-full-sync.service'; +import { GoogleCalendarSyncService } from 'src/modules/calendar/services/google-calendar-sync.service'; export type GoogleCalendarFullSyncJobData = { workspaceId: string; @@ -19,7 +19,7 @@ export class GoogleCalendarFullSyncJob constructor( private readonly googleAPIsRefreshAccessTokenService: GoogleAPIRefreshAccessTokenService, - private readonly googleCalendarFullSyncService: GoogleCalendarFullSyncService, + private readonly googleCalendarFullSyncService: GoogleCalendarSyncService, ) {} async handle(data: GoogleCalendarFullSyncJobData): Promise { diff --git a/packages/twenty-server/src/modules/calendar/services/google-calendar-full-sync.module.ts b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.module.ts similarity index 89% rename from packages/twenty-server/src/modules/calendar/services/google-calendar-full-sync.module.ts rename to packages/twenty-server/src/modules/calendar/services/google-calendar-sync.module.ts index 6b65263ed9af..2294ca5fe903 100644 --- a/packages/twenty-server/src/modules/calendar/services/google-calendar-full-sync.module.ts +++ b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.module.ts @@ -5,7 +5,7 @@ import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature- import { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.module'; import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module'; import { CalendarEventAttendeeModule } from 'src/modules/calendar/services/calendar-event-attendee/calendar-event-attendee.module'; -import { GoogleCalendarFullSyncService } from 'src/modules/calendar/services/google-calendar-full-sync.service'; +import { GoogleCalendarSyncService } from 'src/modules/calendar/services/google-calendar-sync.service'; import { CalendarProvidersModule } from 'src/modules/calendar/services/providers/calendar-providers.module'; import { CalendarChannelEventAssociationObjectMetadata } from 'src/modules/calendar/standard-objects/calendar-channel-event-association.object-metadata'; import { CalendarChannelObjectMetadata } from 'src/modules/calendar/standard-objects/calendar-channel.object-metadata'; @@ -33,7 +33,7 @@ import { WorkspaceMemberObjectMetadata } from 'src/modules/workspace-member/stan TypeOrmModule.forFeature([FeatureFlagEntity], 'core'), WorkspaceDataSourceModule, ], - providers: [GoogleCalendarFullSyncService], - exports: [GoogleCalendarFullSyncService], + providers: [GoogleCalendarSyncService], + exports: [GoogleCalendarSyncService], }) -export class GoogleCalendarFullSyncModule {} +export class GoogleCalendarSyncModule {} diff --git a/packages/twenty-server/src/modules/calendar/services/google-calendar-full-sync.service.ts b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts similarity index 98% rename from packages/twenty-server/src/modules/calendar/services/google-calendar-full-sync.service.ts rename to packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts index 1d7313726597..a7aa0a3a46c8 100644 --- a/packages/twenty-server/src/modules/calendar/services/google-calendar-full-sync.service.ts +++ b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts @@ -31,8 +31,8 @@ import { BlocklistObjectMetadata } from 'src/modules/connected-account/standard- import { CalendarEventAttendeeService } from 'src/modules/calendar/services/calendar-event-attendee/calendar-event-attendee.service'; @Injectable() -export class GoogleCalendarFullSyncService { - private readonly logger = new Logger(GoogleCalendarFullSyncService.name); +export class GoogleCalendarSyncService { + private readonly logger = new Logger(GoogleCalendarSyncService.name); constructor( private readonly googleCalendarClientProvider: GoogleCalendarClientProvider, @@ -349,7 +349,7 @@ export class GoogleCalendarFullSyncService { if (nextPageToken) { await this.messageQueueService.add( - GoogleCalendarFullSyncService.name, + GoogleCalendarSyncService.name, { workspaceId, connectedAccountId, From 6d353dccc4330cebf469fb163ee5e3f9540125ea Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Wed, 3 Apr 2024 14:24:09 +0200 Subject: [PATCH 02/33] finish renaming --- .../auth/services/google-apis.service.ts | 20 +++++++------------ .../integrations/message-queue/jobs.module.ts | 6 +++--- .../commands/google-calendar-sync.command.ts | 12 +++++------ .../jobs/google-calendar-full-sync.job.ts | 14 ++++++------- .../services/google-calendar-sync.service.ts | 6 +++--- 5 files changed, 26 insertions(+), 32 deletions(-) diff --git a/packages/twenty-server/src/engine/core-modules/auth/services/google-apis.service.ts b/packages/twenty-server/src/engine/core-modules/auth/services/google-apis.service.ts index 78f7d28755ac..1b3d490faa1f 100644 --- a/packages/twenty-server/src/engine/core-modules/auth/services/google-apis.service.ts +++ b/packages/twenty-server/src/engine/core-modules/auth/services/google-apis.service.ts @@ -14,8 +14,8 @@ import { GmailFullSyncJobData, } from 'src/modules/messaging/jobs/gmail-full-sync.job'; import { - GoogleCalendarFullSyncJob, - GoogleCalendarFullSyncJobData, + GoogleCalendarSyncJob, + GoogleCalendarSyncJobData, } from 'src/modules/calendar/jobs/google-calendar-full-sync.job'; import { EnvironmentService } from 'src/engine/integrations/environment/environment.service'; import { @@ -139,10 +139,7 @@ export class GoogleAPIsService { this.environmentService.get('CALENDAR_PROVIDER_GOOGLE_ENABLED') && IsCalendarEnabled ) { - await this.enqueueGoogleCalendarFullSyncJob( - workspaceId, - connectedAccountId, - ); + await this.enqueueGoogleCalendarSyncJob(workspaceId, connectedAccountId); } return; @@ -174,10 +171,7 @@ export class GoogleAPIsService { } if (this.environmentService.get('CALENDAR_PROVIDER_GOOGLE_ENABLED')) { - await this.enqueueGoogleCalendarFullSyncJob( - workspaceId, - connectedAccountId, - ); + await this.enqueueGoogleCalendarSyncJob(workspaceId, connectedAccountId); } return; @@ -215,12 +209,12 @@ export class GoogleAPIsService { } } - async enqueueGoogleCalendarFullSyncJob( + async enqueueGoogleCalendarSyncJob( workspaceId: string, connectedAccountId: string, ) { - await this.calendarQueueService.add( - GoogleCalendarFullSyncJob.name, + await this.calendarQueueService.add( + GoogleCalendarSyncJob.name, { workspaceId, connectedAccountId, diff --git a/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts b/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts index 1175998ceb57..fbf3044fbd13 100644 --- a/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts +++ b/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts @@ -30,7 +30,7 @@ import { StripeModule } from 'src/engine/core-modules/billing/stripe/stripe.modu import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity'; import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-flag.entity'; import { DataSourceEntity } from 'src/engine/metadata-modules/data-source/data-source.entity'; -import { GoogleCalendarFullSyncJob } from 'src/modules/calendar/jobs/google-calendar-full-sync.job'; +import { GoogleCalendarSyncJob } from 'src/modules/calendar/jobs/google-calendar-full-sync.job'; import { CalendarEventCleanerModule } from 'src/modules/calendar/services/calendar-event-cleaner/calendar-event-cleaner.module'; import { RecordPositionBackfillJob } from 'src/engine/api/graphql/workspace-query-runner/jobs/record-position-backfill.job'; import { RecordPositionBackfillModule } from 'src/engine/api/graphql/workspace-query-runner/services/record-position-backfill-module'; @@ -99,8 +99,8 @@ import { GmailPartialSyncV2Module } from 'src/modules/messaging/services/gmail-p useClass: GmailPartialSyncJob, }, { - provide: GoogleCalendarFullSyncJob.name, - useClass: GoogleCalendarFullSyncJob, + provide: GoogleCalendarSyncJob.name, + useClass: GoogleCalendarSyncJob, }, { provide: CallWebhookJobsJob.name, diff --git a/packages/twenty-server/src/modules/calendar/commands/google-calendar-sync.command.ts b/packages/twenty-server/src/modules/calendar/commands/google-calendar-sync.command.ts index 20036bb5dde5..838b71e1030e 100644 --- a/packages/twenty-server/src/modules/calendar/commands/google-calendar-sync.command.ts +++ b/packages/twenty-server/src/modules/calendar/commands/google-calendar-sync.command.ts @@ -6,15 +6,15 @@ import { MessageQueue } from 'src/engine/integrations/message-queue/message-queu import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; import { ConnectedAccountRepository } from 'src/modules/connected-account/repositories/connected-account.repository'; import { - GoogleCalendarFullSyncJobData, - GoogleCalendarFullSyncJob, + GoogleCalendarSyncJobData, + GoogleCalendarSyncJob, } from 'src/modules/calendar/jobs/google-calendar-full-sync.job'; import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator'; import { ConnectedAccountObjectMetadata } from 'src/modules/connected-account/standard-objects/connected-account.object-metadata'; import { CalendarChannelRepository } from 'src/modules/calendar/repositories/calendar-channel.repository'; import { CalendarChannelObjectMetadata } from 'src/modules/calendar/standard-objects/calendar-channel.object-metadata'; -interface GoogleCalendarFullSyncOptions { +interface GoogleCalendarSyncOptions { workspaceId: string; } @@ -37,7 +37,7 @@ export class GoogleCalendarSyncCommand extends CommandRunner { async run( _passedParam: string[], - options: GoogleCalendarFullSyncOptions, + options: GoogleCalendarSyncOptions, ): Promise { await this.fetchWorkspaceCalendars(options.workspaceId); @@ -68,8 +68,8 @@ export class GoogleCalendarSyncCommand extends CommandRunner { continue; } - await this.messageQueueService.add( - GoogleCalendarFullSyncJob.name, + await this.messageQueueService.add( + GoogleCalendarSyncJob.name, { workspaceId, connectedAccountId: connectedAccount.id, diff --git a/packages/twenty-server/src/modules/calendar/jobs/google-calendar-full-sync.job.ts b/packages/twenty-server/src/modules/calendar/jobs/google-calendar-full-sync.job.ts index a1e9f5682a04..50924ae7e5ca 100644 --- a/packages/twenty-server/src/modules/calendar/jobs/google-calendar-full-sync.job.ts +++ b/packages/twenty-server/src/modules/calendar/jobs/google-calendar-full-sync.job.ts @@ -5,24 +5,24 @@ import { MessageQueueJob } from 'src/engine/integrations/message-queue/interface import { GoogleAPIRefreshAccessTokenService } from 'src/modules/connected-account/services/google-api-refresh-access-token/google-api-refresh-access-token.service'; import { GoogleCalendarSyncService } from 'src/modules/calendar/services/google-calendar-sync.service'; -export type GoogleCalendarFullSyncJobData = { +export type GoogleCalendarSyncJobData = { workspaceId: string; connectedAccountId: string; nextPageToken?: string; }; @Injectable() -export class GoogleCalendarFullSyncJob - implements MessageQueueJob +export class GoogleCalendarSyncJob + implements MessageQueueJob { - private readonly logger = new Logger(GoogleCalendarFullSyncJob.name); + private readonly logger = new Logger(GoogleCalendarSyncJob.name); constructor( private readonly googleAPIsRefreshAccessTokenService: GoogleAPIRefreshAccessTokenService, - private readonly googleCalendarFullSyncService: GoogleCalendarSyncService, + private readonly googleCalendarSyncService: GoogleCalendarSyncService, ) {} - async handle(data: GoogleCalendarFullSyncJobData): Promise { + async handle(data: GoogleCalendarSyncJobData): Promise { this.logger.log( `google calendar full-sync for workspace ${ data.workspaceId @@ -44,7 +44,7 @@ export class GoogleCalendarFullSyncJob return; } - await this.googleCalendarFullSyncService.startGoogleCalendarFullSync( + await this.googleCalendarSyncService.startGoogleCalendarSync( data.workspaceId, data.connectedAccountId, data.nextPageToken, diff --git a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts index a7aa0a3a46c8..431aeb0ec6b4 100644 --- a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts +++ b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts @@ -19,7 +19,7 @@ import { MessageQueueService } from 'src/engine/integrations/message-queue/servi import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service'; import { CalendarEventRepository } from 'src/modules/calendar/repositories/calendar-event.repository'; import { formatGoogleCalendarEvent } from 'src/modules/calendar/utils/format-google-calendar-event.util'; -import { GoogleCalendarFullSyncJobData } from 'src/modules/calendar/jobs/google-calendar-full-sync.job'; +import { GoogleCalendarSyncJobData } from 'src/modules/calendar/jobs/google-calendar-full-sync.job'; import { CalendarEventAttendeeRepository } from 'src/modules/calendar/repositories/calendar-event-attendee.repository'; import { ConnectedAccountObjectMetadata } from 'src/modules/connected-account/standard-objects/connected-account.object-metadata'; import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator'; @@ -59,7 +59,7 @@ export class GoogleCalendarSyncService { private readonly calendarEventAttendeesService: CalendarEventAttendeeService, ) {} - public async startGoogleCalendarFullSync( + public async startGoogleCalendarSync( workspaceId: string, connectedAccountId: string, pageToken?: string, @@ -348,7 +348,7 @@ export class GoogleCalendarSyncService { ); if (nextPageToken) { - await this.messageQueueService.add( + await this.messageQueueService.add( GoogleCalendarSyncService.name, { workspaceId, From 7d4a1491df66bb7d6753a04859e877f103cc889b Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Wed, 3 Apr 2024 14:29:33 +0200 Subject: [PATCH 03/33] add syncToken parameter --- .../modules/calendar/services/google-calendar-sync.service.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts index 431aeb0ec6b4..4ff04ee393c1 100644 --- a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts +++ b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts @@ -62,6 +62,7 @@ export class GoogleCalendarSyncService { public async startGoogleCalendarSync( workspaceId: string, connectedAccountId: string, + syncToken?: string, pageToken?: string, ): Promise { const connectedAccount = await this.connectedAccountRepository.getById( @@ -168,8 +169,6 @@ export class GoogleCalendarSyncService { }ms.`, ); - // TODO: In V2, we will also import deleted events by doing batch GET queries on the canceled events - // The canceled events start and end are not accessible in the list query const formattedEvents = events .filter((event) => event.status !== 'cancelled') .map((event) => formatGoogleCalendarEvent(event)); From 0dd2d81d7ffc01e0b93a261afd1e10bf6db6c780 Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Wed, 3 Apr 2024 16:07:04 +0200 Subject: [PATCH 04/33] modify calendar sync to not call a job inside a job --- .../services/google-calendar-sync.service.ts | 63 +++++++++---------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts index 4ff04ee393c1..260809fec4f7 100644 --- a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts +++ b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts @@ -3,6 +3,7 @@ import { InjectRepository } from '@nestjs/typeorm'; import { EventEmitter2 } from '@nestjs/event-emitter'; import { Repository } from 'typeorm'; +import { calendar_v3 } from 'googleapis'; import { ConnectedAccountRepository } from 'src/modules/connected-account/repositories/connected-account.repository'; import { BlocklistRepository } from 'src/modules/connected-account/repositories/blocklist.repository'; @@ -19,7 +20,6 @@ import { MessageQueueService } from 'src/engine/integrations/message-queue/servi import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service'; import { CalendarEventRepository } from 'src/modules/calendar/repositories/calendar-event.repository'; import { formatGoogleCalendarEvent } from 'src/modules/calendar/utils/format-google-calendar-event.util'; -import { GoogleCalendarSyncJobData } from 'src/modules/calendar/jobs/google-calendar-full-sync.job'; import { CalendarEventAttendeeRepository } from 'src/modules/calendar/repositories/calendar-event-attendee.repository'; import { ConnectedAccountObjectMetadata } from 'src/modules/connected-account/standard-objects/connected-account.object-metadata'; import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator'; @@ -120,27 +120,40 @@ export class GoogleCalendarSyncService { const blocklistedEmails = blocklist.map((blocklist) => blocklist.handle); let startTime = Date.now(); + let endTime: number; + + let nextSyncToken: string | null | undefined; + let nextPageToken: string | null | undefined = pageToken; + const events: calendar_v3.Schema$Event[] = []; + + while (!nextPageToken) { + const googleCalendarEvents = await googleCalendarClient.events.list({ + calendarId: 'primary', + maxResults: 500, + syncToken, + pageToken, + q: googleCalendarSearchFilterExcludeEmails(blocklistedEmails), + }); - const googleCalendarEvents = await googleCalendarClient.events.list({ - calendarId: 'primary', - maxResults: 500, - pageToken: pageToken, - q: googleCalendarSearchFilterExcludeEmails(blocklistedEmails), - }); + endTime = Date.now(); - let endTime = Date.now(); + this.logger.log( + `google calendar full-sync for workspace ${workspaceId} and account ${connectedAccountId} getting events list in ${ + endTime - startTime + }ms.`, + ); - this.logger.log( - `google calendar full-sync for workspace ${workspaceId} and account ${connectedAccountId} getting events list in ${ - endTime - startTime - }ms.`, - ); + nextSyncToken = googleCalendarEvents.data.nextSyncToken; + nextPageToken = googleCalendarEvents.data.nextPageToken; - const { - items: events, - nextPageToken, - nextSyncToken, - } = googleCalendarEvents.data; + const { items } = googleCalendarEvents.data; + + if (!items || items.length === 0) { + break; + } + + events.push(...items); + } if (!events || events?.length === 0) { this.logger.log( @@ -345,19 +358,5 @@ export class GoogleCalendarSyncService { nextPageToken ? `and ${nextPageToken} pageToken` : '' } done.`, ); - - if (nextPageToken) { - await this.messageQueueService.add( - GoogleCalendarSyncService.name, - { - workspaceId, - connectedAccountId, - nextPageToken, - }, - { - retryLimit: 2, - }, - ); - } } } From 8d537ab72349b2b942375cbbacb5aa784cc52a5c Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Wed, 3 Apr 2024 16:34:54 +0200 Subject: [PATCH 05/33] add deletion logic inside sync --- .../services/google-calendar-sync.service.ts | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts index 260809fec4f7..740bd20ae7f3 100644 --- a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts +++ b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts @@ -29,6 +29,7 @@ import { CalendarChannelEventAssociationObjectMetadata } from 'src/modules/calen import { CalendarEventAttendeeObjectMetadata } from 'src/modules/calendar/standard-objects/calendar-event-attendee.object-metadata'; import { BlocklistObjectMetadata } from 'src/modules/connected-account/standard-objects/blocklist.object-metadata'; import { CalendarEventAttendeeService } from 'src/modules/calendar/services/calendar-event-attendee/calendar-event-attendee.service'; +import { CalendarEventCleanerService } from 'src/modules/calendar/services/calendar-event-cleaner/calendar-event-cleaner.service'; @Injectable() export class GoogleCalendarSyncService { @@ -57,6 +58,7 @@ export class GoogleCalendarSyncService { private readonly workspaceDataSourceService: WorkspaceDataSourceService, private readonly eventEmitter: EventEmitter2, private readonly calendarEventAttendeesService: CalendarEventAttendeeService, + private readonly calendarEventCleanerService: CalendarEventCleanerService, ) {} public async startGoogleCalendarSync( @@ -201,6 +203,14 @@ export class GoogleCalendarSyncService { existingEventExternalIds.includes(event.externalId), ); + const eventsToDelete = existingCalendarChannelEventAssociations.filter( + (association) => !eventExternalIds.includes(association.eventExternalId), + ); + + const eventExternalIdsToDelete = eventsToDelete.map( + (association) => association.eventExternalId, + ); + const calendarChannelEventAssociationsToSave = eventsToSave.map( (event) => ({ calendarEventId: event.id, @@ -309,6 +319,27 @@ export class GoogleCalendarSyncService { endTime - startTime }ms.`, ); + + startTime = Date.now(); + + await this.calendarChannelEventAssociationRepository.deleteByEventExternalIdsAndCalendarChannelId( + eventExternalIdsToDelete, + calendarChannelId, + workspaceId, + transactionManager, + ); + + await this.calendarEventCleanerService.cleanWorkspaceCalendarEvents( + workspaceId, + ); + + endTime = Date.now(); + + this.logger.log( + `google calendar full-sync for workspace ${workspaceId} and account ${connectedAccountId}: deleting calendar channel event associations in ${ + endTime - startTime + }ms.`, + ); }); if (calendarChannel.isContactAutoCreationEnabled) { From 0c5b433474b6da8e9063970cea99913eb6b45432 Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Wed, 3 Apr 2024 17:17:34 +0200 Subject: [PATCH 06/33] get sync cursor inside service --- .../services/google-calendar-sync.module.ts | 2 ++ .../services/google-calendar-sync.service.ts | 26 +++++++++++-------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.module.ts b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.module.ts index 2294ca5fe903..d048f8aedb96 100644 --- a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.module.ts +++ b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.module.ts @@ -5,6 +5,7 @@ import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature- import { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.module'; import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module'; import { CalendarEventAttendeeModule } from 'src/modules/calendar/services/calendar-event-attendee/calendar-event-attendee.module'; +import { CalendarEventCleanerModule } from 'src/modules/calendar/services/calendar-event-cleaner/calendar-event-cleaner.module'; import { GoogleCalendarSyncService } from 'src/modules/calendar/services/google-calendar-sync.service'; import { CalendarProvidersModule } from 'src/modules/calendar/services/providers/calendar-providers.module'; import { CalendarChannelEventAssociationObjectMetadata } from 'src/modules/calendar/standard-objects/calendar-channel-event-association.object-metadata'; @@ -32,6 +33,7 @@ import { WorkspaceMemberObjectMetadata } from 'src/modules/workspace-member/stan CalendarEventAttendeeModule, TypeOrmModule.forFeature([FeatureFlagEntity], 'core'), WorkspaceDataSourceModule, + CalendarEventCleanerModule, ], providers: [GoogleCalendarSyncService], exports: [GoogleCalendarSyncService], diff --git a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts index 740bd20ae7f3..7db9191216e2 100644 --- a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts +++ b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts @@ -64,7 +64,6 @@ export class GoogleCalendarSyncService { public async startGoogleCalendarSync( workspaceId: string, connectedAccountId: string, - syncToken?: string, pageToken?: string, ): Promise { const connectedAccount = await this.connectedAccountRepository.getById( @@ -91,6 +90,8 @@ export class GoogleCalendarSyncService { workspaceId, ); + const syncToken = calendarChannel?.syncCursor; + if (!calendarChannel) { return; } @@ -122,13 +123,12 @@ export class GoogleCalendarSyncService { const blocklistedEmails = blocklist.map((blocklist) => blocklist.handle); let startTime = Date.now(); - let endTime: number; let nextSyncToken: string | null | undefined; let nextPageToken: string | null | undefined = pageToken; const events: calendar_v3.Schema$Event[] = []; - while (!nextPageToken) { + while (true) { const googleCalendarEvents = await googleCalendarClient.events.list({ calendarId: 'primary', maxResults: 500, @@ -137,14 +137,6 @@ export class GoogleCalendarSyncService { q: googleCalendarSearchFilterExcludeEmails(blocklistedEmails), }); - endTime = Date.now(); - - this.logger.log( - `google calendar full-sync for workspace ${workspaceId} and account ${connectedAccountId} getting events list in ${ - endTime - startTime - }ms.`, - ); - nextSyncToken = googleCalendarEvents.data.nextSyncToken; nextPageToken = googleCalendarEvents.data.nextPageToken; @@ -155,8 +147,20 @@ export class GoogleCalendarSyncService { } events.push(...items); + + if (!nextPageToken) { + break; + } } + let endTime = Date.now(); + + this.logger.log( + `google calendar full-sync for workspace ${workspaceId} and account ${connectedAccountId} getting events list in ${ + endTime - startTime + }ms.`, + ); + if (!events || events?.length === 0) { this.logger.log( `google calendar full-sync for workspace ${workspaceId} and account ${connectedAccountId} done with nothing to import.`, From ba41c899e08485b909726f0f86ce259ac1ca266a Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Wed, 3 Apr 2024 17:37:03 +0200 Subject: [PATCH 07/33] update google-calendar-search-filter.util --- .../modules/calendar/services/google-calendar-sync.service.ts | 2 +- .../calendar/utils/google-calendar-search-filter.util.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts index 7db9191216e2..951fe22144d3 100644 --- a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts +++ b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts @@ -90,7 +90,7 @@ export class GoogleCalendarSyncService { workspaceId, ); - const syncToken = calendarChannel?.syncCursor; + const syncToken = calendarChannel?.syncCursor || undefined; if (!calendarChannel) { return; diff --git a/packages/twenty-server/src/modules/calendar/utils/google-calendar-search-filter.util.ts b/packages/twenty-server/src/modules/calendar/utils/google-calendar-search-filter.util.ts index c9c90417a228..2563c0d05841 100644 --- a/packages/twenty-server/src/modules/calendar/utils/google-calendar-search-filter.util.ts +++ b/packages/twenty-server/src/modules/calendar/utils/google-calendar-search-filter.util.ts @@ -1,8 +1,8 @@ export const googleCalendarSearchFilterExcludeEmails = ( emails: string[], -): string => { +): string | undefined => { if (emails.length === 0) { - return ''; + return undefined; } return `email=-${emails.join(', -')}`; From bd9d9d96dd23a58e8e232d308e9db1351cacfca4 Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Wed, 3 Apr 2024 18:28:21 +0200 Subject: [PATCH 08/33] create contacts for updated calendarEventAttendees --- .../calendar-event-attendee.repository.ts | 15 +++++++++++--- .../services/google-calendar-sync.service.ts | 20 ++++++++++++------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/packages/twenty-server/src/modules/calendar/repositories/calendar-event-attendee.repository.ts b/packages/twenty-server/src/modules/calendar/repositories/calendar-event-attendee.repository.ts index 60fabbdeab9d..532b6e3e7ebb 100644 --- a/packages/twenty-server/src/modules/calendar/repositories/calendar-event-attendee.repository.ts +++ b/packages/twenty-server/src/modules/calendar/repositories/calendar-event-attendee.repository.ts @@ -78,14 +78,14 @@ export class CalendarEventAttendeeRepository { ); } - public async updateCalendarEventAttendees( + public async updateCalendarEventAttendeesAndReturnNewOnes( calendarEventAttendees: CalendarEventAttendee[], iCalUIDCalendarEventIdMap: Map, workspaceId: string, transactionManager?: EntityManager, - ): Promise { + ): Promise { if (calendarEventAttendees.length === 0) { - return; + return []; } const dataSourceSchema = @@ -106,6 +106,13 @@ export class CalendarEventAttendeeRepository { existingCalendarEventAttendee.handle === calendarEventAttendee.handle, ); + const newCalendarEventAttendees = differenceWith( + calendarEventAttendees, + existingCalendarEventAttendees, + (calendarEventAttendee, existingCalendarEventAttendee) => + calendarEventAttendee.handle === existingCalendarEventAttendee.handle, + ); + await this.deleteByIds( calendarEventAttendeesToDelete.map( (calendarEventAttendee) => calendarEventAttendee.id, @@ -142,6 +149,8 @@ export class CalendarEventAttendeeRepository { workspaceId, transactionManager, ); + + return newCalendarEventAttendees; } public async getWithoutPersonIdAndWorkspaceMemberId( diff --git a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts index 951fe22144d3..ac40e2ec6e9b 100644 --- a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts +++ b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts @@ -30,6 +30,7 @@ import { CalendarEventAttendeeObjectMetadata } from 'src/modules/calendar/standa import { BlocklistObjectMetadata } from 'src/modules/connected-account/standard-objects/blocklist.object-metadata'; import { CalendarEventAttendeeService } from 'src/modules/calendar/services/calendar-event-attendee/calendar-event-attendee.service'; import { CalendarEventCleanerService } from 'src/modules/calendar/services/calendar-event-cleaner/calendar-event-cleaner.service'; +import { CalendarEventAttendee } from 'src/modules/calendar/types/calendar-event'; @Injectable() export class GoogleCalendarSyncService { @@ -235,6 +236,8 @@ export class GoogleCalendarSyncService { workspaceId, ); + let newCalendarEventAttendees: CalendarEventAttendee[] = []; + if (events.length > 0) { const dataSourceMetadata = await this.workspaceDataSourceService.connectToWorkspaceDataSource( @@ -309,12 +312,13 @@ export class GoogleCalendarSyncService { startTime = Date.now(); - await this.calendarEventAttendeesRepository.updateCalendarEventAttendees( - attendeesToUpdate, - iCalUIDCalendarEventIdMap, - workspaceId, - transactionManager, - ); + newCalendarEventAttendees = + await this.calendarEventAttendeesRepository.updateCalendarEventAttendeesAndReturnNewOnes( + attendeesToUpdate, + iCalUIDCalendarEventIdMap, + workspaceId, + transactionManager, + ); endTime = Date.now(); @@ -347,7 +351,9 @@ export class GoogleCalendarSyncService { }); if (calendarChannel.isContactAutoCreationEnabled) { - const contactsToCreate = attendeesToSave; + const contactsToCreate = attendeesToSave.concat( + newCalendarEventAttendees, + ); this.eventEmitter.emit(`createContacts`, { workspaceId, From 7948dd6ce72491a8a335efb2228466d36868cf5f Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Thu, 4 Apr 2024 11:04:45 +0200 Subject: [PATCH 09/33] refactoring and improving logs --- .../services/google-calendar-sync.service.ts | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts index ac40e2ec6e9b..2543279cf232 100644 --- a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts +++ b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts @@ -257,9 +257,9 @@ export class GoogleCalendarSyncService { endTime = Date.now(); this.logger.log( - `google calendar full-sync for workspace ${workspaceId} and account ${connectedAccountId}: saving events in ${ - endTime - startTime - }ms.`, + `google calendar full-sync for workspace ${workspaceId} and account ${connectedAccountId}: saving ${ + eventsToSave.length + } events in ${endTime - startTime}ms.`, ); startTime = Date.now(); @@ -273,9 +273,9 @@ export class GoogleCalendarSyncService { endTime = Date.now(); this.logger.log( - `google calendar full-sync for workspace ${workspaceId} and account ${connectedAccountId}: updating events in ${ - endTime - startTime - }ms.`, + `google calendar full-sync for workspace ${workspaceId} and account ${connectedAccountId}: updating ${ + eventsToUpdate.length + } events in ${endTime - startTime}ms.`, ); startTime = Date.now(); @@ -337,10 +337,6 @@ export class GoogleCalendarSyncService { transactionManager, ); - await this.calendarEventCleanerService.cleanWorkspaceCalendarEvents( - workspaceId, - ); - endTime = Date.now(); this.logger.log( @@ -350,6 +346,20 @@ export class GoogleCalendarSyncService { ); }); + startTime = Date.now(); + + await this.calendarEventCleanerService.cleanWorkspaceCalendarEvents( + workspaceId, + ); + + endTime = Date.now(); + + this.logger.log( + `google calendar full-sync for workspace ${workspaceId} and account ${connectedAccountId}: cleaning calendar events in ${ + endTime - startTime + }ms.`, + ); + if (calendarChannel.isContactAutoCreationEnabled) { const contactsToCreate = attendeesToSave.concat( newCalendarEventAttendees, From 1178d1bb02e1011b46fd3d2f3aae916906dcbf91 Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Thu, 4 Apr 2024 11:05:44 +0200 Subject: [PATCH 10/33] updating logs --- .../services/google-calendar-sync.service.ts | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts index 2543279cf232..7bb90c30f0b1 100644 --- a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts +++ b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts @@ -81,7 +81,7 @@ export class GoogleCalendarSyncService { if (!refreshToken) { throw new Error( - `No refresh token found for connected account ${connectedAccountId} in workspace ${workspaceId} during full-sync`, + `No refresh token found for connected account ${connectedAccountId} in workspace ${workspaceId} during sync`, ); } @@ -157,14 +157,14 @@ export class GoogleCalendarSyncService { let endTime = Date.now(); this.logger.log( - `google calendar full-sync for workspace ${workspaceId} and account ${connectedAccountId} getting events list in ${ + `google calendar sync for workspace ${workspaceId} and account ${connectedAccountId} getting events list in ${ endTime - startTime }ms.`, ); if (!events || events?.length === 0) { this.logger.log( - `google calendar full-sync for workspace ${workspaceId} and account ${connectedAccountId} done with nothing to import.`, + `google calendar sync for workspace ${workspaceId} and account ${connectedAccountId} done with nothing to import.`, ); return; @@ -184,7 +184,7 @@ export class GoogleCalendarSyncService { endTime = Date.now(); this.logger.log( - `google calendar full-sync for workspace ${workspaceId} and account ${connectedAccountId}: getting existing calendar channel event associations in ${ + `google calendar sync for workspace ${workspaceId} and account ${connectedAccountId}: getting existing calendar channel event associations in ${ endTime - startTime }ms.`, ); @@ -257,7 +257,7 @@ export class GoogleCalendarSyncService { endTime = Date.now(); this.logger.log( - `google calendar full-sync for workspace ${workspaceId} and account ${connectedAccountId}: saving ${ + `google calendar sync for workspace ${workspaceId} and account ${connectedAccountId}: saving ${ eventsToSave.length } events in ${endTime - startTime}ms.`, ); @@ -273,7 +273,7 @@ export class GoogleCalendarSyncService { endTime = Date.now(); this.logger.log( - `google calendar full-sync for workspace ${workspaceId} and account ${connectedAccountId}: updating ${ + `google calendar sync for workspace ${workspaceId} and account ${connectedAccountId}: updating ${ eventsToUpdate.length } events in ${endTime - startTime}ms.`, ); @@ -289,7 +289,7 @@ export class GoogleCalendarSyncService { endTime = Date.now(); this.logger.log( - `google calendar full-sync for workspace ${workspaceId} and account ${connectedAccountId}: saving calendar channel event associations in ${ + `google calendar sync for workspace ${workspaceId} and account ${connectedAccountId}: saving calendar channel event associations in ${ endTime - startTime }ms.`, ); @@ -305,7 +305,7 @@ export class GoogleCalendarSyncService { endTime = Date.now(); this.logger.log( - `google calendar full-sync for workspace ${workspaceId} and account ${connectedAccountId}: saving attendees in ${ + `google calendar sync for workspace ${workspaceId} and account ${connectedAccountId}: saving attendees in ${ endTime - startTime }ms.`, ); @@ -323,7 +323,7 @@ export class GoogleCalendarSyncService { endTime = Date.now(); this.logger.log( - `google calendar full-sync for workspace ${workspaceId} and account ${connectedAccountId}: updating attendees in ${ + `google calendar sync for workspace ${workspaceId} and account ${connectedAccountId}: updating attendees in ${ endTime - startTime }ms.`, ); @@ -340,7 +340,7 @@ export class GoogleCalendarSyncService { endTime = Date.now(); this.logger.log( - `google calendar full-sync for workspace ${workspaceId} and account ${connectedAccountId}: deleting calendar channel event associations in ${ + `google calendar sync for workspace ${workspaceId} and account ${connectedAccountId}: deleting calendar channel event associations in ${ endTime - startTime }ms.`, ); @@ -355,7 +355,7 @@ export class GoogleCalendarSyncService { endTime = Date.now(); this.logger.log( - `google calendar full-sync for workspace ${workspaceId} and account ${connectedAccountId}: cleaning calendar events in ${ + `google calendar sync for workspace ${workspaceId} and account ${connectedAccountId}: cleaning calendar events in ${ endTime - startTime }ms.`, ); @@ -373,18 +373,18 @@ export class GoogleCalendarSyncService { } } catch (error) { this.logger.error( - `Error during google calendar full-sync for workspace ${workspaceId} and account ${connectedAccountId}: ${error.message}`, + `Error during google calendar sync for workspace ${workspaceId} and account ${connectedAccountId}: ${error.message}`, ); } } else { this.logger.log( - `google calendar full-sync for workspace ${workspaceId} and account ${connectedAccountId} done with nothing to import.`, + `google calendar sync for workspace ${workspaceId} and account ${connectedAccountId} done with nothing to import.`, ); } if (!nextSyncToken) { throw new Error( - `No next sync token found for connected account ${connectedAccountId} in workspace ${workspaceId} during full-sync`, + `No next sync token found for connected account ${connectedAccountId} in workspace ${workspaceId} during sync`, ); } @@ -399,13 +399,13 @@ export class GoogleCalendarSyncService { endTime = Date.now(); this.logger.log( - `google calendar full-sync for workspace ${workspaceId} and account ${connectedAccountId}: updating sync cursor in ${ + `google calendar sync for workspace ${workspaceId} and account ${connectedAccountId}: updating sync cursor in ${ endTime - startTime }ms.`, ); this.logger.log( - `google calendar full-sync for workspace ${workspaceId} and account ${connectedAccountId} ${ + `google calendar sync for workspace ${workspaceId} and account ${connectedAccountId} ${ nextPageToken ? `and ${nextPageToken} pageToken` : '' } done.`, ); From 811aa531ed3ca5fb0955e6f40d784c63489f384d Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Thu, 4 Apr 2024 11:21:56 +0200 Subject: [PATCH 11/33] pageToken is no longer needed as an argument for the job --- .../auth/services/google-apis.service.ts | 2 +- .../integrations/message-queue/jobs.module.ts | 2 +- .../commands/google-calendar-sync.command.ts | 2 +- ...-sync.job.ts => google-calendar-sync.job.ts} | 8 +------- .../services/google-calendar-sync.service.ts | 17 ++++++----------- 5 files changed, 10 insertions(+), 21 deletions(-) rename packages/twenty-server/src/modules/calendar/jobs/{google-calendar-full-sync.job.ts => google-calendar-sync.job.ts} (85%) diff --git a/packages/twenty-server/src/engine/core-modules/auth/services/google-apis.service.ts b/packages/twenty-server/src/engine/core-modules/auth/services/google-apis.service.ts index 1b3d490faa1f..60f66350e512 100644 --- a/packages/twenty-server/src/engine/core-modules/auth/services/google-apis.service.ts +++ b/packages/twenty-server/src/engine/core-modules/auth/services/google-apis.service.ts @@ -16,7 +16,7 @@ import { import { GoogleCalendarSyncJob, GoogleCalendarSyncJobData, -} from 'src/modules/calendar/jobs/google-calendar-full-sync.job'; +} from 'src/modules/calendar/jobs/google-calendar-sync.job'; import { EnvironmentService } from 'src/engine/integrations/environment/environment.service'; import { FeatureFlagEntity, diff --git a/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts b/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts index fbf3044fbd13..1dcdec41af0e 100644 --- a/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts +++ b/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts @@ -30,7 +30,7 @@ import { StripeModule } from 'src/engine/core-modules/billing/stripe/stripe.modu import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity'; import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-flag.entity'; import { DataSourceEntity } from 'src/engine/metadata-modules/data-source/data-source.entity'; -import { GoogleCalendarSyncJob } from 'src/modules/calendar/jobs/google-calendar-full-sync.job'; +import { GoogleCalendarSyncJob } from 'src/modules/calendar/jobs/google-calendar-sync.job'; import { CalendarEventCleanerModule } from 'src/modules/calendar/services/calendar-event-cleaner/calendar-event-cleaner.module'; import { RecordPositionBackfillJob } from 'src/engine/api/graphql/workspace-query-runner/jobs/record-position-backfill.job'; import { RecordPositionBackfillModule } from 'src/engine/api/graphql/workspace-query-runner/services/record-position-backfill-module'; diff --git a/packages/twenty-server/src/modules/calendar/commands/google-calendar-sync.command.ts b/packages/twenty-server/src/modules/calendar/commands/google-calendar-sync.command.ts index 838b71e1030e..fcd11fe349f9 100644 --- a/packages/twenty-server/src/modules/calendar/commands/google-calendar-sync.command.ts +++ b/packages/twenty-server/src/modules/calendar/commands/google-calendar-sync.command.ts @@ -8,7 +8,7 @@ import { ConnectedAccountRepository } from 'src/modules/connected-account/reposi import { GoogleCalendarSyncJobData, GoogleCalendarSyncJob, -} from 'src/modules/calendar/jobs/google-calendar-full-sync.job'; +} from 'src/modules/calendar/jobs/google-calendar-sync.job'; import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator'; import { ConnectedAccountObjectMetadata } from 'src/modules/connected-account/standard-objects/connected-account.object-metadata'; import { CalendarChannelRepository } from 'src/modules/calendar/repositories/calendar-channel.repository'; diff --git a/packages/twenty-server/src/modules/calendar/jobs/google-calendar-full-sync.job.ts b/packages/twenty-server/src/modules/calendar/jobs/google-calendar-sync.job.ts similarity index 85% rename from packages/twenty-server/src/modules/calendar/jobs/google-calendar-full-sync.job.ts rename to packages/twenty-server/src/modules/calendar/jobs/google-calendar-sync.job.ts index 50924ae7e5ca..08019f4201b5 100644 --- a/packages/twenty-server/src/modules/calendar/jobs/google-calendar-full-sync.job.ts +++ b/packages/twenty-server/src/modules/calendar/jobs/google-calendar-sync.job.ts @@ -8,7 +8,6 @@ import { GoogleCalendarSyncService } from 'src/modules/calendar/services/google- export type GoogleCalendarSyncJobData = { workspaceId: string; connectedAccountId: string; - nextPageToken?: string; }; @Injectable() @@ -24,11 +23,7 @@ export class GoogleCalendarSyncJob async handle(data: GoogleCalendarSyncJobData): Promise { this.logger.log( - `google calendar full-sync for workspace ${ - data.workspaceId - } and account ${data.connectedAccountId} ${ - data.nextPageToken ? `and ${data.nextPageToken} pageToken` : '' - }`, + `google calendar sync for workspace ${data.workspaceId} and account ${data.connectedAccountId}`, ); try { await this.googleAPIsRefreshAccessTokenService.refreshAndSaveAccessToken( @@ -47,7 +42,6 @@ export class GoogleCalendarSyncJob await this.googleCalendarSyncService.startGoogleCalendarSync( data.workspaceId, data.connectedAccountId, - data.nextPageToken, ); } } diff --git a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts index 7bb90c30f0b1..0a31ad6c69e4 100644 --- a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts +++ b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts @@ -1,4 +1,4 @@ -import { Inject, Injectable, Logger } from '@nestjs/common'; +import { Injectable, Logger } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { EventEmitter2 } from '@nestjs/event-emitter'; @@ -15,8 +15,6 @@ import { GoogleCalendarClientProvider } from 'src/modules/calendar/services/prov import { googleCalendarSearchFilterExcludeEmails } from 'src/modules/calendar/utils/google-calendar-search-filter.util'; import { CalendarChannelEventAssociationRepository } from 'src/modules/calendar/repositories/calendar-channel-event-association.repository'; import { CalendarChannelRepository } from 'src/modules/calendar/repositories/calendar-channel.repository'; -import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; -import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service'; import { CalendarEventRepository } from 'src/modules/calendar/repositories/calendar-event.repository'; import { formatGoogleCalendarEvent } from 'src/modules/calendar/utils/format-google-calendar-event.util'; @@ -38,8 +36,6 @@ export class GoogleCalendarSyncService { constructor( private readonly googleCalendarClientProvider: GoogleCalendarClientProvider, - @Inject(MessageQueue.calendarQueue) - private readonly messageQueueService: MessageQueueService, @InjectObjectMetadataRepository(ConnectedAccountObjectMetadata) private readonly connectedAccountRepository: ConnectedAccountRepository, @InjectObjectMetadataRepository(CalendarEventObjectMetadata) @@ -65,7 +61,6 @@ export class GoogleCalendarSyncService { public async startGoogleCalendarSync( workspaceId: string, connectedAccountId: string, - pageToken?: string, ): Promise { const connectedAccount = await this.connectedAccountRepository.getById( connectedAccountId, @@ -126,7 +121,7 @@ export class GoogleCalendarSyncService { let startTime = Date.now(); let nextSyncToken: string | null | undefined; - let nextPageToken: string | null | undefined = pageToken; + let nextPageToken: string | undefined; const events: calendar_v3.Schema$Event[] = []; while (true) { @@ -134,12 +129,12 @@ export class GoogleCalendarSyncService { calendarId: 'primary', maxResults: 500, syncToken, - pageToken, + pageToken: nextPageToken, q: googleCalendarSearchFilterExcludeEmails(blocklistedEmails), }); nextSyncToken = googleCalendarEvents.data.nextSyncToken; - nextPageToken = googleCalendarEvents.data.nextPageToken; + nextPageToken = googleCalendarEvents.data.nextPageToken || undefined; const { items } = googleCalendarEvents.data; @@ -406,8 +401,8 @@ export class GoogleCalendarSyncService { this.logger.log( `google calendar sync for workspace ${workspaceId} and account ${connectedAccountId} ${ - nextPageToken ? `and ${nextPageToken} pageToken` : '' - } done.`, + syncToken ? `and ${syncToken} syncToken ` : '' + }done.`, ); } } From 4d18d238325c712628b6e41fe2e3e841d83e34fb Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Thu, 4 Apr 2024 11:54:51 +0200 Subject: [PATCH 12/33] fix typo --- .../repositories/calendar-event.repository.ts | 6 +-- .../services/google-calendar-sync.service.ts | 39 ++++++++++--------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/packages/twenty-server/src/modules/calendar/repositories/calendar-event.repository.ts b/packages/twenty-server/src/modules/calendar/repositories/calendar-event.repository.ts index cf8b22949775..c7c87fe164cc 100644 --- a/packages/twenty-server/src/modules/calendar/repositories/calendar-event.repository.ts +++ b/packages/twenty-server/src/modules/calendar/repositories/calendar-event.repository.ts @@ -101,13 +101,13 @@ export class CalendarEventRepository { transactionManager, ); - const iCalUIDsCalendarEvnetIdsMap = new Map(); + const iCalUIDsCalendarEventIdsMap = new Map(); calendarEvents.forEach((calendarEvent) => { - iCalUIDsCalendarEvnetIdsMap.set(calendarEvent.iCalUID, calendarEvent.id); + iCalUIDsCalendarEventIdsMap.set(calendarEvent.iCalUID, calendarEvent.id); }); - return iCalUIDsCalendarEvnetIdsMap; + return iCalUIDsCalendarEventIdsMap; } public async saveCalendarEvents( diff --git a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts index 0a31ad6c69e4..e19d456a49c8 100644 --- a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts +++ b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts @@ -291,34 +291,39 @@ export class GoogleCalendarSyncService { startTime = Date.now(); - await this.calendarEventAttendeesService.saveCalendarEventAttendees( - attendeesToSave, - workspaceId, - transactionManager, - ); + newCalendarEventAttendees = + await this.calendarEventAttendeesRepository.updateCalendarEventAttendeesAndReturnNewOnes( + attendeesToUpdate, + iCalUIDCalendarEventIdMap, + workspaceId, + transactionManager, + ); endTime = Date.now(); + attendeesToSave.push(...newCalendarEventAttendees); + this.logger.log( - `google calendar sync for workspace ${workspaceId} and account ${connectedAccountId}: saving attendees in ${ + `google calendar sync for workspace ${workspaceId} and account ${connectedAccountId}: updating attendees in ${ endTime - startTime }ms.`, ); startTime = Date.now(); - newCalendarEventAttendees = - await this.calendarEventAttendeesRepository.updateCalendarEventAttendeesAndReturnNewOnes( - attendeesToUpdate, - iCalUIDCalendarEventIdMap, - workspaceId, - transactionManager, - ); + console.log('newCalendarEventAttendees', newCalendarEventAttendees); + console.log('attendeesToSave', attendeesToSave); + + await this.calendarEventAttendeesService.saveCalendarEventAttendees( + attendeesToSave, + workspaceId, + transactionManager, + ); endTime = Date.now(); this.logger.log( - `google calendar sync for workspace ${workspaceId} and account ${connectedAccountId}: updating attendees in ${ + `google calendar sync for workspace ${workspaceId} and account ${connectedAccountId}: saving attendees in ${ endTime - startTime }ms.`, ); @@ -356,14 +361,10 @@ export class GoogleCalendarSyncService { ); if (calendarChannel.isContactAutoCreationEnabled) { - const contactsToCreate = attendeesToSave.concat( - newCalendarEventAttendees, - ); - this.eventEmitter.emit(`createContacts`, { workspaceId, connectedAccountHandle: connectedAccount.handle, - contactsToCreate, + contactsToCreate: attendeesToSave, }); } } catch (error) { From 4a8bf8787b1f2529c41a60ece924e65d2193045c Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Thu, 4 Apr 2024 13:34:45 +0200 Subject: [PATCH 13/33] remove console logs --- .../repositories/calendar-event.repository.ts | 20 +++++++------ .../services/google-calendar-sync.service.ts | 29 ++++++++++--------- .../format-google-calendar-event.util.ts | 3 +- 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/packages/twenty-server/src/modules/calendar/repositories/calendar-event.repository.ts b/packages/twenty-server/src/modules/calendar/repositories/calendar-event.repository.ts index c7c87fe164cc..3b1d5cd2c343 100644 --- a/packages/twenty-server/src/modules/calendar/repositories/calendar-event.repository.ts +++ b/packages/twenty-server/src/modules/calendar/repositories/calendar-event.repository.ts @@ -80,30 +80,32 @@ export class CalendarEventRepository { } public async getICalUIDCalendarEventIdMap( - iCalUIDs: string[], + calendarEventIds: string[], workspaceId: string, transactionManager?: EntityManager, ): Promise> { - if (iCalUIDs.length === 0) { + if (calendarEventIds.length === 0) { return new Map(); } const dataSourceSchema = this.workspaceDataSourceService.getSchemaName(workspaceId); - const calendarEvents: { - id: string; - iCalUID: string; - }[] = await this.workspaceDataSourceService.executeRawQuery( - `SELECT id, "iCalUID" FROM ${dataSourceSchema}."calendarEvent" WHERE "iCalUID" = ANY($1)`, - [iCalUIDs], + const calendarEvents: + | { + id: string; + iCalUID: string; + }[] + | undefined = await this.workspaceDataSourceService.executeRawQuery( + `SELECT id, "iCalUID" FROM ${dataSourceSchema}."calendarEvent" WHERE "id" = ANY($1)`, + [calendarEventIds], workspaceId, transactionManager, ); const iCalUIDsCalendarEventIdsMap = new Map(); - calendarEvents.forEach((calendarEvent) => { + calendarEvents?.forEach((calendarEvent) => { iCalUIDsCalendarEventIdsMap.set(calendarEvent.iCalUID, calendarEvent.id); }); diff --git a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts index e19d456a49c8..7dc48e213fcb 100644 --- a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts +++ b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts @@ -184,10 +184,6 @@ export class GoogleCalendarSyncService { }ms.`, ); - const formattedEvents = events - .filter((event) => event.status !== 'cancelled') - .map((event) => formatGoogleCalendarEvent(event)); - // TODO: When we will be able to add unicity contraint on iCalUID, we will do a INSERT ON CONFLICT DO UPDATE const existingEventExternalIds = @@ -195,6 +191,22 @@ export class GoogleCalendarSyncService { (association) => association.eventExternalId, ); + const existingEventsIds = existingCalendarChannelEventAssociations.map( + (association) => association.calendarEventId, + ); + + const iCalUIDCalendarEventIdMap = + await this.calendarEventRepository.getICalUIDCalendarEventIdMap( + existingEventsIds, + workspaceId, + ); + + const formattedEvents = events + .filter((event) => event.status !== 'cancelled') + .map((event) => + formatGoogleCalendarEvent(event, iCalUIDCalendarEventIdMap), + ); + const eventsToSave = formattedEvents.filter( (event) => !existingEventExternalIds.includes(event.externalId), ); @@ -225,12 +237,6 @@ export class GoogleCalendarSyncService { (event) => event.attendees, ); - const iCalUIDCalendarEventIdMap = - await this.calendarEventRepository.getICalUIDCalendarEventIdMap( - eventsToUpdate.map((event) => event.iCalUID), - workspaceId, - ); - let newCalendarEventAttendees: CalendarEventAttendee[] = []; if (events.length > 0) { @@ -311,9 +317,6 @@ export class GoogleCalendarSyncService { startTime = Date.now(); - console.log('newCalendarEventAttendees', newCalendarEventAttendees); - console.log('attendeesToSave', attendeesToSave); - await this.calendarEventAttendeesService.saveCalendarEventAttendees( attendeesToSave, workspaceId, diff --git a/packages/twenty-server/src/modules/calendar/utils/format-google-calendar-event.util.ts b/packages/twenty-server/src/modules/calendar/utils/format-google-calendar-event.util.ts index f23fc9ac7402..9cb92368ec9f 100644 --- a/packages/twenty-server/src/modules/calendar/utils/format-google-calendar-event.util.ts +++ b/packages/twenty-server/src/modules/calendar/utils/format-google-calendar-event.util.ts @@ -6,8 +6,9 @@ import { CalendarEventAttendeeResponseStatus } from 'src/modules/calendar/standa export const formatGoogleCalendarEvent = ( event: calendar_v3.Schema$Event, + iCalUIDCalendarEventIdMap: Map, ): CalendarEventWithAttendees => { - const id = v4(); + const id = iCalUIDCalendarEventIdMap.get(event.iCalUID ?? '') ?? v4(); const formatResponseStatus = (status: string | null | undefined) => { switch (status) { From cf66fa9215d7bd17f9d3be5d6ae43fffdf328761 Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Thu, 4 Apr 2024 14:11:44 +0200 Subject: [PATCH 14/33] renaming --- .../modules/calendar/services/google-calendar-sync.service.ts | 4 ++-- .../calendar/utils/format-google-calendar-event.util.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts index 4e53e2ade494..0872376a0571 100644 --- a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts +++ b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts @@ -3,7 +3,7 @@ import { InjectRepository } from '@nestjs/typeorm'; import { EventEmitter2 } from '@nestjs/event-emitter'; import { Repository } from 'typeorm'; -import { calendar_v3 } from 'googleapis'; +import { calendar_v3 as calendarV3 } from 'googleapis'; import { ConnectedAccountRepository } from 'src/modules/connected-account/repositories/connected-account.repository'; import { BlocklistRepository } from 'src/modules/connected-account/repositories/blocklist.repository'; @@ -122,7 +122,7 @@ export class GoogleCalendarSyncService { let nextSyncToken: string | null | undefined; let nextPageToken: string | undefined; - const events: calendar_v3.Schema$Event[] = []; + const events: calendarV3.Schema$Event[] = []; while (true) { const googleCalendarEvents = await googleCalendarClient.events.list({ diff --git a/packages/twenty-server/src/modules/calendar/utils/format-google-calendar-event.util.ts b/packages/twenty-server/src/modules/calendar/utils/format-google-calendar-event.util.ts index 86c2db8b2320..fb03ec2fb8cb 100644 --- a/packages/twenty-server/src/modules/calendar/utils/format-google-calendar-event.util.ts +++ b/packages/twenty-server/src/modules/calendar/utils/format-google-calendar-event.util.ts @@ -1,11 +1,11 @@ -import { calendar_v3 } from 'googleapis'; +import { calendar_v3 as calendarV3 } from 'googleapis'; import { v4 } from 'uuid'; import { CalendarEventParticipantResponseStatus } from 'src/modules/calendar/standard-objects/calendar-event-participant.object-metadata'; import { CalendarEventWithParticipants } from 'src/modules/calendar/types/calendar-event'; export const formatGoogleCalendarEvent = ( - event: calendar_v3.Schema$Event, + event: calendarV3.Schema$Event, iCalUIDCalendarEventIdMap: Map, ): CalendarEventWithParticipants => { const id = iCalUIDCalendarEventIdMap.get(event.iCalUID ?? '') ?? v4(); From d7d52a1a317cbdb454b64a5e1d76172f9b94fd13 Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Thu, 4 Apr 2024 15:51:52 +0200 Subject: [PATCH 15/33] remove unused logger --- .../commands/crons/fetch-all-messages-from-cache.cron-job.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/twenty-server/src/modules/messaging/commands/crons/fetch-all-messages-from-cache.cron-job.ts b/packages/twenty-server/src/modules/messaging/commands/crons/fetch-all-messages-from-cache.cron-job.ts index 492a7e9ab01e..493adfd29543 100644 --- a/packages/twenty-server/src/modules/messaging/commands/crons/fetch-all-messages-from-cache.cron-job.ts +++ b/packages/twenty-server/src/modules/messaging/commands/crons/fetch-all-messages-from-cache.cron-job.ts @@ -1,4 +1,4 @@ -import { Injectable, Logger } from '@nestjs/common'; +import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository, In } from 'typeorm'; @@ -20,8 +20,6 @@ import { GmailFetchMessageContentFromCacheService } from 'src/modules/messaging/ export class FetchAllMessagesFromCacheCronJob implements MessageQueueJob { - private readonly logger = new Logger(FetchAllMessagesFromCacheCronJob.name); - constructor( @InjectRepository(Workspace, 'core') private readonly workspaceRepository: Repository, From 4ab60656fd068a03261e913bd63762e50d63d526 Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Thu, 4 Apr 2024 15:58:48 +0200 Subject: [PATCH 16/33] create cron job --- .../crons/google-calendar-sync.cron-job.ts | 82 +++++++++++++++++++ .../calendar-channel.repository.ts | 15 ++++ 2 files changed, 97 insertions(+) create mode 100644 packages/twenty-server/src/modules/calendar/commands/crons/google-calendar-sync.cron-job.ts diff --git a/packages/twenty-server/src/modules/calendar/commands/crons/google-calendar-sync.cron-job.ts b/packages/twenty-server/src/modules/calendar/commands/crons/google-calendar-sync.cron-job.ts new file mode 100644 index 000000000000..e36b484742e8 --- /dev/null +++ b/packages/twenty-server/src/modules/calendar/commands/crons/google-calendar-sync.cron-job.ts @@ -0,0 +1,82 @@ +import { Injectable } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; + +import { Repository, In } from 'typeorm'; + +import { MessageQueueJob } from 'src/engine/integrations/message-queue/interfaces/message-queue-job.interface'; + +import { + FeatureFlagEntity, + FeatureFlagKeys, +} from 'src/engine/core-modules/feature-flag/feature-flag.entity'; +import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity'; +import { DataSourceEntity } from 'src/engine/metadata-modules/data-source/data-source.entity'; +import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator'; +import { GmailFetchMessageContentFromCacheService } from 'src/modules/messaging/services/gmail-fetch-message-content-from-cache/gmail-fetch-message-content-from-cache.service'; +import { CalendarChannelRepository } from 'src/modules/calendar/repositories/calendar-channel.repository'; +import { CalendarChannelObjectMetadata } from 'src/modules/calendar/standard-objects/calendar-channel.object-metadata'; + +@Injectable() +export class GoogleCalendarSyncCronJob implements MessageQueueJob { + constructor( + @InjectRepository(Workspace, 'core') + private readonly workspaceRepository: Repository, + @InjectRepository(DataSourceEntity, 'metadata') + private readonly dataSourceRepository: Repository, + @InjectObjectMetadataRepository(CalendarChannelObjectMetadata) + private readonly calendarChannelRepository: CalendarChannelRepository, + @InjectRepository(FeatureFlagEntity, 'core') + private readonly featureFlagRepository: Repository, + private readonly gmailFetchMessageContentFromCacheService: GmailFetchMessageContentFromCacheService, + ) {} + + async handle(): Promise { + const workspaceIds = ( + await this.workspaceRepository.find({ + where: { + subscriptionStatus: 'active', + }, + select: ['id'], + }) + ).map((workspace) => workspace.id); + + const workspacesWithFeatureFlagActive = + await this.featureFlagRepository.find({ + where: { + workspaceId: In(workspaceIds), + key: FeatureFlagKeys.IsCalendarEnabled, + value: true, + }, + }); + + const dataSources = await this.dataSourceRepository.find({ + where: { + workspaceId: In( + workspacesWithFeatureFlagActive.map((w) => w.workspaceId), + ), + }, + }); + + const workspaceIdsWithDataSources = new Set( + dataSources.map((dataSource) => dataSource.workspaceId), + ); + + for (const workspaceId of workspaceIdsWithDataSources) { + await this.startWorkspaceGoogleCalendarFullSync(workspaceId); + } + } + + private async startWorkspaceGoogleCalendarFullSync( + workspaceId: string, + ): Promise { + const calendarChannels = + await this.calendarChannelRepository.getAll(workspaceId); + + for (const calendarChannel of calendarChannels) { + await this.gmailFetchMessageContentFromCacheService.fetchMessageContentFromCache( + workspaceId, + calendarChannel.connectedAccountId, + ); + } + } +} diff --git a/packages/twenty-server/src/modules/calendar/repositories/calendar-channel.repository.ts b/packages/twenty-server/src/modules/calendar/repositories/calendar-channel.repository.ts index 95e8299f23f9..faf1be210988 100644 --- a/packages/twenty-server/src/modules/calendar/repositories/calendar-channel.repository.ts +++ b/packages/twenty-server/src/modules/calendar/repositories/calendar-channel.repository.ts @@ -12,6 +12,21 @@ export class CalendarChannelRepository { private readonly workspaceDataSourceService: WorkspaceDataSourceService, ) {} + public async getAll( + workspaceId: string, + transactionManager?: EntityManager, + ): Promise[]> { + const dataSourceSchema = + this.workspaceDataSourceService.getSchemaName(workspaceId); + + return await this.workspaceDataSourceService.executeRawQuery( + `SELECT * FROM ${dataSourceSchema}."calendarChannel"`, + [], + workspaceId, + transactionManager, + ); + } + public async getByConnectedAccountId( connectedAccountId: string, workspaceId: string, From 38f56ff14f9789b1072531f91a54a7524377d63b Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Thu, 4 Apr 2024 16:01:27 +0200 Subject: [PATCH 17/33] update google-calendar-sync.cron-job --- .../commands/crons/google-calendar-sync.cron-job.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/twenty-server/src/modules/calendar/commands/crons/google-calendar-sync.cron-job.ts b/packages/twenty-server/src/modules/calendar/commands/crons/google-calendar-sync.cron-job.ts index e36b484742e8..404ab91b905e 100644 --- a/packages/twenty-server/src/modules/calendar/commands/crons/google-calendar-sync.cron-job.ts +++ b/packages/twenty-server/src/modules/calendar/commands/crons/google-calendar-sync.cron-job.ts @@ -12,9 +12,9 @@ import { import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity'; import { DataSourceEntity } from 'src/engine/metadata-modules/data-source/data-source.entity'; import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator'; -import { GmailFetchMessageContentFromCacheService } from 'src/modules/messaging/services/gmail-fetch-message-content-from-cache/gmail-fetch-message-content-from-cache.service'; import { CalendarChannelRepository } from 'src/modules/calendar/repositories/calendar-channel.repository'; import { CalendarChannelObjectMetadata } from 'src/modules/calendar/standard-objects/calendar-channel.object-metadata'; +import { GoogleCalendarSyncService } from 'src/modules/calendar/services/google-calendar-sync.service'; @Injectable() export class GoogleCalendarSyncCronJob implements MessageQueueJob { @@ -27,7 +27,7 @@ export class GoogleCalendarSyncCronJob implements MessageQueueJob { private readonly calendarChannelRepository: CalendarChannelRepository, @InjectRepository(FeatureFlagEntity, 'core') private readonly featureFlagRepository: Repository, - private readonly gmailFetchMessageContentFromCacheService: GmailFetchMessageContentFromCacheService, + private readonly googleCalendarSyncService: GoogleCalendarSyncService, ) {} async handle(): Promise { @@ -73,7 +73,7 @@ export class GoogleCalendarSyncCronJob implements MessageQueueJob { await this.calendarChannelRepository.getAll(workspaceId); for (const calendarChannel of calendarChannels) { - await this.gmailFetchMessageContentFromCacheService.fetchMessageContentFromCache( + await this.googleCalendarSyncService.startGoogleCalendarSync( workspaceId, calendarChannel.connectedAccountId, ); From 23914b2f957d434000b96bcb4b6611170b894a86 Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Thu, 4 Apr 2024 16:19:13 +0200 Subject: [PATCH 18/33] add GoogleCalendarSyncCronJob to jobs.module --- .../src/engine/integrations/message-queue/jobs.module.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts b/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts index 1dcdec41af0e..53d3d59005dd 100644 --- a/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts +++ b/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts @@ -54,6 +54,7 @@ import { FetchAllMessagesFromCacheCronJob } from 'src/modules/messaging/commands import { GmailFullSyncV2Job } from 'src/modules/messaging/jobs/gmail-full-sync-v2.job'; import { GmailPartialSyncV2Job } from 'src/modules/messaging/jobs/gmail-partial-sync-v2.job'; import { GmailPartialSyncV2Module } from 'src/modules/messaging/services/gmail-partial-sync-v2/gmail-partial-sync-v2.module'; +import { GoogleCalendarSyncCronJob } from 'src/modules/calendar/commands/crons/google-calendar-sync.cron-job'; @Module({ imports: [ @@ -168,6 +169,10 @@ import { GmailPartialSyncV2Module } from 'src/modules/messaging/services/gmail-p provide: GmailPartialSyncV2Job.name, useClass: GmailPartialSyncV2Job, }, + { + provide: GoogleCalendarSyncCronJob.name, + useClass: GoogleCalendarSyncCronJob, + }, ], }) export class JobsModule { From 8d44db25ce2f2c900e17e8ba0de3d39220b95c61 Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Thu, 4 Apr 2024 16:34:58 +0200 Subject: [PATCH 19/33] move crons --- .../integrations/message-queue/jobs.module.ts | 6 ++-- ...start-google-calendar-sync.cron.command.ts | 31 +++++++++++++++++++ ...workspace-calendar-sync-commands.module.ts | 3 +- .../crons/google-calendar-sync.cron-job.ts | 0 ...spaces-messages-from-cache.cron.command.ts | 2 +- ...ch-all-workspaces-messages.cron.command.ts | 4 +-- ...ch-all-workspaces-messages.cron.command.ts | 4 +-- .../fetch-all-messages-from-cache.cron-job.ts | 0 ...ch-all-workspaces-messages.cron.pattern.ts | 0 .../fetch-all-workspaces-messages.job.ts | 0 10 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 packages/twenty-server/src/modules/calendar/commands/start-google-calendar-sync.cron.command.ts rename packages/twenty-server/src/modules/calendar/{commands => jobs}/crons/google-calendar-sync.cron-job.ts (100%) rename packages/twenty-server/src/modules/messaging/{commands => jobs}/crons/fetch-all-messages-from-cache.cron-job.ts (100%) rename packages/twenty-server/src/modules/messaging/{commands => jobs}/crons/fetch-all-workspaces-messages.cron.pattern.ts (100%) rename packages/twenty-server/src/modules/messaging/{commands => jobs}/crons/fetch-all-workspaces-messages.job.ts (100%) diff --git a/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts b/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts index 53d3d59005dd..5d6723cac1e9 100644 --- a/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts +++ b/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts @@ -15,7 +15,7 @@ import { GmailPartialSyncJob } from 'src/modules/messaging/jobs/gmail-partial-sy import { EmailSenderJob } from 'src/engine/integrations/email/email-sender.job'; import { UserModule } from 'src/engine/core-modules/user/user.module'; import { EnvironmentModule } from 'src/engine/integrations/environment/environment.module'; -import { FetchAllWorkspacesMessagesJob } from 'src/modules/messaging/commands/crons/fetch-all-workspaces-messages.job'; +import { FetchAllWorkspacesMessagesJob } from 'src/modules/messaging/jobs/crons/fetch-all-workspaces-messages.job'; import { MatchMessageParticipantJob } from 'src/modules/messaging/jobs/match-message-participant.job'; import { CreateCompaniesAndContactsAfterSyncJob } from 'src/modules/messaging/jobs/create-companies-and-contacts-after-sync.job'; import { AutoCompaniesAndContactsCreationModule } from 'src/modules/connected-account/auto-companies-and-contacts-creation/auto-companies-and-contacts-creation.module'; @@ -50,11 +50,11 @@ import { EventObjectMetadata } from 'src/modules/event/standard-objects/event.ob import { HandleWorkspaceMemberDeletedJob } from 'src/engine/core-modules/workspace/handle-workspace-member-deleted.job'; import { GmailFullSynV2Module } from 'src/modules/messaging/services/gmail-full-sync-v2/gmail-full-sync.v2.module'; import { GmailFetchMessageContentFromCacheModule } from 'src/modules/messaging/services/gmail-fetch-message-content-from-cache/gmail-fetch-message-content-from-cache.module'; -import { FetchAllMessagesFromCacheCronJob } from 'src/modules/messaging/commands/crons/fetch-all-messages-from-cache.cron-job'; +import { FetchAllMessagesFromCacheCronJob } from 'src/modules/messaging/jobs/crons/fetch-all-messages-from-cache.cron-job'; import { GmailFullSyncV2Job } from 'src/modules/messaging/jobs/gmail-full-sync-v2.job'; import { GmailPartialSyncV2Job } from 'src/modules/messaging/jobs/gmail-partial-sync-v2.job'; import { GmailPartialSyncV2Module } from 'src/modules/messaging/services/gmail-partial-sync-v2/gmail-partial-sync-v2.module'; -import { GoogleCalendarSyncCronJob } from 'src/modules/calendar/commands/crons/google-calendar-sync.cron-job'; +import { GoogleCalendarSyncCronJob } from 'src/modules/calendar/jobs/crons/google-calendar-sync.cron-job'; @Module({ imports: [ diff --git a/packages/twenty-server/src/modules/calendar/commands/start-google-calendar-sync.cron.command.ts b/packages/twenty-server/src/modules/calendar/commands/start-google-calendar-sync.cron.command.ts new file mode 100644 index 000000000000..5cf95a6a8180 --- /dev/null +++ b/packages/twenty-server/src/modules/calendar/commands/start-google-calendar-sync.cron.command.ts @@ -0,0 +1,31 @@ +import { Inject } from '@nestjs/common'; + +import { Command, CommandRunner } from 'nest-commander'; + +import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; +import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; + +@Command({ + name: 'google-calendar-sync:cron:start', + description: 'Starts a cron job to sync google calendar for all workspaces.', +}) +export class StartGoogleCalendarSyncCronJobCommand extends CommandRunner { + constructor( + @Inject(MessageQueue.cronQueue) + private readonly messageQueueService: MessageQueueService, + ) { + super(); + } + + async run(): Promise { + await this.messageQueueService.addCron( + StartGoogleCalendarSyncCronJobCommand.name, + undefined, + { + repeat: { + every: 5000, + }, + }, + ); + } +} diff --git a/packages/twenty-server/src/modules/calendar/commands/workspace-calendar-sync-commands.module.ts b/packages/twenty-server/src/modules/calendar/commands/workspace-calendar-sync-commands.module.ts index 42bf05fb22ba..c3fa4544f04b 100644 --- a/packages/twenty-server/src/modules/calendar/commands/workspace-calendar-sync-commands.module.ts +++ b/packages/twenty-server/src/modules/calendar/commands/workspace-calendar-sync-commands.module.ts @@ -3,11 +3,12 @@ import { Module } from '@nestjs/common'; import { GoogleCalendarSyncCommand } from 'src/modules/calendar/commands/google-calendar-sync.command'; import { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.module'; import { ConnectedAccountObjectMetadata } from 'src/modules/connected-account/standard-objects/connected-account.object-metadata'; +import { StartGoogleCalendarSyncCronJobCommand } from 'src/modules/calendar/commands/start-google-calendar-sync.cron.command'; @Module({ imports: [ ObjectMetadataRepositoryModule.forFeature([ConnectedAccountObjectMetadata]), ], - providers: [GoogleCalendarSyncCommand], + providers: [GoogleCalendarSyncCommand, StartGoogleCalendarSyncCronJobCommand], }) export class WorkspaceCalendarSyncCommandsModule {} diff --git a/packages/twenty-server/src/modules/calendar/commands/crons/google-calendar-sync.cron-job.ts b/packages/twenty-server/src/modules/calendar/jobs/crons/google-calendar-sync.cron-job.ts similarity index 100% rename from packages/twenty-server/src/modules/calendar/commands/crons/google-calendar-sync.cron-job.ts rename to packages/twenty-server/src/modules/calendar/jobs/crons/google-calendar-sync.cron-job.ts diff --git a/packages/twenty-server/src/modules/messaging/commands/start-fetch-all-workspaces-messages-from-cache.cron.command.ts b/packages/twenty-server/src/modules/messaging/commands/start-fetch-all-workspaces-messages-from-cache.cron.command.ts index aede8efc6100..abe97cc0d9ca 100644 --- a/packages/twenty-server/src/modules/messaging/commands/start-fetch-all-workspaces-messages-from-cache.cron.command.ts +++ b/packages/twenty-server/src/modules/messaging/commands/start-fetch-all-workspaces-messages-from-cache.cron.command.ts @@ -4,7 +4,7 @@ import { Command, CommandRunner } from 'nest-commander'; import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; -import { FetchAllMessagesFromCacheCronJob } from 'src/modules/messaging/commands/crons/fetch-all-messages-from-cache.cron-job'; +import { FetchAllMessagesFromCacheCronJob } from 'src/modules/messaging/jobs/crons/fetch-all-messages-from-cache.cron-job'; @Command({ name: 'fetch-all-workspaces-messages-from-cache:cron:start', diff --git a/packages/twenty-server/src/modules/messaging/commands/start-fetch-all-workspaces-messages.cron.command.ts b/packages/twenty-server/src/modules/messaging/commands/start-fetch-all-workspaces-messages.cron.command.ts index 695fbce34ef1..f8ad8b1f9bf2 100644 --- a/packages/twenty-server/src/modules/messaging/commands/start-fetch-all-workspaces-messages.cron.command.ts +++ b/packages/twenty-server/src/modules/messaging/commands/start-fetch-all-workspaces-messages.cron.command.ts @@ -4,8 +4,8 @@ import { Command, CommandRunner } from 'nest-commander'; import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; -import { fetchAllWorkspacesMessagesCronPattern } from 'src/modules/messaging/commands/crons/fetch-all-workspaces-messages.cron.pattern'; -import { FetchAllWorkspacesMessagesJob } from 'src/modules/messaging/commands/crons/fetch-all-workspaces-messages.job'; +import { fetchAllWorkspacesMessagesCronPattern } from 'src/modules/messaging/jobs/crons/fetch-all-workspaces-messages.cron.pattern'; +import { FetchAllWorkspacesMessagesJob } from 'src/modules/messaging/jobs/crons/fetch-all-workspaces-messages.job'; @Command({ name: 'fetch-all-workspaces-messages:cron:start', diff --git a/packages/twenty-server/src/modules/messaging/commands/stop-fetch-all-workspaces-messages.cron.command.ts b/packages/twenty-server/src/modules/messaging/commands/stop-fetch-all-workspaces-messages.cron.command.ts index 3b87c84d3b3e..6b4c11741911 100644 --- a/packages/twenty-server/src/modules/messaging/commands/stop-fetch-all-workspaces-messages.cron.command.ts +++ b/packages/twenty-server/src/modules/messaging/commands/stop-fetch-all-workspaces-messages.cron.command.ts @@ -4,8 +4,8 @@ import { Command, CommandRunner } from 'nest-commander'; import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; -import { fetchAllWorkspacesMessagesCronPattern } from 'src/modules/messaging/commands/crons/fetch-all-workspaces-messages.cron.pattern'; -import { FetchAllWorkspacesMessagesJob } from 'src/modules/messaging/commands/crons/fetch-all-workspaces-messages.job'; +import { fetchAllWorkspacesMessagesCronPattern } from 'src/modules/messaging/jobs/crons/fetch-all-workspaces-messages.cron.pattern'; +import { FetchAllWorkspacesMessagesJob } from 'src/modules/messaging/jobs/crons/fetch-all-workspaces-messages.job'; @Command({ name: 'fetch-all-workspaces-messages:cron:stop', diff --git a/packages/twenty-server/src/modules/messaging/commands/crons/fetch-all-messages-from-cache.cron-job.ts b/packages/twenty-server/src/modules/messaging/jobs/crons/fetch-all-messages-from-cache.cron-job.ts similarity index 100% rename from packages/twenty-server/src/modules/messaging/commands/crons/fetch-all-messages-from-cache.cron-job.ts rename to packages/twenty-server/src/modules/messaging/jobs/crons/fetch-all-messages-from-cache.cron-job.ts diff --git a/packages/twenty-server/src/modules/messaging/commands/crons/fetch-all-workspaces-messages.cron.pattern.ts b/packages/twenty-server/src/modules/messaging/jobs/crons/fetch-all-workspaces-messages.cron.pattern.ts similarity index 100% rename from packages/twenty-server/src/modules/messaging/commands/crons/fetch-all-workspaces-messages.cron.pattern.ts rename to packages/twenty-server/src/modules/messaging/jobs/crons/fetch-all-workspaces-messages.cron.pattern.ts diff --git a/packages/twenty-server/src/modules/messaging/commands/crons/fetch-all-workspaces-messages.job.ts b/packages/twenty-server/src/modules/messaging/jobs/crons/fetch-all-workspaces-messages.job.ts similarity index 100% rename from packages/twenty-server/src/modules/messaging/commands/crons/fetch-all-workspaces-messages.job.ts rename to packages/twenty-server/src/modules/messaging/jobs/crons/fetch-all-workspaces-messages.job.ts From d7af12f6070e47f90cb053724c131c260b9bdbc5 Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Thu, 4 Apr 2024 16:46:21 +0200 Subject: [PATCH 20/33] define cron pattern --- .../commands/start-google-calendar-sync.cron.command.ts | 5 ++--- .../calendar/jobs/crons/google-calendar-sync.cron.pattern.ts | 1 + 2 files changed, 3 insertions(+), 3 deletions(-) create mode 100644 packages/twenty-server/src/modules/calendar/jobs/crons/google-calendar-sync.cron.pattern.ts diff --git a/packages/twenty-server/src/modules/calendar/commands/start-google-calendar-sync.cron.command.ts b/packages/twenty-server/src/modules/calendar/commands/start-google-calendar-sync.cron.command.ts index 5cf95a6a8180..24a94ce5bfcb 100644 --- a/packages/twenty-server/src/modules/calendar/commands/start-google-calendar-sync.cron.command.ts +++ b/packages/twenty-server/src/modules/calendar/commands/start-google-calendar-sync.cron.command.ts @@ -4,6 +4,7 @@ import { Command, CommandRunner } from 'nest-commander'; import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; +import { googleCalendarSyncCronPattern } from 'src/modules/calendar/jobs/crons/google-calendar-sync.cron.pattern'; @Command({ name: 'google-calendar-sync:cron:start', @@ -22,9 +23,7 @@ export class StartGoogleCalendarSyncCronJobCommand extends CommandRunner { StartGoogleCalendarSyncCronJobCommand.name, undefined, { - repeat: { - every: 5000, - }, + repeat: { pattern: googleCalendarSyncCronPattern }, }, ); } diff --git a/packages/twenty-server/src/modules/calendar/jobs/crons/google-calendar-sync.cron.pattern.ts b/packages/twenty-server/src/modules/calendar/jobs/crons/google-calendar-sync.cron.pattern.ts new file mode 100644 index 000000000000..2a0d3f7449df --- /dev/null +++ b/packages/twenty-server/src/modules/calendar/jobs/crons/google-calendar-sync.cron.pattern.ts @@ -0,0 +1 @@ +export const googleCalendarSyncCronPattern = '*/5 * * * *'; From 85458468680f24abe7e9bfbbda8d68499c051b1e Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Thu, 4 Apr 2024 17:02:58 +0200 Subject: [PATCH 21/33] fix cron command --- .../commands/start-google-calendar-sync.cron.command.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/twenty-server/src/modules/calendar/commands/start-google-calendar-sync.cron.command.ts b/packages/twenty-server/src/modules/calendar/commands/start-google-calendar-sync.cron.command.ts index 24a94ce5bfcb..95ae8f8ec4c8 100644 --- a/packages/twenty-server/src/modules/calendar/commands/start-google-calendar-sync.cron.command.ts +++ b/packages/twenty-server/src/modules/calendar/commands/start-google-calendar-sync.cron.command.ts @@ -4,6 +4,7 @@ import { Command, CommandRunner } from 'nest-commander'; import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; +import { GoogleCalendarSyncCronJob } from 'src/modules/calendar/jobs/crons/google-calendar-sync.cron-job'; import { googleCalendarSyncCronPattern } from 'src/modules/calendar/jobs/crons/google-calendar-sync.cron.pattern'; @Command({ @@ -20,7 +21,7 @@ export class StartGoogleCalendarSyncCronJobCommand extends CommandRunner { async run(): Promise { await this.messageQueueService.addCron( - StartGoogleCalendarSyncCronJobCommand.name, + GoogleCalendarSyncCronJob.name, undefined, { repeat: { pattern: googleCalendarSyncCronPattern }, From f658c5effe67996f604bf2020942f2eee19e2cae Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Thu, 4 Apr 2024 18:26:59 +0200 Subject: [PATCH 22/33] event deletion is working --- .../services/google-calendar-sync.service.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts index 0872376a0571..c1c1f01a01ef 100644 --- a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts +++ b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync.service.ts @@ -130,6 +130,7 @@ export class GoogleCalendarSyncService { maxResults: 500, syncToken, pageToken: nextPageToken, + showDeleted: true, q: googleCalendarSearchFilterExcludeEmails(blocklistedEmails), }); @@ -215,13 +216,9 @@ export class GoogleCalendarSyncService { existingEventExternalIds.includes(event.externalId), ); - const eventsToDelete = existingCalendarChannelEventAssociations.filter( - (association) => !eventExternalIds.includes(association.eventExternalId), - ); - - const eventExternalIdsToDelete = eventsToDelete.map( - (association) => association.eventExternalId, - ); + const cancelledEventExternalIds = events + .filter((event) => event.status === 'cancelled') + .map((event) => event.id as string); const calendarChannelEventAssociationsToSave = eventsToSave.map( (event) => ({ @@ -336,7 +333,7 @@ export class GoogleCalendarSyncService { startTime = Date.now(); await this.calendarChannelEventAssociationRepository.deleteByEventExternalIdsAndCalendarChannelId( - eventExternalIdsToDelete, + cancelledEventExternalIds, calendarChannelId, workspaceId, transactionManager, From e2d494a23128a39ff5672475f7d5871531ef1585 Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Tue, 9 Apr 2024 17:04:26 +0200 Subject: [PATCH 23/33] fix import --- .../modules/messaging/commands/messaging-command.module.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/twenty-server/src/modules/messaging/commands/messaging-command.module.ts b/packages/twenty-server/src/modules/messaging/commands/messaging-command.module.ts index 0bfa32f1fc17..48e3c6636402 100644 --- a/packages/twenty-server/src/modules/messaging/commands/messaging-command.module.ts +++ b/packages/twenty-server/src/modules/messaging/commands/messaging-command.module.ts @@ -2,9 +2,8 @@ import { Module } from '@nestjs/common'; import { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.module'; import { ConnectedAccountObjectMetadata } from 'src/modules/connected-account/standard-objects/connected-account.object-metadata'; -import { GmailFetchMessagesFromCacheCronCommand } from 'src/modules/messaging/commands/crons/gmail-fetch-messages-from-cache.cron.command'; -import { GmailPartialSyncCronCommand } from 'src/modules/messaging/commands/crons/gmail-partial-sync.cron.command'; - +import { GmailFetchMessagesFromCacheCronCommand } from 'src/modules/messaging/jobs/crons/gmail-fetch-messages-from-cache.cron.command'; +import { GmailPartialSyncCronCommand } from 'src/modules/messaging/jobs/crons/gmail-partial-sync.cron.command'; @Module({ imports: [ ObjectMetadataRepositoryModule.forFeature([ConnectedAccountObjectMetadata]), From dca062140db4da8793d1c2f86d0fc2563aef5c2f Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Wed, 10 Apr 2024 12:04:50 +0200 Subject: [PATCH 24/33] update command --- .../commands/start-google-calendar-sync.cron.command.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/twenty-server/src/modules/calendar/commands/start-google-calendar-sync.cron.command.ts b/packages/twenty-server/src/modules/calendar/commands/start-google-calendar-sync.cron.command.ts index 95ae8f8ec4c8..6cfb0d994be1 100644 --- a/packages/twenty-server/src/modules/calendar/commands/start-google-calendar-sync.cron.command.ts +++ b/packages/twenty-server/src/modules/calendar/commands/start-google-calendar-sync.cron.command.ts @@ -8,7 +8,7 @@ import { GoogleCalendarSyncCronJob } from 'src/modules/calendar/jobs/crons/googl import { googleCalendarSyncCronPattern } from 'src/modules/calendar/jobs/crons/google-calendar-sync.cron.pattern'; @Command({ - name: 'google-calendar-sync:cron:start', + name: 'cron:calendar:google-calendar-sync', description: 'Starts a cron job to sync google calendar for all workspaces.', }) export class StartGoogleCalendarSyncCronJobCommand extends CommandRunner { From d3057e4c0fa6818a7b5acb79c259be25132febf7 Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Wed, 10 Apr 2024 12:06:05 +0200 Subject: [PATCH 25/33] renaming --- .../calendar/jobs/crons/google-calendar-sync.cron-job.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/twenty-server/src/modules/calendar/jobs/crons/google-calendar-sync.cron-job.ts b/packages/twenty-server/src/modules/calendar/jobs/crons/google-calendar-sync.cron-job.ts index 404ab91b905e..3db394f82804 100644 --- a/packages/twenty-server/src/modules/calendar/jobs/crons/google-calendar-sync.cron-job.ts +++ b/packages/twenty-server/src/modules/calendar/jobs/crons/google-calendar-sync.cron-job.ts @@ -62,11 +62,11 @@ export class GoogleCalendarSyncCronJob implements MessageQueueJob { ); for (const workspaceId of workspaceIdsWithDataSources) { - await this.startWorkspaceGoogleCalendarFullSync(workspaceId); + await this.startWorkspaceGoogleCalendarSync(workspaceId); } } - private async startWorkspaceGoogleCalendarFullSync( + private async startWorkspaceGoogleCalendarSync( workspaceId: string, ): Promise { const calendarChannels = From 2bde974ae771e428320e622ae98a2806f6983f18 Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Wed, 10 Apr 2024 12:07:05 +0200 Subject: [PATCH 26/33] delete duplicate --- .../fetch-all-messages-from-cache.cron-job.ts | 65 ------------------- 1 file changed, 65 deletions(-) delete mode 100644 packages/twenty-server/src/modules/messaging/jobs/crons/fetch-all-messages-from-cache.cron-job.ts diff --git a/packages/twenty-server/src/modules/messaging/jobs/crons/fetch-all-messages-from-cache.cron-job.ts b/packages/twenty-server/src/modules/messaging/jobs/crons/fetch-all-messages-from-cache.cron-job.ts deleted file mode 100644 index 8ed5ea6107e4..000000000000 --- a/packages/twenty-server/src/modules/messaging/jobs/crons/fetch-all-messages-from-cache.cron-job.ts +++ /dev/null @@ -1,65 +0,0 @@ -import { Injectable } from '@nestjs/common'; -import { InjectRepository } from '@nestjs/typeorm'; - -import { Repository, In } from 'typeorm'; - -import { MessageQueueJob } from 'src/engine/integrations/message-queue/interfaces/message-queue-job.interface'; - -import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity'; -import { DataSourceEntity } from 'src/engine/metadata-modules/data-source/data-source.entity'; -import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator'; -import { MessageChannelRepository } from 'src/modules/messaging/repositories/message-channel.repository'; -import { MessageChannelObjectMetadata } from 'src/modules/messaging/standard-objects/message-channel.object-metadata'; -import { GmailFetchMessageContentFromCacheService } from 'src/modules/messaging/services/gmail-fetch-message-content-from-cache/gmail-fetch-message-content-from-cache.service'; - -@Injectable() -export class FetchAllMessagesFromCacheCronJob - implements MessageQueueJob -{ - constructor( - @InjectRepository(Workspace, 'core') - private readonly workspaceRepository: Repository, - @InjectRepository(DataSourceEntity, 'metadata') - private readonly dataSourceRepository: Repository, - @InjectObjectMetadataRepository(MessageChannelObjectMetadata) - private readonly messageChannelRepository: MessageChannelRepository, - private readonly gmailFetchMessageContentFromCacheService: GmailFetchMessageContentFromCacheService, - ) {} - - async handle(): Promise { - const workspaceIds = ( - await this.workspaceRepository.find({ - where: { - subscriptionStatus: 'active', - }, - select: ['id'], - }) - ).map((workspace) => workspace.id); - - const dataSources = await this.dataSourceRepository.find({ - where: { - workspaceId: In(workspaceIds), - }, - }); - - const workspaceIdsWithDataSources = new Set( - dataSources.map((dataSource) => dataSource.workspaceId), - ); - - for (const workspaceId of workspaceIdsWithDataSources) { - await this.fetchWorkspaceMessages(workspaceId); - } - } - - private async fetchWorkspaceMessages(workspaceId: string): Promise { - const messageChannels = - await this.messageChannelRepository.getAll(workspaceId); - - for (const messageChannel of messageChannels) { - await this.gmailFetchMessageContentFromCacheService.fetchMessageContentFromCache( - workspaceId, - messageChannel.connectedAccountId, - ); - } - } -} From af933d2da664815b4af9405e5e5d9fa9d7145fec Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Wed, 10 Apr 2024 12:09:31 +0200 Subject: [PATCH 27/33] delete duplicate --- .../fetch-all-workspaces-messages.job.ts | 83 ------------------- 1 file changed, 83 deletions(-) delete mode 100644 packages/twenty-server/src/modules/messaging/jobs/crons/fetch-all-workspaces-messages.job.ts diff --git a/packages/twenty-server/src/modules/messaging/jobs/crons/fetch-all-workspaces-messages.job.ts b/packages/twenty-server/src/modules/messaging/jobs/crons/fetch-all-workspaces-messages.job.ts deleted file mode 100644 index 905a84d2290e..000000000000 --- a/packages/twenty-server/src/modules/messaging/jobs/crons/fetch-all-workspaces-messages.job.ts +++ /dev/null @@ -1,83 +0,0 @@ -import { Inject, Injectable, Logger } from '@nestjs/common'; -import { InjectRepository } from '@nestjs/typeorm'; - -import { Repository, In } from 'typeorm'; - -import { MessageQueueJob } from 'src/engine/integrations/message-queue/interfaces/message-queue-job.interface'; - -import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; -import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; -import { ConnectedAccountRepository } from 'src/modules/connected-account/repositories/connected-account.repository'; -import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity'; -import { DataSourceEntity } from 'src/engine/metadata-modules/data-source/data-source.entity'; -import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator'; -import { ConnectedAccountObjectMetadata } from 'src/modules/connected-account/standard-objects/connected-account.object-metadata'; -import { - GmailPartialSyncV2Job as GmailPartialSyncV2Job, - GmailPartialSyncV2JobData as GmailPartialSyncV2JobData, -} from 'src/modules/messaging/jobs/gmail-partial-sync-v2.job'; - -@Injectable() -export class GmailPartialSyncCronJob implements MessageQueueJob { - private readonly logger = new Logger(GmailPartialSyncCronJob.name); - - constructor( - @InjectRepository(Workspace, 'core') - private readonly workspaceRepository: Repository, - @InjectRepository(DataSourceEntity, 'metadata') - private readonly dataSourceRepository: Repository, - @Inject(MessageQueue.messagingQueue) - private readonly messageQueueService: MessageQueueService, - @InjectObjectMetadataRepository(ConnectedAccountObjectMetadata) - private readonly connectedAccountRepository: ConnectedAccountRepository, - ) {} - - async handle(): Promise { - const workspaceIds = ( - await this.workspaceRepository.find({ - where: { - subscriptionStatus: 'active', - }, - select: ['id'], - }) - ).map((workspace) => workspace.id); - - const dataSources = await this.dataSourceRepository.find({ - where: { - workspaceId: In(workspaceIds), - }, - }); - - const workspaceIdsWithDataSources = new Set( - dataSources.map((dataSource) => dataSource.workspaceId), - ); - - for (const workspaceId of workspaceIdsWithDataSources) { - await this.enqueuePartialSyncs(workspaceId); - } - } - - private async enqueuePartialSyncs(workspaceId: string): Promise { - try { - const connectedAccounts = - await this.connectedAccountRepository.getAll(workspaceId); - - for (const connectedAccount of connectedAccounts) { - await this.messageQueueService.add( - GmailPartialSyncV2Job.name, - { - workspaceId, - connectedAccountId: connectedAccount.id, - }, - ); - } - } catch (error) { - this.logger.error( - `Error while fetching workspace messages for workspace ${workspaceId}`, - ); - this.logger.error(error); - - return; - } - } -} From 1fb2bba5b66184b01c2f7ff54303ff27ddb87c32 Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Wed, 10 Apr 2024 12:16:18 +0200 Subject: [PATCH 28/33] rename job --- .../src/engine/integrations/message-queue/jobs.module.ts | 2 +- .../commands/start-google-calendar-sync.cron.command.ts | 2 +- ...lendar-sync.cron-job.ts => google-calendar-sync.cron.job.ts} | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename packages/twenty-server/src/modules/calendar/jobs/crons/{google-calendar-sync.cron-job.ts => google-calendar-sync.cron.job.ts} (100%) diff --git a/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts b/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts index 7c162472997e..5c633d8ca8b9 100644 --- a/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts +++ b/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts @@ -48,7 +48,7 @@ import { FetchAllMessagesFromCacheCronJob } from 'src/modules/messaging/jobs/cro import { GmailFullSyncV2Job } from 'src/modules/messaging/jobs/gmail-full-sync-v2.job'; import { GmailPartialSyncV2Job } from 'src/modules/messaging/jobs/gmail-partial-sync-v2.job'; import { GmailPartialSyncV2Module } from 'src/modules/messaging/services/gmail-partial-sync-v2/gmail-partial-sync-v2.module'; -import { GoogleCalendarSyncCronJob } from 'src/modules/calendar/jobs/crons/google-calendar-sync.cron-job'; +import { GoogleCalendarSyncCronJob } from 'src/modules/calendar/jobs/crons/google-calendar-sync.cron.job'; import { CalendarEventParticipantModule } from 'src/modules/calendar/services/calendar-event-participant/calendar-event-participant.module'; import { UnmatchParticipantJob } from 'src/modules/connected-account/jobs/unmatch-participant.job'; import { DeleteConnectedAccountAssociatedCalendarDataJob } from 'src/modules/calendar/jobs/delete-connected-account-associated-calendar-data.job'; diff --git a/packages/twenty-server/src/modules/calendar/commands/start-google-calendar-sync.cron.command.ts b/packages/twenty-server/src/modules/calendar/commands/start-google-calendar-sync.cron.command.ts index 6cfb0d994be1..b0da673bc82b 100644 --- a/packages/twenty-server/src/modules/calendar/commands/start-google-calendar-sync.cron.command.ts +++ b/packages/twenty-server/src/modules/calendar/commands/start-google-calendar-sync.cron.command.ts @@ -4,7 +4,7 @@ import { Command, CommandRunner } from 'nest-commander'; import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; -import { GoogleCalendarSyncCronJob } from 'src/modules/calendar/jobs/crons/google-calendar-sync.cron-job'; +import { GoogleCalendarSyncCronJob } from 'src/modules/calendar/jobs/crons/google-calendar-sync.cron.job'; import { googleCalendarSyncCronPattern } from 'src/modules/calendar/jobs/crons/google-calendar-sync.cron.pattern'; @Command({ diff --git a/packages/twenty-server/src/modules/calendar/jobs/crons/google-calendar-sync.cron-job.ts b/packages/twenty-server/src/modules/calendar/jobs/crons/google-calendar-sync.cron.job.ts similarity index 100% rename from packages/twenty-server/src/modules/calendar/jobs/crons/google-calendar-sync.cron-job.ts rename to packages/twenty-server/src/modules/calendar/jobs/crons/google-calendar-sync.cron.job.ts From f08a139e5fca0882b557496376415dfe729f175f Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Wed, 10 Apr 2024 12:18:40 +0200 Subject: [PATCH 29/33] fix imports --- .../src/engine/integrations/message-queue/jobs.module.ts | 2 +- .../jobs/crons/gmail-fetch-messages-from-cache.cron.command.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts b/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts index 5c633d8ca8b9..0160dc7e82bb 100644 --- a/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts +++ b/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts @@ -44,7 +44,6 @@ import { EventObjectMetadata } from 'src/modules/event/standard-objects/event.ob import { HandleWorkspaceMemberDeletedJob } from 'src/engine/core-modules/workspace/handle-workspace-member-deleted.job'; import { GmailFullSynV2Module } from 'src/modules/messaging/services/gmail-full-sync-v2/gmail-full-sync.v2.module'; import { GmailFetchMessageContentFromCacheModule } from 'src/modules/messaging/services/gmail-fetch-message-content-from-cache/gmail-fetch-message-content-from-cache.module'; -import { FetchAllMessagesFromCacheCronJob } from 'src/modules/messaging/jobs/crons/fetch-all-messages-from-cache.cron-job'; import { GmailFullSyncV2Job } from 'src/modules/messaging/jobs/gmail-full-sync-v2.job'; import { GmailPartialSyncV2Job } from 'src/modules/messaging/jobs/gmail-partial-sync-v2.job'; import { GmailPartialSyncV2Module } from 'src/modules/messaging/services/gmail-partial-sync-v2/gmail-partial-sync-v2.module'; @@ -52,6 +51,7 @@ import { GoogleCalendarSyncCronJob } from 'src/modules/calendar/jobs/crons/googl import { CalendarEventParticipantModule } from 'src/modules/calendar/services/calendar-event-participant/calendar-event-participant.module'; import { UnmatchParticipantJob } from 'src/modules/connected-account/jobs/unmatch-participant.job'; import { DeleteConnectedAccountAssociatedCalendarDataJob } from 'src/modules/calendar/jobs/delete-connected-account-associated-calendar-data.job'; +import { FetchAllMessagesFromCacheCronJob } from 'src/modules/messaging/jobs/crons/fetch-all-messages-from-cache.cron.job'; @Module({ imports: [ diff --git a/packages/twenty-server/src/modules/messaging/jobs/crons/gmail-fetch-messages-from-cache.cron.command.ts b/packages/twenty-server/src/modules/messaging/jobs/crons/gmail-fetch-messages-from-cache.cron.command.ts index bb26a2802e76..9f0013f9cbbb 100644 --- a/packages/twenty-server/src/modules/messaging/jobs/crons/gmail-fetch-messages-from-cache.cron.command.ts +++ b/packages/twenty-server/src/modules/messaging/jobs/crons/gmail-fetch-messages-from-cache.cron.command.ts @@ -4,7 +4,7 @@ import { Command, CommandRunner } from 'nest-commander'; import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; -import { FetchAllMessagesFromCacheCronJob } from 'src/modules/messaging/jobs/crons/fetch-all-messages-from-cache.cron-job'; +import { FetchAllMessagesFromCacheCronJob } from 'src/modules/messaging/jobs/crons/fetch-all-messages-from-cache.cron.job'; @Command({ name: 'cron:messaging:gmail-fetch-messages-from-cache', From bb44589e1f5e156f54893ae8909b488a5a3ad219 Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Wed, 10 Apr 2024 14:04:13 +0200 Subject: [PATCH 30/33] fix jobs being provided twice --- .../src/engine/integrations/message-queue/jobs.module.ts | 8 -------- 1 file changed, 8 deletions(-) diff --git a/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts b/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts index 0160dc7e82bb..4ab0ef59cadc 100644 --- a/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts +++ b/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts @@ -86,14 +86,6 @@ import { FetchAllMessagesFromCacheCronJob } from 'src/modules/messaging/jobs/cro CalendarEventParticipantModule, ], providers: [ - { - provide: GmailFullSyncV2Job.name, - useClass: GmailFullSyncV2Job, - }, - { - provide: GmailPartialSyncV2Job.name, - useClass: GmailPartialSyncV2Job, - }, { provide: GoogleCalendarSyncJob.name, useClass: GoogleCalendarSyncJob, From a6f19bf41f7318e27dcc5555005b5bdd12969146 Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Wed, 10 Apr 2024 14:05:36 +0200 Subject: [PATCH 31/33] modified according to comments --- .../calendar/utils/format-google-calendar-event.util.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/twenty-server/src/modules/calendar/utils/format-google-calendar-event.util.ts b/packages/twenty-server/src/modules/calendar/utils/format-google-calendar-event.util.ts index fb03ec2fb8cb..6636569261a2 100644 --- a/packages/twenty-server/src/modules/calendar/utils/format-google-calendar-event.util.ts +++ b/packages/twenty-server/src/modules/calendar/utils/format-google-calendar-event.util.ts @@ -8,7 +8,8 @@ export const formatGoogleCalendarEvent = ( event: calendarV3.Schema$Event, iCalUIDCalendarEventIdMap: Map, ): CalendarEventWithParticipants => { - const id = iCalUIDCalendarEventIdMap.get(event.iCalUID ?? '') ?? v4(); + const id = + (event.iCalUID && iCalUIDCalendarEventIdMap.get(event.iCalUID)) ?? v4(); const formatResponseStatus = (status: string | null | undefined) => { switch (status) { From da3097b0ce2489b115c5981343f4ad59ca83f818 Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Wed, 10 Apr 2024 15:42:38 +0200 Subject: [PATCH 32/33] fix duplicate and move file --- .../commands/start-google-calendar-sync.cron.command.ts | 2 +- .../crons/{ => pattern}/google-calendar-sync.cron.pattern.ts | 0 .../jobs/crons/fetch-all-workspaces-messages.cron.pattern.ts | 1 - 3 files changed, 1 insertion(+), 2 deletions(-) rename packages/twenty-server/src/modules/calendar/jobs/crons/{ => pattern}/google-calendar-sync.cron.pattern.ts (100%) delete mode 100644 packages/twenty-server/src/modules/messaging/jobs/crons/fetch-all-workspaces-messages.cron.pattern.ts diff --git a/packages/twenty-server/src/modules/calendar/commands/start-google-calendar-sync.cron.command.ts b/packages/twenty-server/src/modules/calendar/commands/start-google-calendar-sync.cron.command.ts index b0da673bc82b..a0f867659a0a 100644 --- a/packages/twenty-server/src/modules/calendar/commands/start-google-calendar-sync.cron.command.ts +++ b/packages/twenty-server/src/modules/calendar/commands/start-google-calendar-sync.cron.command.ts @@ -5,7 +5,7 @@ import { Command, CommandRunner } from 'nest-commander'; import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; import { GoogleCalendarSyncCronJob } from 'src/modules/calendar/jobs/crons/google-calendar-sync.cron.job'; -import { googleCalendarSyncCronPattern } from 'src/modules/calendar/jobs/crons/google-calendar-sync.cron.pattern'; +import { googleCalendarSyncCronPattern } from 'src/modules/calendar/jobs/crons/pattern/google-calendar-sync.cron.pattern'; @Command({ name: 'cron:calendar:google-calendar-sync', diff --git a/packages/twenty-server/src/modules/calendar/jobs/crons/google-calendar-sync.cron.pattern.ts b/packages/twenty-server/src/modules/calendar/jobs/crons/pattern/google-calendar-sync.cron.pattern.ts similarity index 100% rename from packages/twenty-server/src/modules/calendar/jobs/crons/google-calendar-sync.cron.pattern.ts rename to packages/twenty-server/src/modules/calendar/jobs/crons/pattern/google-calendar-sync.cron.pattern.ts diff --git a/packages/twenty-server/src/modules/messaging/jobs/crons/fetch-all-workspaces-messages.cron.pattern.ts b/packages/twenty-server/src/modules/messaging/jobs/crons/fetch-all-workspaces-messages.cron.pattern.ts deleted file mode 100644 index 83d6b6db0449..000000000000 --- a/packages/twenty-server/src/modules/messaging/jobs/crons/fetch-all-workspaces-messages.cron.pattern.ts +++ /dev/null @@ -1 +0,0 @@ -export const fetchAllWorkspacesMessagesCronPattern = '*/5 * * * *'; From 326271ab59ee9e938337def33956e7cf91671b38 Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Wed, 10 Apr 2024 15:46:21 +0200 Subject: [PATCH 33/33] fix import --- .../messaging/jobs/crons/gmail-partial-sync.cron.command.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/twenty-server/src/modules/messaging/jobs/crons/gmail-partial-sync.cron.command.ts b/packages/twenty-server/src/modules/messaging/jobs/crons/gmail-partial-sync.cron.command.ts index f6f9c1f780f7..2cfe7be30777 100644 --- a/packages/twenty-server/src/modules/messaging/jobs/crons/gmail-partial-sync.cron.command.ts +++ b/packages/twenty-server/src/modules/messaging/jobs/crons/gmail-partial-sync.cron.command.ts @@ -4,8 +4,8 @@ import { Command, CommandRunner } from 'nest-commander'; import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; -import { fetchAllWorkspacesMessagesCronPattern } from 'src/modules/messaging/jobs/crons/fetch-all-workspaces-messages.cron.pattern'; import { GmailPartialSyncCronJob } from 'src/modules/messaging/jobs/crons/gmail-partial-sync.cron.job'; +import { fetchAllWorkspacesMessagesCronPattern } from 'src/modules/messaging/jobs/crons/patterns/fetch-all-workspaces-messages.cron.pattern'; @Command({ name: 'cron:messaging:gmail-partial-sync',