2025-08-04 17:45:44 +02:00
|
|
|
import { TRPCError } from "@trpc/server";
|
2025-08-07 14:50:40 +02:00
|
|
|
import { z } from "zod/v4";
|
2026-03-08 01:02:57 +01:00
|
|
|
import type OrderTypeEnum from "~/schemas/public/OrderTypeEnum";
|
2025-08-04 17:45:44 +02:00
|
|
|
import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc";
|
2026-03-08 01:02:57 +01:00
|
|
|
import {
|
|
|
|
|
getPagamentoDataForCheckout,
|
|
|
|
|
preparePayment,
|
|
|
|
|
} from "~/server/controllers/pagamenti.controller";
|
|
|
|
|
|
2025-08-04 17:45:44 +02:00
|
|
|
import { db } from "~/server/db";
|
2026-03-08 01:02:57 +01:00
|
|
|
import { genPdfRicevutaCortesia } from "~/server/services/puppeteer.service";
|
|
|
|
|
import { zOrdineId, zServizioId } from "~/server/utils/zod_types";
|
2025-08-04 17:45:44 +02:00
|
|
|
// 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({
|
2025-08-28 18:27:07 +02:00
|
|
|
getPaymentFromIntent: protectedProcedure
|
|
|
|
|
.input(z.object({ pIntentId: z.string() }))
|
|
|
|
|
.query(async ({ input }) => {
|
|
|
|
|
try {
|
2026-03-08 01:02:57 +01:00
|
|
|
const ordine = await db
|
|
|
|
|
.selectFrom("ordini")
|
|
|
|
|
.selectAll("ordini")
|
|
|
|
|
.where("ordini.intent_id", "=", input.pIntentId)
|
|
|
|
|
.executeTakeFirst();
|
|
|
|
|
return ordine || null;
|
2025-08-28 18:27:07 +02:00
|
|
|
} catch (error) {
|
|
|
|
|
throw new TRPCError({
|
|
|
|
|
code: "INTERNAL_SERVER_ERROR",
|
2026-03-08 01:02:57 +01:00
|
|
|
message: `Errore durante il recupero del pagamento: ${(error as Error).message}`,
|
2025-08-28 18:27:07 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}),
|
2026-03-08 01:02:57 +01:00
|
|
|
preparePayment: protectedProcedure
|
|
|
|
|
.input(
|
|
|
|
|
z.object({
|
|
|
|
|
servizioId: zServizioId,
|
|
|
|
|
type: z.custom<OrderTypeEnum>(),
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
.query(async ({ input }) => {
|
|
|
|
|
return await preparePayment(input.servizioId, input.type);
|
|
|
|
|
}),
|
2026-02-20 11:36:47 +01:00
|
|
|
|
|
|
|
|
getPagamentoDataForCheckout: protectedProcedure
|
2026-03-08 01:02:57 +01:00
|
|
|
.input(z.object({ ordineId: zOrdineId }))
|
2026-02-20 11:36:47 +01:00
|
|
|
.query(async ({ input }) => {
|
2026-03-08 01:02:57 +01:00
|
|
|
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}`,
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-02-20 11:36:47 +01:00
|
|
|
}),
|
2025-08-04 17:45:44 +02:00
|
|
|
});
|