Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | 12x 12x 12x 12x 12x 12x 12x 12x 297x 297x 1x 1x 1x 1x 1x 1x 1x 1x | import { CallHandler, ExecutionContext, Injectable, NestInterceptor, Logger } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { Observable, tap } from 'rxjs';
import { AUDITED_KEY } from '@app/common/decorators/audited.decorator';
import { PrismaService } from '@app/modules/prisma/prisma.service';
@Injectable()
export class AuditInterceptor implements NestInterceptor {
private readonly logger = new Logger(AuditInterceptor.name);
constructor(private readonly reflector: Reflector, private readonly prisma: PrismaService) {}
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const action = this.reflector.get<string>(AUDITED_KEY, context.getHandler());
if (!action) return next.handle();
const req = context.switchToHttp().getRequest();
const user = req?.user;
const entityType = req?.route?.path || 'Unknown';
const oldValue = { body: req?.body, params: req?.params };
return next.handle().pipe(
tap(async (result) => {
try {
// @ts-ignore – generated after prisma generate
await this.prisma.client.auditLog.create({
data: {
entityType,
entityId: 0,
action,
oldValue: oldValue as any,
newValue: result as any,
userId: user?.userId ?? user?.id ?? null,
},
});
} catch (e: any) {
this.logger.warn(`Audit log failed: ${e?.message || e}`);
}
}),
);
}
}
|