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:
martmull
2024-11-21 17:09:36 +01:00
committed by GitHub
parent 395da91071
commit 39373b4a28
61 changed files with 460 additions and 311 deletions

View File

@ -0,0 +1 @@
export type CustomEventName = `${string}_${string}`;

View File

@ -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,
});
}
}