-
-
Notifications
You must be signed in to change notification settings - Fork 634
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: controller standard event (#826)
* feat: controller stardard event * chore: cancel in progress
- Loading branch information
1 parent
1ad330a
commit 0474744
Showing
6 changed files
with
110 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
apps/nestjs-backend/src/event-emitter/decorators/event-emitter.interceptor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import type { NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'; | ||
import { Injectable, SetMetadata } from '@nestjs/common'; | ||
import { Reflector } from '@nestjs/core'; | ||
import type { Observable } from 'rxjs'; | ||
import { tap } from 'rxjs/operators'; | ||
import { EventEmitterService } from '../event-emitter.service'; | ||
|
||
@Injectable() | ||
export class EventEmitterInterceptor implements NestInterceptor { | ||
constructor( | ||
private reflector: Reflector, | ||
private eventEmitterService: EventEmitterService | ||
) {} | ||
|
||
intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> { | ||
const eventMetadata = this.reflector.get<{ eventName: string; paramNames?: string[] }>( | ||
'eventMetadata', | ||
context.getHandler() | ||
); | ||
|
||
if (!eventMetadata) { | ||
return next.handle(); | ||
} | ||
|
||
const { eventName, paramNames } = eventMetadata; | ||
const req = context.switchToHttp().getRequest(); | ||
|
||
return next.handle().pipe( | ||
tap((result) => { | ||
const payload: { result: unknown; params: Record<string, unknown> } = { | ||
result, | ||
params: {}, | ||
}; | ||
|
||
if (paramNames && paramNames.length > 0) { | ||
paramNames.forEach((paramName) => { | ||
if (req.params && paramName in req.params) { | ||
payload.params[paramName] = req.params[paramName]; | ||
} else if (req.body && paramName in req.body) { | ||
payload.params[paramName] = req.body[paramName]; | ||
} else if (req.query && paramName in req.query) { | ||
payload.params[paramName] = req.query[paramName]; | ||
} | ||
}); | ||
} else { | ||
// If no specific param names are provided, include all available params | ||
payload.params = { | ||
...req.params, | ||
...req.query, | ||
...req.body, | ||
}; | ||
} | ||
|
||
this.eventEmitterService.emitAsync(eventName, payload); | ||
}) | ||
); | ||
} | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
export const EmitEvent = (eventName: string, paramNames?: string[]) => | ||
SetMetadata('eventMetadata', { eventName, paramNames }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
apps/nestjs-backend/src/features/record/open-api/record-undo-redo-service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { OnEvent } from '@nestjs/event-emitter'; | ||
import { PrismaService } from '@teable/db-main-prisma'; | ||
import type { ICreateRecordsRo, IRecordsVo } from '@teable/openapi'; | ||
import { Events } from '../../../event-emitter/events'; | ||
|
||
@Injectable() | ||
export class RecordUndoRedoService { | ||
private readonly logger = new Logger(RecordUndoRedoService.name); | ||
|
||
constructor(private readonly prismaService: PrismaService) {} | ||
|
||
@OnEvent(Events.CONTROLLER_RECORDS_CREATE) | ||
createRecords(payload: { | ||
params: { tableId: string; createRecordsRo: ICreateRecordsRo }; | ||
result: IRecordsVo; | ||
}) { | ||
this.logger.log('record created'); | ||
} | ||
} |