40 lines
1.0 KiB
TypeScript
40 lines
1.0 KiB
TypeScript
import type Database from "better-sqlite3";
|
|
|
|
export type AuditInput = {
|
|
requestId: string;
|
|
actorAdminId?: string | null;
|
|
actorUsername?: string | null;
|
|
action: string;
|
|
targetType: string;
|
|
targetId?: string | null;
|
|
outcome?: "success" | "denied" | "failure";
|
|
before?: unknown;
|
|
after?: unknown;
|
|
metadata?: unknown;
|
|
};
|
|
|
|
function json(value: unknown): string | null {
|
|
return value === undefined ? null : JSON.stringify(value);
|
|
}
|
|
|
|
export function writeAudit(sqlite: Database.Database, input: AuditInput): void {
|
|
sqlite.prepare(`
|
|
INSERT INTO audit_events (
|
|
occurred_at, request_id, actor_admin_id, actor_username, action,
|
|
target_type, target_id, outcome, before_json, after_json, metadata_json
|
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
`).run(
|
|
Date.now(),
|
|
input.requestId,
|
|
input.actorAdminId ?? null,
|
|
input.actorUsername ?? null,
|
|
input.action,
|
|
input.targetType,
|
|
input.targetId ?? null,
|
|
input.outcome ?? "success",
|
|
json(input.before),
|
|
json(input.after),
|
|
json(input.metadata),
|
|
);
|
|
}
|