36 lines
901 B
TypeScript
36 lines
901 B
TypeScript
import { randomUUID } from "node:crypto";
|
|
|
|
import type { AuditLog, AuditResult, PublicAccount } from "@/modules/core/types";
|
|
import { updateData } from "@/modules/core/server/store";
|
|
|
|
type AuditInput = {
|
|
actor?: PublicAccount;
|
|
action: string;
|
|
targetType: string;
|
|
targetId?: string;
|
|
result: AuditResult;
|
|
reason: string;
|
|
};
|
|
|
|
export async function recordAuditLog(input: AuditInput): Promise<AuditLog> {
|
|
const timestamp = new Date().toISOString();
|
|
|
|
return await updateData((data) => {
|
|
const nextLog: AuditLog = {
|
|
id: randomUUID(),
|
|
timestamp,
|
|
actorAccountId: input.actor?.id,
|
|
actorEmail: input.actor?.email,
|
|
action: input.action,
|
|
targetType: input.targetType,
|
|
targetId: input.targetId,
|
|
result: input.result,
|
|
reason: input.reason,
|
|
};
|
|
|
|
data.auditLogs = [nextLog, ...data.auditLogs].slice(0, 500);
|
|
return nextLog;
|
|
});
|
|
}
|
|
|