import { Injectable, Logger } from '@nestjs/common'; import { Prisma, PrismaService, Tx } from '../prisma/prisma.service'; import { ActorType } from '../generated/prisma/client'; export type AuditEntry = { /** Null for actions that are not about one venue. */ venueId: string | null; actor: { type: ActorType; id: string | null }; action: string; details?: Prisma.InputJsonObject; }; @Injectable() export class AuditService { private readonly logger = new Logger(AuditService.name); constructor(private prisma: PrismaService) {} /** Records [entry]; inside [tx] it is part of that change, otherwise a failure is only logged. */ async log(entry: AuditEntry, tx?: Tx) { const data = { venueId: entry.venueId, actorType: entry.actor.type, actorId: entry.actor.id, action: entry.action, details: entry.details, }; if (tx) { await tx.auditLog.create({ data }); return; } try { await this.prisma.auditLog.create({ data }); } catch (error) { this.logger.error(`Failed to write audit log: ${error}`); } } }