infoalloggi-monorepo/apps/infoalloggi/src/server/api/routers/pagamenti.ts
Marco Pedone 60e4b187bf Refactor: Update Zod schemas and replace custom types with imported schemas across various files
- Replaced custom Zod types with imported schemas in stripe router and user router.
- Updated payment controller to use new enum imports for order types and payment statuses.
- Refactored payment tests to utilize the new enum structure.
- Removed deprecated zod_types file and created new zod_chat_types file for chat-related schemas.
- Adjusted service files to align with the new order type enums and schemas.
- Enhanced the Caratteristiche type definition using Zod for better validation.

Co-authored-by: Copilot <copilot@github.com>
2026-04-28 20:32:19 +02:00

132 lines
3.7 KiB
TypeScript

import { TRPCError } from "@trpc/server";
import { z } from "zod/v4";
import { orderTypeEnum } from "~/schemas/public/OrderTypeEnum";
import { ordiniOrdineId } from "~/schemas/public/Ordini";
import { rinnoviId } from "~/schemas/public/Rinnovi";
import { servizioServizioId } from "~/schemas/public/Servizio";
import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc";
import {
getPagamentoDataForCheckout,
getRicevutaData,
type PreparedPaymentData,
type PreparePaymentError,
preparePayment,
previewSaldo,
} from "~/server/controllers/pagamenti.controller";
import { db } from "~/server/db";
import { TypstGenerate } from "~/server/services/typst.service";
import { zResult } from "~/utils/result";
// 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: servizioServizioId,
type: orderTypeEnum,
}),
)
.output(zResult<PreparedPaymentData, PreparePaymentError>())
.query(async ({ input }) => {
if (input.type === orderTypeEnum.enum.Rinnovo) {
return {
ok: false,
error: {
_tag: "InvalidInput",
message: "Use preparePaymentRinnovi for Rinnovo payments",
},
};
}
return await preparePayment({
type: input.type,
servizioId: input.servizioId,
});
}),
previewSaldo: protectedProcedure
.input(
z.object({
servizioId: servizioServizioId,
}),
)
.query(async ({ input }) => {
return await previewSaldo(input.servizioId);
}),
preparePaymentRinnovi: protectedProcedure
.input(
z.object({
rinnovoId: rinnoviId,
}),
)
.output(zResult<PreparedPaymentData, PreparePaymentError>())
.query(async ({ input }) => {
return await preparePayment({
type: orderTypeEnum.enum.Rinnovo,
rinnovoId: input.rinnovoId,
});
}),
getPagamentoDataForCheckout: protectedProcedure
.input(z.object({ ordineId: ordiniOrdineId }))
.query(async ({ input }) => {
return await getPagamentoDataForCheckout(input.ordineId);
}),
getRicevutaCortesiaPdf: protectedProcedure
.input(z.object({ ordineId: ordiniOrdineId }))
.mutation(async ({ input }) => {
try {
const ricData = await getRicevutaData(input.ordineId);
const pdf = await TypstGenerate({
templateId: "receipt.typ",
data: ricData.data,
});
return { title: ricData.title, pdf: pdf.toString("base64") };
} catch (e) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `Errore nella generazione della ricevuta di cortesia: ${(e as Error).message}`,
});
}
}),
getRicevutaCortesiaPdfQuery: protectedProcedure
.input(z.object({ ordineId: ordiniOrdineId }))
.query(async ({ input }) => {
try {
const ricData = await getRicevutaData(input.ordineId);
const pdf = await TypstGenerate({
templateId: "receipt.typ",
data: ricData.data,
});
return { title: ricData.title, pdf: pdf.toString("base64") };
} catch (e) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `Errore nella generazione della ricevuta di cortesia: ${(e as Error).message}`,
});
}
}),
});