Productionize EventSphere platform
This commit is contained in:
29
apps/api/src/modules/audit/audit.service.ts
Normal file
29
apps/api/src/modules/audit/audit.service.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
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
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user