8643 fix sentry error (#8644)
- fixes missing data in event payload when adding a new workspaceMember - add strong typing to database event emitters
This commit is contained in:
@ -0,0 +1 @@
|
||||
export type CustomEventName = `${string}_${string}`;
|
||||
@ -1,21 +1,61 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
|
||||
import { WorkspaceEventBatch } from 'src/engine/workspace-event-emitter/workspace-event.type';
|
||||
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 emit(eventName: string, events: any[], workspaceId: string) {
|
||||
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;
|
||||
}
|
||||
|
||||
return this.eventEmitter.emit(eventName, {
|
||||
const eventName = `${objectMetadataNameSingular}.${action}`;
|
||||
|
||||
this.eventEmitter.emit(eventName, {
|
||||
name: eventName,
|
||||
workspaceId,
|
||||
events,
|
||||
} satisfies WorkspaceEventBatch<any>);
|
||||
});
|
||||
}
|
||||
|
||||
public emitCustomBatchEvent(
|
||||
eventName: CustomEventName,
|
||||
events: object[],
|
||||
workspaceId: string,
|
||||
) {
|
||||
if (!events.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.eventEmitter.emit(eventName, {
|
||||
name: eventName,
|
||||
workspaceId,
|
||||
events,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user