Refactor calendar to use new sync statuses and stages (#6141)
- Refactor calendar modules and some messaging modules to better organize them by business rules and decouple them - Work toward a common architecture for the different calendar providers by introducing interfaces for the drivers - Modify cron job to use the new sync statuses and stages
This commit is contained in:
@ -28,14 +28,14 @@ import { InjectWorkspaceRepository } from 'src/engine/twenty-orm/decorators/inje
|
|||||||
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
|
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
|
||||||
import { WorkspaceDataSource } from 'src/engine/twenty-orm/datasource/workspace.datasource';
|
import { WorkspaceDataSource } from 'src/engine/twenty-orm/datasource/workspace.datasource';
|
||||||
import { InjectWorkspaceDatasource } from 'src/engine/twenty-orm/decorators/inject-workspace-datasource.decorator';
|
import { InjectWorkspaceDatasource } from 'src/engine/twenty-orm/decorators/inject-workspace-datasource.decorator';
|
||||||
import {
|
|
||||||
CalendarEventsImportJob,
|
|
||||||
CalendarEventsImportJobData,
|
|
||||||
} from 'src/modules/calendar/calendar-event-import-manager/jobs/calendar-events-import.job';
|
|
||||||
import {
|
import {
|
||||||
CalendarChannelWorkspaceEntity,
|
CalendarChannelWorkspaceEntity,
|
||||||
CalendarChannelVisibility,
|
CalendarChannelVisibility,
|
||||||
} from 'src/modules/calendar/common/standard-objects/calendar-channel.workspace-entity';
|
} from 'src/modules/calendar/common/standard-objects/calendar-channel.workspace-entity';
|
||||||
|
import {
|
||||||
|
CalendarEventsImportJobData,
|
||||||
|
CalendarEventListFetchJob,
|
||||||
|
} from 'src/modules/calendar/calendar-event-import-manager/jobs/calendar-event-list-fetch.job';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class GoogleAPIsService {
|
export class GoogleAPIsService {
|
||||||
@ -167,13 +167,21 @@ export class GoogleAPIsService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isCalendarEnabled) {
|
if (isCalendarEnabled) {
|
||||||
await this.calendarQueueService.add<CalendarEventsImportJobData>(
|
const calendarChannels = await this.calendarChannelRepository.find({
|
||||||
CalendarEventsImportJob.name,
|
where: {
|
||||||
{
|
|
||||||
workspaceId,
|
|
||||||
connectedAccountId: newOrExistingConnectedAccountId,
|
connectedAccountId: newOrExistingConnectedAccountId,
|
||||||
},
|
},
|
||||||
);
|
});
|
||||||
|
|
||||||
|
for (const calendarChannel of calendarChannels) {
|
||||||
|
await this.calendarQueueService.add<CalendarEventsImportJobData>(
|
||||||
|
CalendarEventListFetchJob.name,
|
||||||
|
{
|
||||||
|
calendarChannelId: calendarChannel.id,
|
||||||
|
workspaceId,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
export enum CacheStorageNamespace {
|
export enum CacheStorageNamespace {
|
||||||
Messaging = 'messaging',
|
Messaging = 'messaging',
|
||||||
|
Calendar = 'calendar',
|
||||||
WorkspaceSchema = 'workspaceSchema',
|
WorkspaceSchema = 'workspaceSchema',
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,7 +22,7 @@ import { AutoCompaniesAndContactsCreationJobModule } from 'src/modules/connected
|
|||||||
import { MessagingModule } from 'src/modules/messaging/messaging.module';
|
import { MessagingModule } from 'src/modules/messaging/messaging.module';
|
||||||
import { TimelineJobModule } from 'src/modules/timeline/jobs/timeline-job.module';
|
import { TimelineJobModule } from 'src/modules/timeline/jobs/timeline-job.module';
|
||||||
import { CalendarModule } from 'src/modules/calendar/calendar.module';
|
import { CalendarModule } from 'src/modules/calendar/calendar.module';
|
||||||
import { CalendarEventParticipantModule } from 'src/modules/calendar/calendar-event-participant-manager/calendar-event-participant.module';
|
import { CalendarEventParticipantManagerModule } from 'src/modules/calendar/calendar-event-participant-manager/calendar-event-participant-manager.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@ -37,7 +37,7 @@ import { CalendarEventParticipantModule } from 'src/modules/calendar/calendar-ev
|
|||||||
WorkspaceModule,
|
WorkspaceModule,
|
||||||
MessagingModule,
|
MessagingModule,
|
||||||
CalendarModule,
|
CalendarModule,
|
||||||
CalendarEventParticipantModule,
|
CalendarEventParticipantManagerModule,
|
||||||
TimelineActivityModule,
|
TimelineActivityModule,
|
||||||
StripeModule,
|
StripeModule,
|
||||||
WorkspaceQueryRunnerJobModule,
|
WorkspaceQueryRunnerJobModule,
|
||||||
|
|||||||
@ -2,11 +2,39 @@ import { Injectable, Optional, Type } from '@nestjs/common';
|
|||||||
|
|
||||||
import { ObjectLiteral } from 'typeorm';
|
import { ObjectLiteral } from 'typeorm';
|
||||||
|
|
||||||
import { EntitySchemaFactory } from 'src/engine/twenty-orm/factories/entity-schema.factory';
|
|
||||||
import { WorkspaceDataSource } from 'src/engine/twenty-orm/datasource/workspace.datasource';
|
import { WorkspaceDataSource } from 'src/engine/twenty-orm/datasource/workspace.datasource';
|
||||||
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
|
import { EntitySchemaFactory } from 'src/engine/twenty-orm/factories/entity-schema.factory';
|
||||||
import { WorkspaceDatasourceFactory } from 'src/engine/twenty-orm/factories/workspace-datasource.factory';
|
import { WorkspaceDatasourceFactory } from 'src/engine/twenty-orm/factories/workspace-datasource.factory';
|
||||||
import { ObjectLiteralStorage } from 'src/engine/twenty-orm/storage/object-literal.storage';
|
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
|
||||||
|
import { ActivityTargetWorkspaceEntity } from 'src/modules/activity/standard-objects/activity-target.workspace-entity';
|
||||||
|
import { ActivityWorkspaceEntity } from 'src/modules/activity/standard-objects/activity.workspace-entity';
|
||||||
|
import { CommentWorkspaceEntity } from 'src/modules/activity/standard-objects/comment.workspace-entity';
|
||||||
|
import { ApiKeyWorkspaceEntity } from 'src/modules/api-key/standard-objects/api-key.workspace-entity';
|
||||||
|
import { AttachmentWorkspaceEntity } from 'src/modules/attachment/standard-objects/attachment.workspace-entity';
|
||||||
|
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 { CompanyWorkspaceEntity } from 'src/modules/company/standard-objects/company.workspace-entity';
|
||||||
|
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 { FavoriteWorkspaceEntity } from 'src/modules/favorite/standard-objects/favorite.workspace-entity';
|
||||||
|
import { MessageChannelMessageAssociationWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-channel-message-association.workspace-entity';
|
||||||
|
import { MessageChannelWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity';
|
||||||
|
import { MessageParticipantWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-participant.workspace-entity';
|
||||||
|
import { MessageThreadWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-thread.workspace-entity';
|
||||||
|
import { MessageWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message.workspace-entity';
|
||||||
|
import { OpportunityWorkspaceEntity } from 'src/modules/opportunity/standard-objects/opportunity.workspace-entity';
|
||||||
|
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
|
||||||
|
import { AuditLogWorkspaceEntity } from 'src/modules/timeline/standard-objects/audit-log.workspace-entity';
|
||||||
|
import { BehavioralEventWorkspaceEntity } from 'src/modules/timeline/standard-objects/behavioral-event.workspace-entity';
|
||||||
|
import { TimelineActivityWorkspaceEntity } from 'src/modules/timeline/standard-objects/timeline-activity.workspace-entity';
|
||||||
|
import { ViewFieldWorkspaceEntity } from 'src/modules/view/standard-objects/view-field.workspace-entity';
|
||||||
|
import { ViewFilterWorkspaceEntity } from 'src/modules/view/standard-objects/view-filter.workspace-entity';
|
||||||
|
import { ViewSortWorkspaceEntity } from 'src/modules/view/standard-objects/view-sort.workspace-entity';
|
||||||
|
import { ViewWorkspaceEntity } from 'src/modules/view/standard-objects/view.workspace-entity';
|
||||||
|
import { WebhookWorkspaceEntity } from 'src/modules/webhook/standard-objects/webhook.workspace-entity';
|
||||||
|
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class TwentyORMManager {
|
export class TwentyORMManager {
|
||||||
@ -33,7 +61,43 @@ export class TwentyORMManager {
|
|||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
entityClass: Type<T>,
|
entityClass: Type<T>,
|
||||||
): Promise<WorkspaceRepository<T>> {
|
): Promise<WorkspaceRepository<T>> {
|
||||||
const entities = ObjectLiteralStorage.getAllEntitySchemas();
|
// TODO: This is a temporary solution to get all workspace entities
|
||||||
|
const workspaceEntities = [
|
||||||
|
ActivityTargetWorkspaceEntity,
|
||||||
|
ActivityWorkspaceEntity,
|
||||||
|
ApiKeyWorkspaceEntity,
|
||||||
|
AttachmentWorkspaceEntity,
|
||||||
|
BlocklistWorkspaceEntity,
|
||||||
|
BehavioralEventWorkspaceEntity,
|
||||||
|
CalendarChannelEventAssociationWorkspaceEntity,
|
||||||
|
CalendarChannelWorkspaceEntity,
|
||||||
|
CalendarEventParticipantWorkspaceEntity,
|
||||||
|
CalendarEventWorkspaceEntity,
|
||||||
|
CommentWorkspaceEntity,
|
||||||
|
CompanyWorkspaceEntity,
|
||||||
|
ConnectedAccountWorkspaceEntity,
|
||||||
|
FavoriteWorkspaceEntity,
|
||||||
|
AuditLogWorkspaceEntity,
|
||||||
|
MessageChannelMessageAssociationWorkspaceEntity,
|
||||||
|
MessageChannelWorkspaceEntity,
|
||||||
|
MessageParticipantWorkspaceEntity,
|
||||||
|
MessageThreadWorkspaceEntity,
|
||||||
|
MessageWorkspaceEntity,
|
||||||
|
OpportunityWorkspaceEntity,
|
||||||
|
PersonWorkspaceEntity,
|
||||||
|
TimelineActivityWorkspaceEntity,
|
||||||
|
ViewFieldWorkspaceEntity,
|
||||||
|
ViewFilterWorkspaceEntity,
|
||||||
|
ViewSortWorkspaceEntity,
|
||||||
|
ViewWorkspaceEntity,
|
||||||
|
WebhookWorkspaceEntity,
|
||||||
|
WorkspaceMemberWorkspaceEntity,
|
||||||
|
];
|
||||||
|
|
||||||
|
const entities = workspaceEntities.map((workspaceEntity) =>
|
||||||
|
this.entitySchemaFactory.create(workspaceEntity as any),
|
||||||
|
);
|
||||||
|
|
||||||
const workspaceDataSource = await this.workspaceDataSourceFactory.create(
|
const workspaceDataSource = await this.workspaceDataSourceFactory.create(
|
||||||
entities,
|
entities,
|
||||||
workspaceId,
|
workspaceId,
|
||||||
|
|||||||
@ -7,6 +7,8 @@ import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/work
|
|||||||
import { PersonRepository } from 'src/modules/person/repositories/person.repository';
|
import { PersonRepository } from 'src/modules/person/repositories/person.repository';
|
||||||
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
|
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
|
||||||
|
|
||||||
|
// TODO: Move inside person module and workspace-member module
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AddPersonIdAndWorkspaceMemberIdService {
|
export class AddPersonIdAndWorkspaceMemberIdService {
|
||||||
constructor(
|
constructor(
|
||||||
@ -1,4 +1,4 @@
|
|||||||
import { isEmailBlocklisted } from 'src/modules/calendar-messaging-participant/utils/is-email-blocklisted.util';
|
import { isEmailBlocklisted } from 'src/modules/calendar-messaging-participant-manager/utils/is-email-blocklisted.util';
|
||||||
|
|
||||||
describe('isEmailBlocklisted', () => {
|
describe('isEmailBlocklisted', () => {
|
||||||
it('should return true if email is blocklisted', () => {
|
it('should return true if email is blocklisted', () => {
|
||||||
@ -1,3 +1,5 @@
|
|||||||
|
// TODO: Move inside blocklist module
|
||||||
|
|
||||||
export const isEmailBlocklisted = (
|
export const isEmailBlocklisted = (
|
||||||
channelHandle: string,
|
channelHandle: string,
|
||||||
email: string | null | undefined,
|
email: string | null | undefined,
|
||||||
@ -1,12 +0,0 @@
|
|||||||
import { Module } from '@nestjs/common';
|
|
||||||
|
|
||||||
import { MatchParticipantJob } from 'src/modules/calendar-messaging-participant/jobs/match-participant.job';
|
|
||||||
import { UnmatchParticipantJob } from 'src/modules/calendar-messaging-participant/jobs/unmatch-participant.job';
|
|
||||||
import { CalendarEventParticipantModule } from 'src/modules/calendar/calendar-event-participant-manager/calendar-event-participant.module';
|
|
||||||
import { MessagingCommonModule } from 'src/modules/messaging/common/messaging-common.module';
|
|
||||||
|
|
||||||
@Module({
|
|
||||||
imports: [CalendarEventParticipantModule, MessagingCommonModule],
|
|
||||||
providers: [MatchParticipantJob, UnmatchParticipantJob],
|
|
||||||
})
|
|
||||||
export class CalendarMessagingParticipantJobModule {}
|
|
||||||
@ -1,16 +0,0 @@
|
|||||||
import { Module } from '@nestjs/common';
|
|
||||||
|
|
||||||
import { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.module';
|
|
||||||
import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module';
|
|
||||||
import { AddPersonIdAndWorkspaceMemberIdService } from 'src/modules/calendar-messaging-participant/services/add-person-id-and-workspace-member-id/add-person-id-and-workspace-member-id.service';
|
|
||||||
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
|
|
||||||
|
|
||||||
@Module({
|
|
||||||
imports: [
|
|
||||||
WorkspaceDataSourceModule,
|
|
||||||
ObjectMetadataRepositoryModule.forFeature([PersonWorkspaceEntity]),
|
|
||||||
],
|
|
||||||
providers: [AddPersonIdAndWorkspaceMemberIdService],
|
|
||||||
exports: [AddPersonIdAndWorkspaceMemberIdService],
|
|
||||||
})
|
|
||||||
export class AddPersonIdAndWorkspaceMemberIdModule {}
|
|
||||||
@ -1,4 +1,6 @@
|
|||||||
import { Logger, Scope } from '@nestjs/common';
|
import { Scope } from '@nestjs/common';
|
||||||
|
|
||||||
|
import { Any } from 'typeorm';
|
||||||
|
|
||||||
import { Processor } from 'src/engine/integrations/message-queue/decorators/processor.decorator';
|
import { Processor } from 'src/engine/integrations/message-queue/decorators/processor.decorator';
|
||||||
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
|
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
|
||||||
@ -6,12 +8,16 @@ import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repos
|
|||||||
import { ConnectedAccountRepository } from 'src/modules/connected-account/repositories/connected-account.repository';
|
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 { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
|
||||||
import { Process } from 'src/engine/integrations/message-queue/decorators/process.decorator';
|
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';
|
import { InjectWorkspaceRepository } from 'src/engine/twenty-orm/decorators/inject-workspace-repository.decorator';
|
||||||
|
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
|
||||||
|
import {
|
||||||
|
CalendarChannelSyncStage,
|
||||||
|
CalendarChannelWorkspaceEntity,
|
||||||
|
} from 'src/modules/calendar/common/standard-objects/calendar-channel.workspace-entity';
|
||||||
|
|
||||||
export type BlocklistReimportCalendarEventsJobData = {
|
export type BlocklistReimportCalendarEventsJobData = {
|
||||||
workspaceId: string;
|
workspaceId: string;
|
||||||
workspaceMemberId: string;
|
workspaceMemberId: string;
|
||||||
handle: string;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
@Processor({
|
@Processor({
|
||||||
@ -19,44 +25,37 @@ export type BlocklistReimportCalendarEventsJobData = {
|
|||||||
scope: Scope.REQUEST,
|
scope: Scope.REQUEST,
|
||||||
})
|
})
|
||||||
export class BlocklistReimportCalendarEventsJob {
|
export class BlocklistReimportCalendarEventsJob {
|
||||||
private readonly logger = new Logger(BlocklistReimportCalendarEventsJob.name);
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@InjectObjectMetadataRepository(ConnectedAccountWorkspaceEntity)
|
@InjectObjectMetadataRepository(ConnectedAccountWorkspaceEntity)
|
||||||
private readonly connectedAccountRepository: ConnectedAccountRepository,
|
private readonly connectedAccountRepository: ConnectedAccountRepository,
|
||||||
private readonly googleCalendarSyncService: CalendarEventsImportService,
|
@InjectWorkspaceRepository(CalendarChannelWorkspaceEntity)
|
||||||
|
private readonly calendarChannelRepository: WorkspaceRepository<CalendarChannelWorkspaceEntity>,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@Process(BlocklistReimportCalendarEventsJob.name)
|
@Process(BlocklistReimportCalendarEventsJob.name)
|
||||||
async handle(data: BlocklistReimportCalendarEventsJobData): Promise<void> {
|
async handle(data: BlocklistReimportCalendarEventsJobData): Promise<void> {
|
||||||
const { workspaceId, workspaceMemberId, handle } = data;
|
const { workspaceId, workspaceMemberId } = data;
|
||||||
|
|
||||||
this.logger.log(
|
const connectedAccounts =
|
||||||
`Reimporting calendar events from handle ${handle} in workspace ${workspaceId} for workspace member ${workspaceMemberId}`,
|
|
||||||
);
|
|
||||||
|
|
||||||
const connectedAccount =
|
|
||||||
await this.connectedAccountRepository.getAllByWorkspaceMemberId(
|
await this.connectedAccountRepository.getAllByWorkspaceMemberId(
|
||||||
workspaceMemberId,
|
workspaceMemberId,
|
||||||
workspaceId,
|
workspaceId,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!connectedAccount || connectedAccount.length === 0) {
|
if (!connectedAccounts || connectedAccounts.length === 0) {
|
||||||
this.logger.error(
|
|
||||||
`No connected account found for workspace member ${workspaceMemberId} in workspace ${workspaceId}`,
|
|
||||||
);
|
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.googleCalendarSyncService.processCalendarEventsImport(
|
await this.calendarChannelRepository.update(
|
||||||
workspaceId,
|
{
|
||||||
connectedAccount[0].id,
|
connectedAccountId: Any(
|
||||||
handle,
|
connectedAccounts.map((connectedAccount) => connectedAccount.id),
|
||||||
);
|
),
|
||||||
|
},
|
||||||
this.logger.log(
|
{
|
||||||
`Reimporting calendar events from ${handle} in workspace ${workspaceId} for workspace member ${workspaceMemberId} done`,
|
syncStage:
|
||||||
|
CalendarChannelSyncStage.FULL_CALENDAR_EVENT_LIST_FETCH_PENDING,
|
||||||
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -46,7 +46,6 @@ export class CalendarBlocklistListener {
|
|||||||
{
|
{
|
||||||
workspaceId: payload.workspaceId,
|
workspaceId: payload.workspaceId,
|
||||||
workspaceMemberId: payload.properties.before.workspaceMember.id,
|
workspaceMemberId: payload.properties.before.workspaceMember.id,
|
||||||
handle: payload.properties.before.handle,
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -68,7 +67,6 @@ export class CalendarBlocklistListener {
|
|||||||
{
|
{
|
||||||
workspaceId: payload.workspaceId,
|
workspaceId: payload.workspaceId,
|
||||||
workspaceMemberId: payload.properties.after.workspaceMember.id,
|
workspaceMemberId: payload.properties.after.workspaceMember.id,
|
||||||
handle: payload.properties.before.handle,
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,12 +8,16 @@ import { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repos
|
|||||||
import { TwentyORMModule } from 'src/engine/twenty-orm/twenty-orm.module';
|
import { TwentyORMModule } from 'src/engine/twenty-orm/twenty-orm.module';
|
||||||
import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.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 { 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 { CalendarEventListFetchCronCommand } from 'src/modules/calendar/calendar-event-import-manager/crons/commands/calendar-event-list-fetch.cron.command';
|
||||||
import { CalendarEventsImportCronJob } from 'src/modules/calendar/calendar-event-import-manager/crons/jobs/calendar-events-import.cron.job';
|
import { CalendarEventListFetchCronJob } from 'src/modules/calendar/calendar-event-import-manager/crons/jobs/calendar-event-list-fetch.cron.job';
|
||||||
import { GoogleCalendarDriverModule } from 'src/modules/calendar/calendar-event-import-manager/drivers/google-calendar/google-calendar-driver.module';
|
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 { CalendarEventListFetchJob } from 'src/modules/calendar/calendar-event-import-manager/jobs/calendar-event-list-fetch.job';
|
||||||
|
import { CalendarChannelSyncStatusService } from 'src/modules/calendar/calendar-event-import-manager/services/calendar-channel-sync-status.service';
|
||||||
import { CalendarEventsImportService } from 'src/modules/calendar/calendar-event-import-manager/services/calendar-events-import.service';
|
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 { CalendarGetCalendarEventsService } from 'src/modules/calendar/calendar-event-import-manager/services/calendar-get-events.service';
|
||||||
|
import { CalendarSaveEventsService } from 'src/modules/calendar/calendar-event-import-manager/services/calendar-save-events.service';
|
||||||
|
import { CalendarEventParticipantManagerModule } from 'src/modules/calendar/calendar-event-participant-manager/calendar-event-participant-manager.module';
|
||||||
|
import { CalendarCommonModule } from 'src/modules/calendar/common/calendar-common.module';
|
||||||
import { CalendarChannelEventAssociationWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-channel-event-association.workspace-entity';
|
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 { 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 { CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event-participant.workspace-entity';
|
||||||
@ -38,7 +42,7 @@ import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/sta
|
|||||||
PersonWorkspaceEntity,
|
PersonWorkspaceEntity,
|
||||||
WorkspaceMemberWorkspaceEntity,
|
WorkspaceMemberWorkspaceEntity,
|
||||||
]),
|
]),
|
||||||
CalendarEventParticipantModule,
|
CalendarEventParticipantManagerModule,
|
||||||
TypeOrmModule.forFeature([FeatureFlagEntity], 'core'),
|
TypeOrmModule.forFeature([FeatureFlagEntity], 'core'),
|
||||||
TypeOrmModule.forFeature([DataSourceEntity], 'metadata'),
|
TypeOrmModule.forFeature([DataSourceEntity], 'metadata'),
|
||||||
WorkspaceDataSourceModule,
|
WorkspaceDataSourceModule,
|
||||||
@ -46,12 +50,17 @@ import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/sta
|
|||||||
GoogleCalendarDriverModule,
|
GoogleCalendarDriverModule,
|
||||||
BillingModule,
|
BillingModule,
|
||||||
GoogleAPIRefreshAccessTokenModule,
|
GoogleAPIRefreshAccessTokenModule,
|
||||||
|
CalendarCommonModule,
|
||||||
|
CalendarEventParticipantManagerModule,
|
||||||
],
|
],
|
||||||
providers: [
|
providers: [
|
||||||
|
CalendarChannelSyncStatusService,
|
||||||
CalendarEventsImportService,
|
CalendarEventsImportService,
|
||||||
CalendarEventsImportCronJob,
|
CalendarGetCalendarEventsService,
|
||||||
CalendarEventsImportCronCommand,
|
CalendarSaveEventsService,
|
||||||
CalendarEventsImportJob,
|
CalendarEventListFetchCronJob,
|
||||||
|
CalendarEventListFetchCronCommand,
|
||||||
|
CalendarEventListFetchJob,
|
||||||
],
|
],
|
||||||
exports: [CalendarEventsImportService],
|
exports: [CalendarEventsImportService],
|
||||||
})
|
})
|
||||||
|
|||||||
@ -3,15 +3,15 @@ import { Command, CommandRunner } from 'nest-commander';
|
|||||||
import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
|
import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
|
||||||
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
|
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
|
||||||
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
|
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';
|
import { CalendarEventListFetchCronJob } from 'src/modules/calendar/calendar-event-import-manager/crons/jobs/calendar-event-list-fetch.cron.job';
|
||||||
|
|
||||||
const CALENDAR_EVENTS_IMPORT_CRON_PATTERN = '*/5 * * * *';
|
const CALENDAR_EVENTS_IMPORT_CRON_PATTERN = '*/5 * * * *';
|
||||||
|
|
||||||
@Command({
|
@Command({
|
||||||
name: 'cron:calendar:calendar-events-import',
|
name: 'cron:calendar:calendar-event-list-fetch',
|
||||||
description: 'Starts a cron job to import calendar events',
|
description: 'Starts a cron job to fetch the calendar event list',
|
||||||
})
|
})
|
||||||
export class CalendarEventsImportCronCommand extends CommandRunner {
|
export class CalendarEventListFetchCronCommand extends CommandRunner {
|
||||||
constructor(
|
constructor(
|
||||||
@InjectMessageQueue(MessageQueue.cronQueue)
|
@InjectMessageQueue(MessageQueue.cronQueue)
|
||||||
private readonly messageQueueService: MessageQueueService,
|
private readonly messageQueueService: MessageQueueService,
|
||||||
@ -21,7 +21,7 @@ export class CalendarEventsImportCronCommand extends CommandRunner {
|
|||||||
|
|
||||||
async run(): Promise<void> {
|
async run(): Promise<void> {
|
||||||
await this.messageQueueService.addCron<undefined>(
|
await this.messageQueueService.addCron<undefined>(
|
||||||
CalendarEventsImportCronJob.name,
|
CalendarEventListFetchCronJob.name,
|
||||||
undefined,
|
undefined,
|
||||||
{
|
{
|
||||||
repeat: { pattern: CALENDAR_EVENTS_IMPORT_CRON_PATTERN },
|
repeat: { pattern: CALENDAR_EVENTS_IMPORT_CRON_PATTERN },
|
||||||
@ -1,25 +1,28 @@
|
|||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
|
|
||||||
import { Repository, In } from 'typeorm';
|
import { Any, In, Repository } 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 { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
|
|
||||||
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
|
|
||||||
import { BillingService } from 'src/engine/core-modules/billing/billing.service';
|
import { BillingService } from 'src/engine/core-modules/billing/billing.service';
|
||||||
|
import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
|
||||||
|
import { Process } from 'src/engine/integrations/message-queue/decorators/process.decorator';
|
||||||
|
import { Processor } from 'src/engine/integrations/message-queue/decorators/processor.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 { DataSourceEntity } from 'src/engine/metadata-modules/data-source/data-source.entity';
|
||||||
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
|
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
|
||||||
import {
|
import {
|
||||||
|
CalendarEventListFetchJob,
|
||||||
CalendarEventsImportJobData,
|
CalendarEventsImportJobData,
|
||||||
CalendarEventsImportJob,
|
} from 'src/modules/calendar/calendar-event-import-manager/jobs/calendar-event-list-fetch.job';
|
||||||
} from 'src/modules/calendar/calendar-event-import-manager/jobs/calendar-events-import.job';
|
import {
|
||||||
import { CalendarChannelWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-channel.workspace-entity';
|
CalendarChannelSyncStage,
|
||||||
|
CalendarChannelWorkspaceEntity,
|
||||||
|
} from 'src/modules/calendar/common/standard-objects/calendar-channel.workspace-entity';
|
||||||
|
|
||||||
@Processor({
|
@Processor({
|
||||||
queueName: MessageQueue.cronQueue,
|
queueName: MessageQueue.cronQueue,
|
||||||
})
|
})
|
||||||
export class CalendarEventsImportCronJob {
|
export class CalendarEventListFetchCronJob {
|
||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(DataSourceEntity, 'metadata')
|
@InjectRepository(DataSourceEntity, 'metadata')
|
||||||
private readonly dataSourceRepository: Repository<DataSourceEntity>,
|
private readonly dataSourceRepository: Repository<DataSourceEntity>,
|
||||||
@ -29,7 +32,7 @@ export class CalendarEventsImportCronJob {
|
|||||||
private readonly twentyORMManager: TwentyORMManager,
|
private readonly twentyORMManager: TwentyORMManager,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@Process(CalendarEventsImportCronJob.name)
|
@Process(CalendarEventListFetchCronJob.name)
|
||||||
async handle(): Promise<void> {
|
async handle(): Promise<void> {
|
||||||
const workspaceIds =
|
const workspaceIds =
|
||||||
await this.billingService.getActiveSubscriptionWorkspaceIds();
|
await this.billingService.getActiveSubscriptionWorkspaceIds();
|
||||||
@ -51,18 +54,22 @@ export class CalendarEventsImportCronJob {
|
|||||||
CalendarChannelWorkspaceEntity,
|
CalendarChannelWorkspaceEntity,
|
||||||
);
|
);
|
||||||
|
|
||||||
const calendarChannels = await calendarChannelRepository.find({});
|
const calendarChannels = await calendarChannelRepository.find({
|
||||||
|
where: {
|
||||||
|
isSyncEnabled: true,
|
||||||
|
syncStage: Any([
|
||||||
|
CalendarChannelSyncStage.FULL_CALENDAR_EVENT_LIST_FETCH_PENDING,
|
||||||
|
CalendarChannelSyncStage.PARTIAL_CALENDAR_EVENT_LIST_FETCH_PENDING,
|
||||||
|
]),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
for (const calendarChannel of calendarChannels) {
|
for (const calendarChannel of calendarChannels) {
|
||||||
if (!calendarChannel?.isSyncEnabled) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.messageQueueService.add<CalendarEventsImportJobData>(
|
await this.messageQueueService.add<CalendarEventsImportJobData>(
|
||||||
CalendarEventsImportJob.name,
|
CalendarEventListFetchJob.name,
|
||||||
{
|
{
|
||||||
|
calendarChannelId: calendarChannel.id,
|
||||||
workspaceId,
|
workspaceId,
|
||||||
connectedAccountId: calendarChannel.connectedAccountId,
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -2,11 +2,12 @@ import { Module } from '@nestjs/common';
|
|||||||
|
|
||||||
import { EnvironmentModule } from 'src/engine/integrations/environment/environment.module';
|
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 { GoogleCalendarClientProvider } from 'src/modules/calendar/calendar-event-import-manager/drivers/google-calendar/providers/google-calendar.provider';
|
||||||
|
import { GoogleCalendarGetEventsService } from 'src/modules/calendar/calendar-event-import-manager/drivers/google-calendar/services/google-calendar-get-events.service';
|
||||||
import { OAuth2ClientManagerModule } from 'src/modules/connected-account/oauth2-client-manager/oauth2-client-manager.module';
|
import { OAuth2ClientManagerModule } from 'src/modules/connected-account/oauth2-client-manager/oauth2-client-manager.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [EnvironmentModule, OAuth2ClientManagerModule],
|
imports: [EnvironmentModule, OAuth2ClientManagerModule],
|
||||||
providers: [GoogleCalendarClientProvider],
|
providers: [GoogleCalendarClientProvider, GoogleCalendarGetEventsService],
|
||||||
exports: [GoogleCalendarClientProvider],
|
exports: [GoogleCalendarGetEventsService],
|
||||||
})
|
})
|
||||||
export class GoogleCalendarDriverModule {}
|
export class GoogleCalendarDriverModule {}
|
||||||
|
|||||||
@ -12,7 +12,10 @@ export class GoogleCalendarClientProvider {
|
|||||||
) {}
|
) {}
|
||||||
|
|
||||||
public async getGoogleCalendarClient(
|
public async getGoogleCalendarClient(
|
||||||
connectedAccount: ConnectedAccountWorkspaceEntity,
|
connectedAccount: Pick<
|
||||||
|
ConnectedAccountWorkspaceEntity,
|
||||||
|
'provider' | 'refreshToken'
|
||||||
|
>,
|
||||||
): Promise<calendarV3.Calendar> {
|
): Promise<calendarV3.Calendar> {
|
||||||
const oAuth2Client =
|
const oAuth2Client =
|
||||||
await this.oAuth2ClientManagerService.getOAuth2Client(connectedAccount);
|
await this.oAuth2ClientManagerService.getOAuth2Client(connectedAccount);
|
||||||
|
|||||||
@ -0,0 +1,93 @@
|
|||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
|
||||||
|
import { calendar_v3 as calendarV3 } from 'googleapis';
|
||||||
|
import { GaxiosError } from 'gaxios';
|
||||||
|
|
||||||
|
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
|
||||||
|
import { InjectWorkspaceRepository } from 'src/engine/twenty-orm/decorators/inject-workspace-repository.decorator';
|
||||||
|
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
|
||||||
|
import { GoogleCalendarClientProvider } from 'src/modules/calendar/calendar-event-import-manager/drivers/google-calendar/providers/google-calendar.provider';
|
||||||
|
import { CalendarChannelWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-channel.workspace-entity';
|
||||||
|
import { GetCalendarEventsResponse } from 'src/modules/calendar/calendar-event-import-manager/services/calendar-get-events.service';
|
||||||
|
import { formatGoogleCalendarEvents } from 'src/modules/calendar/calendar-event-import-manager/drivers/google-calendar/utils/format-google-calendar-event.util';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class GoogleCalendarGetEventsService {
|
||||||
|
constructor(
|
||||||
|
private readonly googleCalendarClientProvider: GoogleCalendarClientProvider,
|
||||||
|
@InjectWorkspaceRepository(CalendarChannelWorkspaceEntity)
|
||||||
|
private readonly calendarChannelRepository: WorkspaceRepository<CalendarChannelWorkspaceEntity>,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public async getCalendarEvents(
|
||||||
|
connectedAccount: Pick<
|
||||||
|
ConnectedAccountWorkspaceEntity,
|
||||||
|
'provider' | 'refreshToken' | 'id'
|
||||||
|
>,
|
||||||
|
syncCursor?: string,
|
||||||
|
): Promise<GetCalendarEventsResponse> {
|
||||||
|
const googleCalendarClient =
|
||||||
|
await this.googleCalendarClientProvider.getGoogleCalendarClient(
|
||||||
|
connectedAccount,
|
||||||
|
);
|
||||||
|
|
||||||
|
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: syncCursor,
|
||||||
|
pageToken: nextPageToken,
|
||||||
|
showDeleted: true,
|
||||||
|
})
|
||||||
|
.catch(async (error: GaxiosError) => {
|
||||||
|
if (error.response?.status !== 410) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.calendarChannelRepository.update(
|
||||||
|
{
|
||||||
|
connectedAccountId: connectedAccount.id,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
syncCursor: '',
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
calendarEvents: formatGoogleCalendarEvents(events),
|
||||||
|
nextSyncCursor: nextSyncToken || '',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,16 +1,17 @@
|
|||||||
import { calendar_v3 as calendarV3 } from 'googleapis';
|
import { calendar_v3 as calendarV3 } from 'googleapis';
|
||||||
import { v4 } from 'uuid';
|
|
||||||
|
|
||||||
import { CalendarEventWithParticipants } from 'src/modules/calendar/common/types/calendar-event';
|
import { CalendarEventWithParticipants } from 'src/modules/calendar/common/types/calendar-event';
|
||||||
import { CalendarEventParticipantResponseStatus } from 'src/modules/calendar/common/standard-objects/calendar-event-participant.workspace-entity';
|
import { CalendarEventParticipantResponseStatus } from 'src/modules/calendar/common/standard-objects/calendar-event-participant.workspace-entity';
|
||||||
|
|
||||||
export const formatGoogleCalendarEvent = (
|
export const formatGoogleCalendarEvents = (
|
||||||
event: calendarV3.Schema$Event,
|
events: calendarV3.Schema$Event[],
|
||||||
iCalUIDCalendarEventIdMap: Map<string, string>,
|
): CalendarEventWithParticipants[] => {
|
||||||
): CalendarEventWithParticipants => {
|
return events.map(formatGoogleCalendarEvent);
|
||||||
const id =
|
};
|
||||||
(event.iCalUID && iCalUIDCalendarEventIdMap.get(event.iCalUID)) ?? v4();
|
|
||||||
|
|
||||||
|
const formatGoogleCalendarEvent = (
|
||||||
|
event: calendarV3.Schema$Event,
|
||||||
|
): CalendarEventWithParticipants => {
|
||||||
const formatResponseStatus = (status: string | null | undefined) => {
|
const formatResponseStatus = (status: string | null | undefined) => {
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case 'accepted':
|
case 'accepted':
|
||||||
@ -25,7 +26,6 @@ export const formatGoogleCalendarEvent = (
|
|||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id,
|
|
||||||
title: event.summary ?? '',
|
title: event.summary ?? '',
|
||||||
isCanceled: event.status === 'cancelled',
|
isCanceled: event.status === 'cancelled',
|
||||||
isFullDay: event.start?.dateTime == null,
|
isFullDay: event.start?.dateTime == null,
|
||||||
@ -44,12 +44,12 @@ export const formatGoogleCalendarEvent = (
|
|||||||
recurringEventExternalId: event.recurringEventId ?? '',
|
recurringEventExternalId: event.recurringEventId ?? '',
|
||||||
participants:
|
participants:
|
||||||
event.attendees?.map((attendee) => ({
|
event.attendees?.map((attendee) => ({
|
||||||
calendarEventId: id,
|
|
||||||
iCalUID: event.iCalUID ?? '',
|
iCalUID: event.iCalUID ?? '',
|
||||||
handle: attendee.email ?? '',
|
handle: attendee.email ?? '',
|
||||||
displayName: attendee.displayName ?? '',
|
displayName: attendee.displayName ?? '',
|
||||||
isOrganizer: attendee.organizer === true,
|
isOrganizer: attendee.organizer === true,
|
||||||
responseStatus: formatResponseStatus(attendee.responseStatus),
|
responseStatus: formatResponseStatus(attendee.responseStatus),
|
||||||
})) ?? [],
|
})) ?? [],
|
||||||
|
status: event.status ?? '',
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@ -0,0 +1,92 @@
|
|||||||
|
import { Scope } from '@nestjs/common';
|
||||||
|
|
||||||
|
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';
|
||||||
|
import { isThrottled } from 'src/modules/connected-account/utils/is-throttled';
|
||||||
|
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 {
|
||||||
|
CalendarChannelSyncStage,
|
||||||
|
CalendarChannelWorkspaceEntity,
|
||||||
|
} from 'src/modules/calendar/common/standard-objects/calendar-channel.workspace-entity';
|
||||||
|
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
|
||||||
|
import { InjectWorkspaceRepository } from 'src/engine/twenty-orm/decorators/inject-workspace-repository.decorator';
|
||||||
|
|
||||||
|
export type CalendarEventsImportJobData = {
|
||||||
|
calendarChannelId: string;
|
||||||
|
workspaceId: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
@Processor({
|
||||||
|
queueName: MessageQueue.calendarQueue,
|
||||||
|
scope: Scope.REQUEST,
|
||||||
|
})
|
||||||
|
export class CalendarEventListFetchJob {
|
||||||
|
constructor(
|
||||||
|
private readonly calendarEventsImportService: CalendarEventsImportService,
|
||||||
|
@InjectObjectMetadataRepository(ConnectedAccountWorkspaceEntity)
|
||||||
|
private readonly connectedAccountRepository: ConnectedAccountRepository,
|
||||||
|
@InjectWorkspaceRepository(CalendarChannelWorkspaceEntity)
|
||||||
|
private readonly calendarChannelRepository: WorkspaceRepository<CalendarChannelWorkspaceEntity>,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
@Process(CalendarEventListFetchJob.name)
|
||||||
|
async handle(data: CalendarEventsImportJobData): Promise<void> {
|
||||||
|
const { workspaceId, calendarChannelId } = data;
|
||||||
|
|
||||||
|
const calendarChannel = await this.calendarChannelRepository.findOne({
|
||||||
|
where: {
|
||||||
|
id: calendarChannelId,
|
||||||
|
isSyncEnabled: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!calendarChannel) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
isThrottled(
|
||||||
|
calendarChannel.syncStageStartedAt,
|
||||||
|
calendarChannel.throttleFailureCount,
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const connectedAccount =
|
||||||
|
await this.connectedAccountRepository.getConnectedAccountOrThrow(
|
||||||
|
workspaceId,
|
||||||
|
calendarChannel.connectedAccountId,
|
||||||
|
);
|
||||||
|
|
||||||
|
switch (calendarChannel.syncStage) {
|
||||||
|
case CalendarChannelSyncStage.FULL_CALENDAR_EVENT_LIST_FETCH_PENDING:
|
||||||
|
await this.calendarChannelRepository.update(calendarChannelId, {
|
||||||
|
syncCursor: '',
|
||||||
|
syncStageStartedAt: null,
|
||||||
|
});
|
||||||
|
|
||||||
|
await this.calendarEventsImportService.processCalendarEventsImport(
|
||||||
|
calendarChannel,
|
||||||
|
connectedAccount,
|
||||||
|
workspaceId,
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case CalendarChannelSyncStage.PARTIAL_CALENDAR_EVENT_LIST_FETCH_PENDING:
|
||||||
|
await this.calendarEventsImportService.processCalendarEventsImport(
|
||||||
|
calendarChannel,
|
||||||
|
connectedAccount,
|
||||||
|
workspaceId,
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,35 +0,0 @@
|
|||||||
import { Logger, Scope } from '@nestjs/common';
|
|
||||||
|
|
||||||
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 googleCalendarSyncService: CalendarEventsImportService,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
@Process(CalendarEventsImportJob.name)
|
|
||||||
async handle(data: CalendarEventsImportJobData): Promise<void> {
|
|
||||||
this.logger.log(
|
|
||||||
`google calendar sync for workspace ${data.workspaceId} and account ${data.connectedAccountId}`,
|
|
||||||
);
|
|
||||||
|
|
||||||
await this.googleCalendarSyncService.processCalendarEventsImport(
|
|
||||||
data.workspaceId,
|
|
||||||
data.connectedAccountId,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,111 @@
|
|||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
|
||||||
|
import { CacheStorageService } from 'src/engine/integrations/cache-storage/cache-storage.service';
|
||||||
|
import { InjectCacheStorage } from 'src/engine/integrations/cache-storage/decorators/cache-storage.decorator';
|
||||||
|
import { CacheStorageNamespace } from 'src/engine/integrations/cache-storage/types/cache-storage-namespace.enum';
|
||||||
|
import { InjectWorkspaceRepository } from 'src/engine/twenty-orm/decorators/inject-workspace-repository.decorator';
|
||||||
|
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
|
||||||
|
import {
|
||||||
|
CalendarChannelWorkspaceEntity,
|
||||||
|
CalendarChannelSyncStage,
|
||||||
|
CalendarChannelSyncStatus,
|
||||||
|
} from 'src/modules/calendar/common/standard-objects/calendar-channel.workspace-entity';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class CalendarChannelSyncStatusService {
|
||||||
|
constructor(
|
||||||
|
@InjectWorkspaceRepository(CalendarChannelWorkspaceEntity)
|
||||||
|
private readonly calendarChannelRepository: WorkspaceRepository<CalendarChannelWorkspaceEntity>,
|
||||||
|
@InjectCacheStorage(CacheStorageNamespace.Calendar)
|
||||||
|
private readonly cacheStorage: CacheStorageService,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public async scheduleFullCalendarEventListFetch(calendarChannelId: string) {
|
||||||
|
await this.calendarChannelRepository.update(calendarChannelId, {
|
||||||
|
syncStage:
|
||||||
|
CalendarChannelSyncStage.FULL_CALENDAR_EVENT_LIST_FETCH_PENDING,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public async schedulePartialCalendarEventListFetch(
|
||||||
|
calendarChannelId: string,
|
||||||
|
) {
|
||||||
|
await this.calendarChannelRepository.update(calendarChannelId, {
|
||||||
|
syncStage:
|
||||||
|
CalendarChannelSyncStage.PARTIAL_CALENDAR_EVENT_LIST_FETCH_PENDING,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public async markAsCalendarEventListFetchOngoing(calendarChannelId: string) {
|
||||||
|
await this.calendarChannelRepository.update(calendarChannelId, {
|
||||||
|
syncStage: CalendarChannelSyncStage.CALENDAR_EVENT_LIST_FETCH_ONGOING,
|
||||||
|
syncStatus: CalendarChannelSyncStatus.ONGOING,
|
||||||
|
syncStageStartedAt: new Date().toISOString(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public async resetAndScheduleFullCalendarEventListFetch(
|
||||||
|
calendarChannelId: string,
|
||||||
|
workspaceId: string,
|
||||||
|
) {
|
||||||
|
await this.cacheStorage.del(
|
||||||
|
`calendar-events-to-import:${workspaceId}:google-calendar:${calendarChannelId}`,
|
||||||
|
);
|
||||||
|
|
||||||
|
await this.calendarChannelRepository.update(calendarChannelId, {
|
||||||
|
syncCursor: '',
|
||||||
|
syncStageStartedAt: null,
|
||||||
|
throttleFailureCount: 0,
|
||||||
|
});
|
||||||
|
|
||||||
|
await this.scheduleFullCalendarEventListFetch(calendarChannelId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async scheduleCalendarEventsImport(calendarChannelId: string) {
|
||||||
|
await this.calendarChannelRepository.update(calendarChannelId, {
|
||||||
|
syncStage: CalendarChannelSyncStage.CALENDAR_EVENTS_IMPORT_PENDING,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public async markAsCalendarEventsImportOngoing(calendarChannelId: string) {
|
||||||
|
await this.calendarChannelRepository.update(calendarChannelId, {
|
||||||
|
syncStage: CalendarChannelSyncStage.CALENDAR_EVENTS_IMPORT_ONGOING,
|
||||||
|
syncStatus: CalendarChannelSyncStatus.ONGOING,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public async markAsCalendarEventsImportCompleted(calendarChannelId: string) {
|
||||||
|
await this.calendarChannelRepository.update(calendarChannelId, {
|
||||||
|
syncStage: CalendarChannelSyncStage.CALENDAR_EVENTS_IMPORT_PENDING,
|
||||||
|
syncStatus: CalendarChannelSyncStatus.ACTIVE,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public async markAsFailedUnknownAndFlushCalendarEventsToImport(
|
||||||
|
calendarChannelId: string,
|
||||||
|
workspaceId: string,
|
||||||
|
) {
|
||||||
|
await this.cacheStorage.del(
|
||||||
|
`calendar-events-to-import:${workspaceId}:google-calendar:${calendarChannelId}`,
|
||||||
|
);
|
||||||
|
|
||||||
|
await this.calendarChannelRepository.update(calendarChannelId, {
|
||||||
|
syncStatus: CalendarChannelSyncStatus.FAILED_UNKNOWN,
|
||||||
|
syncStage: CalendarChannelSyncStage.FAILED,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public async markAsFailedInsufficientPermissionsAndFlushCalendarEventsToImport(
|
||||||
|
calendarChannelId: string,
|
||||||
|
workspaceId: string,
|
||||||
|
) {
|
||||||
|
await this.cacheStorage.del(
|
||||||
|
`calendar-events-to-import:${workspaceId}:google-calendar:${calendarChannelId}`,
|
||||||
|
);
|
||||||
|
|
||||||
|
await this.calendarChannelRepository.update(calendarChannelId, {
|
||||||
|
syncStatus: CalendarChannelSyncStatus.FAILED_INSUFFICIENT_PERMISSIONS,
|
||||||
|
syncStage: CalendarChannelSyncStage.FAILED,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,600 +1,111 @@
|
|||||||
import { Injectable, Logger } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
|
||||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
|
||||||
|
|
||||||
import { Any, Repository } from 'typeorm';
|
import { Any } 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 { 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 { InjectWorkspaceRepository } from 'src/engine/twenty-orm/decorators/inject-workspace-repository.decorator';
|
||||||
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
|
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 { 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 { CalendarChannelSyncStatusService } from 'src/modules/calendar/calendar-event-import-manager/services/calendar-channel-sync-status.service';
|
||||||
import { GoogleCalendarClientProvider } from 'src/modules/calendar/calendar-event-import-manager/drivers/google-calendar/providers/google-calendar.provider';
|
import { CalendarGetCalendarEventsService } from 'src/modules/calendar/calendar-event-import-manager/services/calendar-get-events.service';
|
||||||
|
import { CalendarSaveEventsService } from 'src/modules/calendar/calendar-event-import-manager/services/calendar-save-events.service';
|
||||||
|
import { filterEventsAndReturnCancelledEvents } from 'src/modules/calendar/calendar-event-import-manager/utils/filter-events.util';
|
||||||
import { CalendarChannelEventAssociationWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-channel-event-association.workspace-entity';
|
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 { 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 { BlocklistRepository } from 'src/modules/connected-account/repositories/blocklist.repository';
|
||||||
import { CalendarEventWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event.workspace-entity';
|
import { BlocklistWorkspaceEntity } from 'src/modules/connected-account/standard-objects/blocklist.workspace-entity';
|
||||||
import { filterOutBlocklistedEvents } from 'src/modules/calendar/calendar-event-import-manager/utils/filter-out-blocklisted-events.util';
|
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class CalendarEventsImportService {
|
export class CalendarEventsImportService {
|
||||||
private readonly logger = new Logger(CalendarEventsImportService.name);
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly googleCalendarClientProvider: GoogleCalendarClientProvider,
|
|
||||||
@InjectObjectMetadataRepository(ConnectedAccountWorkspaceEntity)
|
|
||||||
private readonly connectedAccountRepository: ConnectedAccountRepository,
|
|
||||||
@InjectWorkspaceRepository(CalendarEventWorkspaceEntity)
|
|
||||||
private readonly calendarEventRepository: WorkspaceRepository<CalendarEventWorkspaceEntity>,
|
|
||||||
@InjectWorkspaceRepository(CalendarChannelWorkspaceEntity)
|
@InjectWorkspaceRepository(CalendarChannelWorkspaceEntity)
|
||||||
private readonly calendarChannelRepository: WorkspaceRepository<CalendarChannelWorkspaceEntity>,
|
private readonly calendarChannelRepository: WorkspaceRepository<CalendarChannelWorkspaceEntity>,
|
||||||
@InjectWorkspaceRepository(CalendarChannelEventAssociationWorkspaceEntity)
|
@InjectWorkspaceRepository(CalendarChannelEventAssociationWorkspaceEntity)
|
||||||
private readonly calendarChannelEventAssociationRepository: WorkspaceRepository<CalendarChannelEventAssociationWorkspaceEntity>,
|
private readonly calendarChannelEventAssociationRepository: WorkspaceRepository<CalendarChannelEventAssociationWorkspaceEntity>,
|
||||||
@InjectWorkspaceRepository(CalendarEventParticipantWorkspaceEntity)
|
|
||||||
private readonly calendarEventParticipantsRepository: WorkspaceRepository<CalendarEventParticipantWorkspaceEntity>,
|
|
||||||
@InjectObjectMetadataRepository(BlocklistWorkspaceEntity)
|
@InjectObjectMetadataRepository(BlocklistWorkspaceEntity)
|
||||||
private readonly blocklistRepository: BlocklistRepository,
|
private readonly blocklistRepository: BlocklistRepository,
|
||||||
@InjectRepository(FeatureFlagEntity, 'core')
|
|
||||||
private readonly featureFlagRepository: Repository<FeatureFlagEntity>,
|
|
||||||
@InjectWorkspaceDatasource()
|
|
||||||
private readonly workspaceDataSource: WorkspaceDataSource,
|
|
||||||
private readonly calendarEventCleanerService: CalendarEventCleanerService,
|
private readonly calendarEventCleanerService: CalendarEventCleanerService,
|
||||||
private readonly calendarEventParticipantsService: CalendarEventParticipantService,
|
private readonly calendarChannelSyncStatusService: CalendarChannelSyncStatusService,
|
||||||
@InjectMessageQueue(MessageQueue.contactCreationQueue)
|
private readonly getCalendarEventsService: CalendarGetCalendarEventsService,
|
||||||
private readonly messageQueueService: MessageQueueService,
|
private readonly calendarSaveEventsService: CalendarSaveEventsService,
|
||||||
private readonly eventEmitter: EventEmitter2,
|
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public async processCalendarEventsImport(
|
public async processCalendarEventsImport(
|
||||||
|
calendarChannel: CalendarChannelWorkspaceEntity,
|
||||||
|
connectedAccount: ConnectedAccountWorkspaceEntity,
|
||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
connectedAccountId: string,
|
|
||||||
emailOrDomainToReimport?: string,
|
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const connectedAccount = await this.connectedAccountRepository.getById(
|
await this.calendarChannelSyncStatusService.markAsCalendarEventListFetchOngoing(
|
||||||
connectedAccountId,
|
calendarChannel.id,
|
||||||
workspaceId,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!connectedAccount) {
|
const { calendarEvents, nextSyncCursor } =
|
||||||
return;
|
await this.getCalendarEventsService.getCalendarEvents(
|
||||||
}
|
connectedAccount,
|
||||||
|
calendarChannel.syncCursor,
|
||||||
|
);
|
||||||
|
|
||||||
const refreshToken = connectedAccount.refreshToken;
|
if (!calendarEvents || calendarEvents?.length === 0) {
|
||||||
const workspaceMemberId = connectedAccount.accountOwnerId;
|
await this.calendarChannelRepository.update(
|
||||||
|
{
|
||||||
|
id: calendarChannel.id,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
syncCursor: nextSyncCursor,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
if (!refreshToken) {
|
await this.calendarChannelSyncStatusService.schedulePartialCalendarEventListFetch(
|
||||||
throw new Error(
|
calendarChannel.id,
|
||||||
`No refresh token found for connected account ${connectedAccountId} in workspace ${workspaceId} during sync`,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const calendarChannel = await this.calendarChannelRepository.findOneBy({
|
const blocklist = await this.blocklistRepository.getByWorkspaceMemberId(
|
||||||
connectedAccount: {
|
connectedAccount.accountOwnerId,
|
||||||
id: connectedAccountId,
|
workspaceId,
|
||||||
},
|
);
|
||||||
});
|
|
||||||
|
|
||||||
const syncToken = calendarChannel?.syncCursor || undefined;
|
const { filteredEvents, cancelledEvents } =
|
||||||
|
filterEventsAndReturnCancelledEvents(
|
||||||
|
calendarChannel,
|
||||||
|
calendarEvents,
|
||||||
|
blocklist.map((blocklist) => blocklist.handle),
|
||||||
|
);
|
||||||
|
|
||||||
if (!calendarChannel) {
|
const cancelledEventExternalIds = cancelledEvents.map(
|
||||||
return;
|
(event) => event.externalId,
|
||||||
}
|
);
|
||||||
|
|
||||||
const calendarChannelId = calendarChannel.id;
|
await this.calendarSaveEventsService.saveCalendarEventsAndEnqueueContactCreationJob(
|
||||||
|
filteredEvents,
|
||||||
const { events, nextSyncToken } = await this.getEventsFromGoogleCalendar(
|
calendarChannel,
|
||||||
connectedAccount,
|
connectedAccount,
|
||||||
workspaceId,
|
workspaceId,
|
||||||
emailOrDomainToReimport,
|
|
||||||
syncToken,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!events || events?.length === 0) {
|
await this.calendarChannelEventAssociationRepository.delete({
|
||||||
this.logger.log(
|
eventExternalId: Any(cancelledEventExternalIds),
|
||||||
`google calendar sync for workspace ${workspaceId} and account ${connectedAccountId} done with nothing to import.`,
|
calendarChannel: {
|
||||||
);
|
id: calendarChannel.id,
|
||||||
|
|
||||||
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(
|
await this.calendarEventCleanerService.cleanWorkspaceCalendarEvents(
|
||||||
existingCalendarEvents.map((calendarEvent) => [
|
workspaceId,
|
||||||
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(
|
await this.calendarChannelRepository.update(
|
||||||
{
|
{
|
||||||
id: calendarChannel.id,
|
id: calendarChannel.id,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
syncCursor: nextSyncToken,
|
syncCursor: nextSyncCursor,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
endTime = Date.now();
|
await this.calendarChannelSyncStatusService.schedulePartialCalendarEventListFetch(
|
||||||
|
calendarChannel.id,
|
||||||
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}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,37 @@
|
|||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
|
||||||
|
import { GoogleCalendarGetEventsService as GoogleCalendarGetCalendarEventsService } from 'src/modules/calendar/calendar-event-import-manager/drivers/google-calendar/services/google-calendar-get-events.service';
|
||||||
|
import { CalendarEventWithParticipants } from 'src/modules/calendar/common/types/calendar-event';
|
||||||
|
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
|
||||||
|
|
||||||
|
export type GetCalendarEventsResponse = {
|
||||||
|
calendarEvents: CalendarEventWithParticipants[];
|
||||||
|
nextSyncCursor: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class CalendarGetCalendarEventsService {
|
||||||
|
constructor(
|
||||||
|
private readonly googleCalendarGetCalendarEventsService: GoogleCalendarGetCalendarEventsService,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public async getCalendarEvents(
|
||||||
|
connectedAccount: Pick<
|
||||||
|
ConnectedAccountWorkspaceEntity,
|
||||||
|
'provider' | 'refreshToken' | 'id'
|
||||||
|
>,
|
||||||
|
syncCursor?: string,
|
||||||
|
): Promise<GetCalendarEventsResponse> {
|
||||||
|
switch (connectedAccount.provider) {
|
||||||
|
case 'google':
|
||||||
|
return this.googleCalendarGetCalendarEventsService.getCalendarEvents(
|
||||||
|
connectedAccount,
|
||||||
|
syncCursor,
|
||||||
|
);
|
||||||
|
default:
|
||||||
|
throw new Error(
|
||||||
|
`Provider ${connectedAccount.provider} is not supported.`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,160 @@
|
|||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||||
|
|
||||||
|
import { Any } from 'typeorm';
|
||||||
|
|
||||||
|
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 { WorkspaceDataSource } from 'src/engine/twenty-orm/datasource/workspace.datasource';
|
||||||
|
import { InjectWorkspaceDatasource } from 'src/engine/twenty-orm/decorators/inject-workspace-datasource.decorator';
|
||||||
|
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 { injectIdsInCalendarEvents } from 'src/modules/calendar/calendar-event-import-manager/utils/inject-ids-in-calendar-events.util';
|
||||||
|
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
|
||||||
|
import { CalendarEventWithParticipants } from 'src/modules/calendar/common/types/calendar-event';
|
||||||
|
import { CalendarEventParticipantService } from 'src/modules/calendar/calendar-event-participant-manager/services/calendar-event-participant.service';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class CalendarSaveEventsService {
|
||||||
|
constructor(
|
||||||
|
@InjectWorkspaceRepository(CalendarEventWorkspaceEntity)
|
||||||
|
private readonly calendarEventRepository: WorkspaceRepository<CalendarEventWorkspaceEntity>,
|
||||||
|
@InjectWorkspaceRepository(CalendarChannelEventAssociationWorkspaceEntity)
|
||||||
|
private readonly calendarChannelEventAssociationRepository: WorkspaceRepository<CalendarChannelEventAssociationWorkspaceEntity>,
|
||||||
|
@InjectWorkspaceDatasource()
|
||||||
|
private readonly workspaceDataSource: WorkspaceDataSource,
|
||||||
|
private readonly calendarEventParticipantService: CalendarEventParticipantService,
|
||||||
|
@InjectMessageQueue(MessageQueue.contactCreationQueue)
|
||||||
|
private readonly messageQueueService: MessageQueueService,
|
||||||
|
private readonly eventEmitter: EventEmitter2,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public async saveCalendarEventsAndEnqueueContactCreationJob(
|
||||||
|
filteredEvents: CalendarEventWithParticipants[],
|
||||||
|
calendarChannel: CalendarChannelWorkspaceEntity,
|
||||||
|
connectedAccount: ConnectedAccountWorkspaceEntity,
|
||||||
|
workspaceId: string,
|
||||||
|
): Promise<void> {
|
||||||
|
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 calendarEventsWithIds = injectIdsInCalendarEvents(
|
||||||
|
filteredEvents,
|
||||||
|
iCalUIDCalendarEventIdMap,
|
||||||
|
);
|
||||||
|
|
||||||
|
// TODO: When we will be able to add unicity contraint on iCalUID, we will do a INSERT ON CONFLICT DO UPDATE
|
||||||
|
|
||||||
|
const existingEventsICalUIDs = existingCalendarEvents.map(
|
||||||
|
(calendarEvent) => calendarEvent.iCalUID,
|
||||||
|
);
|
||||||
|
|
||||||
|
const eventsToSave = calendarEventsWithIds.filter(
|
||||||
|
(calendarEvent) =>
|
||||||
|
!existingEventsICalUIDs.includes(calendarEvent.iCalUID),
|
||||||
|
);
|
||||||
|
|
||||||
|
const eventsToUpdate = calendarEventsWithIds.filter((calendarEvent) =>
|
||||||
|
existingEventsICalUIDs.includes(calendarEvent.iCalUID),
|
||||||
|
);
|
||||||
|
|
||||||
|
const existingCalendarChannelEventAssociations =
|
||||||
|
await this.calendarChannelEventAssociationRepository.find({
|
||||||
|
where: {
|
||||||
|
eventExternalId: Any(
|
||||||
|
calendarEventsWithIds.map((calendarEvent) => calendarEvent.id),
|
||||||
|
),
|
||||||
|
calendarChannel: {
|
||||||
|
id: calendarChannel.id,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const calendarChannelEventAssociationsToSave = calendarEventsWithIds
|
||||||
|
.filter(
|
||||||
|
(calendarEvent) =>
|
||||||
|
!existingCalendarChannelEventAssociations.some(
|
||||||
|
(association) => association.eventExternalId === calendarEvent.id,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.map((calendarEvent) => ({
|
||||||
|
calendarEventId: calendarEvent.id,
|
||||||
|
eventExternalId: calendarEvent.externalId,
|
||||||
|
calendarChannelId: calendarChannel.id,
|
||||||
|
}));
|
||||||
|
|
||||||
|
const participantsToSave = eventsToSave.flatMap(
|
||||||
|
(event) => event.participants,
|
||||||
|
);
|
||||||
|
|
||||||
|
const participantsToUpdate = eventsToUpdate.flatMap(
|
||||||
|
(event) => event.participants,
|
||||||
|
);
|
||||||
|
|
||||||
|
const savedCalendarEventParticipantsToEmit: CalendarEventParticipantWorkspaceEntity[] =
|
||||||
|
[];
|
||||||
|
|
||||||
|
await this.workspaceDataSource?.transaction(async (transactionManager) => {
|
||||||
|
await this.calendarEventRepository.save(
|
||||||
|
eventsToSave,
|
||||||
|
{},
|
||||||
|
transactionManager,
|
||||||
|
);
|
||||||
|
|
||||||
|
await this.calendarEventRepository.save(
|
||||||
|
eventsToUpdate,
|
||||||
|
{},
|
||||||
|
transactionManager,
|
||||||
|
);
|
||||||
|
|
||||||
|
await this.calendarChannelEventAssociationRepository.save(
|
||||||
|
calendarChannelEventAssociationsToSave,
|
||||||
|
{},
|
||||||
|
transactionManager,
|
||||||
|
);
|
||||||
|
|
||||||
|
await this.calendarEventParticipantService.upsertAndDeleteCalendarEventParticipants(
|
||||||
|
participantsToSave,
|
||||||
|
participantsToUpdate,
|
||||||
|
workspaceId,
|
||||||
|
transactionManager,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
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,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,40 @@
|
|||||||
|
import { filterOutBlocklistedEvents } from 'src/modules/calendar/calendar-event-import-manager/utils/filter-out-blocklisted-events.util';
|
||||||
|
import { CalendarChannelWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-channel.workspace-entity';
|
||||||
|
import { CalendarEventWithParticipants } from 'src/modules/calendar/common/types/calendar-event';
|
||||||
|
|
||||||
|
export const filterEventsAndReturnCancelledEvents = (
|
||||||
|
calendarChannel: Pick<CalendarChannelWorkspaceEntity, 'handle'>,
|
||||||
|
events: CalendarEventWithParticipants[],
|
||||||
|
blocklist: string[],
|
||||||
|
): {
|
||||||
|
filteredEvents: CalendarEventWithParticipants[];
|
||||||
|
cancelledEvents: CalendarEventWithParticipants[];
|
||||||
|
} => {
|
||||||
|
const filteredEvents = filterOutBlocklistedEvents(
|
||||||
|
calendarChannel.handle,
|
||||||
|
events,
|
||||||
|
blocklist,
|
||||||
|
);
|
||||||
|
|
||||||
|
return filteredEvents.reduce(
|
||||||
|
(
|
||||||
|
acc: {
|
||||||
|
filteredEvents: CalendarEventWithParticipants[];
|
||||||
|
cancelledEvents: CalendarEventWithParticipants[];
|
||||||
|
},
|
||||||
|
event,
|
||||||
|
) => {
|
||||||
|
if (event.status === 'cancelled') {
|
||||||
|
acc.cancelledEvents.push(event);
|
||||||
|
} else {
|
||||||
|
acc.filteredEvents.push(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
return acc;
|
||||||
|
},
|
||||||
|
{
|
||||||
|
filteredEvents: [],
|
||||||
|
cancelledEvents: [],
|
||||||
|
},
|
||||||
|
);
|
||||||
|
};
|
||||||
@ -1,20 +1,19 @@
|
|||||||
import { calendar_v3 as calendarV3 } from 'googleapis';
|
import { isEmailBlocklisted } from 'src/modules/calendar-messaging-participant-manager/utils/is-email-blocklisted.util';
|
||||||
|
import { CalendarEventWithParticipants } from 'src/modules/calendar/common/types/calendar-event';
|
||||||
import { isEmailBlocklisted } from 'src/modules/calendar-messaging-participant/utils/is-email-blocklisted.util';
|
|
||||||
|
|
||||||
export const filterOutBlocklistedEvents = (
|
export const filterOutBlocklistedEvents = (
|
||||||
calendarChannelHandle: string,
|
calendarChannelHandle: string,
|
||||||
events: calendarV3.Schema$Event[],
|
events: CalendarEventWithParticipants[],
|
||||||
blocklist: string[],
|
blocklist: string[],
|
||||||
) => {
|
) => {
|
||||||
return events.filter((event) => {
|
return events.filter((event) => {
|
||||||
if (!event.attendees) {
|
if (!event.participants) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return event.attendees.every(
|
return event.participants.every(
|
||||||
(attendee) =>
|
(attendee) =>
|
||||||
!isEmailBlocklisted(calendarChannelHandle, attendee.email, blocklist),
|
!isEmailBlocklisted(calendarChannelHandle, attendee.handle, blocklist),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@ -0,0 +1,31 @@
|
|||||||
|
import { v4 } from 'uuid';
|
||||||
|
|
||||||
|
import {
|
||||||
|
CalendarEventWithParticipants,
|
||||||
|
CalendarEventWithParticipantsAndCalendarEventId,
|
||||||
|
} from 'src/modules/calendar/common/types/calendar-event';
|
||||||
|
|
||||||
|
export const injectIdsInCalendarEvents = (
|
||||||
|
calendarEvents: CalendarEventWithParticipants[],
|
||||||
|
iCalUIDCalendarEventIdMap: Map<string, string>,
|
||||||
|
): CalendarEventWithParticipantsAndCalendarEventId[] => {
|
||||||
|
return calendarEvents.map((calendarEvent) => {
|
||||||
|
const id = iCalUIDCalendarEventIdMap.get(calendarEvent.iCalUID) ?? v4();
|
||||||
|
|
||||||
|
return injectIdInCalendarEvent(calendarEvent, id);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const injectIdInCalendarEvent = (
|
||||||
|
calendarEvent: CalendarEventWithParticipants,
|
||||||
|
id: string,
|
||||||
|
): CalendarEventWithParticipantsAndCalendarEventId => {
|
||||||
|
return {
|
||||||
|
...calendarEvent,
|
||||||
|
id,
|
||||||
|
participants: calendarEvent.participants.map((participant) => ({
|
||||||
|
...participant,
|
||||||
|
calendarEventId: id,
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
};
|
||||||
@ -0,0 +1,46 @@
|
|||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
|
|
||||||
|
import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
|
||||||
|
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.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 { AddPersonIdAndWorkspaceMemberIdService } from 'src/modules/calendar-messaging-participant-manager/services/add-person-id-and-workspace-member-id/add-person-id-and-workspace-member-id.service';
|
||||||
|
import { CalendarCreateCompanyAndContactAfterSyncJob } from 'src/modules/calendar/calendar-event-participant-manager/jobs/calendar-create-company-and-contact-after-sync.job';
|
||||||
|
import { CalendarEventParticipantMatchParticipantJob } from 'src/modules/calendar/calendar-event-participant-manager/jobs/calendar-event-participant-match-participant.job';
|
||||||
|
import { CalendarEventParticipantUnmatchParticipantJob } from 'src/modules/calendar/calendar-event-participant-manager/jobs/calendar-event-participant-unmatch-participant.job';
|
||||||
|
import { CalendarEventParticipantPersonListener } from 'src/modules/calendar/calendar-event-participant-manager/listeners/calendar-event-participant-person.listener';
|
||||||
|
import { CalendarEventParticipantWorkspaceMemberListener } from 'src/modules/calendar/calendar-event-participant-manager/listeners/calendar-event-participant-workspace-member.listener';
|
||||||
|
import { CalendarEventParticipantListener } from 'src/modules/calendar/calendar-event-participant-manager/listeners/calendar-event-participant.listener';
|
||||||
|
import { CalendarEventParticipantService } from 'src/modules/calendar/calendar-event-participant-manager/services/calendar-event-participant.service';
|
||||||
|
import { CalendarCommonModule } from 'src/modules/calendar/common/calendar-common.module';
|
||||||
|
import { CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event-participant.workspace-entity';
|
||||||
|
import { AutoCompaniesAndContactsCreationModule } from 'src/modules/connected-account/auto-companies-and-contacts-creation/auto-companies-and-contacts-creation.module';
|
||||||
|
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
imports: [
|
||||||
|
WorkspaceDataSourceModule,
|
||||||
|
TwentyORMModule.forFeature([CalendarEventParticipantWorkspaceEntity]),
|
||||||
|
ObjectMetadataRepositoryModule.forFeature([PersonWorkspaceEntity]),
|
||||||
|
TypeOrmModule.forFeature(
|
||||||
|
[ObjectMetadataEntity, FieldMetadataEntity],
|
||||||
|
'metadata',
|
||||||
|
),
|
||||||
|
AutoCompaniesAndContactsCreationModule,
|
||||||
|
CalendarCommonModule,
|
||||||
|
],
|
||||||
|
providers: [
|
||||||
|
CalendarEventParticipantService,
|
||||||
|
CalendarCreateCompanyAndContactAfterSyncJob,
|
||||||
|
CalendarEventParticipantMatchParticipantJob,
|
||||||
|
CalendarEventParticipantUnmatchParticipantJob,
|
||||||
|
CalendarEventParticipantListener,
|
||||||
|
CalendarEventParticipantPersonListener,
|
||||||
|
CalendarEventParticipantWorkspaceMemberListener,
|
||||||
|
AddPersonIdAndWorkspaceMemberIdService,
|
||||||
|
],
|
||||||
|
exports: [CalendarEventParticipantService],
|
||||||
|
})
|
||||||
|
export class CalendarEventParticipantManagerModule {}
|
||||||
@ -1,32 +0,0 @@
|
|||||||
import { Module } from '@nestjs/common';
|
|
||||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
|
||||||
|
|
||||||
import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
|
|
||||||
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.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 { AddPersonIdAndWorkspaceMemberIdModule } from 'src/modules/calendar-messaging-participant/services/add-person-id-and-workspace-member-id/add-person-id-and-workspace-member-id.module';
|
|
||||||
import { CalendarEventParticipantListener } from 'src/modules/calendar/calendar-event-participant-manager/listeners/calendar-event-participant.listener';
|
|
||||||
import { CalendarEventParticipantService } from 'src/modules/calendar/calendar-event-participant-manager/services/calendar-event-participant.service';
|
|
||||||
import { CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event-participant.workspace-entity';
|
|
||||||
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
|
|
||||||
|
|
||||||
@Module({
|
|
||||||
imports: [
|
|
||||||
WorkspaceDataSourceModule,
|
|
||||||
TwentyORMModule.forFeature([CalendarEventParticipantWorkspaceEntity]),
|
|
||||||
ObjectMetadataRepositoryModule.forFeature([PersonWorkspaceEntity]),
|
|
||||||
TypeOrmModule.forFeature(
|
|
||||||
[ObjectMetadataEntity, FieldMetadataEntity],
|
|
||||||
'metadata',
|
|
||||||
),
|
|
||||||
AddPersonIdAndWorkspaceMemberIdModule,
|
|
||||||
],
|
|
||||||
providers: [
|
|
||||||
CalendarEventParticipantService,
|
|
||||||
CalendarEventParticipantListener,
|
|
||||||
],
|
|
||||||
exports: [CalendarEventParticipantService],
|
|
||||||
})
|
|
||||||
export class CalendarEventParticipantModule {}
|
|
||||||
@ -4,9 +4,8 @@ import { Process } from 'src/engine/integrations/message-queue/decorators/proces
|
|||||||
import { Processor } from 'src/engine/integrations/message-queue/decorators/processor.decorator';
|
import { Processor } from 'src/engine/integrations/message-queue/decorators/processor.decorator';
|
||||||
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
|
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
|
||||||
import { CalendarEventParticipantService } from 'src/modules/calendar/calendar-event-participant-manager/services/calendar-event-participant.service';
|
import { CalendarEventParticipantService } from 'src/modules/calendar/calendar-event-participant-manager/services/calendar-event-participant.service';
|
||||||
import { MessagingMessageParticipantService } from 'src/modules/messaging/common/services/messaging-message-participant.service';
|
|
||||||
|
|
||||||
export type MatchParticipantJobData = {
|
export type CalendarEventParticipantMatchParticipantJobData = {
|
||||||
workspaceId: string;
|
workspaceId: string;
|
||||||
email: string;
|
email: string;
|
||||||
personId?: string;
|
personId?: string;
|
||||||
@ -14,26 +13,20 @@ export type MatchParticipantJobData = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
@Processor({
|
@Processor({
|
||||||
queueName: MessageQueue.messagingQueue,
|
queueName: MessageQueue.calendarQueue,
|
||||||
scope: Scope.REQUEST,
|
scope: Scope.REQUEST,
|
||||||
})
|
})
|
||||||
export class MatchParticipantJob {
|
export class CalendarEventParticipantMatchParticipantJob {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly messageParticipantService: MessagingMessageParticipantService,
|
|
||||||
private readonly calendarEventParticipantService: CalendarEventParticipantService,
|
private readonly calendarEventParticipantService: CalendarEventParticipantService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@Process(MatchParticipantJob.name)
|
@Process(CalendarEventParticipantMatchParticipantJob.name)
|
||||||
async handle(data: MatchParticipantJobData): Promise<void> {
|
async handle(
|
||||||
|
data: CalendarEventParticipantMatchParticipantJobData,
|
||||||
|
): Promise<void> {
|
||||||
const { workspaceId, email, personId, workspaceMemberId } = data;
|
const { workspaceId, email, personId, workspaceMemberId } = data;
|
||||||
|
|
||||||
await this.messageParticipantService.matchMessageParticipants(
|
|
||||||
workspaceId,
|
|
||||||
email,
|
|
||||||
personId,
|
|
||||||
workspaceMemberId,
|
|
||||||
);
|
|
||||||
|
|
||||||
await this.calendarEventParticipantService.matchCalendarEventParticipants(
|
await this.calendarEventParticipantService.matchCalendarEventParticipants(
|
||||||
workspaceId,
|
workspaceId,
|
||||||
email,
|
email,
|
||||||
@ -2,11 +2,10 @@ import { Scope } from '@nestjs/common';
|
|||||||
|
|
||||||
import { Processor } from 'src/engine/integrations/message-queue/decorators/processor.decorator';
|
import { Processor } from 'src/engine/integrations/message-queue/decorators/processor.decorator';
|
||||||
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
|
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
|
||||||
import { MessagingMessageParticipantService } from 'src/modules/messaging/common/services/messaging-message-participant.service';
|
|
||||||
import { Process } from 'src/engine/integrations/message-queue/decorators/process.decorator';
|
import { Process } from 'src/engine/integrations/message-queue/decorators/process.decorator';
|
||||||
import { CalendarEventParticipantService } from 'src/modules/calendar/calendar-event-participant-manager/services/calendar-event-participant.service';
|
import { CalendarEventParticipantService } from 'src/modules/calendar/calendar-event-participant-manager/services/calendar-event-participant.service';
|
||||||
|
|
||||||
export type UnmatchParticipantJobData = {
|
export type CalendarEventParticipantUnmatchParticipantJobData = {
|
||||||
workspaceId: string;
|
workspaceId: string;
|
||||||
email: string;
|
email: string;
|
||||||
personId?: string;
|
personId?: string;
|
||||||
@ -14,26 +13,20 @@ export type UnmatchParticipantJobData = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
@Processor({
|
@Processor({
|
||||||
queueName: MessageQueue.messagingQueue,
|
queueName: MessageQueue.calendarQueue,
|
||||||
scope: Scope.REQUEST,
|
scope: Scope.REQUEST,
|
||||||
})
|
})
|
||||||
export class UnmatchParticipantJob {
|
export class CalendarEventParticipantUnmatchParticipantJob {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly messageParticipantService: MessagingMessageParticipantService,
|
|
||||||
private readonly calendarEventParticipantService: CalendarEventParticipantService,
|
private readonly calendarEventParticipantService: CalendarEventParticipantService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@Process(UnmatchParticipantJob.name)
|
@Process(CalendarEventParticipantUnmatchParticipantJob.name)
|
||||||
async handle(data: UnmatchParticipantJobData): Promise<void> {
|
async handle(
|
||||||
|
data: CalendarEventParticipantUnmatchParticipantJobData,
|
||||||
|
): Promise<void> {
|
||||||
const { workspaceId, email, personId, workspaceMemberId } = data;
|
const { workspaceId, email, personId, workspaceMemberId } = data;
|
||||||
|
|
||||||
await this.messageParticipantService.unmatchMessageParticipants(
|
|
||||||
workspaceId,
|
|
||||||
email,
|
|
||||||
personId,
|
|
||||||
workspaceMemberId,
|
|
||||||
);
|
|
||||||
|
|
||||||
await this.calendarEventParticipantService.unmatchCalendarEventParticipants(
|
await this.calendarEventParticipantService.unmatchCalendarEventParticipants(
|
||||||
workspaceId,
|
workspaceId,
|
||||||
email,
|
email,
|
||||||
@ -0,0 +1,74 @@
|
|||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { OnEvent } from '@nestjs/event-emitter';
|
||||||
|
|
||||||
|
import { ObjectRecordCreateEvent } from 'src/engine/integrations/event-emitter/types/object-record-create.event';
|
||||||
|
import { ObjectRecordUpdateEvent } from 'src/engine/integrations/event-emitter/types/object-record-update.event';
|
||||||
|
import { objectRecordChangedProperties as objectRecordUpdateEventChangedProperties } from 'src/engine/integrations/event-emitter/utils/object-record-changed-properties.util';
|
||||||
|
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 {
|
||||||
|
CalendarEventParticipantMatchParticipantJobData,
|
||||||
|
CalendarEventParticipantMatchParticipantJob,
|
||||||
|
} from 'src/modules/calendar/calendar-event-participant-manager/jobs/calendar-event-participant-match-participant.job';
|
||||||
|
import {
|
||||||
|
CalendarEventParticipantUnmatchParticipantJobData,
|
||||||
|
CalendarEventParticipantUnmatchParticipantJob,
|
||||||
|
} from 'src/modules/calendar/calendar-event-participant-manager/jobs/calendar-event-participant-unmatch-participant.job';
|
||||||
|
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class CalendarEventParticipantPersonListener {
|
||||||
|
constructor(
|
||||||
|
@InjectMessageQueue(MessageQueue.calendarQueue)
|
||||||
|
private readonly messageQueueService: MessageQueueService,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
@OnEvent('person.created')
|
||||||
|
async handleCreatedEvent(
|
||||||
|
payload: ObjectRecordCreateEvent<PersonWorkspaceEntity>,
|
||||||
|
) {
|
||||||
|
if (payload.properties.after.email === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.messageQueueService.add<CalendarEventParticipantMatchParticipantJobData>(
|
||||||
|
CalendarEventParticipantMatchParticipantJob.name,
|
||||||
|
{
|
||||||
|
workspaceId: payload.workspaceId,
|
||||||
|
email: payload.properties.after.email,
|
||||||
|
personId: payload.recordId,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnEvent('person.updated')
|
||||||
|
async handleUpdatedEvent(
|
||||||
|
payload: ObjectRecordUpdateEvent<PersonWorkspaceEntity>,
|
||||||
|
) {
|
||||||
|
if (
|
||||||
|
objectRecordUpdateEventChangedProperties(
|
||||||
|
payload.properties.before,
|
||||||
|
payload.properties.after,
|
||||||
|
).includes('email')
|
||||||
|
) {
|
||||||
|
await this.messageQueueService.add<CalendarEventParticipantUnmatchParticipantJobData>(
|
||||||
|
CalendarEventParticipantUnmatchParticipantJob.name,
|
||||||
|
{
|
||||||
|
workspaceId: payload.workspaceId,
|
||||||
|
email: payload.properties.before.email,
|
||||||
|
personId: payload.recordId,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
await this.messageQueueService.add<CalendarEventParticipantMatchParticipantJobData>(
|
||||||
|
CalendarEventParticipantMatchParticipantJob.name,
|
||||||
|
{
|
||||||
|
workspaceId: payload.workspaceId,
|
||||||
|
email: payload.properties.after.email,
|
||||||
|
personId: payload.recordId,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,74 @@
|
|||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { OnEvent } from '@nestjs/event-emitter';
|
||||||
|
|
||||||
|
import { ObjectRecordCreateEvent } from 'src/engine/integrations/event-emitter/types/object-record-create.event';
|
||||||
|
import { ObjectRecordUpdateEvent } from 'src/engine/integrations/event-emitter/types/object-record-update.event';
|
||||||
|
import { objectRecordChangedProperties as objectRecordUpdateEventChangedProperties } from 'src/engine/integrations/event-emitter/utils/object-record-changed-properties.util';
|
||||||
|
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 {
|
||||||
|
CalendarEventParticipantMatchParticipantJob,
|
||||||
|
CalendarEventParticipantMatchParticipantJobData,
|
||||||
|
} from 'src/modules/calendar/calendar-event-participant-manager/jobs/calendar-event-participant-match-participant.job';
|
||||||
|
import {
|
||||||
|
CalendarEventParticipantUnmatchParticipantJobData,
|
||||||
|
CalendarEventParticipantUnmatchParticipantJob,
|
||||||
|
} from 'src/modules/calendar/calendar-event-participant-manager/jobs/calendar-event-participant-unmatch-participant.job';
|
||||||
|
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class CalendarEventParticipantWorkspaceMemberListener {
|
||||||
|
constructor(
|
||||||
|
@InjectMessageQueue(MessageQueue.calendarQueue)
|
||||||
|
private readonly messageQueueService: MessageQueueService,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
@OnEvent('workspaceMember.created')
|
||||||
|
async handleCreatedEvent(
|
||||||
|
payload: ObjectRecordCreateEvent<WorkspaceMemberWorkspaceEntity>,
|
||||||
|
) {
|
||||||
|
if (payload.properties.after.userEmail === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.messageQueueService.add<CalendarEventParticipantMatchParticipantJobData>(
|
||||||
|
CalendarEventParticipantMatchParticipantJob.name,
|
||||||
|
{
|
||||||
|
workspaceId: payload.workspaceId,
|
||||||
|
email: payload.properties.after.userEmail,
|
||||||
|
workspaceMemberId: payload.properties.after.id,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnEvent('workspaceMember.updated')
|
||||||
|
async handleUpdatedEvent(
|
||||||
|
payload: ObjectRecordUpdateEvent<WorkspaceMemberWorkspaceEntity>,
|
||||||
|
) {
|
||||||
|
if (
|
||||||
|
objectRecordUpdateEventChangedProperties<WorkspaceMemberWorkspaceEntity>(
|
||||||
|
payload.properties.before,
|
||||||
|
payload.properties.after,
|
||||||
|
).includes('userEmail')
|
||||||
|
) {
|
||||||
|
await this.messageQueueService.add<CalendarEventParticipantUnmatchParticipantJobData>(
|
||||||
|
CalendarEventParticipantUnmatchParticipantJob.name,
|
||||||
|
{
|
||||||
|
workspaceId: payload.workspaceId,
|
||||||
|
email: payload.properties.before.userEmail,
|
||||||
|
personId: payload.recordId,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
await this.messageQueueService.add<CalendarEventParticipantMatchParticipantJobData>(
|
||||||
|
CalendarEventParticipantMatchParticipantJob.name,
|
||||||
|
{
|
||||||
|
workspaceId: payload.workspaceId,
|
||||||
|
email: payload.properties.after.userEmail,
|
||||||
|
workspaceMemberId: payload.recordId,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -2,14 +2,18 @@ import { Injectable } from '@nestjs/common';
|
|||||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||||
|
|
||||||
import { Any, EntityManager } from 'typeorm';
|
import { Any, EntityManager } from 'typeorm';
|
||||||
|
import { isDefined } from 'class-validator';
|
||||||
|
|
||||||
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
|
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
|
||||||
import { PersonRepository } from 'src/modules/person/repositories/person.repository';
|
import { PersonRepository } from 'src/modules/person/repositories/person.repository';
|
||||||
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
|
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
|
||||||
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
|
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
|
||||||
import { getFlattenedValuesAndValuesStringForBatchRawQuery } from 'src/modules/calendar/calendar-event-import-manager/utils/get-flattened-values-and-values-string-for-batch-raw-query.util';
|
import { getFlattenedValuesAndValuesStringForBatchRawQuery } from 'src/modules/calendar/calendar-event-import-manager/utils/get-flattened-values-and-values-string-for-batch-raw-query.util';
|
||||||
import { CalendarEventParticipant } from 'src/modules/calendar/common/types/calendar-event';
|
import {
|
||||||
import { AddPersonIdAndWorkspaceMemberIdService } from 'src/modules/calendar-messaging-participant/services/add-person-id-and-workspace-member-id/add-person-id-and-workspace-member-id.service';
|
CalendarEventParticipant,
|
||||||
|
CalendarEventParticipantWithCalendarEventId,
|
||||||
|
} from 'src/modules/calendar/common/types/calendar-event';
|
||||||
|
import { AddPersonIdAndWorkspaceMemberIdService } from 'src/modules/calendar-messaging-participant-manager/services/add-person-id-and-workspace-member-id/add-person-id-and-workspace-member-id.service';
|
||||||
import { InjectWorkspaceRepository } from 'src/engine/twenty-orm/decorators/inject-workspace-repository.decorator';
|
import { InjectWorkspaceRepository } from 'src/engine/twenty-orm/decorators/inject-workspace-repository.decorator';
|
||||||
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
|
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
|
||||||
import { CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event-participant.workspace-entity';
|
import { CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event-participant.workspace-entity';
|
||||||
@ -125,6 +129,70 @@ export class CalendarEventParticipantService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async upsertAndDeleteCalendarEventParticipants(
|
||||||
|
participantsToSave: CalendarEventParticipantWithCalendarEventId[],
|
||||||
|
participantsToUpdate: CalendarEventParticipantWithCalendarEventId[],
|
||||||
|
workspaceId: string,
|
||||||
|
transactionManager?: any,
|
||||||
|
): Promise<CalendarEventParticipantWorkspaceEntity[]> {
|
||||||
|
const existingCalendarEventParticipants =
|
||||||
|
await this.calendarEventParticipantRepository.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 CalendarEventParticipantWithCalendarEventId[],
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
await this.calendarEventParticipantRepository.delete({
|
||||||
|
id: Any(
|
||||||
|
calendarEventParticipantsToDelete.map(
|
||||||
|
(calendarEventParticipant) => calendarEventParticipant.id,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
});
|
||||||
|
|
||||||
|
await this.calendarEventParticipantRepository.save(participantsToUpdate);
|
||||||
|
|
||||||
|
participantsToSave.push(...newCalendarEventParticipants);
|
||||||
|
|
||||||
|
return await this.saveCalendarEventParticipants(
|
||||||
|
participantsToSave,
|
||||||
|
workspaceId,
|
||||||
|
transactionManager,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public async matchCalendarEventParticipants(
|
public async matchCalendarEventParticipants(
|
||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
email: string,
|
email: string,
|
||||||
|
|||||||
@ -3,14 +3,16 @@ import { Module } from '@nestjs/common';
|
|||||||
import { CalendarBlocklistManagerModule } from 'src/modules/calendar/blocklist-manager/calendar-blocklist-manager.module';
|
import { CalendarBlocklistManagerModule } from 'src/modules/calendar/blocklist-manager/calendar-blocklist-manager.module';
|
||||||
import { CalendarEventCleanerModule } from 'src/modules/calendar/calendar-event-cleaner/calendar-event-cleaner.module';
|
import { CalendarEventCleanerModule } from 'src/modules/calendar/calendar-event-cleaner/calendar-event-cleaner.module';
|
||||||
import { CalendarEventImportManagerModule } from 'src/modules/calendar/calendar-event-import-manager/calendar-event-import-manager.module';
|
import { CalendarEventImportManagerModule } from 'src/modules/calendar/calendar-event-import-manager/calendar-event-import-manager.module';
|
||||||
import { CalendarEventParticipantModule } from 'src/modules/calendar/calendar-event-participant-manager/calendar-event-participant.module';
|
import { CalendarEventParticipantManagerModule } from 'src/modules/calendar/calendar-event-participant-manager/calendar-event-participant-manager.module';
|
||||||
|
import { CalendarCommonModule } from 'src/modules/calendar/common/calendar-common.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
CalendarBlocklistManagerModule,
|
CalendarBlocklistManagerModule,
|
||||||
CalendarEventCleanerModule,
|
CalendarEventCleanerModule,
|
||||||
CalendarEventImportManagerModule,
|
CalendarEventImportManagerModule,
|
||||||
CalendarEventParticipantModule,
|
CalendarEventParticipantManagerModule,
|
||||||
|
CalendarCommonModule,
|
||||||
],
|
],
|
||||||
providers: [],
|
providers: [],
|
||||||
exports: [],
|
exports: [],
|
||||||
|
|||||||
@ -0,0 +1,11 @@
|
|||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
|
||||||
|
import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module';
|
||||||
|
import { AddPersonIdAndWorkspaceMemberIdService } from 'src/modules/calendar-messaging-participant-manager/services/add-person-id-and-workspace-member-id/add-person-id-and-workspace-member-id.service';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
imports: [WorkspaceDataSourceModule],
|
||||||
|
providers: [AddPersonIdAndWorkspaceMemberIdService],
|
||||||
|
exports: [],
|
||||||
|
})
|
||||||
|
export class CalendarCommonModule {}
|
||||||
@ -8,6 +8,7 @@ export type CalendarEvent = Omit<
|
|||||||
| 'calendarChannelEventAssociations'
|
| 'calendarChannelEventAssociations'
|
||||||
| 'calendarEventParticipants'
|
| 'calendarEventParticipants'
|
||||||
| 'conferenceLink'
|
| 'conferenceLink'
|
||||||
|
| 'id'
|
||||||
> & {
|
> & {
|
||||||
conferenceLinkLabel: string;
|
conferenceLinkLabel: string;
|
||||||
conferenceLinkUrl: string;
|
conferenceLinkUrl: string;
|
||||||
@ -23,15 +24,25 @@ export type CalendarEventParticipant = Omit<
|
|||||||
| 'person'
|
| 'person'
|
||||||
| 'workspaceMember'
|
| 'workspaceMember'
|
||||||
| 'calendarEvent'
|
| 'calendarEvent'
|
||||||
|
| 'calendarEventId'
|
||||||
> & {
|
> & {
|
||||||
iCalUID: string;
|
iCalUID: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type CalendarEventParticipantWithCalendarEventId =
|
||||||
|
CalendarEventParticipant & {
|
||||||
|
calendarEventId: string;
|
||||||
|
};
|
||||||
|
|
||||||
export type CalendarEventWithParticipants = CalendarEvent & {
|
export type CalendarEventWithParticipants = CalendarEvent & {
|
||||||
externalId: string;
|
externalId: string;
|
||||||
participants: CalendarEventParticipant[];
|
participants: CalendarEventParticipant[];
|
||||||
|
status: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type CalendarEventParticipantWithId = CalendarEventParticipant & {
|
export type CalendarEventWithParticipantsAndCalendarEventId = CalendarEvent & {
|
||||||
id: string;
|
id: string;
|
||||||
|
externalId: string;
|
||||||
|
participants: CalendarEventParticipantWithCalendarEventId[];
|
||||||
|
status: string;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -9,10 +9,9 @@ import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/perso
|
|||||||
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
|
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
|
||||||
import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module';
|
import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module';
|
||||||
import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-flag.entity';
|
import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-flag.entity';
|
||||||
import { MessagingCommonModule } from 'src/modules/messaging/common/messaging-common.module';
|
|
||||||
import { CalendarEventParticipantModule } from 'src/modules/calendar/calendar-event-participant-manager/calendar-event-participant.module';
|
|
||||||
import { AutoCompaniesAndContactsCreationMessageChannelListener } from 'src/modules/connected-account/auto-companies-and-contacts-creation/listeners/auto-companies-and-contacts-creation-message-channel.listener';
|
import { AutoCompaniesAndContactsCreationMessageChannelListener } from 'src/modules/connected-account/auto-companies-and-contacts-creation/listeners/auto-companies-and-contacts-creation-message-channel.listener';
|
||||||
import { AutoCompaniesAndContactsCreationCalendarChannelListener } from 'src/modules/connected-account/auto-companies-and-contacts-creation/listeners/auto-companies-and-contacts-creation-calendar-channel.listener';
|
import { AutoCompaniesAndContactsCreationCalendarChannelListener } from 'src/modules/connected-account/auto-companies-and-contacts-creation/listeners/auto-companies-and-contacts-creation-calendar-channel.listener';
|
||||||
|
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@ -22,10 +21,9 @@ import { AutoCompaniesAndContactsCreationCalendarChannelListener } from 'src/mod
|
|||||||
PersonWorkspaceEntity,
|
PersonWorkspaceEntity,
|
||||||
WorkspaceMemberWorkspaceEntity,
|
WorkspaceMemberWorkspaceEntity,
|
||||||
]),
|
]),
|
||||||
MessagingCommonModule,
|
|
||||||
WorkspaceDataSourceModule,
|
WorkspaceDataSourceModule,
|
||||||
CalendarEventParticipantModule,
|
|
||||||
TypeOrmModule.forFeature([FeatureFlagEntity], 'core'),
|
TypeOrmModule.forFeature([FeatureFlagEntity], 'core'),
|
||||||
|
TypeOrmModule.forFeature([ObjectMetadataEntity], 'metadata'),
|
||||||
],
|
],
|
||||||
providers: [
|
providers: [
|
||||||
CreateCompanyAndContactService,
|
CreateCompanyAndContactService,
|
||||||
|
|||||||
@ -5,9 +5,9 @@ import { v4 } from 'uuid';
|
|||||||
import axios, { AxiosInstance } from 'axios';
|
import axios, { AxiosInstance } from 'axios';
|
||||||
|
|
||||||
import { CompanyRepository } from 'src/modules/company/repositories/company.repository';
|
import { CompanyRepository } from 'src/modules/company/repositories/company.repository';
|
||||||
import { getCompanyNameFromDomainName } from 'src/modules/calendar-messaging-participant/utils/get-company-name-from-domain-name.util';
|
|
||||||
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
|
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
|
||||||
import { CompanyWorkspaceEntity } from 'src/modules/company/standard-objects/company.workspace-entity';
|
import { CompanyWorkspaceEntity } from 'src/modules/company/standard-objects/company.workspace-entity';
|
||||||
|
import { getCompanyNameFromDomainName } from 'src/modules/connected-account/auto-companies-and-contacts-creation/utils/get-company-name-from-domain-name.util';
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class CreateCompanyService {
|
export class CreateCompanyService {
|
||||||
private readonly httpService: AxiosInstance;
|
private readonly httpService: AxiosInstance;
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import { EntityManager } from 'typeorm';
|
|||||||
import { v4 } from 'uuid';
|
import { v4 } from 'uuid';
|
||||||
|
|
||||||
import { PersonRepository } from 'src/modules/person/repositories/person.repository';
|
import { PersonRepository } from 'src/modules/person/repositories/person.repository';
|
||||||
import { getFirstNameAndLastNameFromHandleAndDisplayName } from 'src/modules/calendar-messaging-participant/utils/get-first-name-and-last-name-from-handle-and-display-name.util';
|
import { getFirstNameAndLastNameFromHandleAndDisplayName } from 'src/modules/connected-account/auto-companies-and-contacts-creation/utils/get-first-name-and-last-name-from-handle-and-display-name.util';
|
||||||
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
|
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
|
||||||
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
|
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
|
||||||
|
|
||||||
|
|||||||
@ -6,11 +6,11 @@ import { objectRecordChangedProperties } from 'src/engine/integrations/event-emi
|
|||||||
import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
|
import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator';
|
||||||
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
|
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
|
||||||
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
|
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
|
||||||
import { MessageChannelWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity';
|
|
||||||
import {
|
import {
|
||||||
CalendarCreateCompanyAndContactAfterSyncJobData,
|
CalendarCreateCompanyAndContactAfterSyncJobData,
|
||||||
CalendarCreateCompanyAndContactAfterSyncJob,
|
CalendarCreateCompanyAndContactAfterSyncJob,
|
||||||
} from 'src/modules/messaging/message-participants-manager/jobs/calendar-create-company-and-contact-after-sync.job';
|
} from 'src/modules/calendar/calendar-event-participant-manager/jobs/calendar-create-company-and-contact-after-sync.job';
|
||||||
|
import { MessageChannelWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AutoCompaniesAndContactsCreationCalendarChannelListener {
|
export class AutoCompaniesAndContactsCreationCalendarChannelListener {
|
||||||
|
|||||||
@ -10,7 +10,7 @@ import { MessageChannelWorkspaceEntity } from 'src/modules/messaging/common/stan
|
|||||||
import {
|
import {
|
||||||
MessagingCreateCompanyAndContactAfterSyncJobData,
|
MessagingCreateCompanyAndContactAfterSyncJobData,
|
||||||
MessagingCreateCompanyAndContactAfterSyncJob,
|
MessagingCreateCompanyAndContactAfterSyncJob,
|
||||||
} from 'src/modules/messaging/message-participants-manager/jobs/messaging-create-company-and-contact-after-sync.job';
|
} from 'src/modules/messaging/message-participant-manager/jobs/messaging-create-company-and-contact-after-sync.job';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AutoCompaniesAndContactsCreationMessageChannelListener {
|
export class AutoCompaniesAndContactsCreationMessageChannelListener {
|
||||||
|
|||||||
@ -1,25 +1,23 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||||
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
|
|
||||||
import chunk from 'lodash.chunk';
|
import chunk from 'lodash.chunk';
|
||||||
import compact from 'lodash.compact';
|
import compact from 'lodash.compact';
|
||||||
import { EntityManager } from 'typeorm';
|
import { EntityManager, Repository } from 'typeorm';
|
||||||
|
|
||||||
|
import { ObjectRecordCreateEvent } from 'src/engine/integrations/event-emitter/types/object-record-create.event';
|
||||||
|
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
|
||||||
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
|
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
|
||||||
import { WorkspaceDataSource } from 'src/engine/twenty-orm/datasource/workspace.datasource';
|
import { STANDARD_OBJECT_IDS } from 'src/engine/workspace-manager/workspace-sync-metadata/constants/standard-object-ids';
|
||||||
import { InjectWorkspaceDatasource } from 'src/engine/twenty-orm/decorators/inject-workspace-datasource.decorator';
|
|
||||||
import { getDomainNameFromHandle } from 'src/modules/calendar-messaging-participant/utils/get-domain-name-from-handle.util';
|
|
||||||
import { CalendarEventParticipantService } from 'src/modules/calendar/calendar-event-participant-manager/services/calendar-event-participant.service';
|
|
||||||
import { CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event-participant.workspace-entity';
|
|
||||||
import { CONTACTS_CREATION_BATCH_SIZE } from 'src/modules/connected-account/auto-companies-and-contacts-creation/constants/contacts-creation-batch-size.constant';
|
import { CONTACTS_CREATION_BATCH_SIZE } from 'src/modules/connected-account/auto-companies-and-contacts-creation/constants/contacts-creation-batch-size.constant';
|
||||||
import { CreateCompanyService } from 'src/modules/connected-account/auto-companies-and-contacts-creation/create-company/create-company.service';
|
import { CreateCompanyService } from 'src/modules/connected-account/auto-companies-and-contacts-creation/create-company/create-company.service';
|
||||||
import { CreateContactService } from 'src/modules/connected-account/auto-companies-and-contacts-creation/create-contact/create-contact.service';
|
import { CreateContactService } from 'src/modules/connected-account/auto-companies-and-contacts-creation/create-contact/create-contact.service';
|
||||||
import { Contact } from 'src/modules/connected-account/auto-companies-and-contacts-creation/types/contact.type';
|
import { Contact } from 'src/modules/connected-account/auto-companies-and-contacts-creation/types/contact.type';
|
||||||
import { filterOutSelfAndContactsFromCompanyOrWorkspace } from 'src/modules/connected-account/auto-companies-and-contacts-creation/utils/filter-out-contacts-from-company-or-workspace.util';
|
import { filterOutSelfAndContactsFromCompanyOrWorkspace } from 'src/modules/connected-account/auto-companies-and-contacts-creation/utils/filter-out-contacts-from-company-or-workspace.util';
|
||||||
|
import { getDomainNameFromHandle } from 'src/modules/connected-account/auto-companies-and-contacts-creation/utils/get-domain-name-from-handle.util';
|
||||||
import { getUniqueContactsAndHandles } from 'src/modules/connected-account/auto-companies-and-contacts-creation/utils/get-unique-contacts-and-handles.util';
|
import { getUniqueContactsAndHandles } from 'src/modules/connected-account/auto-companies-and-contacts-creation/utils/get-unique-contacts-and-handles.util';
|
||||||
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
|
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
|
||||||
import { MessagingMessageParticipantService } from 'src/modules/messaging/common/services/messaging-message-participant.service';
|
|
||||||
import { MessageParticipantWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-participant.workspace-entity';
|
|
||||||
import { PersonRepository } from 'src/modules/person/repositories/person.repository';
|
import { PersonRepository } from 'src/modules/person/repositories/person.repository';
|
||||||
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
|
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
|
||||||
import { WorkspaceMemberRepository } from 'src/modules/workspace-member/repositories/workspace-member.repository';
|
import { WorkspaceMemberRepository } from 'src/modules/workspace-member/repositories/workspace-member.repository';
|
||||||
@ -35,14 +33,12 @@ export class CreateCompanyAndContactService {
|
|||||||
private readonly personRepository: PersonRepository,
|
private readonly personRepository: PersonRepository,
|
||||||
@InjectObjectMetadataRepository(WorkspaceMemberWorkspaceEntity)
|
@InjectObjectMetadataRepository(WorkspaceMemberWorkspaceEntity)
|
||||||
private readonly workspaceMemberRepository: WorkspaceMemberRepository,
|
private readonly workspaceMemberRepository: WorkspaceMemberRepository,
|
||||||
@InjectWorkspaceDatasource()
|
|
||||||
private readonly workspaceDataSource: WorkspaceDataSource,
|
|
||||||
private readonly messageParticipantService: MessagingMessageParticipantService,
|
|
||||||
private readonly calendarEventParticipantService: CalendarEventParticipantService,
|
|
||||||
private readonly eventEmitter: EventEmitter2,
|
private readonly eventEmitter: EventEmitter2,
|
||||||
|
@InjectRepository(ObjectMetadataEntity, 'metadata')
|
||||||
|
private readonly objectMetadataRepository: Repository<ObjectMetadataEntity>,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async createCompaniesAndPeople(
|
private async createCompaniesAndPeople(
|
||||||
connectedAccount: ConnectedAccountWorkspaceEntity,
|
connectedAccount: ConnectedAccountWorkspaceEntity,
|
||||||
contactsToCreate: Contact[],
|
contactsToCreate: Contact[],
|
||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
@ -137,47 +133,37 @@ export class CreateCompanyAndContactService {
|
|||||||
CONTACTS_CREATION_BATCH_SIZE,
|
CONTACTS_CREATION_BATCH_SIZE,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// TODO: Remove this when events are emitted directly inside TwentyORM
|
||||||
|
|
||||||
|
const objectMetadata = await this.objectMetadataRepository.findOne({
|
||||||
|
where: {
|
||||||
|
standardId: STANDARD_OBJECT_IDS.person,
|
||||||
|
workspaceId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!objectMetadata) {
|
||||||
|
throw new Error('Object metadata not found');
|
||||||
|
}
|
||||||
|
|
||||||
for (const contactsBatch of contactsBatches) {
|
for (const contactsBatch of contactsBatches) {
|
||||||
let updatedMessageParticipants: MessageParticipantWorkspaceEntity[] = [];
|
const createdPeople = await this.createCompaniesAndPeople(
|
||||||
let updatedCalendarEventParticipants: CalendarEventParticipantWorkspaceEntity[] =
|
connectedAccount,
|
||||||
[];
|
contactsBatch,
|
||||||
|
workspaceId,
|
||||||
await this.workspaceDataSource?.transaction(
|
|
||||||
async (transactionManager: EntityManager) => {
|
|
||||||
const createdPeople = await this.createCompaniesAndPeople(
|
|
||||||
connectedAccount,
|
|
||||||
contactsBatch,
|
|
||||||
workspaceId,
|
|
||||||
transactionManager,
|
|
||||||
);
|
|
||||||
|
|
||||||
updatedMessageParticipants =
|
|
||||||
await this.messageParticipantService.updateMessageParticipantsAfterPeopleCreation(
|
|
||||||
createdPeople,
|
|
||||||
workspaceId,
|
|
||||||
transactionManager,
|
|
||||||
);
|
|
||||||
|
|
||||||
updatedCalendarEventParticipants =
|
|
||||||
await this.calendarEventParticipantService.updateCalendarEventParticipantsAfterPeopleCreation(
|
|
||||||
createdPeople,
|
|
||||||
workspaceId,
|
|
||||||
transactionManager,
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
|
|
||||||
this.eventEmitter.emit(`messageParticipant.matched`, {
|
for (const createdPerson of createdPeople) {
|
||||||
workspaceId,
|
this.eventEmitter.emit('person.created', {
|
||||||
workspaceMemberId: connectedAccount.accountOwnerId,
|
name: 'person.created',
|
||||||
messageParticipants: updatedMessageParticipants,
|
workspaceId,
|
||||||
});
|
recordId: createdPerson.id,
|
||||||
|
objectMetadata,
|
||||||
this.eventEmitter.emit(`calendarEventParticipant.matched`, {
|
properties: {
|
||||||
workspaceId,
|
after: createdPerson,
|
||||||
workspaceMemberId: connectedAccount.accountOwnerId,
|
},
|
||||||
calendarEventParticipants: updatedCalendarEventParticipants,
|
} satisfies ObjectRecordCreateEvent<any>);
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { getDomainNameFromHandle } from 'src/modules/calendar-messaging-participant/utils/get-domain-name-from-handle.util';
|
import { getDomainNameFromHandle } from 'src/modules/connected-account/auto-companies-and-contacts-creation/utils/get-domain-name-from-handle.util';
|
||||||
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
|
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
|
||||||
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
|
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
|
||||||
import { Contact } from 'src/modules/connected-account/auto-companies-and-contacts-creation/types/contact.type';
|
import { Contact } from 'src/modules/connected-account/auto-companies-and-contacts-creation/types/contact.type';
|
||||||
|
|||||||
@ -12,7 +12,10 @@ export class OAuth2ClientManagerService {
|
|||||||
) {}
|
) {}
|
||||||
|
|
||||||
public async getOAuth2Client(
|
public async getOAuth2Client(
|
||||||
connectedAccount: ConnectedAccountWorkspaceEntity,
|
connectedAccount: Pick<
|
||||||
|
ConnectedAccountWorkspaceEntity,
|
||||||
|
'provider' | 'refreshToken'
|
||||||
|
>,
|
||||||
): Promise<OAuth2Client> {
|
): Promise<OAuth2Client> {
|
||||||
const { refreshToken } = connectedAccount;
|
const { refreshToken } = connectedAccount;
|
||||||
|
|
||||||
|
|||||||
@ -6,14 +6,12 @@ import { AnalyticsModule } from 'src/engine/core-modules/analytics/analytics.mod
|
|||||||
import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-flag.entity';
|
import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-flag.entity';
|
||||||
import { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.module';
|
import { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.module';
|
||||||
import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module';
|
import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module';
|
||||||
import { AddPersonIdAndWorkspaceMemberIdModule } from 'src/modules/calendar-messaging-participant/services/add-person-id-and-workspace-member-id/add-person-id-and-workspace-member-id.module';
|
import { AddPersonIdAndWorkspaceMemberIdService } from 'src/modules/calendar-messaging-participant-manager/services/add-person-id-and-workspace-member-id/add-person-id-and-workspace-member-id.service';
|
||||||
import { MessagingChannelSyncStatusService } from 'src/modules/messaging/common/services/messaging-channel-sync-status.service';
|
import { MessagingChannelSyncStatusService } from 'src/modules/messaging/common/services/messaging-channel-sync-status.service';
|
||||||
import { MessagingErrorHandlingService } from 'src/modules/messaging/common/services/messaging-error-handling.service';
|
import { MessagingErrorHandlingService } from 'src/modules/messaging/common/services/messaging-error-handling.service';
|
||||||
import { MessagingFetchByBatchesService } from 'src/modules/messaging/common/services/messaging-fetch-by-batch.service';
|
import { MessagingFetchByBatchesService } from 'src/modules/messaging/common/services/messaging-fetch-by-batch.service';
|
||||||
import { MessagingMessageParticipantService } from 'src/modules/messaging/common/services/messaging-message-participant.service';
|
|
||||||
import { MessagingMessageThreadService } from 'src/modules/messaging/common/services/messaging-message-thread.service';
|
import { MessagingMessageThreadService } from 'src/modules/messaging/common/services/messaging-message-thread.service';
|
||||||
import { MessagingMessageService } from 'src/modules/messaging/common/services/messaging-message.service';
|
import { MessagingMessageService } from 'src/modules/messaging/common/services/messaging-message.service';
|
||||||
import { MessagingSaveMessagesAndEnqueueContactCreationService } from 'src/modules/messaging/common/services/messaging-save-messages-and-enqueue-contact-creation.service';
|
|
||||||
import { MessagingTelemetryService } from 'src/modules/messaging/common/services/messaging-telemetry.service';
|
import { MessagingTelemetryService } from 'src/modules/messaging/common/services/messaging-telemetry.service';
|
||||||
import { MessageParticipantWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-participant.workspace-entity';
|
import { MessageParticipantWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-participant.workspace-entity';
|
||||||
import { MessageThreadWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-thread.workspace-entity';
|
import { MessageThreadWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-thread.workspace-entity';
|
||||||
@ -34,26 +32,22 @@ import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/perso
|
|||||||
MessageThreadWorkspaceEntity,
|
MessageThreadWorkspaceEntity,
|
||||||
]),
|
]),
|
||||||
TypeOrmModule.forFeature([FeatureFlagEntity], 'core'),
|
TypeOrmModule.forFeature([FeatureFlagEntity], 'core'),
|
||||||
AddPersonIdAndWorkspaceMemberIdModule,
|
|
||||||
],
|
],
|
||||||
providers: [
|
providers: [
|
||||||
MessagingMessageService,
|
MessagingMessageService,
|
||||||
MessagingMessageThreadService,
|
MessagingMessageThreadService,
|
||||||
MessagingSaveMessagesAndEnqueueContactCreationService,
|
|
||||||
MessagingErrorHandlingService,
|
MessagingErrorHandlingService,
|
||||||
MessagingTelemetryService,
|
MessagingTelemetryService,
|
||||||
MessagingChannelSyncStatusService,
|
MessagingChannelSyncStatusService,
|
||||||
MessagingMessageParticipantService,
|
|
||||||
MessagingFetchByBatchesService,
|
MessagingFetchByBatchesService,
|
||||||
|
AddPersonIdAndWorkspaceMemberIdService,
|
||||||
],
|
],
|
||||||
exports: [
|
exports: [
|
||||||
MessagingMessageService,
|
MessagingMessageService,
|
||||||
MessagingMessageThreadService,
|
MessagingMessageThreadService,
|
||||||
MessagingSaveMessagesAndEnqueueContactCreationService,
|
|
||||||
MessagingErrorHandlingService,
|
MessagingErrorHandlingService,
|
||||||
MessagingTelemetryService,
|
MessagingTelemetryService,
|
||||||
MessagingChannelSyncStatusService,
|
MessagingChannelSyncStatusService,
|
||||||
MessagingMessageParticipantService,
|
|
||||||
MessagingFetchByBatchesService,
|
MessagingFetchByBatchesService,
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
|
|||||||
@ -61,8 +61,6 @@ export class MessagingChannelSyncStatusService {
|
|||||||
`messages-to-import:${workspaceId}:gmail:${messageChannelId}`,
|
`messages-to-import:${workspaceId}:gmail:${messageChannelId}`,
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO: remove nextPageToken from cache
|
|
||||||
|
|
||||||
await this.messageChannelRepository.resetSyncCursor(
|
await this.messageChannelRepository.resetSyncCursor(
|
||||||
messageChannelId,
|
messageChannelId,
|
||||||
workspaceId,
|
workspaceId,
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-
|
|||||||
import { FeatureFlagModule } from 'src/engine/core-modules/feature-flag/feature-flag.module';
|
import { FeatureFlagModule } from 'src/engine/core-modules/feature-flag/feature-flag.module';
|
||||||
import { EnvironmentModule } from 'src/engine/integrations/environment/environment.module';
|
import { EnvironmentModule } from 'src/engine/integrations/environment/environment.module';
|
||||||
import { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.module';
|
import { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.module';
|
||||||
|
import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module';
|
||||||
import { EmailAliasManagerModule } from 'src/modules/connected-account/email-alias-manager/email-alias-manager.module';
|
import { EmailAliasManagerModule } from 'src/modules/connected-account/email-alias-manager/email-alias-manager.module';
|
||||||
import { OAuth2ClientManagerModule } from 'src/modules/connected-account/oauth2-client-manager/oauth2-client-manager.module';
|
import { OAuth2ClientManagerModule } from 'src/modules/connected-account/oauth2-client-manager/oauth2-client-manager.module';
|
||||||
import { GoogleAPIRefreshAccessTokenModule } from 'src/modules/connected-account/services/google-api-refresh-access-token/google-api-refresh-access-token.module';
|
import { GoogleAPIRefreshAccessTokenModule } from 'src/modules/connected-account/services/google-api-refresh-access-token/google-api-refresh-access-token.module';
|
||||||
@ -20,6 +21,8 @@ import { MessagingGmailFullMessageListFetchService } from 'src/modules/messaging
|
|||||||
import { MessagingGmailHistoryService } from 'src/modules/messaging/message-import-manager/drivers/gmail/services/messaging-gmail-history.service';
|
import { MessagingGmailHistoryService } from 'src/modules/messaging/message-import-manager/drivers/gmail/services/messaging-gmail-history.service';
|
||||||
import { MessagingGmailMessagesImportService } from 'src/modules/messaging/message-import-manager/drivers/gmail/services/messaging-gmail-messages-import.service';
|
import { MessagingGmailMessagesImportService } from 'src/modules/messaging/message-import-manager/drivers/gmail/services/messaging-gmail-messages-import.service';
|
||||||
import { MessagingGmailPartialMessageListFetchService } from 'src/modules/messaging/message-import-manager/drivers/gmail/services/messaging-gmail-partial-message-list-fetch.service';
|
import { MessagingGmailPartialMessageListFetchService } from 'src/modules/messaging/message-import-manager/drivers/gmail/services/messaging-gmail-partial-message-list-fetch.service';
|
||||||
|
import { MessagingSaveMessagesAndEnqueueContactCreationService } from 'src/modules/messaging/message-import-manager/drivers/gmail/services/messaging-save-messages-and-enqueue-contact-creation.service';
|
||||||
|
import { MessageParticipantManagerModule } from 'src/modules/messaging/message-participant-manager/message-participant-manager.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@ -36,6 +39,8 @@ import { MessagingGmailPartialMessageListFetchService } from 'src/modules/messag
|
|||||||
OAuth2ClientManagerModule,
|
OAuth2ClientManagerModule,
|
||||||
EmailAliasManagerModule,
|
EmailAliasManagerModule,
|
||||||
FeatureFlagModule,
|
FeatureFlagModule,
|
||||||
|
WorkspaceDataSourceModule,
|
||||||
|
MessageParticipantManagerModule,
|
||||||
],
|
],
|
||||||
providers: [
|
providers: [
|
||||||
MessagingGmailClientProvider,
|
MessagingGmailClientProvider,
|
||||||
@ -45,6 +50,7 @@ import { MessagingGmailPartialMessageListFetchService } from 'src/modules/messag
|
|||||||
MessagingGmailFullMessageListFetchService,
|
MessagingGmailFullMessageListFetchService,
|
||||||
MessagingGmailMessagesImportService,
|
MessagingGmailMessagesImportService,
|
||||||
MessagingGmailFetchMessageIdsToExcludeService,
|
MessagingGmailFetchMessageIdsToExcludeService,
|
||||||
|
MessagingSaveMessagesAndEnqueueContactCreationService,
|
||||||
],
|
],
|
||||||
exports: [
|
exports: [
|
||||||
MessagingGmailClientProvider,
|
MessagingGmailClientProvider,
|
||||||
|
|||||||
@ -12,7 +12,10 @@ export class MessagingGmailClientProvider {
|
|||||||
) {}
|
) {}
|
||||||
|
|
||||||
public async getGmailClient(
|
public async getGmailClient(
|
||||||
connectedAccount: ConnectedAccountWorkspaceEntity,
|
connectedAccount: Pick<
|
||||||
|
ConnectedAccountWorkspaceEntity,
|
||||||
|
'provider' | 'refreshToken'
|
||||||
|
>,
|
||||||
): Promise<gmail_v1.Gmail> {
|
): Promise<gmail_v1.Gmail> {
|
||||||
const oAuth2Client =
|
const oAuth2Client =
|
||||||
await this.oAuth2ClientManagerService.getOAuth2Client(connectedAccount);
|
await this.oAuth2ClientManagerService.getOAuth2Client(connectedAccount);
|
||||||
|
|||||||
@ -15,7 +15,6 @@ import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/s
|
|||||||
import { MessageChannelRepository } from 'src/modules/messaging/common/repositories/message-channel.repository';
|
import { MessageChannelRepository } from 'src/modules/messaging/common/repositories/message-channel.repository';
|
||||||
import { MessagingChannelSyncStatusService } from 'src/modules/messaging/common/services/messaging-channel-sync-status.service';
|
import { MessagingChannelSyncStatusService } from 'src/modules/messaging/common/services/messaging-channel-sync-status.service';
|
||||||
import { MessagingErrorHandlingService } from 'src/modules/messaging/common/services/messaging-error-handling.service';
|
import { MessagingErrorHandlingService } from 'src/modules/messaging/common/services/messaging-error-handling.service';
|
||||||
import { MessagingSaveMessagesAndEnqueueContactCreationService } from 'src/modules/messaging/common/services/messaging-save-messages-and-enqueue-contact-creation.service';
|
|
||||||
import { MessagingTelemetryService } from 'src/modules/messaging/common/services/messaging-telemetry.service';
|
import { MessagingTelemetryService } from 'src/modules/messaging/common/services/messaging-telemetry.service';
|
||||||
import {
|
import {
|
||||||
MessageChannelSyncStage,
|
MessageChannelSyncStage,
|
||||||
@ -23,6 +22,7 @@ import {
|
|||||||
} from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity';
|
} from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity';
|
||||||
import { MESSAGING_GMAIL_USERS_MESSAGES_GET_BATCH_SIZE } from 'src/modules/messaging/message-import-manager/drivers/gmail/constants/messaging-gmail-users-messages-get-batch-size.constant';
|
import { MESSAGING_GMAIL_USERS_MESSAGES_GET_BATCH_SIZE } from 'src/modules/messaging/message-import-manager/drivers/gmail/constants/messaging-gmail-users-messages-get-batch-size.constant';
|
||||||
import { MessagingGmailFetchMessagesByBatchesService } from 'src/modules/messaging/message-import-manager/drivers/gmail/services/messaging-gmail-fetch-messages-by-batches.service';
|
import { MessagingGmailFetchMessagesByBatchesService } from 'src/modules/messaging/message-import-manager/drivers/gmail/services/messaging-gmail-fetch-messages-by-batches.service';
|
||||||
|
import { MessagingSaveMessagesAndEnqueueContactCreationService } from 'src/modules/messaging/message-import-manager/drivers/gmail/services/messaging-save-messages-and-enqueue-contact-creation.service';
|
||||||
import { filterEmails } from 'src/modules/messaging/message-import-manager/utils/filter-emails.util';
|
import { filterEmails } from 'src/modules/messaging/message-import-manager/utils/filter-emails.util';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
|
|||||||
@ -14,7 +14,6 @@ import {
|
|||||||
CreateCompanyAndContactJobData,
|
CreateCompanyAndContactJobData,
|
||||||
} from 'src/modules/connected-account/auto-companies-and-contacts-creation/jobs/create-company-and-contact.job';
|
} from 'src/modules/connected-account/auto-companies-and-contacts-creation/jobs/create-company-and-contact.job';
|
||||||
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
|
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
|
||||||
import { MessagingMessageParticipantService } from 'src/modules/messaging/common/services/messaging-message-participant.service';
|
|
||||||
import { MessagingMessageService } from 'src/modules/messaging/common/services/messaging-message.service';
|
import { MessagingMessageService } from 'src/modules/messaging/common/services/messaging-message.service';
|
||||||
import {
|
import {
|
||||||
MessageChannelContactAutoCreationPolicy,
|
MessageChannelContactAutoCreationPolicy,
|
||||||
@ -26,6 +25,7 @@ import {
|
|||||||
Participant,
|
Participant,
|
||||||
ParticipantWithMessageId,
|
ParticipantWithMessageId,
|
||||||
} from 'src/modules/messaging/message-import-manager/drivers/gmail/types/gmail-message';
|
} from 'src/modules/messaging/message-import-manager/drivers/gmail/types/gmail-message';
|
||||||
|
import { MessagingMessageParticipantService } from 'src/modules/messaging/message-participant-manager/services/messaging-message-participant.service';
|
||||||
import { isGroupEmail } from 'src/utils/is-group-email';
|
import { isGroupEmail } from 'src/utils/is-group-email';
|
||||||
import { isWorkEmail } from 'src/utils/is-work-email';
|
import { isWorkEmail } from 'src/utils/is-work-email';
|
||||||
|
|
||||||
@ -14,7 +14,7 @@ import {
|
|||||||
} from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity';
|
} from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity';
|
||||||
import { MessagingGmailFullMessageListFetchService } from 'src/modules/messaging/message-import-manager/drivers/gmail/services/messaging-gmail-full-message-list-fetch.service';
|
import { MessagingGmailFullMessageListFetchService } from 'src/modules/messaging/message-import-manager/drivers/gmail/services/messaging-gmail-full-message-list-fetch.service';
|
||||||
import { MessagingGmailPartialMessageListFetchService } from 'src/modules/messaging/message-import-manager/drivers/gmail/services/messaging-gmail-partial-message-list-fetch.service';
|
import { MessagingGmailPartialMessageListFetchService } from 'src/modules/messaging/message-import-manager/drivers/gmail/services/messaging-gmail-partial-message-list-fetch.service';
|
||||||
import { isThrottled } from 'src/modules/messaging/message-import-manager/drivers/gmail/utils/is-throttled';
|
import { isThrottled } from 'src/modules/connected-account/utils/is-throttled';
|
||||||
|
|
||||||
export type MessagingMessageListFetchJobData = {
|
export type MessagingMessageListFetchJobData = {
|
||||||
messageChannelId: string;
|
messageChannelId: string;
|
||||||
|
|||||||
@ -13,7 +13,7 @@ import {
|
|||||||
MessageChannelWorkspaceEntity,
|
MessageChannelWorkspaceEntity,
|
||||||
} from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity';
|
} from 'src/modules/messaging/common/standard-objects/message-channel.workspace-entity';
|
||||||
import { MessagingGmailMessagesImportService } from 'src/modules/messaging/message-import-manager/drivers/gmail/services/messaging-gmail-messages-import.service';
|
import { MessagingGmailMessagesImportService } from 'src/modules/messaging/message-import-manager/drivers/gmail/services/messaging-gmail-messages-import.service';
|
||||||
import { isThrottled } from 'src/modules/messaging/message-import-manager/drivers/gmail/utils/is-throttled';
|
import { isThrottled } from 'src/modules/connected-account/utils/is-throttled';
|
||||||
|
|
||||||
export type MessagingMessagesImportJobData = {
|
export type MessagingMessagesImportJobData = {
|
||||||
messageChannelId: string;
|
messageChannelId: string;
|
||||||
|
|||||||
@ -21,9 +21,11 @@ import { MessagingMessagesImportJob } from 'src/modules/messaging/message-import
|
|||||||
import { MessagingOngoingStaleJob } from 'src/modules/messaging/message-import-manager/jobs/messaging-ongoing-stale.job';
|
import { MessagingOngoingStaleJob } from 'src/modules/messaging/message-import-manager/jobs/messaging-ongoing-stale.job';
|
||||||
import { MessagingMessageImportManagerMessageChannelListener } from 'src/modules/messaging/message-import-manager/listeners/messaging-import-manager-message-channel.listener';
|
import { MessagingMessageImportManagerMessageChannelListener } from 'src/modules/messaging/message-import-manager/listeners/messaging-import-manager-message-channel.listener';
|
||||||
import { BillingModule } from 'src/engine/core-modules/billing/billing.module';
|
import { BillingModule } from 'src/engine/core-modules/billing/billing.module';
|
||||||
|
import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
|
WorkspaceDataSourceModule,
|
||||||
MessagingGmailDriverModule,
|
MessagingGmailDriverModule,
|
||||||
MessagingCommonModule,
|
MessagingCommonModule,
|
||||||
TypeOrmModule.forFeature([Workspace], 'core'),
|
TypeOrmModule.forFeature([Workspace], 'core'),
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { isEmailBlocklisted } from 'src/modules/calendar-messaging-participant/utils/is-email-blocklisted.util';
|
import { isEmailBlocklisted } from 'src/modules/calendar-messaging-participant-manager/utils/is-email-blocklisted.util';
|
||||||
import { GmailMessage } from 'src/modules/messaging/message-import-manager/drivers/gmail/types/gmail-message';
|
import { GmailMessage } from 'src/modules/messaging/message-import-manager/drivers/gmail/types/gmail-message';
|
||||||
|
|
||||||
// Todo: refactor this into several utils
|
// Todo: refactor this into several utils
|
||||||
|
|||||||
@ -0,0 +1,35 @@
|
|||||||
|
import { Scope } from '@nestjs/common';
|
||||||
|
|
||||||
|
import { Process } from 'src/engine/integrations/message-queue/decorators/process.decorator';
|
||||||
|
import { Processor } from 'src/engine/integrations/message-queue/decorators/processor.decorator';
|
||||||
|
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
|
||||||
|
import { MessagingMessageParticipantService } from 'src/modules/messaging/message-participant-manager/services/messaging-message-participant.service';
|
||||||
|
|
||||||
|
export type MessageParticipantMatchParticipantJobData = {
|
||||||
|
workspaceId: string;
|
||||||
|
email: string;
|
||||||
|
personId?: string;
|
||||||
|
workspaceMemberId?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
@Processor({
|
||||||
|
queueName: MessageQueue.messagingQueue,
|
||||||
|
scope: Scope.REQUEST,
|
||||||
|
})
|
||||||
|
export class MessageParticipantMatchParticipantJob {
|
||||||
|
constructor(
|
||||||
|
private readonly messageParticipantService: MessagingMessageParticipantService,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
@Process(MessageParticipantMatchParticipantJob.name)
|
||||||
|
async handle(data: MessageParticipantMatchParticipantJobData): Promise<void> {
|
||||||
|
const { workspaceId, email, personId, workspaceMemberId } = data;
|
||||||
|
|
||||||
|
await this.messageParticipantService.matchMessageParticipants(
|
||||||
|
workspaceId,
|
||||||
|
email,
|
||||||
|
personId,
|
||||||
|
workspaceMemberId,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,37 @@
|
|||||||
|
import { Scope } from '@nestjs/common';
|
||||||
|
|
||||||
|
import { Processor } from 'src/engine/integrations/message-queue/decorators/processor.decorator';
|
||||||
|
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
|
||||||
|
import { MessagingMessageParticipantService } from 'src/modules/messaging/message-participant-manager/services/messaging-message-participant.service';
|
||||||
|
import { Process } from 'src/engine/integrations/message-queue/decorators/process.decorator';
|
||||||
|
|
||||||
|
export type MessageParticipantUnmatchParticipantJobData = {
|
||||||
|
workspaceId: string;
|
||||||
|
email: string;
|
||||||
|
personId?: string;
|
||||||
|
workspaceMemberId?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
@Processor({
|
||||||
|
queueName: MessageQueue.messagingQueue,
|
||||||
|
scope: Scope.REQUEST,
|
||||||
|
})
|
||||||
|
export class MessageParticipantUnmatchParticipantJob {
|
||||||
|
constructor(
|
||||||
|
private readonly messageParticipantService: MessagingMessageParticipantService,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
@Process(MessageParticipantUnmatchParticipantJob.name)
|
||||||
|
async handle(
|
||||||
|
data: MessageParticipantUnmatchParticipantJobData,
|
||||||
|
): Promise<void> {
|
||||||
|
const { workspaceId, email, personId, workspaceMemberId } = data;
|
||||||
|
|
||||||
|
await this.messageParticipantService.unmatchMessageParticipants(
|
||||||
|
workspaceId,
|
||||||
|
email,
|
||||||
|
personId,
|
||||||
|
workspaceMemberId,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -8,17 +8,17 @@ import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decora
|
|||||||
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
|
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
|
||||||
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
|
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
|
||||||
import {
|
import {
|
||||||
MatchParticipantJobData,
|
MessageParticipantMatchParticipantJobData,
|
||||||
MatchParticipantJob,
|
MessageParticipantMatchParticipantJob,
|
||||||
} from 'src/modules/calendar-messaging-participant/jobs/match-participant.job';
|
} from 'src/modules/messaging/message-participant-manager/jobs/message-participant-match-participant.job';
|
||||||
import {
|
import {
|
||||||
UnmatchParticipantJobData,
|
MessageParticipantUnmatchParticipantJobData,
|
||||||
UnmatchParticipantJob,
|
MessageParticipantUnmatchParticipantJob,
|
||||||
} from 'src/modules/calendar-messaging-participant/jobs/unmatch-participant.job';
|
} from 'src/modules/messaging/message-participant-manager/jobs/message-participant-unmatch-participant.job';
|
||||||
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
|
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ParticipantPersonListener {
|
export class MessageParticipantPersonListener {
|
||||||
constructor(
|
constructor(
|
||||||
@InjectMessageQueue(MessageQueue.messagingQueue)
|
@InjectMessageQueue(MessageQueue.messagingQueue)
|
||||||
private readonly messageQueueService: MessageQueueService,
|
private readonly messageQueueService: MessageQueueService,
|
||||||
@ -32,8 +32,8 @@ export class ParticipantPersonListener {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.messageQueueService.add<MatchParticipantJobData>(
|
await this.messageQueueService.add<MessageParticipantMatchParticipantJobData>(
|
||||||
MatchParticipantJob.name,
|
MessageParticipantMatchParticipantJob.name,
|
||||||
{
|
{
|
||||||
workspaceId: payload.workspaceId,
|
workspaceId: payload.workspaceId,
|
||||||
email: payload.properties.after.email,
|
email: payload.properties.after.email,
|
||||||
@ -52,8 +52,8 @@ export class ParticipantPersonListener {
|
|||||||
payload.properties.after,
|
payload.properties.after,
|
||||||
).includes('email')
|
).includes('email')
|
||||||
) {
|
) {
|
||||||
await this.messageQueueService.add<UnmatchParticipantJobData>(
|
await this.messageQueueService.add<MessageParticipantUnmatchParticipantJobData>(
|
||||||
UnmatchParticipantJob.name,
|
MessageParticipantUnmatchParticipantJob.name,
|
||||||
{
|
{
|
||||||
workspaceId: payload.workspaceId,
|
workspaceId: payload.workspaceId,
|
||||||
email: payload.properties.before.email,
|
email: payload.properties.before.email,
|
||||||
@ -61,8 +61,8 @@ export class ParticipantPersonListener {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
await this.messageQueueService.add<MatchParticipantJobData>(
|
await this.messageQueueService.add<MessageParticipantMatchParticipantJobData>(
|
||||||
MatchParticipantJob.name,
|
MessageParticipantMatchParticipantJob.name,
|
||||||
{
|
{
|
||||||
workspaceId: payload.workspaceId,
|
workspaceId: payload.workspaceId,
|
||||||
email: payload.properties.after.email,
|
email: payload.properties.after.email,
|
||||||
@ -8,17 +8,17 @@ import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decora
|
|||||||
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
|
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
|
||||||
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
|
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
|
||||||
import {
|
import {
|
||||||
MatchParticipantJobData,
|
MessageParticipantMatchParticipantJobData,
|
||||||
MatchParticipantJob,
|
MessageParticipantMatchParticipantJob,
|
||||||
} from 'src/modules/calendar-messaging-participant/jobs/match-participant.job';
|
} from 'src/modules/messaging/message-participant-manager/jobs/message-participant-match-participant.job';
|
||||||
import {
|
import {
|
||||||
UnmatchParticipantJobData,
|
MessageParticipantUnmatchParticipantJobData,
|
||||||
UnmatchParticipantJob,
|
MessageParticipantUnmatchParticipantJob,
|
||||||
} from 'src/modules/calendar-messaging-participant/jobs/unmatch-participant.job';
|
} from 'src/modules/messaging/message-participant-manager/jobs/message-participant-unmatch-participant.job';
|
||||||
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
|
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ParticipantWorkspaceMemberListener {
|
export class MessageParticipantWorkspaceMemberListener {
|
||||||
constructor(
|
constructor(
|
||||||
@InjectMessageQueue(MessageQueue.messagingQueue)
|
@InjectMessageQueue(MessageQueue.messagingQueue)
|
||||||
private readonly messageQueueService: MessageQueueService,
|
private readonly messageQueueService: MessageQueueService,
|
||||||
@ -32,8 +32,8 @@ export class ParticipantWorkspaceMemberListener {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.messageQueueService.add<MatchParticipantJobData>(
|
await this.messageQueueService.add<MessageParticipantMatchParticipantJobData>(
|
||||||
MatchParticipantJob.name,
|
MessageParticipantMatchParticipantJob.name,
|
||||||
{
|
{
|
||||||
workspaceId: payload.workspaceId,
|
workspaceId: payload.workspaceId,
|
||||||
email: payload.properties.after.userEmail,
|
email: payload.properties.after.userEmail,
|
||||||
@ -52,8 +52,8 @@ export class ParticipantWorkspaceMemberListener {
|
|||||||
payload.properties.after,
|
payload.properties.after,
|
||||||
).includes('userEmail')
|
).includes('userEmail')
|
||||||
) {
|
) {
|
||||||
await this.messageQueueService.add<UnmatchParticipantJobData>(
|
await this.messageQueueService.add<MessageParticipantUnmatchParticipantJobData>(
|
||||||
UnmatchParticipantJob.name,
|
MessageParticipantUnmatchParticipantJob.name,
|
||||||
{
|
{
|
||||||
workspaceId: payload.workspaceId,
|
workspaceId: payload.workspaceId,
|
||||||
email: payload.properties.before.userEmail,
|
email: payload.properties.before.userEmail,
|
||||||
@ -61,8 +61,8 @@ export class ParticipantWorkspaceMemberListener {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
await this.messageQueueService.add<MatchParticipantJobData>(
|
await this.messageQueueService.add<MessageParticipantMatchParticipantJobData>(
|
||||||
MatchParticipantJob.name,
|
MessageParticipantMatchParticipantJob.name,
|
||||||
{
|
{
|
||||||
workspaceId: payload.workspaceId,
|
workspaceId: payload.workspaceId,
|
||||||
email: payload.properties.after.userEmail,
|
email: payload.properties.after.userEmail,
|
||||||
@ -7,19 +7,23 @@ import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadat
|
|||||||
import { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.module';
|
import { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.module';
|
||||||
import { TwentyORMModule } from 'src/engine/twenty-orm/twenty-orm.module';
|
import { TwentyORMModule } from 'src/engine/twenty-orm/twenty-orm.module';
|
||||||
import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module';
|
import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module';
|
||||||
|
import { AddPersonIdAndWorkspaceMemberIdService } from 'src/modules/calendar-messaging-participant-manager/services/add-person-id-and-workspace-member-id/add-person-id-and-workspace-member-id.service';
|
||||||
import { CalendarChannelWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-channel.workspace-entity';
|
import { CalendarChannelWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-channel.workspace-entity';
|
||||||
import { AutoCompaniesAndContactsCreationModule } from 'src/modules/connected-account/auto-companies-and-contacts-creation/auto-companies-and-contacts-creation.module';
|
import { AutoCompaniesAndContactsCreationModule } from 'src/modules/connected-account/auto-companies-and-contacts-creation/auto-companies-and-contacts-creation.module';
|
||||||
import { MessagingGmailDriverModule } from 'src/modules/messaging/message-import-manager/drivers/gmail/messaging-gmail-driver.module';
|
import { MessagingCommonModule } from 'src/modules/messaging/common/messaging-common.module';
|
||||||
import { CalendarCreateCompanyAndContactAfterSyncJob } from 'src/modules/messaging/message-participants-manager/jobs/calendar-create-company-and-contact-after-sync.job';
|
import { MessageParticipantMatchParticipantJob } from 'src/modules/messaging/message-participant-manager/jobs/message-participant-match-participant.job';
|
||||||
import { MessagingCreateCompanyAndContactAfterSyncJob } from 'src/modules/messaging/message-participants-manager/jobs/messaging-create-company-and-contact-after-sync.job';
|
import { MessageParticipantUnmatchParticipantJob } from 'src/modules/messaging/message-participant-manager/jobs/message-participant-unmatch-participant.job';
|
||||||
import { MessageParticipantListener } from 'src/modules/messaging/message-participants-manager/listeners/message-participant.listener';
|
import { MessagingCreateCompanyAndContactAfterSyncJob } from 'src/modules/messaging/message-participant-manager/jobs/messaging-create-company-and-contact-after-sync.job';
|
||||||
|
import { MessageParticipantPersonListener } from 'src/modules/messaging/message-participant-manager/listeners/message-participant-person.listener';
|
||||||
|
import { MessageParticipantWorkspaceMemberListener } from 'src/modules/messaging/message-participant-manager/listeners/message-participant-workspace-member.listener';
|
||||||
|
import { MessageParticipantListener } from 'src/modules/messaging/message-participant-manager/listeners/message-participant.listener';
|
||||||
|
import { MessagingMessageParticipantService } from 'src/modules/messaging/message-participant-manager/services/messaging-message-participant.service';
|
||||||
import { TimelineActivityWorkspaceEntity } from 'src/modules/timeline/standard-objects/timeline-activity.workspace-entity';
|
import { TimelineActivityWorkspaceEntity } from 'src/modules/timeline/standard-objects/timeline-activity.workspace-entity';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
TypeOrmModule.forFeature([FeatureFlagEntity], 'core'),
|
TypeOrmModule.forFeature([FeatureFlagEntity], 'core'),
|
||||||
AnalyticsModule,
|
AnalyticsModule,
|
||||||
MessagingGmailDriverModule,
|
|
||||||
AutoCompaniesAndContactsCreationModule,
|
AutoCompaniesAndContactsCreationModule,
|
||||||
WorkspaceDataSourceModule,
|
WorkspaceDataSourceModule,
|
||||||
ObjectMetadataRepositoryModule.forFeature([
|
ObjectMetadataRepositoryModule.forFeature([
|
||||||
@ -27,11 +31,18 @@ import { TimelineActivityWorkspaceEntity } from 'src/modules/timeline/standard-o
|
|||||||
]),
|
]),
|
||||||
TypeOrmModule.forFeature([ObjectMetadataEntity], 'metadata'),
|
TypeOrmModule.forFeature([ObjectMetadataEntity], 'metadata'),
|
||||||
TwentyORMModule.forFeature([CalendarChannelWorkspaceEntity]),
|
TwentyORMModule.forFeature([CalendarChannelWorkspaceEntity]),
|
||||||
|
MessagingCommonModule,
|
||||||
],
|
],
|
||||||
providers: [
|
providers: [
|
||||||
|
MessagingMessageParticipantService,
|
||||||
|
MessageParticipantMatchParticipantJob,
|
||||||
|
MessageParticipantUnmatchParticipantJob,
|
||||||
MessagingCreateCompanyAndContactAfterSyncJob,
|
MessagingCreateCompanyAndContactAfterSyncJob,
|
||||||
CalendarCreateCompanyAndContactAfterSyncJob,
|
|
||||||
MessageParticipantListener,
|
MessageParticipantListener,
|
||||||
|
MessageParticipantPersonListener,
|
||||||
|
MessageParticipantWorkspaceMemberListener,
|
||||||
|
AddPersonIdAndWorkspaceMemberIdService,
|
||||||
],
|
],
|
||||||
|
exports: [MessagingMessageParticipantService],
|
||||||
})
|
})
|
||||||
export class MessaginParticipantsManagerModule {}
|
export class MessageParticipantManagerModule {}
|
||||||
@ -8,13 +8,11 @@ import { PersonRepository } from 'src/modules/person/repositories/person.reposit
|
|||||||
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
|
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
|
||||||
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
|
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
|
||||||
import { getFlattenedValuesAndValuesStringForBatchRawQuery } from 'src/modules/calendar/calendar-event-import-manager/utils/get-flattened-values-and-values-string-for-batch-raw-query.util';
|
import { getFlattenedValuesAndValuesStringForBatchRawQuery } from 'src/modules/calendar/calendar-event-import-manager/utils/get-flattened-values-and-values-string-for-batch-raw-query.util';
|
||||||
import { AddPersonIdAndWorkspaceMemberIdService } from 'src/modules/calendar-messaging-participant/services/add-person-id-and-workspace-member-id/add-person-id-and-workspace-member-id.service';
|
import { AddPersonIdAndWorkspaceMemberIdService } from 'src/modules/calendar-messaging-participant-manager/services/add-person-id-and-workspace-member-id/add-person-id-and-workspace-member-id.service';
|
||||||
import { MessageParticipantRepository } from 'src/modules/messaging/common/repositories/message-participant.repository';
|
import { MessageParticipantRepository } from 'src/modules/messaging/common/repositories/message-participant.repository';
|
||||||
import { MessageParticipantWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-participant.workspace-entity';
|
import { MessageParticipantWorkspaceEntity } from 'src/modules/messaging/common/standard-objects/message-participant.workspace-entity';
|
||||||
import { ParticipantWithMessageId } from 'src/modules/messaging/message-import-manager/drivers/gmail/types/gmail-message';
|
import { ParticipantWithMessageId } from 'src/modules/messaging/message-import-manager/drivers/gmail/types/gmail-message';
|
||||||
|
|
||||||
// Todo: this is not the right place for this file. The code needs to be refactored in term of business modules with a precise scope.
|
|
||||||
// Putting it here to avoid circular dependencies for now.
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class MessagingMessageParticipantService {
|
export class MessagingMessageParticipantService {
|
||||||
constructor(
|
constructor(
|
||||||
@ -3,14 +3,14 @@ import { Module } from '@nestjs/common';
|
|||||||
import { MessagingBlocklistManagerModule } from 'src/modules/messaging/blocklist-manager/messaging-blocklist-manager.module';
|
import { MessagingBlocklistManagerModule } from 'src/modules/messaging/blocklist-manager/messaging-blocklist-manager.module';
|
||||||
import { MessagingMessageCleanerModule } from 'src/modules/messaging/message-cleaner/messaging-message-cleaner.module';
|
import { MessagingMessageCleanerModule } from 'src/modules/messaging/message-cleaner/messaging-message-cleaner.module';
|
||||||
import { MessagingImportManagerModule } from 'src/modules/messaging/message-import-manager/messaging-import-manager.module';
|
import { MessagingImportManagerModule } from 'src/modules/messaging/message-import-manager/messaging-import-manager.module';
|
||||||
import { MessaginParticipantsManagerModule } from 'src/modules/messaging/message-participants-manager/messaging-participants-manager.module';
|
import { MessageParticipantManagerModule } from 'src/modules/messaging/message-participant-manager/message-participant-manager.module';
|
||||||
import { MessagingMonitoringModule } from 'src/modules/messaging/monitoring/messaging-monitoring.module';
|
import { MessagingMonitoringModule } from 'src/modules/messaging/monitoring/messaging-monitoring.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
MessagingImportManagerModule,
|
MessagingImportManagerModule,
|
||||||
MessagingMessageCleanerModule,
|
MessagingMessageCleanerModule,
|
||||||
MessaginParticipantsManagerModule,
|
MessageParticipantManagerModule,
|
||||||
MessagingBlocklistManagerModule,
|
MessagingBlocklistManagerModule,
|
||||||
MessagingMonitoringModule,
|
MessagingMonitoringModule,
|
||||||
],
|
],
|
||||||
|
|||||||
Reference in New Issue
Block a user