import { getDb, schema } from '../db/index.js'; import { generateSnowflake } from './snowflake.js'; import type { AuditAction } from '@backspace/shared/src/audit.js'; interface RecordAuditEventInput { spaceId: string; actorId: string | null; action: AuditAction; targetType?: string | null; targetId?: string | null; metadata?: Record | null; } /** * Appends one entry to a space's audit log. * * Never throws: an audit write must not be able to fail the action it is * describing. A moderator kicking someone must not see the kick fail because * the log could not be written — the kick already happened. */ export function recordAuditEvent(input: RecordAuditEventInput): void { try { getDb().insert(schema.auditEvents).values({ id: generateSnowflake(), spaceId: input.spaceId, actorId: input.actorId, action: input.action, targetType: input.targetType ?? null, targetId: input.targetId ?? null, metadata: input.metadata ? JSON.stringify(input.metadata) : null, createdAt: Date.now(), }).run(); } catch (err) { console.warn('[audit] failed to record event', input.action, err); } }