Письма подрядчикам: отбор адресата, khb-mail, журнал contractor_email, правило в промте (п.82)
Some checks failed
ci / test (push) Successful in 35s
ci / deploy (push) Has been cancelled

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-10 22:57:28 +03:00
parent 7237edadbe
commit 2987e8f139
16 changed files with 650 additions and 2 deletions

View File

@@ -4,6 +4,8 @@ import type { Pool } from "khb_common";
import { registerAgentRoutes } from "./agents-api.js";
import { usageSummary, type UsageLog } from "./usage.js";
import type { PrepJob, PrepResult } from "./jobs.js";
import { checkMailAuth } from "./mail/auth.js";
import { resolveForManager, sendForManager, type MailDeps } from "./mail/service.js";
export interface ChatApi {
/** Имитация входящего сообщения менеджера (без Slack): текст и локальные файлы; publish - слать ли ответ в Slack. */
@@ -20,6 +22,8 @@ export interface PrepApiDeps {
run(job: PrepJob, opts: { projectId?: string; dryRun?: boolean }): Promise<PrepResult>;
chat?: ChatApi;
usageLog?: UsageLog;
/** Письма подрядчикам (команда khb-mail): токен менеджера, не общий токен сервиса. */
mail?: { deps: MailDeps; secret: string };
}
interface RunState {
@@ -37,11 +41,24 @@ export function buildApi(deps: PrepApiDeps): FastifyInstance {
const runs = new Map<string, RunState>(); // состояние последних запусков в памяти; постоянный след - ap_*, sched_run
app.addHook("onRequest", async (req, reply) => {
if (req.url === "/healthz") return;
if (req.url.startsWith("/mail/")) {
if (!deps.mail || !checkMailAuth(deps.mail.secret, String(req.headers["x-khb-caller"] ?? ""), req.headers.authorization)) return reply.code(401).send({ error: "unauthorized" });
return;
}
if (req.headers.authorization !== `Bearer ${deps.token}`) return reply.code(401).send({ error: "unauthorized" });
});
registerAgentRoutes(app, deps.pool);
if (deps.mail) {
const mail = deps.mail.deps;
const caller = (req: { headers: Record<string, unknown> }) => String(req.headers["x-khb-caller"]);
app.post<{ Body: { project?: string; vendor?: string; work?: string } }>("/mail/resolve", async (req) =>
resolveForManager(mail, { managerId: caller(req), project: req.body?.project, vendor: req.body?.vendor, work: req.body?.work }));
app.post<{ Body: { project?: string; vendor?: string; work?: string; date?: string; note?: string; channel?: string } }>("/mail/send", async (req) =>
sendForManager(mail, { managerId: caller(req), project: req.body?.project, vendor: req.body?.vendor, work: req.body?.work, date: String(req.body?.date ?? ""), note: req.body?.note, channel: req.body?.channel }));
}
app.get("/healthz", async () => {
let db = false;
try {