acc_invoices: журнал изменений (триггеры, автор из WebUI, панель История, API log)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,7 @@ import {
|
||||
QUICK_FILTERS, STATUS_LABELS, canCancel, canMarkPaid, canReturnToDraft, canSendToBank, canView, dayInTz, editableFields, errorsOf, isStaff, matchesFilter, normResult, statusOf, submitProblems,
|
||||
type QuickFilter,
|
||||
} from "./rules.js";
|
||||
import { auditStore, withAudit } from "./audit.js";
|
||||
import type { AccInvoicesSettings } from "./settings.js";
|
||||
|
||||
export interface ApiDeps {
|
||||
@@ -65,10 +66,12 @@ const DETAIL = z.object({
|
||||
});
|
||||
|
||||
export function buildApi(deps: ApiDeps): FastifyInstance {
|
||||
const { pool, settings } = deps;
|
||||
const { settings } = deps;
|
||||
const rawPool = deps.pool;
|
||||
const pool = withAudit(rawPool);
|
||||
const now = deps.now ?? (() => new Date());
|
||||
const app = Fastify({ logger: false, bodyLimit: 1_000_000 });
|
||||
const authenticate = createAuth(pool, settings, deps.verifyJwt);
|
||||
const authenticate = createAuth(rawPool, settings, deps.verifyJwt);
|
||||
const ctx = () => ({ today: dayInTz(now(), settings.tz), now: now(), bankConfirmHours: settings.bankConfirmHours });
|
||||
|
||||
const decorate = (r: ListItem, actor: Actor) => {
|
||||
@@ -94,7 +97,14 @@ export function buildApi(deps: ApiDeps): FastifyInstance {
|
||||
return reply.code(500).send({ error: "внутренняя ошибка" });
|
||||
});
|
||||
|
||||
const need = async (req: FastifyRequest): Promise<Actor> => authenticate(req.headers.authorization, typeof req.headers["x-acting-role"] === "string" ? req.headers["x-acting-role"] : undefined);
|
||||
// контекст автора для журнала: в каждом запросе свой, заполняется после входа
|
||||
app.addHook("onRequest", (_req, _reply, done) => { auditStore.run({}, done); });
|
||||
const need = async (req: FastifyRequest): Promise<Actor> => {
|
||||
const a = await authenticate(req.headers.authorization, typeof req.headers["x-acting-role"] === "string" ? req.headers["x-acting-role"] : undefined);
|
||||
const st = auditStore.getStore();
|
||||
if (st) Object.assign(st, { actorId: a.personId, actorName: a.displayName, actingRole: a.acting, action: `${req.method} ${req.routeOptions?.url ?? req.url}` });
|
||||
return a;
|
||||
};
|
||||
const fail = (reply: FastifyReply, status: number, error: string) => reply.code(status).send({ error });
|
||||
|
||||
/** Загружает инвойс и проверяет, что он виден этому человеку (чужой выглядит как несуществующий). */
|
||||
@@ -151,6 +161,16 @@ export function buildApi(deps: ApiDeps): FastifyInstance {
|
||||
};
|
||||
});
|
||||
|
||||
/** Журнал изменений инвойса (только бухгалтерия): кто, когда, какое действие и что изменилось. */
|
||||
app.get<{ Params: { id: string } }>("/api/invoices/:id/log", async (req, reply) => {
|
||||
const a = await need(req);
|
||||
if (!isStaff(a)) return fail(reply, 403, "журнал доступен бухгалтерии");
|
||||
if (!uuid.safeParse(req.params.id).success) return fail(reply, 400, "неверный id");
|
||||
const rows = (await pool.query(
|
||||
`select id, at, tbl, op, row_id, actor_name, acting_role, action, db_user, changes from acc_invoices_log where invoice_id = $1 order by id`, [req.params.id])).rows;
|
||||
return { items: rows };
|
||||
});
|
||||
|
||||
/* ------------------------------------------------------------- создание */
|
||||
app.post("/api/invoices", async (req, reply) => {
|
||||
const a = await need(req);
|
||||
|
||||
Reference in New Issue
Block a user