Archived
- Retention policies (Admin -> Privacy -> Retention) applied by a nightly
`retention` job: inactive accounts anonymised after 5 years with a
30-day warning (on by default; never staff roles or people with an
upcoming registration), form answers, login history, payer details in
payment notifications, audit/access logs
- User.lastLoginAt (set on login, backfilled) and inactivityWarnedAt
- Breach register (SecurityBreach) with drafts for the people affected
and the Information Regulator
- Readiness checklist with a score
- Children's/medical flag in the access log; {retention} placeholder in
the privacy notice
- Unit tests (retention.test.js), e2e (privacy-retention.spec.ts); docs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012mt6TCv85T9VKRq3cV7rXq
232 lines
13 KiB
TypeScript
232 lines
13 KiB
TypeScript
"use client";
|
|
|
|
import React, { useCallback, useEffect, useState } from "react";
|
|
import Link from "next/link";
|
|
import { ArrowUp, ArrowDown, Trash2, Plus, RotateCcw, Eye, PencilLine } from "lucide-react";
|
|
import { apiFetch } from "@/lib/api";
|
|
import { errorMessage } from "@/lib/utils";
|
|
import { PrivacyNoticeSection, type NoticeSection } from "@/components/shared/PrivacyNoticeSection";
|
|
|
|
type Version = { version: number; publishedAt: string; changeSummary: string | null; requiresAcceptance: boolean; publishedBy: string | null; acceptances: number };
|
|
type Draft = { sections: NoticeSection[]; rendered: NoticeSection[]; isDraft: boolean; savedAt: string | null; versions: Version[]; builtIn: NoticeSection[] };
|
|
type Msg = { type: "ok" | "err"; text: string } | null;
|
|
|
|
const PLACEHOLDER_HELP: [string, string][] = [
|
|
["{org}", "organisation name"],
|
|
["{orgEmail}", "organisation email"],
|
|
["{website}", "website (Site settings → Legal)"],
|
|
["{ioName}", "Information Officer"],
|
|
["{ioEmail}", "Information Officer's email"],
|
|
["{medicalRetentionDays}", "days before medical details are deleted"],
|
|
["{operators}", "on its own line: one bullet per active operator"],
|
|
["{retention}", "on its own line: one bullet per retention period"],
|
|
];
|
|
|
|
const fmt = (d: string) => new Date(d).toLocaleString(undefined, { day: "numeric", month: "short", year: "numeric", hour: "2-digit", minute: "2-digit" });
|
|
const inputCls = "w-full border rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-brand-500";
|
|
|
|
/** Admin → Privacy → Privacy notice: edit the sections, preview, publish versions. */
|
|
export function NoticeEditor({ token }: { token: string }) {
|
|
const [draft, setDraft] = useState<Draft | null>(null);
|
|
const [sections, setSections] = useState<NoticeSection[]>([]);
|
|
const [dirty, setDirty] = useState(false);
|
|
const [preview, setPreview] = useState(false);
|
|
const [busy, setBusy] = useState(false);
|
|
const [msg, setMsg] = useState<Msg>(null);
|
|
const [publishing, setPublishing] = useState(false);
|
|
const [changeSummary, setChangeSummary] = useState("");
|
|
const [requiresAcceptance, setRequiresAcceptance] = useState(false);
|
|
|
|
const load = useCallback(async () => {
|
|
try {
|
|
const d = await apiFetch<Draft>("/api/privacy/notice/draft", { authToken: token });
|
|
setDraft(d);
|
|
setSections(d.sections);
|
|
setDirty(false);
|
|
} catch (e) {
|
|
setMsg({ type: "err", text: errorMessage(e) || "Could not load the notice." });
|
|
}
|
|
}, [token]);
|
|
useEffect(() => { load(); }, [load]);
|
|
|
|
const change = (next: NoticeSection[]) => { setSections(next); setDirty(true); };
|
|
const update = (i: number, patch: Partial<NoticeSection>) => change(sections.map((s, j) => (j === i ? { ...s, ...patch } : s)));
|
|
const move = (i: number, by: number) => {
|
|
const next = [...sections];
|
|
const [s] = next.splice(i, 1);
|
|
next.splice(i + by, 0, s);
|
|
change(next);
|
|
};
|
|
|
|
const save = async () => {
|
|
setBusy(true);
|
|
setMsg(null);
|
|
try {
|
|
const res = await apiFetch<{ sections: NoticeSection[]; rendered: NoticeSection[]; savedAt: string }>("/api/privacy/notice/draft", { method: "PUT", authToken: token, body: { sections } });
|
|
setDraft(d => (d ? { ...d, sections: res.sections, rendered: res.rendered, isDraft: true, savedAt: res.savedAt } : d));
|
|
setSections(res.sections);
|
|
setDirty(false);
|
|
setMsg({ type: "ok", text: "Draft saved. It isn't public until you publish it." });
|
|
} catch (e) {
|
|
setMsg({ type: "err", text: errorMessage(e) || "Could not save." });
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
};
|
|
|
|
const discard = async () => {
|
|
if (!confirm("Throw away the draft and go back to the published text?")) return;
|
|
setBusy(true);
|
|
try {
|
|
await apiFetch("/api/privacy/notice/draft", { method: "DELETE", authToken: token });
|
|
await load();
|
|
setMsg({ type: "ok", text: "Draft discarded." });
|
|
} catch (e) {
|
|
setMsg({ type: "err", text: errorMessage(e) || "Could not discard." });
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
};
|
|
|
|
const publish = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
setBusy(true);
|
|
setMsg(null);
|
|
try {
|
|
const res = await apiFetch<{ version: number }>("/api/privacy/notice/publish", { method: "POST", authToken: token, body: { sections, changeSummary, requiresAcceptance } });
|
|
setPublishing(false);
|
|
setChangeSummary("");
|
|
setRequiresAcceptance(false);
|
|
await load();
|
|
setMsg({ type: "ok", text: `Version ${res.version} is published.${requiresAcceptance ? " Everyone will be asked to accept it after they next log in." : ""}` });
|
|
} catch (e) {
|
|
setMsg({ type: "err", text: errorMessage(e) || "Could not publish." });
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
};
|
|
|
|
if (!draft) return msg ? <p className="text-sm text-red-700">{msg.text}</p> : <p className="text-sm text-gray-400">Loading…</p>;
|
|
|
|
const builtInById = new Map(draft.builtIn.map(s => [s.id, s]));
|
|
const current = draft.versions[0];
|
|
|
|
return (
|
|
<div className="space-y-5">
|
|
<div className="text-sm text-gray-600">
|
|
{current ? (
|
|
<>Published: <strong>version {current.version}</strong> ({fmt(current.publishedAt)}). </>
|
|
) : (
|
|
<>Nothing published yet — the site shows the built-in notice. </>
|
|
)}
|
|
{draft.isDraft && <span className="text-amber-700">You have an unpublished draft{draft.savedAt ? ` (saved ${fmt(draft.savedAt)})` : ""}. </span>}
|
|
<Link href="/legal/privacy" target="_blank" className="text-brand-600 hover:underline">View the public page</Link>
|
|
</div>
|
|
|
|
<details className="text-xs text-gray-500 border rounded-lg p-3 bg-gray-50">
|
|
<summary className="cursor-pointer font-medium text-gray-700">How to write it</summary>
|
|
<p className="mt-2">One paragraph per line; start a line with "• " for a bullet. These are filled in when the notice is shown, so they stay up to date:</p>
|
|
<ul className="mt-1 grid sm:grid-cols-2 gap-x-4">
|
|
{PLACEHOLDER_HELP.map(([p, d]) => <li key={p}><code>{p}</code> — {d}</li>)}
|
|
</ul>
|
|
<p className="mt-2">This is tooling to help you comply, not legal advice — your Information Officer should check the wording.</p>
|
|
</details>
|
|
|
|
<div className="flex flex-wrap gap-2">
|
|
<button onClick={() => setPreview(p => !p)} className="inline-flex items-center gap-1.5 px-3 py-1.5 rounded-lg border bg-white text-sm hover:bg-gray-50">
|
|
{preview ? <><PencilLine className="w-4 h-4" /> Edit</> : <><Eye className="w-4 h-4" /> Preview</>}
|
|
</button>
|
|
<button onClick={save} disabled={busy || !dirty} className="px-3 py-1.5 rounded-lg bg-white border border-brand-600 text-brand-700 text-sm font-medium hover:bg-brand-50 disabled:opacity-50">
|
|
Save draft
|
|
</button>
|
|
<button onClick={() => setPublishing(true)} disabled={busy} className="px-3 py-1.5 rounded-lg bg-brand-600 text-white text-sm font-medium hover:bg-brand-700 disabled:opacity-50">
|
|
Publish…
|
|
</button>
|
|
{draft.isDraft && (
|
|
<button onClick={discard} disabled={busy} className="px-3 py-1.5 rounded-lg text-sm text-gray-600 hover:bg-gray-100">Discard draft</button>
|
|
)}
|
|
{dirty && <span className="self-center text-xs text-amber-700">Unsaved changes</span>}
|
|
</div>
|
|
|
|
{msg && <div className={`p-3 rounded-lg text-sm ${msg.type === "ok" ? "bg-green-50 text-green-800 border border-green-200" : "bg-red-50 text-red-800 border border-red-200"}`}>{msg.text}</div>}
|
|
|
|
{publishing && (
|
|
<form onSubmit={publish} className="border rounded-xl p-4 bg-brand-50/40 space-y-3">
|
|
<div className="font-medium text-gray-900">Publish version {(current?.version ?? 0) + 1}</div>
|
|
<div>
|
|
<label className="block text-sm font-medium text-gray-700 mb-1">What changed</label>
|
|
<textarea className={inputCls} rows={2} maxLength={2000} value={changeSummary} onChange={e => setChangeSummary(e.target.value)} placeholder="e.g. We now use PayFast for online payments." />
|
|
</div>
|
|
<label className="flex items-start gap-2 text-sm text-gray-700">
|
|
<input type="checkbox" className="mt-0.5 h-4 w-4 accent-brand-600" checked={requiresAcceptance} onChange={e => setRequiresAcceptance(e.target.checked)} />
|
|
<span>
|
|
<strong>Significant change</strong> — ask everyone to accept this version the next time they log in.
|
|
<span className="block text-xs text-gray-500">Leave unticked for small fixes (wording, typos, contact details).</span>
|
|
</span>
|
|
</label>
|
|
<div className="flex gap-2">
|
|
<button type="button" onClick={() => setPublishing(false)} className="px-3 py-1.5 rounded-lg bg-gray-100 text-sm hover:bg-gray-200">Cancel</button>
|
|
<button type="submit" disabled={busy || (requiresAcceptance && !changeSummary.trim())} className="px-3 py-1.5 rounded-lg bg-brand-600 text-white text-sm font-medium hover:bg-brand-700 disabled:opacity-50">
|
|
{busy ? "Publishing…" : "Publish"}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
)}
|
|
|
|
{preview ? (
|
|
<div className="border rounded-xl p-6 bg-white text-gray-700 leading-relaxed">
|
|
{dirty && <p className="mb-4 text-xs text-amber-700">Showing the last saved draft — save to preview your latest edits.</p>}
|
|
{draft.rendered.map((s, i) => <PrivacyNoticeSection key={s.id} section={s} first={i === 0} />)}
|
|
</div>
|
|
) : (
|
|
<div className="space-y-3">
|
|
{sections.map((s, i) => {
|
|
const original = builtInById.get(s.id);
|
|
return (
|
|
<div key={`${s.id}-${i}`} className="border rounded-xl p-3 bg-white">
|
|
<div className="flex items-center gap-2 mb-2">
|
|
<input className={`${inputCls} font-medium`} value={s.title} onChange={e => update(i, { title: e.target.value })} aria-label="Section title" />
|
|
<button onClick={() => move(i, -1)} disabled={i === 0} className="p-1.5 rounded hover:bg-gray-100 disabled:opacity-30" aria-label="Move up"><ArrowUp className="w-4 h-4" /></button>
|
|
<button onClick={() => move(i, 1)} disabled={i === sections.length - 1} className="p-1.5 rounded hover:bg-gray-100 disabled:opacity-30" aria-label="Move down"><ArrowDown className="w-4 h-4" /></button>
|
|
{original && (original.body !== s.body || original.title !== s.title) && (
|
|
<button onClick={() => update(i, { title: original.title, body: original.body })} className="p-1.5 rounded hover:bg-gray-100" title="Restore the built-in text" aria-label="Restore the built-in text"><RotateCcw className="w-4 h-4" /></button>
|
|
)}
|
|
<button onClick={() => confirm(`Remove "${s.title}"?`) && change(sections.filter((_, j) => j !== i))} className="p-1.5 rounded hover:bg-red-50 text-red-600" aria-label="Remove section"><Trash2 className="w-4 h-4" /></button>
|
|
</div>
|
|
<textarea className={`${inputCls} font-mono text-xs`} rows={Math.min(18, Math.max(3, s.body.split("\n").length + 1))} value={s.body} onChange={e => update(i, { body: e.target.value })} aria-label={`${s.title} text`} />
|
|
</div>
|
|
);
|
|
})}
|
|
<button onClick={() => change([...sections, { id: `section-${sections.length + 1}`, title: "New section", body: "" }])} className="inline-flex items-center gap-1.5 px-3 py-1.5 rounded-lg border bg-white text-sm hover:bg-gray-50">
|
|
<Plus className="w-4 h-4" /> Add section
|
|
</button>
|
|
</div>
|
|
)}
|
|
|
|
{draft.versions.length > 0 && (
|
|
<div>
|
|
<h3 className="text-sm font-semibold text-gray-900 mb-2">Published versions</h3>
|
|
<div className="overflow-x-auto border rounded-xl bg-white">
|
|
<table className="w-full text-sm">
|
|
<thead className="bg-gray-50 text-left text-xs text-gray-500">
|
|
<tr><th className="p-2">Version</th><th className="p-2">Published</th><th className="p-2">What changed</th><th className="p-2">Re-accept</th><th className="p-2 text-right">Accepted by</th></tr>
|
|
</thead>
|
|
<tbody>
|
|
{draft.versions.map(v => (
|
|
<tr key={v.version} className="border-t">
|
|
<td className="p-2"><Link href={`/legal/privacy?version=${v.version}`} target="_blank" className="text-brand-600 hover:underline">{v.version}</Link></td>
|
|
<td className="p-2 whitespace-nowrap">{fmt(v.publishedAt)}{v.publishedBy ? ` · ${v.publishedBy}` : ""}</td>
|
|
<td className="p-2 text-gray-600">{v.changeSummary || "—"}</td>
|
|
<td className="p-2">{v.requiresAcceptance ? "Yes" : "No"}</td>
|
|
<td className="p-2 text-right">{v.acceptances}</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|