import { TRPCError } from "@trpc/server"; import { z } from "zod/v4"; import type OrderTypeEnum from "~/schemas/public/OrderTypeEnum"; import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc"; import { getPagamentoDataForCheckout, preparePayment, } from "~/server/controllers/pagamenti.controller"; import { db } from "~/server/db"; import { genPdfRicevutaCortesia } from "~/server/services/puppeteer.service"; import { zOrdineId, zServizioId } from "~/server/utils/zod_types"; // Create a new ratelimiter, that allows 10 requests per 10 seconds /* const ratelimit = new RateLimiterHandler({ maxRequests: 10, windowSize: 60, analytics: false, }); */ export const pagamentiRouter = createTRPCRouter({ getPaymentFromIntent: protectedProcedure .input(z.object({ pIntentId: z.string() })) .query(async ({ input }) => { try { const ordine = await db .selectFrom("ordini") .selectAll("ordini") .where("ordini.intent_id", "=", input.pIntentId) .executeTakeFirst(); return ordine || null; } catch (error) { throw new TRPCError({ code: "INTERNAL_SERVER_ERROR", message: `Errore durante il recupero del pagamento: ${(error as Error).message}`, }); } }), preparePayment: protectedProcedure .input( z.object({ servizioId: zServizioId, type: z.custom(), }), ) .query(async ({ input }) => { return await preparePayment(input.servizioId, input.type); }), getPagamentoDataForCheckout: protectedProcedure .input(z.object({ ordineId: zOrdineId })) .query(async ({ input }) => { return await getPagamentoDataForCheckout(input.ordineId); }), getRicevutaCortesiaPdf: protectedProcedure .input(z.object({ ordineId: zOrdineId })) .mutation(async ({ input }) => { try { return await genPdfRicevutaCortesia(input.ordineId); } catch (e) { throw new TRPCError({ code: "INTERNAL_SERVER_ERROR", message: `Errore nella generazione della ricevuta di cortesia: ${(e as Error).message}`, }); } }), });