diff --git a/frontends/nextjs/src/lib/security/secure-db/audit-log-store.ts b/frontends/nextjs/src/lib/security/secure-db/audit-log-store.ts new file mode 100644 index 000000000..a33739e34 --- /dev/null +++ b/frontends/nextjs/src/lib/security/secure-db/audit-log-store.ts @@ -0,0 +1,32 @@ +import type { AuditLog } from './types' + +const DEFAULT_AUDIT_LOG_LIMIT = 100 +const MAX_AUDIT_LOGS = 1000 +const auditLogs: AuditLog[] = [] + +const normalizeLimit = (limit: number): number => { + if (!Number.isFinite(limit)) return DEFAULT_AUDIT_LOG_LIMIT + return Math.max(0, Math.floor(limit)) +} + +export const addAuditLog = (log: AuditLog): void => { + auditLogs.push(log) + + if (auditLogs.length > MAX_AUDIT_LOGS) { + auditLogs.splice(0, auditLogs.length - MAX_AUDIT_LOGS) + } +} + +export const listAuditLogs = (limit: number = DEFAULT_AUDIT_LOG_LIMIT): AuditLog[] => { + const normalizedLimit = normalizeLimit(limit) + if (normalizedLimit === 0) return [] + + const slice = auditLogs.slice(-normalizedLimit) + return slice.reverse() +} + +export const clearAuditLogs = (): void => { + auditLogs.length = 0 +} + +export { DEFAULT_AUDIT_LOG_LIMIT, MAX_AUDIT_LOGS }