qbo_auth: страница ручных тестов /tests, разбор CloudEvents в вебхуке; список тестовых событий
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import Fastify, { type FastifyInstance } from "fastify";
|
||||
import { QboAuth, ReauthNeeded } from "./service.js";
|
||||
import type { QboSync } from "./sync.js";
|
||||
import { TESTS_PAGE } from "./tests-page.js";
|
||||
import { parseWebhook, verifyWebhook } from "./webhook.js";
|
||||
|
||||
export interface WebhookOptions {
|
||||
@@ -29,7 +30,7 @@ export function buildApi(auth: QboAuth, token: string, sync?: QboSync, webhook?:
|
||||
});
|
||||
app.addHook("onRequest", async (req, reply) => {
|
||||
const path = req.url.split("?")[0];
|
||||
if (path === "/healthz" || path === "/callback" || path === "/webhook") return;
|
||||
if (path === "/healthz" || path === "/callback" || path === "/webhook" || path === "/tests") return;
|
||||
if (req.headers.authorization !== `Bearer ${token}`) return reply.code(401).send({ error: "unauthorized" });
|
||||
});
|
||||
|
||||
@@ -43,6 +44,31 @@ export function buildApi(auth: QboAuth, token: string, sync?: QboSync, webhook?:
|
||||
return reply.code(200).send({ ok: true, events: events.length });
|
||||
});
|
||||
|
||||
/** Страница ручных тестов (форма открыта, действия /tests/* закрыты Bearer-токеном). Наружу: /api/qbo/tests. */
|
||||
app.get("/tests", async (_req, reply) => reply.type("text/html").send(TESTS_PAGE));
|
||||
app.get("/tests/status", async () => auth.status());
|
||||
app.post("/tests/connect", async () => auth.startConnect());
|
||||
app.post("/tests/disconnect", async () => auth.disconnect());
|
||||
app.post("/tests/sync", async (req, reply) => {
|
||||
if (!sync) return reply.code(501).send({ error: "sync_not_configured" });
|
||||
if (sync.isRunning) return reply.code(409).send({ error: "already_running" });
|
||||
try {
|
||||
const r = await sync.run({ full: false });
|
||||
return reply.code(r.status === "ok" ? 200 : 502).send(r);
|
||||
} catch (e) {
|
||||
if (e instanceof ReauthNeeded) return reply.code(409).send({ error: "reauth_needed", message: e.message });
|
||||
req.log.error({ err: String(e) }, "sync (tests)");
|
||||
return reply.code(502).send({ error: "sync_failed" });
|
||||
}
|
||||
});
|
||||
/** Заметка администратора о ручном тесте: пишется в журнал сервиса. */
|
||||
app.post("/tests/note", async (req, reply) => {
|
||||
const text = String((req.body as { text?: string } | undefined)?.text ?? "").slice(0, 500).trim();
|
||||
if (!text) return reply.code(400).send({ error: "empty" });
|
||||
req.log.info({ note: text }, "заметка ручного теста");
|
||||
return { ok: true };
|
||||
});
|
||||
|
||||
app.get("/healthz", async () => ({ ok: true, time: new Date().toISOString() }));
|
||||
|
||||
app.get("/callback", async (req, reply) => {
|
||||
|
||||
Reference in New Issue
Block a user