|
| 1 | +import { Injectable, Logger } from '@nestjs/common'; |
| 2 | +import { OnEvent } from '@nestjs/event-emitter'; |
| 3 | + |
| 4 | +import { FeatureFlagKey } from 'src/engine/core-modules/feature-flag/enums/feature-flag-key.enum'; |
| 5 | +import { IsFeatureEnabledService } from 'src/engine/core-modules/feature-flag/services/is-feature-enabled.service'; |
| 6 | +import { ObjectRecordCreateEvent } from 'src/engine/integrations/event-emitter/types/object-record-create.event'; |
| 7 | +import { ObjectRecordDeleteEvent } from 'src/engine/integrations/event-emitter/types/object-record-delete.event'; |
| 8 | +import { ObjectRecordUpdateEvent } from 'src/engine/integrations/event-emitter/types/object-record-update.event'; |
| 9 | +import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decorators/message-queue.decorator'; |
| 10 | +import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants'; |
| 11 | +import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service'; |
| 12 | +import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager'; |
| 13 | +import { WorkflowEventListenerWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow-event-listener.workspace-entity'; |
| 14 | +import { |
| 15 | + WorkflowEventTriggerJob, |
| 16 | + WorkflowEventTriggerJobData, |
| 17 | +} from 'src/modules/workflow/workflow-trigger/jobs/workflow-event-trigger.job'; |
| 18 | + |
| 19 | +@Injectable() |
| 20 | +export class DatabaseEventTriggerListener { |
| 21 | + private readonly logger = new Logger('DatabaseEventTriggerListener'); |
| 22 | + |
| 23 | + constructor( |
| 24 | + private readonly twentyORMGlobalManager: TwentyORMGlobalManager, |
| 25 | + @InjectMessageQueue(MessageQueue.workflowQueue) |
| 26 | + private readonly messageQueueService: MessageQueueService, |
| 27 | + private readonly isFeatureFlagEnabledService: IsFeatureEnabledService, |
| 28 | + ) {} |
| 29 | + |
| 30 | + @OnEvent('*.created') |
| 31 | + async handleObjectRecordCreateEvent(payload: ObjectRecordCreateEvent<any>) { |
| 32 | + await this.handleEvent(payload); |
| 33 | + } |
| 34 | + |
| 35 | + @OnEvent('*.updated') |
| 36 | + async handleObjectRecordUpdateEvent(payload: ObjectRecordUpdateEvent<any>) { |
| 37 | + await this.handleEvent(payload); |
| 38 | + } |
| 39 | + |
| 40 | + @OnEvent('*.deleted') |
| 41 | + async handleObjectRecordDeleteEvent(payload: ObjectRecordDeleteEvent<any>) { |
| 42 | + await this.handleEvent(payload); |
| 43 | + } |
| 44 | + |
| 45 | + private async handleEvent( |
| 46 | + payload: |
| 47 | + | ObjectRecordCreateEvent<any> |
| 48 | + | ObjectRecordUpdateEvent<any> |
| 49 | + | ObjectRecordDeleteEvent<any>, |
| 50 | + ) { |
| 51 | + const workspaceId = payload.workspaceId; |
| 52 | + const eventName = payload.name; |
| 53 | + |
| 54 | + if (!workspaceId || !eventName) { |
| 55 | + this.logger.error( |
| 56 | + `Missing workspaceId or eventName in payload ${JSON.stringify( |
| 57 | + payload, |
| 58 | + )}`, |
| 59 | + ); |
| 60 | + |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + const isWorkflowEnabled = |
| 65 | + await this.isFeatureFlagEnabledService.isFeatureEnabled( |
| 66 | + FeatureFlagKey.IsWorkflowEnabled, |
| 67 | + workspaceId, |
| 68 | + ); |
| 69 | + |
| 70 | + if (!isWorkflowEnabled) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + const workflowEventListenerRepository = |
| 75 | + await this.twentyORMGlobalManager.getRepositoryForWorkspace<WorkflowEventListenerWorkspaceEntity>( |
| 76 | + workspaceId, |
| 77 | + 'workflowEventListener', |
| 78 | + ); |
| 79 | + |
| 80 | + const eventListeners = await workflowEventListenerRepository.find({ |
| 81 | + where: { |
| 82 | + eventName, |
| 83 | + }, |
| 84 | + }); |
| 85 | + |
| 86 | + for (const eventListener of eventListeners) { |
| 87 | + this.messageQueueService.add<WorkflowEventTriggerJobData>( |
| 88 | + WorkflowEventTriggerJob.name, |
| 89 | + { |
| 90 | + workspaceId, |
| 91 | + workflowId: eventListener.workflowId, |
| 92 | + payload, |
| 93 | + }, |
| 94 | + { retryLimit: 3 }, |
| 95 | + ); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments