@@ -1,6 +1,14 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
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 {
|
||||
@@ -8,20 +16,23 @@ export class AuditService {
|
||||
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
async log(venueId: string, actorType: string, actorId: string, action: string, details?: Record<string, any>) {
|
||||
/** 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: {
|
||||
id: uuidv4(),
|
||||
venueId,
|
||||
actorType,
|
||||
actorId,
|
||||
action,
|
||||
details: details ? JSON.stringify(details) : null,
|
||||
},
|
||||
});
|
||||
await this.prisma.auditLog.create({ data });
|
||||
} catch (error) {
|
||||
this.logger.error(`Failed to write audit log: ${error}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user