Files
khb_microservices/packages/khb_common/migrations/085_acc_invoices_log.sql
Yuri Yaryshev 76eafc995a
Some checks failed
ci / test (push) Failing after 3m42s
ci / deploy (push) Has been skipped
acc_invoices: журнал изменений (триггеры, автор из WebUI, панель История, API log)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-12-04 20:18:26 +03:00

46 lines
3.3 KiB
PL/PgSQL

-- acc_invoices: журнал изменений (кто, когда, что и через какое действие поменял), пишется триггерами, поэтому ловит и UI, и задания, и ручные правки в БД
create table if not exists acc_invoices_log (
id bigserial primary key,
at timestamptz not null default now(),
invoice_id uuid not null, -- без внешнего ключа: журнал переживает удаление инвойса
tbl text not null, -- acc_invoices | acc_invoices_details | acc_invoices_approves
op text not null, -- INSERT | UPDATE | DELETE
row_id uuid,
actor_id uuid, -- person.id пользователя WebUI; пусто - не через WebUI
actor_name text,
acting_role text, -- роль, под которой работал (переключатель CFO/accountant/employee)
action text, -- действие WebUI, например "POST /api/invoices/:id/sent-to-bank"
db_user text not null default current_user,
changes jsonb not null -- INSERT/DELETE: значения полей; UPDATE: {поле: {from, to}}
);
create index if not exists acc_invoices_log_inv_idx on acc_invoices_log (invoice_id, at);
alter table acc_invoices_log enable row level security;
create or replace function acc_invoices_audit() returns trigger language plpgsql as $$
declare
o jsonb; n jsonb; ch jsonb; inv uuid; rid uuid;
begin
if tg_op = 'INSERT' then n := to_jsonb(new); ch := jsonb_strip_nulls(n);
elsif tg_op = 'DELETE' then o := to_jsonb(old); ch := jsonb_strip_nulls(o);
else
o := to_jsonb(old); n := to_jsonb(new);
select coalesce(jsonb_object_agg(k, jsonb_build_object('from', o -> k, 'to', n -> k)), '{}'::jsonb) into ch
from jsonb_object_keys(n) k where k <> 'updated_at' and (o -> k) is distinct from (n -> k);
if ch = '{}'::jsonb then return null; end if;
end if;
rid := coalesce(n ->> 'id', o ->> 'id')::uuid;
inv := case when tg_table_name = 'acc_invoices' then rid else coalesce(n ->> 'invoice_id', o ->> 'invoice_id')::uuid end;
insert into acc_invoices_log (invoice_id, tbl, op, row_id, actor_id, actor_name, acting_role, action, changes)
values (inv, tg_table_name, tg_op, rid,
nullif(current_setting('acc.actor_id', true), '')::uuid, nullif(current_setting('acc.actor_name', true), ''),
nullif(current_setting('acc.acting_role', true), ''), nullif(current_setting('acc.action', true), ''), ch);
return null;
end $$;
drop trigger if exists acc_invoices_audit_t on acc_invoices;
create trigger acc_invoices_audit_t after insert or update or delete on acc_invoices for each row execute function acc_invoices_audit();
drop trigger if exists acc_invoices_details_audit_t on acc_invoices_details;
create trigger acc_invoices_details_audit_t after insert or update or delete on acc_invoices_details for each row execute function acc_invoices_audit();
drop trigger if exists acc_invoices_approves_audit_t on acc_invoices_approves;
create trigger acc_invoices_approves_audit_t after insert or update or delete on acc_invoices_approves for each row execute function acc_invoices_audit();