import { createControlPlaneDb } from "@/worker/db/d1/client"; import { createAuditEvent } from "@/worker/db/d1/repository"; import type { JsonObject } from "@/worker/db/types"; import type { AppContext } from "@/worker/types"; import { createLogger, errorContext } from "@/worker/util/logging"; const log = createLogger("audit"); export async function safeAudit( c: AppContext, input: { subjectId?: string | null; actorSubjectId?: string | null; eventType: string; data?: JsonObject; createdAtMs?: number; }, ): Promise { try { await createAuditEvent(createControlPlaneDb(c.env.DAV_CONTROL_PLANE), { ...input, createdAtMs: input.createdAtMs ?? Date.now(), }); } catch (error) { log.warn("audit_write_failed", { auditEventType: input.eventType, ...errorContext(error) }); } }