Skip to content

Commit

Permalink
feat: controller standard event (#826)
Browse files Browse the repository at this point in the history
* feat: controller stardard event

* chore: cancel in progress
  • Loading branch information
tea-artist authored Aug 15, 2024
1 parent 1ad330a commit 0474744
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/docker-push.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
name: Build and Push to Docker Registry

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

on:
push:
branches:
Expand Down
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 });
4 changes: 4 additions & 0 deletions apps/nestjs-backend/src/event-emitter/events/event.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export enum Events {
TABLE_VIEW_DELETE = 'table.view.delete',
TABLE_VIEW_UPDATE = 'table.view.update',

CONTROLLER_RECORDS_CREATE = 'controller.records.create',
CONTROLLER_RECORDS_UPDATE = 'controller.records.update',
CONTROLLER_RECORDS_DELETE = 'controller.records.delete',

TABLE_USER_RENAME_COMPLETE = 'table.user.rename.complete',

SHARED_VIEW_CREATE = 'shared.view.create',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
/* eslint-disable sonarjs/no-duplicate-string */
import { Body, Controller, Delete, Get, Param, Patch, Post, Query } from '@nestjs/common';
import {
Body,
Controller,
Delete,
Get,
Param,
Patch,
Post,
Query,
UseInterceptors,
} from '@nestjs/common';
import type { ICreateRecordsVo, IRecord, IRecordsVo } from '@teable/openapi';
import {
createRecordsRoSchema,
Expand All @@ -15,13 +25,19 @@ import {
getRecordHistoryQuerySchema,
IGetRecordHistoryQuery,
} from '@teable/openapi';
import {
EmitEvent,
EventEmitterInterceptor,
} from '../../../event-emitter/decorators/event-emitter.interceptor';
import { Events } from '../../../event-emitter/events';
import { ZodValidationPipe } from '../../../zod.validation.pipe';
import { Permissions } from '../../auth/decorators/permissions.decorator';
import { RecordService } from '../record.service';
import { RecordOpenApiService } from './record-open-api.service';
import { TqlPipe } from './tql.pipe';

@Controller('api/table/:tableId/record')
@UseInterceptors(EventEmitterInterceptor)
export class RecordOpenApiController {
constructor(
private readonly recordService: RecordService,
Expand Down Expand Up @@ -78,6 +94,7 @@ export class RecordOpenApiController {

@Permissions('record|create')
@Post()
@EmitEvent(Events.CONTROLLER_RECORDS_CREATE)
async createRecords(
@Param('tableId') tableId: string,
@Body(new ZodValidationPipe(createRecordsRoSchema)) createRecordsRo: ICreateRecordsRo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { RecordCalculateModule } from '../record-calculate/record-calculate.modu
import { RecordModule } from '../record.module';
import { RecordOpenApiController } from './record-open-api.controller';
import { RecordOpenApiService } from './record-open-api.service';
import { RecordUndoRedoService } from './record-undo-redo-service';

@Module({
imports: [
Expand All @@ -20,7 +21,7 @@ import { RecordOpenApiService } from './record-open-api.service';
ViewOpenApiModule,
],
controllers: [RecordOpenApiController],
providers: [RecordOpenApiService],
providers: [RecordOpenApiService, RecordUndoRedoService],
exports: [RecordOpenApiService],
})
export class RecordOpenApiModule {}
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');
}
}

0 comments on commit 0474744

Please sign in to comment.