Skip to content

Commit

Permalink
docs: explicando uso de decorator de audit genérico
Browse files Browse the repository at this point in the history
  • Loading branch information
Danilo Romano committed May 22, 2024
1 parent 21f03ae commit 7e4e193
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/decorators/audit.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import { getSessionData } from '@/utils/utils';
import { ExecutionContext, createParamDecorator } from '@nestjs/common';
import { Prisma } from '@prisma/client';

const AuditProperties: <T>(auditProps: (keyof T)[]) => ParameterDecorator =
createParamDecorator((auditProps, ctx: ExecutionContext) => {
/**
* Decorator que recupera o ip / id do usuário atual da requisição e permite passar
* quais propriedades do modelo do prisma
*
* ```ts
* updateFoo(@AuditProperties<'User'>(['phone','name']) auditProperties:any)){
* // ...sua lógica aqui
* // irá registar as alterações nos campos `phone` e `name` na auditoria.
* }
* ```
*/
const AuditProperties: <T extends keyof typeof Prisma.ModelName>(
auditProps: (keyof Prisma.TypeMap['model'][T]['fields'])[],
) => ParameterDecorator = createParamDecorator(
(auditProps, ctx: ExecutionContext) => {
const req = ctx.switchToHttp().getRequest();
const { headers } = req;
const ipAddress = headers['x-real-ip'] || req.ip;
Expand All @@ -14,8 +28,12 @@ const AuditProperties: <T>(auditProps: (keyof T)[]) => ParameterDecorator =
ipAddress,
userId,
};
});
},
);

/**
* Decorator que recupera o ip / id do usuário atual da requisição.
*/
const SessionData = createParamDecorator((_, ctx: ExecutionContext) => {
const req = ctx.switchToHttp().getRequest();
const { headers } = req;
Expand Down

0 comments on commit 7e4e193

Please sign in to comment.