import { Injectable } from '@nestjs/common'; import { EventEmitter2 } from '@nestjs/event-emitter'; import { ObjectRecordCreateEvent } from 'src/engine/core-modules/event-emitter/types/object-record-create.event'; import { ObjectRecordUpdateEvent } from 'src/engine/core-modules/event-emitter/types/object-record-update.event'; import { ObjectRecordDeleteEvent } from 'src/engine/core-modules/event-emitter/types/object-record-delete.event'; import { ObjectRecordDestroyEvent } from 'src/engine/core-modules/event-emitter/types/object-record-destroy.event'; import { DatabaseEventAction } from 'src/engine/api/graphql/graphql-query-runner/enums/database-event-action'; import { CustomEventName } from 'src/engine/workspace-event-emitter/types/custom-event-name.type'; type ActionEventMap = { [DatabaseEventAction.CREATED]: ObjectRecordCreateEvent; [DatabaseEventAction.UPDATED]: ObjectRecordUpdateEvent; [DatabaseEventAction.DELETED]: ObjectRecordDeleteEvent; [DatabaseEventAction.DESTROYED]: ObjectRecordDestroyEvent; }; @Injectable() export class WorkspaceEventEmitter { constructor(private readonly eventEmitter: EventEmitter2) {} public emitDatabaseBatchEvent>({ objectMetadataNameSingular, action, events, workspaceId, }: { objectMetadataNameSingular: string; action: A; events: ActionEventMap[A][]; workspaceId: string; }) { if (!events.length) { return; } const eventName = `${objectMetadataNameSingular}.${action}`; this.eventEmitter.emit(eventName, { name: eventName, workspaceId, events, }); } public emitCustomBatchEvent( eventName: CustomEventName, events: object[], workspaceId: string, ) { if (!events.length) { return; } this.eventEmitter.emit(eventName, { name: eventName, workspaceId, events, }); } }