forked from twentyhq/twenty
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
4710 implement google calendar incremental sync (twentyhq#4822)
Closes twentyhq#4710
- Loading branch information
1 parent
a986995
commit 0a6dc75
Showing
20 changed files
with
333 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...es/twenty-server/src/modules/calendar/commands/start-google-calendar-sync.cron.command.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; | ||
import { GoogleCalendarSyncCronJob } from 'src/modules/calendar/jobs/crons/google-calendar-sync.cron.job'; | ||
import { googleCalendarSyncCronPattern } from 'src/modules/calendar/jobs/crons/pattern/google-calendar-sync.cron.pattern'; | ||
|
||
@Command({ | ||
name: 'cron:calendar:google-calendar-sync', | ||
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<void> { | ||
await this.messageQueueService.addCron<undefined>( | ||
GoogleCalendarSyncCronJob.name, | ||
undefined, | ||
{ | ||
repeat: { pattern: googleCalendarSyncCronPattern }, | ||
}, | ||
); | ||
} | ||
} |
5 changes: 3 additions & 2 deletions
5
...es/twenty-server/src/modules/calendar/commands/workspace-calendar-sync-commands.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
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'; | ||
import { StartGoogleCalendarSyncCronJobCommand } from 'src/modules/calendar/commands/start-google-calendar-sync.cron.command'; | ||
|
||
@Module({ | ||
imports: [ | ||
ObjectMetadataRepositoryModule.forFeature([ConnectedAccountObjectMetadata]), | ||
], | ||
providers: [GoogleCalendarFullSyncCommand], | ||
providers: [GoogleCalendarSyncCommand, StartGoogleCalendarSyncCronJobCommand], | ||
}) | ||
export class WorkspaceCalendarSyncCommandsModule {} |
82 changes: 82 additions & 0 deletions
82
packages/twenty-server/src/modules/calendar/jobs/crons/google-calendar-sync.cron.job.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { 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<undefined> { | ||
constructor( | ||
@InjectRepository(Workspace, 'core') | ||
private readonly workspaceRepository: Repository<Workspace>, | ||
@InjectRepository(DataSourceEntity, 'metadata') | ||
private readonly dataSourceRepository: Repository<DataSourceEntity>, | ||
@InjectObjectMetadataRepository(CalendarChannelObjectMetadata) | ||
private readonly calendarChannelRepository: CalendarChannelRepository, | ||
@InjectRepository(FeatureFlagEntity, 'core') | ||
private readonly featureFlagRepository: Repository<FeatureFlagEntity>, | ||
private readonly googleCalendarSyncService: GoogleCalendarSyncService, | ||
) {} | ||
|
||
async handle(): Promise<void> { | ||
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.startWorkspaceGoogleCalendarSync(workspaceId); | ||
} | ||
} | ||
|
||
private async startWorkspaceGoogleCalendarSync( | ||
workspaceId: string, | ||
): Promise<void> { | ||
const calendarChannels = | ||
await this.calendarChannelRepository.getAll(workspaceId); | ||
|
||
for (const calendarChannel of calendarChannels) { | ||
await this.googleCalendarSyncService.startGoogleCalendarSync( | ||
workspaceId, | ||
calendarChannel.connectedAccountId, | ||
); | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...wenty-server/src/modules/calendar/jobs/crons/pattern/google-calendar-sync.cron.pattern.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const googleCalendarSyncCronPattern = '*/5 * * * *'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.