acc_invoices: журнал изменений (триггеры, автор из WebUI, панель История, API log)
Some checks failed
ci / test (push) Failing after 3m42s
ci / deploy (push) Has been skipped

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-04 20:18:26 +03:00
parent dd2b8f6df3
commit 76eafc995a
8 changed files with 151 additions and 5 deletions

View File

@@ -1,6 +1,6 @@
import * as Ariakit from "@ariakit/react";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { ApiError, api, getActing, getToken, setActing, type Detail, type Line, type Lookups, type Me, type Row } from "./api.js";
import { ApiError, api, getActing, getToken, setActing, type Detail, type Line, type LogItem, type Lookups, type Me, type Row } from "./api.js";
import { FormDialog } from "./FormDialog.js";
import { Login } from "./Login.js";
import { finishLogin, logout, relogin } from "./oidc.js";
@@ -19,6 +19,7 @@ function LinkBtn({ href, label, off }: { href?: string | null; label: string; of
: <button className="btn small" disabled title={off}>{label}</button>;
}
const isOkText = (v: string): boolean => v.trim().toLowerCase().replace(/о/g, "o").replace(/к/g, "k") === "ok";
const fmtVal = (v: unknown): string => (v === null || v === undefined ? "∅" : typeof v === "object" ? JSON.stringify(v).slice(0, 60) : String(v).slice(0, 80));
const hashFilter = (): string => new URLSearchParams(location.hash.slice(1)).get("f") ?? "to_pay";
export function App() {
@@ -51,6 +52,7 @@ function Main({ me, onLogout }: { me: Me; onLogout: () => void }) {
const [list, setList] = useState<ListResp>({ items: [], counts: {} });
const [selected, setSelected] = useState<string | null>(null);
const [detail, setDetail] = useState<Detail | null>(null);
const [history, setHistory] = useState<LogItem[] | null>(null);
const [lookups, setLookups] = useState<Lookups | null>(null);
const [toast, setToast] = useState<{ text: string; bad?: boolean } | null>(null);
const [formRow, setFormRow] = useState<Row | null>(null);
@@ -83,6 +85,7 @@ function Main({ me, onLogout }: { me: Me; onLogout: () => void }) {
const loadDetail = useCallback(async (id: string | null) => {
if (!id) { setDetail(null); return; }
try { setDetail(await api<Detail>(`invoices/${id}`)); } catch { setDetail(null); }
if (me.is_staff) { try { setHistory((await api<{ items: LogItem[] }>(`invoices/${id}/log`)).items); } catch { setHistory(null); } } else setHistory(null);
}, []);
useEffect(() => { const t = setTimeout(loadList, q ? 250 : 0); return () => clearTimeout(t); }, [loadList, q]);
@@ -257,6 +260,22 @@ function Main({ me, onLogout }: { me: Me; onLogout: () => void }) {
</ul>
</>
)}
{me.is_staff && history && history.length > 0 && (
<details className="history">
<summary>История изменений ({history.length})</summary>
<ul>
{history.map((h) => (
<li key={h.id}>
<b>{new Date(h.at).toLocaleString("ru-RU")}</b> {h.actor_name ?? `система (${h.db_user})`}{h.acting_role ? ` [${h.acting_role}]` : ""}
<span className="muted"> {h.action ?? ""} · {h.tbl.replace("acc_invoices_", "")} {h.op}</span>
<div className="chg">{h.op === "UPDATE"
? Object.entries(h.changes as Record<string, { from: unknown; to: unknown }>).map(([k, v]) => `${k}: ${fmtVal(v.from)} → ${fmtVal(v.to)}`).join("; ")
: Object.entries(h.changes).filter(([k]) => !["id", "invoice_id", "conclusion", "lines"].includes(k)).map(([k, v]) => `${k}=${fmtVal(v)}`).join("; ")}</div>
</li>
))}
</ul>
</details>
)}
</section>
<section className="panel" aria-label="Контракт">