From 06ef33a0369c20f056d533d5e04fa3a3833a7fab Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Tue, 23 Apr 2024 18:28:34 +0200 Subject: [PATCH 1/2] dispatch job instead of emitting event --- .../google-calendar-sync.service.ts | 24 ++++++++---- .../create-company-and-contact.listener.ts | 32 ---------------- ...and-emit-contact-creation-event.service.ts | 37 +++++++++++++------ 3 files changed, 42 insertions(+), 51 deletions(-) delete mode 100644 packages/twenty-server/src/modules/connected-account/auto-companies-and-contacts-creation/listeners/create-company-and-contact.listener.ts diff --git a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync/google-calendar-sync.service.ts b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync/google-calendar-sync.service.ts index 4629fec9d97e..d7cbca9586e4 100644 --- a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync/google-calendar-sync.service.ts +++ b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync/google-calendar-sync.service.ts @@ -1,6 +1,5 @@ import { Injectable, Logger } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; -import { EventEmitter2 } from '@nestjs/event-emitter'; import { Repository } from 'typeorm'; import { calendar_v3 as calendarV3 } from 'googleapis'; @@ -29,6 +28,13 @@ import { CalendarEventCleanerService } from 'src/modules/calendar/services/calen import { CalendarEventParticipantService } from 'src/modules/calendar/services/calendar-event-participant/calendar-event-participant.service'; import { CalendarEventParticipant } from 'src/modules/calendar/types/calendar-event'; import { filterOutBlocklistedEvents } from 'src/modules/calendar/utils/filter-out-blocklisted-events.util'; +import { + CreateCompanyAndContactJobData, + CreateCompanyAndContactJob, +} from 'src/modules/connected-account/auto-companies-and-contacts-creation/jobs/create-company-and-contact.job'; +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'; @Injectable() export class GoogleCalendarSyncService { @@ -53,9 +59,10 @@ export class GoogleCalendarSyncService { @InjectRepository(FeatureFlagEntity, 'core') private readonly featureFlagRepository: Repository, private readonly workspaceDataSourceService: WorkspaceDataSourceService, - private readonly eventEmitter: EventEmitter2, private readonly calendarEventCleanerService: CalendarEventCleanerService, private readonly calendarEventParticipantsService: CalendarEventParticipantService, + @InjectMessageQueue(MessageQueue.emailQueue) + private readonly messageQueueService: MessageQueueService, ) {} public async startGoogleCalendarSync( @@ -369,11 +376,14 @@ export class GoogleCalendarSyncService { ); if (calendarChannel.isContactAutoCreationEnabled) { - this.eventEmitter.emit(`createContacts`, { - workspaceId, - connectedAccountHandle: connectedAccount.handle, - contactsToCreate: participantsToSave, - }); + await this.messageQueueService.add( + CreateCompanyAndContactJob.name, + { + workspaceId, + connectedAccountHandle: connectedAccount.handle, + contactsToCreate: participantsToSave, + }, + ); } } catch (error) { this.logger.error( diff --git a/packages/twenty-server/src/modules/connected-account/auto-companies-and-contacts-creation/listeners/create-company-and-contact.listener.ts b/packages/twenty-server/src/modules/connected-account/auto-companies-and-contacts-creation/listeners/create-company-and-contact.listener.ts deleted file mode 100644 index 40b8741b095d..000000000000 --- a/packages/twenty-server/src/modules/connected-account/auto-companies-and-contacts-creation/listeners/create-company-and-contact.listener.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { Injectable, Inject } from '@nestjs/common'; -import { OnEvent } from '@nestjs/event-emitter'; - -import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; -import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; -import { - CreateCompanyAndContactJobData, - CreateCompanyAndContactJob, -} from 'src/modules/connected-account/auto-companies-and-contacts-creation/jobs/create-company-and-contact.job'; - -@Injectable() -export class CreateCompanyAndContactListener { - constructor( - @Inject(MessageQueue.contactCreationQueue) - private readonly messageQueueService: MessageQueueService, - ) {} - - @OnEvent('createContacts') - async handleContactCreationEvent(payload: { - workspaceId: string; - connectedAccountHandle: string; - contactsToCreate: { - displayName: string; - handle: string; - }[]; - }) { - await this.messageQueueService.add( - CreateCompanyAndContactJob.name, - payload, - ); - } -} diff --git a/packages/twenty-server/src/modules/messaging/services/save-message-and-emit-contact-creation-event/save-message-and-emit-contact-creation-event.service.ts b/packages/twenty-server/src/modules/messaging/services/save-message-and-emit-contact-creation-event/save-message-and-emit-contact-creation-event.service.ts index 8a609b108410..7fc4587a7e8d 100644 --- a/packages/twenty-server/src/modules/messaging/services/save-message-and-emit-contact-creation-event/save-message-and-emit-contact-creation-event.service.ts +++ b/packages/twenty-server/src/modules/messaging/services/save-message-and-emit-contact-creation-event/save-message-and-emit-contact-creation-event.service.ts @@ -1,5 +1,4 @@ import { Injectable, Logger } from '@nestjs/common'; -import { EventEmitter2 } from '@nestjs/event-emitter'; import { EntityManager } from 'typeorm'; @@ -15,6 +14,13 @@ import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repos import { MessageChannelObjectMetadata } from 'src/modules/messaging/standard-objects/message-channel.object-metadata'; import { MessageService } from 'src/modules/messaging/services/message/message.service'; import { MessageParticipantService } from 'src/modules/messaging/services/message-participant/message-participant.service'; +import { + CreateCompanyAndContactJobData, + CreateCompanyAndContactJob, +} from 'src/modules/connected-account/auto-companies-and-contacts-creation/jobs/create-company-and-contact.job'; +import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator'; +import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; +import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; @Injectable() export class SaveMessageAndEmitContactCreationEventService { @@ -26,9 +32,10 @@ export class SaveMessageAndEmitContactCreationEventService { private readonly messageService: MessageService, @InjectObjectMetadataRepository(MessageChannelObjectMetadata) private readonly messageChannelRepository: MessageChannelRepository, - private readonly eventEmitter: EventEmitter2, private readonly workspaceDataSourceService: WorkspaceDataSourceService, private readonly messageParticipantService: MessageParticipantService, + @InjectMessageQueue(MessageQueue.emailQueue) + private readonly messageQueueService: MessageQueueService, ) {} public async saveMessagesAndEmitContactCreationEventWithinTransaction( @@ -75,11 +82,14 @@ export class SaveMessageAndEmitContactCreationEventService { (participant) => participant.shouldCreateContact, ); - this.eventEmitter.emit(`createContacts`, { - workspaceId, - connectedAccountHandle: connectedAccount.handle, - contactsToCreate, - }); + await this.messageQueueService.add( + CreateCompanyAndContactJob.name, + { + workspaceId, + connectedAccountHandle: connectedAccount.handle, + contactsToCreate, + }, + ); } } @@ -180,11 +190,14 @@ export class SaveMessageAndEmitContactCreationEventService { (participant) => participant.shouldCreateContact, ); - this.eventEmitter.emit(`createContacts`, { - workspaceId, - connectedAccountHandle: connectedAccount.handle, - contactsToCreate, - }); + await this.messageQueueService.add( + CreateCompanyAndContactJob.name, + { + workspaceId, + connectedAccountHandle: connectedAccount.handle, + contactsToCreate, + }, + ); } } catch (error) { this.logger.error( From 3cd7be723a8b43419bf79a1c9a584ceb444d3992 Mon Sep 17 00:00:00 2001 From: bosiraphael Date: Tue, 23 Apr 2024 18:35:06 +0200 Subject: [PATCH 2/2] fix module --- .../auto-companies-and-contacts-creation.module.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/twenty-server/src/modules/connected-account/auto-companies-and-contacts-creation/auto-companies-and-contacts-creation.module.ts b/packages/twenty-server/src/modules/connected-account/auto-companies-and-contacts-creation/auto-companies-and-contacts-creation.module.ts index 9acc759785a5..ad9973928f88 100644 --- a/packages/twenty-server/src/modules/connected-account/auto-companies-and-contacts-creation/auto-companies-and-contacts-creation.module.ts +++ b/packages/twenty-server/src/modules/connected-account/auto-companies-and-contacts-creation/auto-companies-and-contacts-creation.module.ts @@ -9,7 +9,6 @@ import { PersonObjectMetadata } from 'src/modules/person/standard-objects/person import { WorkspaceMemberObjectMetadata } from 'src/modules/workspace-member/standard-objects/workspace-member.object-metadata'; import { MessageParticipantModule } from 'src/modules/messaging/services/message-participant/message-participant.module'; import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module'; -import { CreateCompanyAndContactListener } from 'src/modules/connected-account/auto-companies-and-contacts-creation/listeners/create-company-and-contact.listener'; import { CalendarEventParticipantObjectMetadata } from 'src/modules/calendar/standard-objects/calendar-event-participant.object-metadata'; import { CalendarEventParticipantModule } from 'src/modules/calendar/services/calendar-event-participant/calendar-event-participant.module'; import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-flag.entity'; @@ -28,7 +27,7 @@ import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature- CalendarEventParticipantModule, TypeOrmModule.forFeature([FeatureFlagEntity], 'core'), ], - providers: [CreateCompanyAndContactService, CreateCompanyAndContactListener], + providers: [CreateCompanyAndContactService], exports: [CreateCompanyAndContactService], }) export class AutoCompaniesAndContactsCreationModule {}