Очередь: столбец «списано с» и ссылка на QuickBooks, ответ на проблему и contract_basis (миграция 077), инструкции и процессы; review-done-action.md
All checks were successful
ci / test (push) Successful in 1m7s
ci / deploy (push) Successful in 3m16s

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-30 17:47:01 +03:00
parent 09f6f81038
commit 1a7515dfbc
9 changed files with 163 additions and 16 deletions

View File

@@ -22,7 +22,7 @@ export class McpToolError extends Error {}
export interface ReviewDeps {
pool: Pool;
settings: Pick<AccountingSettings, "reviewers">;
settings: Pick<AccountingSettings, "reviewers"> & { receipts?: { qboBankingUrl?: string } };
/** Лимит расхода без чека (USD), по умолчанию 500. */
noReceiptLimitUsd?: number;
now?: () => Date;
@@ -71,6 +71,11 @@ const confidenceF = (v: unknown): unknown => {
if (typeof v !== "number" || !Number.isInteger(v) || v < 0 || v > 100) throw new McpToolError("ожидалось целое 0..100");
return v;
};
const contractBasisF = (v: unknown): unknown => {
if (isNull(v)) return null;
if (v !== "contract" && v !== "invoice" && v !== "none") throw new McpToolError("contract_basis: contract, invoice или none");
return v;
};
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
const problem: FieldSpec = { cast: "text", check: text(true) };
@@ -90,6 +95,10 @@ export const KINDS: Record<string, KindMeta> = {
jt_job_id: { cast: "text", check: text(true) },
contract_id: { cast: "uuid", check: uuidF },
problem_highlight: problem,
/** Ответ человека на проблему: по нему агент решает, что делать дальше. */
answer: { cast: "text", check: text(true) },
/** Основание вместо договора: contract - договор, invoice - сам инвойс (доп. заказ, change order), none - без основания. */
contract_basis: { cast: "text", check: contractBasisF },
},
},
categorization: {
@@ -232,7 +241,7 @@ export class ReviewTools {
const feedIds = rows.filter((r) => r.kind === "categorization").map((r) => String(r.ref_id));
const feed = new Map<string, Json>();
if (feedIds.length) {
const f = await this.d.pool.query("select feed_txn_id, txn_date, amount, vendor_name, description from acc_feed_txn where feed_txn_id = any($1)", [feedIds]);
const f = await this.d.pool.query("select feed_txn_id, txn_date, amount, vendor_name, description, card_or_account from acc_feed_txn where feed_txn_id = any($1)", [feedIds]);
for (const x of f.rows) feed.set(String(x.feed_txn_id), x as Json);
}
const sugg = (r: { row: unknown }): Json => ((r.row as Json | null)?.suggestion ?? {}) as Json;
@@ -242,6 +251,7 @@ export class ReviewTools {
};
const projectIds = [...new Set(rows.map(projectOf).filter((x): x is string => !!x))];
const accountIds = [...new Set(rows.filter((r) => r.kind === "categorization").map((r) => sugg(r).account).filter((x) => x !== null && x !== undefined && x !== "").map(String))];
const bankIds = [...new Set(rows.filter((r) => r.kind === "categorization").map((r) => sugg(r).bank_account_id).filter((x) => x !== null && x !== undefined && x !== "").map(String))];
const projects = new Map<string, string>();
if (projectIds.length) {
const p = await this.d.pool.query(
@@ -259,13 +269,32 @@ export class ReviewTools {
const a = await this.d.pool.query("select id, coalesce(fully_qualified_name, name) as n from qbo_account where id = any($1)", [accountIds]);
for (const x of a.rows) accounts.set(String(x.id), String(x.n));
}
// счёт или карта списания: по id из предложения, иначе по последним 4 цифрам из ленты (в названии счёта QuickBooks они есть)
const banks = new Map<string, string>();
if (bankIds.length) {
const b = await this.d.pool.query("select id, coalesce(fully_qualified_name, name) as n from qbo_account where id = any($1)", [bankIds]);
for (const x of b.rows) banks.set(String(x.id), String(x.n));
}
const last4 = new Map<string, string>();
for (const f of feed.values()) {
const d = String(f.card_or_account ?? "").replace(/D/g, "").slice(-4);
if (d.length === 4 && !last4.has(d)) {
const m = await this.d.pool.query("select coalesce(fully_qualified_name, name) as n from qbo_account where account_type in ('Credit Card','Bank') and active and name like $1 limit 2", [`%${d}%`]);
if (m.rows.length === 1) last4.set(d, String(m.rows[0].n));
}
}
const bankingUrl = this.d.settings.receipts?.qboBankingUrl ?? "https://app.qbo.intuit.com/app/banking";
for (const r of rows) {
const pid = projectOf(r);
const acc = r.kind === "categorization" ? sugg(r).account : null;
const bid = r.kind === "categorization" && sugg(r).bank_account_id ? String(sugg(r).bank_account_id) : null;
const f4 = r.kind === "categorization" ? String(feed.get(String(r.ref_id))?.card_or_account ?? "").replace(/D/g, "").slice(-4) : "";
out.set(String(r.id), {
feed: r.kind === "categorization" ? (feed.get(String(r.ref_id)) ?? null) : null,
project: pid ? (projects.get(pid) ?? null) : null,
account: acc ? (accounts.get(String(acc)) ?? null) : null,
bank_account: bid ? (banks.get(bid) ?? null) : (last4.get(f4) ?? null),
qbo_url: r.kind === "categorization" ? bankingUrl.split("{feed_txn_id}").join(encodeURIComponent(String(r.ref_id))) : null,
});
}
return out;