Слияние части C этапа 2 (сертификаты, сверка, история, chargeback, fraud)

This commit is contained in:
2026-11-23 16:58:46 +03:00
32 changed files with 2620 additions and 38 deletions

View File

@@ -1,6 +1,8 @@
import { readFileSync } from "node:fs";
import { createLogger, createPool, itNotify, loadServiceSettings } from "khb_common";
import { runCategorization } from "./categorize.js";
import { runFraudWeekly } from "./chargeback.js";
import { manualMessenger, runCertificates } from "./certificates.js";
import { makeClaudeRunner } from "./claude-runner.js";
import { DriveClient } from "./drive.js";
import { runFinalize } from "./finalize.js";
@@ -9,6 +11,8 @@ import { runInvoiceCheck } from "./invoice-check.js";
import { runInvoicePaid } from "./invoice-paid.js";
import { GmailClient } from "./gmail.js";
import { GoogleAuth } from "./google.js";
import { runHistoryWeekly } from "./history-report.js";
import { makeClaudeStatementReader, runMonthlyReconcile } from "./reconcile.js";
import { buildApi } from "./http-api.js";
import { instructionsFromFile } from "./mcp.js";
import { runInvoiceIngest } from "./invoices-mail.js";
@@ -50,6 +54,12 @@ async function realmId(): Promise<string> {
const nonWorkdays = (): Promise<ReadonlySet<string>> => loadNonWorkdays(pool);
/** Куда слать отчёты Анастасии (сверка, разбор правок, fraud). */
function reportSlack(): { slack: NonNullable<typeof slack>; channelId: string } {
if (!slack || !s.slack) throw new Error("не настроен slack (botToken, accountingChannelId)");
return { slack, channelId: s.slack.reportChannelId ?? s.slack.accountingChannelId };
}
let mcpInstructions: string | undefined;
try {
mcpInstructions = instructionsFromFile(readFileSync(`${process.env.KHB_REPO_ROOT ?? process.cwd()}/docs/proposed/modules/quickbooks_bookkeeper/p-mcp-instructions.md`, "utf8"));
@@ -96,6 +106,31 @@ const app = buildApi({
"fraud-flags": async () => runFraudFlags(pool, { settings: s.fraud, realmId: await realmId() }),
"invoice-check": () => runInvoiceCheck({ pool }),
"invoice-paid": async () => runInvoicePaid({ pool, realmId: await realmId() }),
certificates: () =>
runCertificates({
pool,
log,
// реальной отправки в JobTread пока нет (data_collector только читает): письмо готовится, Юля отправляет вручную
messenger: manualMessenger,
autoSend: s.stage2.certificateAutoSend,
loadTemplate: s.stage2.certificateTemplateDocId && drive ? () => drive.exportDocText(s.stage2.certificateTemplateDocId!) : undefined,
}),
"reconcile-monthly": async () => {
if (!drive) throw new Error("не настроены google (ключ сервисного аккаунта, rootFolderId)");
return runMonthlyReconcile({
pool,
log,
...reportSlack(),
realmId,
toleranceDays: s.stage2.statementDateToleranceDays,
reader: makeClaudeStatementReader({ claude }),
source: {
list: async () => (await drive.listPdfFiles([s.stage2.statementsFolder])).map((f) => ({ id: f.id, name: f.name, getBytes: () => drive.download(f.id) })),
},
});
},
"history-weekly": () => runHistoryWeekly({ pool, ...reportSlack() }),
"fraud-weekly": () => runFraudWeekly({ pool, ...reportSlack() }),
summary: async () => {
if (!slack || !s.slack) throw new Error("не настроен slack (botToken, accountingChannelId)");
const text = await postDailySummary({ pool, slack, channelId: s.slack.accountingChannelId, tz: "Asia/Nicosia", options: { nonWorkdays: await nonWorkdays() } });