import type { SupportLogEntryDto } from "@mrp/shared"; import { randomUUID } from "node:crypto"; const SUPPORT_LOG_LIMIT = 200; const supportLogs: SupportLogEntryDto[] = []; function serializeContext(context?: Record) { if (!context) { return "{}"; } try { return JSON.stringify(context); } catch { return JSON.stringify({ serializationError: "Unable to serialize support log context." }); } } export function recordSupportLog(entry: { level: SupportLogEntryDto["level"]; source: string; message: string; context?: Record; }) { supportLogs.unshift({ id: randomUUID(), level: entry.level, source: entry.source, message: entry.message, contextJson: serializeContext(entry.context), createdAt: new Date().toISOString(), }); if (supportLogs.length > SUPPORT_LOG_LIMIT) { supportLogs.length = SUPPORT_LOG_LIMIT; } } export function listSupportLogs(limit = 50) { return supportLogs.slice(0, Math.max(0, limit)); } export function getSupportLogCount() { return supportLogs.length; }