30 lines
958 B
TypeScript
30 lines
958 B
TypeScript
import { Injectable } from "@nestjs/common";
|
|
import type { Request } from "express";
|
|
import { PrismaService } from "../../prisma/prisma.service";
|
|
|
|
@Injectable()
|
|
export class AuditService {
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
async log(action: string, req?: Request, options?: { tenantId?: string; actorUserId?: string; entityType?: string; entityId?: string; metadata?: any }) {
|
|
const ip =
|
|
(req?.headers?.["x-forwarded-for"] as string | undefined)?.split(",")[0]?.trim() ??
|
|
(req as any)?.ip ??
|
|
undefined;
|
|
const userAgent = (req?.headers?.["user-agent"] as string | undefined) ?? undefined;
|
|
|
|
await this.prisma.auditLog.create({
|
|
data: {
|
|
action,
|
|
tenantId: options?.tenantId,
|
|
actorUserId: options?.actorUserId,
|
|
entityType: options?.entityType,
|
|
entityId: options?.entityId,
|
|
metadata: options?.metadata,
|
|
ip,
|
|
userAgent
|
|
}
|
|
});
|
|
}
|
|
}
|