|
| 1 | +import { type Static, Type } from "@sinclair/typebox"; |
| 2 | +import type { FastifyInstance } from "fastify"; |
| 3 | +import { StatusCodes } from "http-status-codes"; |
| 4 | +import { TransactionDB } from "../../../shared/db/transactions/db"; |
| 5 | +import { standardResponseSchema } from "../../schemas/shared-api-schemas"; |
| 6 | + |
| 7 | +const requestBodySchema = Type.Object({ |
| 8 | + entries: Type.Array( |
| 9 | + Type.Object({ |
| 10 | + queueId: Type.String({ description: "Queue ID (UUID)" }), |
| 11 | + transactionHash: Type.String({ description: "Transaction hash (0x...)" }), |
| 12 | + }), |
| 13 | + { description: "Array of queueId to transactionHash mappings", maxItems: 10000 }, |
| 14 | + ), |
| 15 | +}); |
| 16 | + |
| 17 | +const responseBodySchema = Type.Object({ |
| 18 | + result: Type.Object({ |
| 19 | + inserted: Type.Integer({ description: "Number of entries inserted" }), |
| 20 | + skipped: Type.Integer({ |
| 21 | + description: "Number of entries skipped (already exist)", |
| 22 | + }), |
| 23 | + }), |
| 24 | +}); |
| 25 | + |
| 26 | +export async function loadBackfillRoute(fastify: FastifyInstance) { |
| 27 | + fastify.route<{ |
| 28 | + Body: Static<typeof requestBodySchema>; |
| 29 | + Reply: Static<typeof responseBodySchema>; |
| 30 | + }>({ |
| 31 | + method: "POST", |
| 32 | + url: "/admin/backfill", |
| 33 | + schema: { |
| 34 | + summary: "Load backfill entries", |
| 35 | + description: |
| 36 | + "Load queueId to transactionHash mappings into the backfill table. Uses SETNX to never overwrite existing entries.", |
| 37 | + tags: ["Admin"], |
| 38 | + operationId: "loadBackfill", |
| 39 | + body: requestBodySchema, |
| 40 | + response: { |
| 41 | + ...standardResponseSchema, |
| 42 | + [StatusCodes.OK]: responseBodySchema, |
| 43 | + }, |
| 44 | + hide: true, |
| 45 | + }, |
| 46 | + handler: async (request, reply) => { |
| 47 | + const { entries } = request.body; |
| 48 | + |
| 49 | + const { inserted, skipped } = |
| 50 | + await TransactionDB.bulkSetBackfill(entries); |
| 51 | + |
| 52 | + reply.status(StatusCodes.OK).send({ |
| 53 | + result: { inserted, skipped }, |
| 54 | + }); |
| 55 | + }, |
| 56 | + }); |
| 57 | +} |
0 commit comments