acc_invoices: журнал изменений (триггеры, автор из WebUI, панель История, API log)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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="Контракт">
|
||||
|
||||
@@ -41,3 +41,5 @@ export async function api<T = unknown>(path: string, init: { method?: string; bo
|
||||
if (!r.ok) throw new ApiError(data.error ?? `ошибка ${r.status}`, r.status, data.problems ?? data.issues);
|
||||
return data as T;
|
||||
}
|
||||
|
||||
export interface LogItem { id: number; at: string; tbl: string; op: string; actor_name: string | null; acting_role: string | null; action: string | null; db_user: string; changes: Record<string, unknown> }
|
||||
|
||||
@@ -108,3 +108,4 @@ body { margin: 0; background: var(--bg); color: var(--text); font: 14px/1.4 syst
|
||||
.panel-links { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
|
||||
.field.wide { margin-top: 10px; }
|
||||
.desc { margin: 0 0 8px; white-space: pre-wrap; }
|
||||
.history { margin-top: 12px; font-size: 13px; } .history ul { list-style: none; padding: 0; margin: 6px 0; } .history li { padding: 4px 0; border-bottom: 1px solid var(--line); } .history .chg { color: var(--text); word-break: break-word; } .history .muted { color: var(--muted); }
|
||||
|
||||
Reference in New Issue
Block a user