Reorganise calendar module (#6089)

Refactor Calendar into functional sub modules
<img width="437" alt="image"
src="https://github.com/twentyhq/twenty/assets/12035771/d9de3285-a226-4fe8-b3ef-2d8a21def2a5">

---------

Co-authored-by: bosiraphael <raphael.bosi@gmail.com>
This commit is contained in:
Charles Bochet
2024-07-02 13:55:11 +02:00
committed by GitHub
parent ea7d52fba8
commit f8dd2cc733
66 changed files with 267 additions and 401 deletions

View File

@ -0,0 +1,58 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { BillingModule } from 'src/engine/core-modules/billing/billing.module';
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 { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.module';
import { TwentyORMModule } from 'src/engine/twenty-orm/twenty-orm.module';
import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module';
import { CalendarEventCleanerModule } from 'src/modules/calendar/calendar-event-cleaner/calendar-event-cleaner.module';
import { CalendarEventsImportCronCommand } from 'src/modules/calendar/calendar-event-import-manager/crons/commands/calendar-events-import.cron.command';
import { CalendarEventsImportCronJob } from 'src/modules/calendar/calendar-event-import-manager/crons/jobs/calendar-events-import.cron.job';
import { GoogleCalendarDriverModule } from 'src/modules/calendar/calendar-event-import-manager/drivers/google-calendar/google-calendar-driver.module';
import { CalendarEventsImportJob } from 'src/modules/calendar/calendar-event-import-manager/jobs/calendar-events-import.job';
import { CalendarEventsImportService } from 'src/modules/calendar/calendar-event-import-manager/services/calendar-events-import.service';
import { CalendarEventParticipantModule } from 'src/modules/calendar/calendar-event-participant-manager/calendar-event-participant.module';
import { CalendarChannelEventAssociationWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-channel-event-association.workspace-entity';
import { CalendarChannelWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-channel.workspace-entity';
import { CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event-participant.workspace-entity';
import { CalendarEventWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event.workspace-entity';
import { GoogleAPIRefreshAccessTokenModule } from 'src/modules/connected-account/services/google-api-refresh-access-token/google-api-refresh-access-token.module';
import { BlocklistWorkspaceEntity } from 'src/modules/connected-account/standard-objects/blocklist.workspace-entity';
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
@Module({
imports: [
TwentyORMModule.forFeature([
CalendarEventWorkspaceEntity,
CalendarChannelWorkspaceEntity,
CalendarChannelEventAssociationWorkspaceEntity,
CalendarEventParticipantWorkspaceEntity,
]),
ObjectMetadataRepositoryModule.forFeature([
ConnectedAccountWorkspaceEntity,
BlocklistWorkspaceEntity,
PersonWorkspaceEntity,
WorkspaceMemberWorkspaceEntity,
]),
CalendarEventParticipantModule,
TypeOrmModule.forFeature([FeatureFlagEntity], 'core'),
TypeOrmModule.forFeature([DataSourceEntity], 'metadata'),
WorkspaceDataSourceModule,
CalendarEventCleanerModule,
GoogleCalendarDriverModule,
BillingModule,
GoogleAPIRefreshAccessTokenModule,
],
providers: [
CalendarEventsImportService,
CalendarEventsImportCronJob,
CalendarEventsImportCronCommand,
CalendarEventsImportJob,
],
exports: [CalendarEventsImportService],
})
export class CalendarEventImportManagerModule {}

View File

@ -0,0 +1,31 @@
import { Command, CommandRunner } from 'nest-commander';
import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
import { CalendarEventsImportCronJob } from 'src/modules/calendar/calendar-event-import-manager/crons/jobs/calendar-events-import.cron.job';
const CALENDAR_EVENTS_IMPORT_CRON_PATTERN = '*/5 * * * *';
@Command({
name: 'cron:calendar:calendar-events-import',
description: 'Starts a cron job to import calendar events',
})
export class CalendarEventsImportCronCommand extends CommandRunner {
constructor(
@InjectMessageQueue(MessageQueue.cronQueue)
private readonly messageQueueService: MessageQueueService,
) {
super();
}
async run(): Promise<void> {
await this.messageQueueService.addCron<undefined>(
CalendarEventsImportCronJob.name,
undefined,
{
repeat: { pattern: CALENDAR_EVENTS_IMPORT_CRON_PATTERN },
},
);
}
}

View File

@ -0,0 +1,72 @@
import { InjectRepository } from '@nestjs/typeorm';
import { Scope } from '@nestjs/common';
import { Repository, In } from 'typeorm';
import { DataSourceEntity } from 'src/engine/metadata-modules/data-source/data-source.entity';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { Processor } from 'src/engine/integrations/message-queue/decorators/processor.decorator';
import { Process } from 'src/engine/integrations/message-queue/decorators/process.decorator';
import {
CalendarEventsImportJobData,
CalendarEventsImportJob,
} from 'src/modules/calendar/calendar-event-import-manager/jobs/calendar-events-import.job';
import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
import { InjectWorkspaceRepository } from 'src/engine/twenty-orm/decorators/inject-workspace-repository.decorator';
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
import { BillingService } from 'src/engine/core-modules/billing/billing.service';
import { CalendarChannelWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-channel.workspace-entity';
@Processor({
queueName: MessageQueue.cronQueue,
scope: Scope.REQUEST,
})
export class CalendarEventsImportCronJob {
constructor(
@InjectRepository(DataSourceEntity, 'metadata')
private readonly dataSourceRepository: Repository<DataSourceEntity>,
@InjectWorkspaceRepository(CalendarChannelWorkspaceEntity)
private readonly calendarChannelRepository: WorkspaceRepository<CalendarChannelWorkspaceEntity>,
@InjectMessageQueue(MessageQueue.calendarQueue)
private readonly messageQueueService: MessageQueueService,
private readonly billingService: BillingService,
) {}
@Process(CalendarEventsImportCronJob.name)
async handle(): Promise<void> {
const workspaceIds =
await this.billingService.getActiveSubscriptionWorkspaceIds();
const dataSources = await this.dataSourceRepository.find({
where: {
workspaceId: In(workspaceIds),
},
});
const workspaceIdsWithDataSources = new Set(
dataSources.map((dataSource) => dataSource.workspaceId),
);
for (const workspaceId of workspaceIdsWithDataSources) {
const calendarChannels = await this.calendarChannelRepository.find({});
for (const calendarChannel of calendarChannels) {
if (!calendarChannel?.isSyncEnabled) {
continue;
}
await this.messageQueueService.add<CalendarEventsImportJobData>(
CalendarEventsImportJob.name,
{
workspaceId,
connectedAccountId: calendarChannel.connectedAccount.id,
},
{
retryLimit: 2,
},
);
}
}
}
}

View File

@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { EnvironmentModule } from 'src/engine/integrations/environment/environment.module';
import { GoogleCalendarClientProvider } from 'src/modules/calendar/calendar-event-import-manager/drivers/google-calendar/providers/google-calendar.provider';
import { OAuth2ClientManagerModule } from 'src/modules/connected-account/oauth2-client-manager/oauth2-client-manager.module';
@Module({
imports: [EnvironmentModule, OAuth2ClientManagerModule],
providers: [GoogleCalendarClientProvider],
exports: [GoogleCalendarClientProvider],
})
export class GoogleCalendarDriverModule {}

View File

@ -0,0 +1,27 @@
import { Injectable } from '@nestjs/common';
import { calendar_v3 as calendarV3, google } from 'googleapis';
import { OAuth2ClientManagerService } from 'src/modules/connected-account/oauth2-client-manager/services/oauth2-client-manager.service';
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
@Injectable()
export class GoogleCalendarClientProvider {
constructor(
private readonly oAuth2ClientManagerService: OAuth2ClientManagerService,
) {}
public async getGoogleCalendarClient(
connectedAccount: ConnectedAccountWorkspaceEntity,
): Promise<calendarV3.Calendar> {
const oAuth2Client =
await this.oAuth2ClientManagerService.getOAuth2Client(connectedAccount);
const googleCalendarClient = google.calendar({
version: 'v3',
auth: oAuth2Client,
});
return googleCalendarClient;
}
}

View File

@ -0,0 +1,68 @@
import { Logger, Scope } from '@nestjs/common';
import { GoogleAPIRefreshAccessTokenService } from 'src/modules/connected-account/services/google-api-refresh-access-token/google-api-refresh-access-token.service';
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
import { ConnectedAccountRepository } from 'src/modules/connected-account/repositories/connected-account.repository';
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
import { Processor } from 'src/engine/integrations/message-queue/decorators/processor.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { Process } from 'src/engine/integrations/message-queue/decorators/process.decorator';
import { CalendarEventsImportService } from 'src/modules/calendar/calendar-event-import-manager/services/calendar-events-import.service';
export type CalendarEventsImportJobData = {
workspaceId: string;
connectedAccountId: string;
};
@Processor({
queueName: MessageQueue.calendarQueue,
scope: Scope.REQUEST,
})
export class CalendarEventsImportJob {
private readonly logger = new Logger(CalendarEventsImportJob.name);
constructor(
private readonly googleAPIsRefreshAccessTokenService: GoogleAPIRefreshAccessTokenService,
private readonly googleCalendarSyncService: CalendarEventsImportService,
@InjectObjectMetadataRepository(ConnectedAccountWorkspaceEntity)
private readonly connectedAccountRepository: ConnectedAccountRepository,
) {}
@Process(CalendarEventsImportJob.name)
async handle(data: CalendarEventsImportJobData): Promise<void> {
this.logger.log(
`google calendar sync for workspace ${data.workspaceId} and account ${data.connectedAccountId}`,
);
try {
const { connectedAccountId, workspaceId } = data;
const connectedAccount = await this.connectedAccountRepository.getById(
connectedAccountId,
workspaceId,
);
if (!connectedAccount) {
throw new Error(
`No connected account found for ${connectedAccountId} in workspace ${workspaceId}`,
);
}
await this.googleAPIsRefreshAccessTokenService.refreshAndSaveAccessToken(
connectedAccount,
workspaceId,
);
} catch (e) {
this.logger.error(
`Error refreshing access token for connected account ${data.connectedAccountId} in workspace ${data.workspaceId}`,
e,
);
return;
}
await this.googleCalendarSyncService.processCalendarEventsImport(
data.workspaceId,
data.connectedAccountId,
);
}
}

View File

@ -0,0 +1,600 @@
import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { Any, Repository } from 'typeorm';
import { calendar_v3 as calendarV3 } from 'googleapis';
import { GaxiosError } from 'gaxios';
import { ConnectedAccountRepository } from 'src/modules/connected-account/repositories/connected-account.repository';
import { BlocklistRepository } from 'src/modules/connected-account/repositories/blocklist.repository';
import {
FeatureFlagEntity,
FeatureFlagKeys,
} from 'src/engine/core-modules/feature-flag/feature-flag.entity';
import { formatGoogleCalendarEvent } from 'src/modules/calendar/calendar-event-import-manager/utils/format-google-calendar-event.util';
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
import { BlocklistWorkspaceEntity } from 'src/modules/connected-account/standard-objects/blocklist.workspace-entity';
import {
CalendarEventParticipant,
CalendarEventWithParticipants,
} from 'src/modules/calendar/common/types/calendar-event';
import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
import {
CreateCompanyAndContactJob,
CreateCompanyAndContactJobData,
} from 'src/modules/connected-account/auto-companies-and-contacts-creation/jobs/create-company-and-contact.job';
import { InjectWorkspaceRepository } from 'src/engine/twenty-orm/decorators/inject-workspace-repository.decorator';
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
import { isDefined } from 'src/utils/is-defined';
import { WorkspaceDataSource } from 'src/engine/twenty-orm/datasource/workspace.datasource';
import { InjectWorkspaceDatasource } from 'src/engine/twenty-orm/decorators/inject-workspace-datasource.decorator';
import { CalendarEventCleanerService } from 'src/modules/calendar/calendar-event-cleaner/services/calendar-event-cleaner.service';
import { CalendarEventParticipantService } from 'src/modules/calendar/calendar-event-participant-manager/services/calendar-event-participant.service';
import { GoogleCalendarClientProvider } from 'src/modules/calendar/calendar-event-import-manager/drivers/google-calendar/providers/google-calendar.provider';
import { CalendarChannelEventAssociationWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-channel-event-association.workspace-entity';
import { CalendarChannelWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-channel.workspace-entity';
import { CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event-participant.workspace-entity';
import { CalendarEventWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event.workspace-entity';
import { filterOutBlocklistedEvents } from 'src/modules/calendar/calendar-event-import-manager/utils/filter-out-blocklisted-events.util';
@Injectable()
export class CalendarEventsImportService {
private readonly logger = new Logger(CalendarEventsImportService.name);
constructor(
private readonly googleCalendarClientProvider: GoogleCalendarClientProvider,
@InjectObjectMetadataRepository(ConnectedAccountWorkspaceEntity)
private readonly connectedAccountRepository: ConnectedAccountRepository,
@InjectWorkspaceRepository(CalendarEventWorkspaceEntity)
private readonly calendarEventRepository: WorkspaceRepository<CalendarEventWorkspaceEntity>,
@InjectWorkspaceRepository(CalendarChannelWorkspaceEntity)
private readonly calendarChannelRepository: WorkspaceRepository<CalendarChannelWorkspaceEntity>,
@InjectWorkspaceRepository(CalendarChannelEventAssociationWorkspaceEntity)
private readonly calendarChannelEventAssociationRepository: WorkspaceRepository<CalendarChannelEventAssociationWorkspaceEntity>,
@InjectWorkspaceRepository(CalendarEventParticipantWorkspaceEntity)
private readonly calendarEventParticipantsRepository: WorkspaceRepository<CalendarEventParticipantWorkspaceEntity>,
@InjectObjectMetadataRepository(BlocklistWorkspaceEntity)
private readonly blocklistRepository: BlocklistRepository,
@InjectRepository(FeatureFlagEntity, 'core')
private readonly featureFlagRepository: Repository<FeatureFlagEntity>,
@InjectWorkspaceDatasource()
private readonly workspaceDataSource: WorkspaceDataSource,
private readonly calendarEventCleanerService: CalendarEventCleanerService,
private readonly calendarEventParticipantsService: CalendarEventParticipantService,
@InjectMessageQueue(MessageQueue.contactCreationQueue)
private readonly messageQueueService: MessageQueueService,
private readonly eventEmitter: EventEmitter2,
) {}
public async processCalendarEventsImport(
workspaceId: string,
connectedAccountId: string,
emailOrDomainToReimport?: string,
): Promise<void> {
const connectedAccount = await this.connectedAccountRepository.getById(
connectedAccountId,
workspaceId,
);
if (!connectedAccount) {
return;
}
const refreshToken = connectedAccount.refreshToken;
const workspaceMemberId = connectedAccount.accountOwnerId;
if (!refreshToken) {
throw new Error(
`No refresh token found for connected account ${connectedAccountId} in workspace ${workspaceId} during sync`,
);
}
const calendarChannel = await this.calendarChannelRepository.findOneBy({
connectedAccount: {
id: connectedAccountId,
},
});
const syncToken = calendarChannel?.syncCursor || undefined;
if (!calendarChannel) {
return;
}
const calendarChannelId = calendarChannel.id;
const { events, nextSyncToken } = await this.getEventsFromGoogleCalendar(
connectedAccount,
workspaceId,
emailOrDomainToReimport,
syncToken,
);
if (!events || events?.length === 0) {
this.logger.log(
`google calendar sync for workspace ${workspaceId} and account ${connectedAccountId} done with nothing to import.`,
);
return;
}
if (!workspaceMemberId) {
throw new Error(
`Workspace member ID is undefined for connected account ${connectedAccountId} in workspace ${workspaceId}`,
);
}
const blocklist = await this.getBlocklist(workspaceMemberId, workspaceId);
let filteredEvents = filterOutBlocklistedEvents(
calendarChannel.handle,
events,
blocklist,
).filter((event) => event.status !== 'cancelled');
if (emailOrDomainToReimport) {
filteredEvents = filteredEvents.filter(
(event) =>
event.attendees?.some(
(attendee) => attendee.email?.endsWith(emailOrDomainToReimport),
),
);
}
const cancelledEventExternalIds = filteredEvents
.filter((event) => event.status === 'cancelled')
.map((event) => event.id as string);
const existingCalendarEvents = await this.calendarEventRepository.find({
where: {
iCalUID: Any(filteredEvents.map((event) => event.iCalUID as string)),
},
});
const iCalUIDCalendarEventIdMap = new Map(
existingCalendarEvents.map((calendarEvent) => [
calendarEvent.iCalUID,
calendarEvent.id,
]),
);
const formattedEvents = filteredEvents.map((event) =>
formatGoogleCalendarEvent(event, iCalUIDCalendarEventIdMap),
);
// TODO: When we will be able to add unicity contraint on iCalUID, we will do a INSERT ON CONFLICT DO UPDATE
let startTime = Date.now();
const existingEventsICalUIDs = existingCalendarEvents.map(
(calendarEvent) => calendarEvent.iCalUID,
);
let endTime = Date.now();
const eventsToSave = formattedEvents.filter(
(calendarEvent) =>
!existingEventsICalUIDs.includes(calendarEvent.iCalUID),
);
const eventsToUpdate = formattedEvents.filter((calendarEvent) =>
existingEventsICalUIDs.includes(calendarEvent.iCalUID),
);
startTime = Date.now();
const existingCalendarChannelEventAssociations =
await this.calendarChannelEventAssociationRepository.find({
where: {
eventExternalId: Any(
formattedEvents.map((calendarEvent) => calendarEvent.id),
),
calendarChannel: {
id: calendarChannelId,
},
},
});
endTime = Date.now();
this.logger.log(
`google calendar sync for workspace ${workspaceId} and account ${connectedAccountId}: getting existing calendar channel event associations in ${
endTime - startTime
}ms.`,
);
const calendarChannelEventAssociationsToSave = formattedEvents
.filter(
(calendarEvent) =>
!existingCalendarChannelEventAssociations.some(
(association) => association.eventExternalId === calendarEvent.id,
),
)
.map((calendarEvent) => ({
calendarEventId: calendarEvent.id,
eventExternalId: calendarEvent.externalId,
calendarChannelId,
}));
if (events.length > 0) {
await this.saveGoogleCalendarEvents(
eventsToSave,
eventsToUpdate,
calendarChannelEventAssociationsToSave,
connectedAccount,
calendarChannel,
workspaceId,
);
startTime = Date.now();
await this.calendarChannelEventAssociationRepository.delete({
eventExternalId: Any(cancelledEventExternalIds),
calendarChannel: {
id: calendarChannelId,
},
});
endTime = Date.now();
this.logger.log(
`google calendar sync for workspace ${workspaceId} and account ${connectedAccountId}: deleting calendar channel event associations in ${
endTime - startTime
}ms.`,
);
startTime = Date.now();
await this.calendarEventCleanerService.cleanWorkspaceCalendarEvents(
workspaceId,
);
endTime = Date.now();
this.logger.log(
`google calendar sync for workspace ${workspaceId} and account ${connectedAccountId}: cleaning calendar events in ${
endTime - startTime
}ms.`,
);
} else {
this.logger.log(
`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 sync`,
);
}
startTime = Date.now();
await this.calendarChannelRepository.update(
{
id: calendarChannel.id,
},
{
syncCursor: nextSyncToken,
},
);
endTime = Date.now();
this.logger.log(
`google calendar sync for workspace ${workspaceId} and account ${connectedAccountId}: updating sync cursor in ${
endTime - startTime
}ms.`,
);
this.logger.log(
`google calendar sync for workspace ${workspaceId} and account ${connectedAccountId} ${
syncToken ? `and ${syncToken} syncToken ` : ''
}done.`,
);
}
public async getBlocklist(workspaceMemberId: string, workspaceId: string) {
const isBlocklistEnabledFeatureFlag =
await this.featureFlagRepository.findOneBy({
workspaceId,
key: FeatureFlagKeys.IsBlocklistEnabled,
value: true,
});
const isBlocklistEnabled =
isBlocklistEnabledFeatureFlag && isBlocklistEnabledFeatureFlag.value;
const blocklist = isBlocklistEnabled
? await this.blocklistRepository.getByWorkspaceMemberId(
workspaceMemberId,
workspaceId,
)
: [];
return blocklist.map((blocklist) => blocklist.handle);
}
public async getEventsFromGoogleCalendar(
connectedAccount: ConnectedAccountWorkspaceEntity,
workspaceId: string,
emailOrDomainToReimport?: string,
syncToken?: string,
): Promise<{
events: calendarV3.Schema$Event[];
nextSyncToken: string | null | undefined;
}> {
const googleCalendarClient =
await this.googleCalendarClientProvider.getGoogleCalendarClient(
connectedAccount,
);
const startTime = Date.now();
let nextSyncToken: string | null | undefined;
let nextPageToken: string | undefined;
const events: calendarV3.Schema$Event[] = [];
let hasMoreEvents = true;
while (hasMoreEvents) {
const googleCalendarEvents = await googleCalendarClient.events
.list({
calendarId: 'primary',
maxResults: 500,
syncToken: emailOrDomainToReimport ? undefined : syncToken,
pageToken: nextPageToken,
q: emailOrDomainToReimport,
showDeleted: true,
})
.catch(async (error: GaxiosError) => {
if (error.response?.status !== 410) {
throw error;
}
await this.calendarChannelRepository.update(
{
id: connectedAccount.id,
},
{
syncCursor: '',
},
);
this.logger.log(
`Sync token is no longer valid for connected account ${connectedAccount.id} in workspace ${workspaceId}, resetting sync cursor.`,
);
return {
data: {
items: [],
nextSyncToken: undefined,
nextPageToken: undefined,
},
};
});
nextSyncToken = googleCalendarEvents.data.nextSyncToken;
nextPageToken = googleCalendarEvents.data.nextPageToken || undefined;
const { items } = googleCalendarEvents.data;
if (!items || items.length === 0) {
break;
}
events.push(...items);
if (!nextPageToken) {
hasMoreEvents = false;
}
}
const endTime = Date.now();
this.logger.log(
`google calendar sync for workspace ${workspaceId} and account ${
connectedAccount.id
} getting events list in ${endTime - startTime}ms.`,
);
return { events, nextSyncToken };
}
public async saveGoogleCalendarEvents(
eventsToSave: CalendarEventWithParticipants[],
eventsToUpdate: CalendarEventWithParticipants[],
calendarChannelEventAssociationsToSave: {
calendarEventId: string;
eventExternalId: string;
calendarChannelId: string;
}[],
connectedAccount: ConnectedAccountWorkspaceEntity,
calendarChannel: CalendarChannelWorkspaceEntity,
workspaceId: string,
): Promise<void> {
const participantsToSave = eventsToSave.flatMap(
(event) => event.participants,
);
const participantsToUpdate = eventsToUpdate.flatMap(
(event) => event.participants,
);
let startTime: number;
let endTime: number;
const savedCalendarEventParticipantsToEmit: CalendarEventParticipantWorkspaceEntity[] =
[];
try {
await this.workspaceDataSource?.transaction(
async (transactionManager) => {
startTime = Date.now();
await this.calendarEventRepository.save(
eventsToSave,
{},
transactionManager,
);
endTime = Date.now();
this.logger.log(
`google calendar sync for workspace ${workspaceId} and account ${
connectedAccount.id
}: saving ${eventsToSave.length} events in ${
endTime - startTime
}ms.`,
);
startTime = Date.now();
await this.calendarChannelRepository.save(
eventsToUpdate,
{},
transactionManager,
);
endTime = Date.now();
this.logger.log(
`google calendar sync for workspace ${workspaceId} and account ${
connectedAccount.id
}: updating ${eventsToUpdate.length} events in ${
endTime - startTime
}ms.`,
);
startTime = Date.now();
await this.calendarChannelEventAssociationRepository.save(
calendarChannelEventAssociationsToSave,
{},
transactionManager,
);
endTime = Date.now();
this.logger.log(
`google calendar sync for workspace ${workspaceId} and account ${
connectedAccount.id
}: saving calendar channel event associations in ${
endTime - startTime
}ms.`,
);
startTime = Date.now();
const existingCalendarEventParticipants =
await this.calendarEventParticipantsRepository.find({
where: {
calendarEventId: Any(
participantsToUpdate
.map((participant) => participant.calendarEventId)
.filter(isDefined),
),
},
});
const {
calendarEventParticipantsToDelete,
newCalendarEventParticipants,
} = participantsToUpdate.reduce(
(acc, calendarEventParticipant) => {
const existingCalendarEventParticipant =
existingCalendarEventParticipants.find(
(existingCalendarEventParticipant) =>
existingCalendarEventParticipant.handle ===
calendarEventParticipant.handle,
);
if (existingCalendarEventParticipant) {
acc.calendarEventParticipantsToDelete.push(
existingCalendarEventParticipant,
);
} else {
acc.newCalendarEventParticipants.push(calendarEventParticipant);
}
return acc;
},
{
calendarEventParticipantsToDelete:
[] as CalendarEventParticipantWorkspaceEntity[],
newCalendarEventParticipants: [] as CalendarEventParticipant[],
},
);
await this.calendarEventParticipantsRepository.delete({
id: Any(
calendarEventParticipantsToDelete.map(
(calendarEventParticipant) => calendarEventParticipant.id,
),
),
});
await this.calendarEventParticipantsRepository.save(
participantsToUpdate,
);
endTime = Date.now();
participantsToSave.push(...newCalendarEventParticipants);
this.logger.log(
`google calendar sync for workspace ${workspaceId} and account ${
connectedAccount.id
}: updating participants in ${endTime - startTime}ms.`,
);
startTime = Date.now();
const savedCalendarEventParticipants =
await this.calendarEventParticipantsService.saveCalendarEventParticipants(
participantsToSave,
workspaceId,
transactionManager,
);
savedCalendarEventParticipantsToEmit.push(
...savedCalendarEventParticipants,
);
endTime = Date.now();
this.logger.log(
`google calendar sync for workspace ${workspaceId} and account ${
connectedAccount.id
}: saving participants in ${endTime - startTime}ms.`,
);
},
);
this.eventEmitter.emit(`calendarEventParticipant.matched`, {
workspaceId,
workspaceMemberId: connectedAccount.accountOwnerId,
calendarEventParticipants: savedCalendarEventParticipantsToEmit,
});
if (calendarChannel.isContactAutoCreationEnabled) {
await this.messageQueueService.add<CreateCompanyAndContactJobData>(
CreateCompanyAndContactJob.name,
{
workspaceId,
connectedAccount,
contactsToCreate: participantsToSave,
},
);
}
} catch (error) {
this.logger.error(
`Error during google calendar sync for workspace ${workspaceId} and account ${connectedAccount.id}: ${error.message}`,
);
}
}
}

View File

@ -0,0 +1,20 @@
import { calendar_v3 as calendarV3 } from 'googleapis';
import { isEmailBlocklisted } from 'src/modules/calendar-messaging-participant/utils/is-email-blocklisted.util';
export const filterOutBlocklistedEvents = (
calendarChannelHandle: string,
events: calendarV3.Schema$Event[],
blocklist: string[],
) => {
return events.filter((event) => {
if (!event.attendees) {
return true;
}
return event.attendees.every(
(attendee) =>
!isEmailBlocklisted(calendarChannelHandle, attendee.email, blocklist),
);
});
};

View File

@ -0,0 +1,55 @@
import { calendar_v3 as calendarV3 } from 'googleapis';
import { v4 } from 'uuid';
import { CalendarEventWithParticipants } from 'src/modules/calendar/common/types/calendar-event';
import { CalendarEventParticipantResponseStatus } from 'src/modules/calendar/common/standard-objects/calendar-event-participant.workspace-entity';
export const formatGoogleCalendarEvent = (
event: calendarV3.Schema$Event,
iCalUIDCalendarEventIdMap: Map<string, string>,
): CalendarEventWithParticipants => {
const id =
(event.iCalUID && iCalUIDCalendarEventIdMap.get(event.iCalUID)) ?? v4();
const formatResponseStatus = (status: string | null | undefined) => {
switch (status) {
case 'accepted':
return CalendarEventParticipantResponseStatus.ACCEPTED;
case 'declined':
return CalendarEventParticipantResponseStatus.DECLINED;
case 'tentative':
return CalendarEventParticipantResponseStatus.TENTATIVE;
default:
return CalendarEventParticipantResponseStatus.NEEDS_ACTION;
}
};
return {
id,
title: event.summary ?? '',
isCanceled: event.status === 'cancelled',
isFullDay: event.start?.dateTime == null,
startsAt: event.start?.dateTime ?? event.start?.date ?? null,
endsAt: event.end?.dateTime ?? event.end?.date ?? null,
externalId: event.id ?? '',
externalCreatedAt: event.created ?? null,
externalUpdatedAt: event.updated ?? null,
description: event.description ?? '',
location: event.location ?? '',
iCalUID: event.iCalUID ?? '',
conferenceSolution:
event.conferenceData?.conferenceSolution?.key?.type ?? '',
conferenceLinkLabel: event.conferenceData?.entryPoints?.[0]?.uri ?? '',
conferenceLinkUrl: event.conferenceData?.entryPoints?.[0]?.uri ?? '',
recurringEventExternalId: event.recurringEventId ?? '',
participants:
event.attendees?.map((attendee) => ({
calendarEventId: id,
iCalUID: event.iCalUID ?? '',
handle: attendee.email ?? '',
displayName: attendee.displayName ?? '',
isOrganizer: attendee.organizer === true,
responseStatus: formatResponseStatus(attendee.responseStatus),
})) ?? [],
};
};

View File

@ -0,0 +1,56 @@
export const valuesStringForBatchRawQuery = (
values: {
[key: string]: any;
}[],
typesArray: string[] = [],
) => {
const castedValues = values.reduce((acc, _, rowIndex) => {
const numberOfColumns = typesArray.length;
const rowValues = Array.from(
{ length: numberOfColumns },
(_, columnIndex) => {
const placeholder = `$${rowIndex * numberOfColumns + columnIndex + 1}`;
const typeCast = typesArray[columnIndex]
? `::${typesArray[columnIndex]}`
: '';
return `${placeholder}${typeCast}`;
},
).join(', ');
acc.push(`(${rowValues})`);
return acc;
}, [] as string[]);
return castedValues.join(', ');
};
export const getFlattenedValuesAndValuesStringForBatchRawQuery = (
values: {
[key: string]: any;
}[],
keyTypeMap: {
[key: string]: string;
},
): {
flattenedValues: any[];
valuesString: string;
} => {
const keysToInsert = Object.keys(keyTypeMap);
const flattenedValues = values.flatMap((value) =>
keysToInsert.map((key) => value[key]),
);
const valuesString = valuesStringForBatchRawQuery(
values,
Object.values(keyTypeMap),
);
return {
flattenedValues,
valuesString,
};
};