All files / src/app/admin/audit/_components RetentionLine.tsx

48.14% Statements 13/27
25% Branches 5/20
71.42% Functions 5/7
48% Lines 12/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 964x                       35x 35x 35x 35x 35x 22x 22x 22x     35x 22x   35x                                                                                                                                            
"use client";
 
import { fetchApi } from "@/lib/api";
import { useEffect, useState, useCallback } from "react";
import type { RetentionState } from "./types";
import { T, useT } from "@/lib/i18n/translate";
 
/**
 * Art. 73(1)(e): the logs are kept at least one year. Shows the state of
 * the records and lets the access body delete only what is past its date.
 */
export function RetentionLine() {
  const t = useT("admin");
  const [state, setState] = useState<RetentionState | null>(null);
  const [msg, setMsg] = useState<string | null>(null);
  const [busy, setBusy] = useState(false);
  const load = useCallback(() => {
    fetchApi("/api/admin/audit/retention")
      .then((r) => r.json())
      .then((d) => (d?.policy && d?.events ? setState(d) : setState(null)))
      .catch(() => setState(null));
  }, []);
  useEffect(() => {
    load();
  }, [load]);
  Eif (!state?.policy || !state.events) return null;
  const purge = async () => {
    setBusy(true);
    setMsg(null);
    try {
      const r = await fetchApi("/api/admin/audit/retention", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ confirm: true }),
      });
      const d = await r.json();
      if (!r.ok) throw new Error(d.error ?? `HTTP ${r.status}`);
      setMsg(
        t(
          "Deleted {events} event(s) and {transfers} transfer(s) past their retention date; {kept} event(s) kept.",
          {
            events: d.deleted.events,
            transfers: d.deleted.transfers,
            kept: d.events.protectedCount,
          },
        ),
      );
      load();
    } catch (e) {
      setMsg(e instanceof Error ? e.message : String(e));
    } finally {
      setBusy(false);
    }
  };
  return (
    <div
      className="mb-4 rounded-lg border border-(--border) bg-(--surface) p-3 text-xs flex flex-wrap items-center gap-x-4 gap-y-2"
      data-testid="retention-line"
    >
      <span>
        <T
          area="admin"
          vars={{
            months: state.policy.months,
            article: state.policy.article.replace(
              "Regulation (EU) 2025/327, ",
              "",
            ),
            total: state.events.total,
            oldest: state.events.oldest
              ? state.events.oldest.slice(0, 10)
              : "—",
            expired: state.events.expired,
            kept: state.events.protectedCount,
          }}
          tags={{ b: <strong /> }}
        >
          {
            "<b>Retention:</b> at least {months} months ({article}) · {total} access event(s), oldest {oldest} · {expired} past their date, {kept} protected"
          }
        </T>
      </span>
      <button
        type="button"
        onClick={purge}
        disabled={busy}
        className="px-2 py-1 rounded-sm border border-(--border) disabled:opacity-50"
        title={state.policy.rule}
      >
        {busy ? t("Deleting…") : t("Delete expired records")}
      </button>
      {msg && <span role="status">{msg}</span>}
    </div>
  );
}