Слияние частей A и B этапа 2 (разрешены конфликты: задания, MCP-инструменты, формат ответа категоризации)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-23 16:55:16 +03:00
parent a5bf9bf71e
commit fa83bfbc2f
24 changed files with 1818 additions and 30 deletions

View File

@@ -0,0 +1,66 @@
-- Этап 2 бухгалтерской автоматизации: «Чек по карте от сотрудника» и «Поиск чека».
-- Только добавляющая миграция (if not exists), ничего не удаляется. Описание: docs/proposed/data_objects/accounting_automation/.
-- Данные операции ленты QuickBooks (сумма, вендор, дата, карта): в ленту сервис напрямую не ходит, их присылает агент категоризации.
create table if not exists acc_feed_txn (
feed_txn_id text primary key,
txn_date date,
amount numeric(14,2), -- положительная сумма списания
vendor_name text,
card_or_account text, -- последние 4 цифры карты или название счёта
description text,
created_at timestamptz not null default now()
);
-- Чеки: откуда пришли (для ответа в ту же ветку Slack), ожидается ли ответ про проект.
alter table acc_receipts add column if not exists slack_channel text;
alter table acc_receipts add column if not exists slack_ts text;
alter table acc_receipts add column if not exists project_question_at timestamptz;
alter table acc_receipts add column if not exists card_last4 text;
create unique index if not exists acc_receipts_slack_ref_uq on acc_receipts (slack_ref) where slack_ref is not null;
create index if not exists acc_receipts_feed_idx on acc_receipts (feed_txn_id);
-- Поиск чека: данные операции, ход вопросов, согласие Анастасии провести без чека. Ответственный может быть неизвестен (тогда задача Юле).
alter table acc_receipt_search alter column responsible_person_id drop not null;
alter table acc_receipt_search add column if not exists created_at timestamptz not null default now();
alter table acc_receipt_search add column if not exists txn_date date;
alter table acc_receipt_search add column if not exists amount numeric(14,2);
alter table acc_receipt_search add column if not exists vendor_name text;
alter table acc_receipt_search add column if not exists card_or_account text;
alter table acc_receipt_search add column if not exists project_customer_id text;
alter table acc_receipt_search add column if not exists asks jsonb not null default '[]'; -- [{channel, ts, person_id, level}] личные сообщения с вопросом
alter table acc_receipt_search add column if not exists approved_without_receipt_by text;
alter table acc_receipt_search add column if not exists approved_without_receipt_at timestamptz;
alter table acc_receipt_search add column if not exists approved_reason text;
create unique index if not exists acc_receipt_search_feed_uq on acc_receipt_search (feed_txn_id);
-- Сообщения Slack, уже разобранные (одно сообщение - одна строка, даже если чека в нём нет).
create table if not exists acc_slack_processed (
slack_ref text primary key, -- канал:метка времени
processed_at timestamptz not null default now(),
result text not null, -- receipt, answer, question, ignored, error
details jsonb
);
alter table acc_feed_txn enable row level security;
alter table acc_slack_processed enable row level security;
-- Чтение ролью khb_agent_acc (если роль есть): как в миграции 038.
do $$
declare
t text;
r text := 'khb_agent_acc';
begin
if not exists (select 1 from pg_roles where rolname = r) then
return;
end if;
begin
foreach t in array array['acc_feed_txn', 'acc_receipt_search'] loop
execute format('grant select on %I to %I', t, r);
execute format('drop policy if exists %I on %I', r || '_read', t);
execute format('create policy %I on %I for select to %I using (true)', r || '_read', t, r);
end loop;
exception when insufficient_privilege then
raise notice 'нет прав выдать чтение роли % (выдать вручную администратором БД)', r;
end;
end $$;