File
Blob: src/worker/util/audit.ts
| 1 | import { createControlPlaneDb } from "@/worker/db/d1/client"; |
| 2 | import { createAuditEvent } from "@/worker/db/d1/repository"; |
| 3 | import type { JsonObject } from "@/worker/db/types"; |
| 4 | import type { AppContext } from "@/worker/types"; |
| 5 | import { createLogger, errorContext } from "@/worker/util/logging"; |
| 6 | |
| 7 | const log = createLogger("audit"); |
| 8 | |
| 9 | export async function safeAudit( |
| 10 | c: AppContext, |
| 11 | input: { |
| 12 | subjectId?: string | null; |
| 13 | actorSubjectId?: string | null; |
| 14 | eventType: string; |
| 15 | data?: JsonObject; |
| 16 | createdAtMs?: number; |
| 17 | }, |
| 18 | ): Promise<void> { |
| 19 | try { |
| 20 | await createAuditEvent(createControlPlaneDb(c.env.DAV_CONTROL_PLANE), { |
| 21 | ...input, |
| 22 | createdAtMs: input.createdAtMs ?? Date.now(), |
| 23 | }); |
| 24 | } catch (error) { |
| 25 | log.warn("audit_write_failed", { auditEventType: input.eventType, ...errorContext(error) }); |
| 26 | } |
| 27 | } |