SPB Git

Repository Public

logic.ts 1.7 KB 43 lines typescript

1// Pure helpers of the audit screen (unit-tested). Audit times are entered and shown in UTC.23/** `<input type="datetime-local">` value read as UTC → ISO instant. */4export function utcInputToIso(value: string): string | null {5  if (!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2})?$/.test(value)) return null;6  const d = new Date(`${value}${value.length === 16 ? ":00" : ""}Z`);7  return Number.isNaN(d.getTime()) ? null : d.toISOString();8}9/** ISO instant → `datetime-local` value expressed in UTC. */10export function isoToUtcInput(iso: string | undefined | null): string {11  if (!iso) return "";12  const d = new Date(iso);13  return Number.isNaN(d.getTime()) ? "" : d.toISOString().slice(0, 16);14}1516export const AUDIT_FILTER_KEYS = ["action", "actor_id", "target_kind", "target_id", "outcome", "correlation_id", "from", "to"] as const;17export type AuditFilterKey = (typeof AUDIT_FILTER_KEYS)[number];18export type AuditFilters = Partial<Record<AuditFilterKey, string>>;1920export function filtersFromParams(params: { get: (k: string) => string | null }): AuditFilters {21  const out: AuditFilters = {};22  for (const k of AUDIT_FILTER_KEYS) {23    const v = params.get(k)?.trim();24    if (v) out[k] = v;25  }26  if (out.outcome && !["success", "denied", "failure"].includes(out.outcome)) delete out.outcome;27  return out;28}29export function filtersToQueryString(f: AuditFilters): string {30  const p = new URLSearchParams();31  for (const k of AUDIT_FILTER_KEYS) {32    const v = f[k];33    if (v) p.set(k, v);34  }35  const s = p.toString();36  return s ? `?${s}` : "";37}3839export function exportFileName(fromIso: string, toIso: string): string {40  const c = (s: string) => s.slice(0, 16).replace(/[-:]/g, "");41  return `mcc-audit-${c(fromIso)}Z-${c(toIso)}Z.jsonl`;42}43
Files main
Menu

Theme