import "server-only"; import { getDb, auditLogs } from "@fetcha/db"; import { newId } from "@fetcha/core"; export type AuditAction = | "account.created" | "login" | "logout" | "email.verified" | "password.changed" | "password.reset" | "email.changed" | "account.deleted" | "api_key.created" | "api_key.revoked" | "api_key.rotated" | "project.created" | "project.updated" | "project.archived" | "billing.changed" | "webhook.modified" | "limits.updated" | "session.revoked" | "admin.action"; export async function writeAudit(input: { userId?: string | null; organizationId?: string | null; action: AuditAction; target?: string | null; metadata?: Record; ipAddress?: string | null; userAgent?: string | null; }): Promise { try { await getDb().insert(auditLogs).values({ id: newId("aud"), userId: input.userId ?? null, organizationId: input.organizationId ?? null, action: input.action, target: input.target ?? null, metadata: input.metadata ?? null, ipAddress: input.ipAddress ?? null, userAgent: input.userAgent ?? null, }); } catch (e) { console.error("[audit] failed", (e as Error).message); } }