import type { AuditLogEntry } from '@admin/actions/audit/types' const CSV_COLUMNS: ReadonlyArray string]> = [ ['Time', (auditLog) => auditLog.createdAt], ['Category', (auditLog) => auditLog.category], ['Action', (auditLog) => auditLog.action], ['Target type', (auditLog) => auditLog.targetType], ['Target ID', (auditLog) => auditLog.targetId ?? ''], ['Target', (auditLog) => auditLog.targetLabel ?? ''], ['Actor email', (auditLog) => auditLog.actorEmail ?? ''], ['Actor name', (auditLog) => auditLog.actorName ?? ''], ['Actor role', (auditLog) => auditLog.actorRole ?? ''], ['Source', (auditLog) => auditLog.sourceType], ['IP address', (auditLog) => auditLog.ipAddress ?? ''], ['User agent', (auditLog) => auditLog.userAgent ?? ''], ['Changed fields', (auditLog) => auditLog.changedFields.join(' ')] ] function escapeCsvValue(value: string): string { return /[",\r\n]/.test(value) ? `"${value.replaceAll('"', '""')}"` : value } export function toAuditLogCsv(auditLogs: AuditLogEntry[]): string { const header = CSV_COLUMNS.map(([label]) => escapeCsvValue(label)).join(',') const rows = auditLogs.map((auditLog) => CSV_COLUMNS.map(([, read]) => escapeCsvValue(read(auditLog))).join(',') ) return [header, ...rows].join('\r\n') }