- fixes missing data in event payload when adding a new workspaceMember - add strong typing to database event emitters
62 lines
1.9 KiB
TypeScript
62 lines
1.9 KiB
TypeScript
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<T> = {
|
|
[DatabaseEventAction.CREATED]: ObjectRecordCreateEvent<T>;
|
|
[DatabaseEventAction.UPDATED]: ObjectRecordUpdateEvent<T>;
|
|
[DatabaseEventAction.DELETED]: ObjectRecordDeleteEvent<T>;
|
|
[DatabaseEventAction.DESTROYED]: ObjectRecordDestroyEvent<T>;
|
|
};
|
|
|
|
@Injectable()
|
|
export class WorkspaceEventEmitter {
|
|
constructor(private readonly eventEmitter: EventEmitter2) {}
|
|
|
|
public emitDatabaseBatchEvent<T, A extends keyof ActionEventMap<T>>({
|
|
objectMetadataNameSingular,
|
|
action,
|
|
events,
|
|
workspaceId,
|
|
}: {
|
|
objectMetadataNameSingular: string;
|
|
action: A;
|
|
events: ActionEventMap<T>[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,
|
|
});
|
|
}
|
|
}
|