Зеркало QuickBooks qbo_* (миграция 032) и загрузка POST /sync в qbo_auth, задание scheduler qbo_sync (п.85)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import Fastify, { type FastifyInstance } from "fastify";
|
||||
import { QboAuth, ReauthNeeded } from "./service.js";
|
||||
import type { QboSync } from "./sync.js";
|
||||
|
||||
const page = (title: string, text: string) =>
|
||||
`<!doctype html><html lang="ru"><meta charset="utf-8"><meta name="robots" content="noindex"><title>${title}</title><body style="font-family:system-ui;max-width:32rem;margin:4rem auto;padding:0 1rem"><h1>${title}</h1><p>${text}</p></body></html>`;
|
||||
@@ -8,7 +9,7 @@ const page = (title: string, text: string) =>
|
||||
* Служебные ручки закрыты токеном (settings.json5 `qbo_auth.http.token`); открыты /healthz и /callback.
|
||||
* /callback защищён одноразовым state; наружу (nginx services) публикуется только он: /api/qbo/callback.
|
||||
*/
|
||||
export function buildApi(auth: QboAuth, token: string): FastifyInstance {
|
||||
export function buildApi(auth: QboAuth, token: string, sync?: QboSync): FastifyInstance {
|
||||
const app = Fastify();
|
||||
app.addHook("onRequest", async (req, reply) => {
|
||||
const path = req.url.split("?")[0];
|
||||
@@ -44,6 +45,19 @@ export function buildApi(auth: QboAuth, token: string): FastifyInstance {
|
||||
return reply.code(502).send({ error: "token_error" });
|
||||
}
|
||||
});
|
||||
/** Полная загрузка зеркала QuickBooks в таблицы qbo_* (вызывает scheduler и ручной запуск). Ждёт итога: 200 ok, 502 ошибка, 409 уже идёт. */
|
||||
app.post("/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();
|
||||
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");
|
||||
return reply.code(502).send({ error: "sync_failed" });
|
||||
}
|
||||
});
|
||||
app.post("/keepalive", async () => ({ result: await auth.keepAlive() }));
|
||||
return app;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user