Files
MikrotikManager/backend/src/routes/manual-data-collection.ts
T

25 lines
1.3 KiB
TypeScript

import type { FastifyPluginAsyncZod } from "@fastify/type-provider-zod"
import type { ManualDataCollection } from "../services/manual-data-collection-execution.js"
export function createServersManualCollectionRoute(operation: ManualDataCollection["collectServersRestPing"]): FastifyPluginAsyncZod {
return async app => {
app.post("/servers-api-ping/collect-now", async (_req, reply) => reply.send(await operation()))
}
}
export function createUptimeManualCollectionRoutes(operations: Pick<ManualDataCollection, "collectUptime" | "collectPingGroup">): FastifyPluginAsyncZod {
return async app => {
app.post("/uptime/collect-now", async (_req, reply) => reply.send(await operations.collectUptime()))
app.post("/uptime/probes/collect-group", async (req, reply) => {
const raw = (req.body as { probeIds?: unknown } | undefined)?.probeIds
const probeIds = Array.isArray(raw) ? raw.filter((x): x is string => typeof x === "string") : []
try { return reply.send(await operations.collectPingGroup(probeIds)) }
catch (error) {
if ((error as Error & { statusCode?: number }).statusCode === 409) {
return reply.status(409).send({ error: error instanceof Error ? error.message : "busy" })
}
throw error
}
})
}
}