// Client-safe wrappers for the /api/audit-log/** BFF routes. import { apiGet } from "@/app/lib/clientApi"; export interface AuditChange { before: unknown; after: unknown; } export interface AuditEvent { id: number; occurredAt: string; actorType: string; actor: string; platform: string; module: string; action: string; httpMethod: string; pathTemplate: string; targetType: string; targetId: string; outcome: "SUCCESS" | "FAILURE"; httpStatus: number; clientIp: string; changes: string | null; // JSON string of { "": { "before": , "after": } } } export interface AuditLogPage { content: AuditEvent[]; totalElements: number; totalPages: number; number: number; size: number; } export interface AuditFilter { actor?: string; actorType?: "ADMIN" | "MEMBER"; module?: string; action?: string; from?: string; to?: string; ip?: string; page: number; pageSize: number; sortBy?: string; sortDir?: "asc" | "desc"; } export function buildQueryString(filter: AuditFilter): string { const qs = new URLSearchParams(); if (filter.actor) qs.set("actor", filter.actor); if (filter.actorType) qs.set("actorType", filter.actorType); if (filter.module) qs.set("module", filter.module); if (filter.action) qs.set("action", filter.action); if (filter.from) qs.set("from", filter.from); if (filter.to) qs.set("to", filter.to); if (filter.ip) qs.set("ip", filter.ip); qs.set("page", String(Math.max(0, filter.page - 1))); qs.set("size", String(filter.pageSize)); return qs.toString(); } /** Parse the changes JSON string into a map of field -> { before, after }. Returns null when changes is empty. */ export function parseChanges(raw: string | null): Record | null { if (!raw || raw.trim() === "") return null; try { const parsed = JSON.parse(raw); return typeof parsed === "object" && parsed !== null ? (parsed as Record) : null; } catch { return null; } } export async function listAuditLog(filter: AuditFilter): Promise { const qs = buildQueryString(filter); return apiGet(`/api/audit-log?${qs}`); } export async function getAuditEvent(id: number): Promise { return apiGet(`/api/audit-log/${id}`); }