infoalloggi-monorepo/apps/infoalloggi/src/server/api/routers/servizio.ts

404 lines
8.7 KiB
TypeScript
Raw Normal View History

import z from "zod";
import type { NewOrdini, OrdiniUpdate } from "~/schemas/public/Ordini";
2025-08-28 18:27:07 +02:00
import type { NewServizio, ServizioUpdate } from "~/schemas/public/Servizio";
import type { ServizioAnnunciUpdate } from "~/schemas/public/ServizioAnnunci";
import type { Users } from "~/schemas/public/Users";
import type { UsersAnagraficaUpdate } from "~/schemas/public/UsersAnagrafica";
2025-08-04 17:45:44 +02:00
import {
2025-08-28 18:27:07 +02:00
adminProcedure,
createTRPCRouter,
overridableProcedure,
2025-08-28 18:27:07 +02:00
protectedProcedure,
2025-08-04 17:45:44 +02:00
} from "~/server/api/trpc";
import {
createOrdine,
2025-08-28 18:27:07 +02:00
getOrdineById,
getOrdini,
removeOrder,
updateOrder,
} from "~/server/controllers/ordini.controller";
import { getPacksPerServizio } from "~/server/controllers/pagamenti.controller";
2025-08-04 17:45:44 +02:00
import {
2025-08-28 18:27:07 +02:00
addServizio,
addServizioAnnunci,
adminUpdateConfermaStatus,
attivaServizio_SaltaPagamentoAcconto,
2025-08-28 18:27:07 +02:00
deleteServizio,
deleteServizioAnnuncio,
getAllServizioAnnunci,
getAnnunciAvailableToAdd,
getCompatibileAnnunci,
getDataPerAcquisto,
getOrdineRicevutaData,
getServiziByUserId,
getServizioAnnuncio,
getServizioById,
getServizioDataById,
2025-08-28 18:27:07 +02:00
InteractionLogicTipologia,
interruzioneServizio,
processServizioOnboard,
2025-08-28 18:27:07 +02:00
SbloccaContatti,
updateServizio,
updateServizioAnnuncio,
userAccettaConferma,
userSendConfermaIntent,
2025-08-04 17:45:44 +02:00
} from "~/server/controllers/servizio.controller";
import {
2025-08-28 18:27:07 +02:00
zAnnuncioId,
zOrdineId,
zServizioId,
zUserId,
} from "~/server/utils/zod_types";
2025-08-04 17:45:44 +02:00
export const servizioRouter = createTRPCRouter({
2025-08-29 16:18:32 +02:00
AnnuncioServizioTipologiaMatch: protectedProcedure
2025-08-28 18:27:07 +02:00
.input(
z.object({
2025-08-29 16:18:32 +02:00
annuncioId: zAnnuncioId,
tipologia: z.string(),
userId: zUserId,
2025-08-28 18:27:07 +02:00
}),
)
.query(async ({ input }) => {
2025-08-29 16:18:32 +02:00
return await InteractionLogicTipologia({
annuncioId: input.annuncioId,
tipologia: input.tipologia,
userId: input.userId,
});
2025-08-28 18:27:07 +02:00
}),
addServizio: adminProcedure
.input(
z.object({
data: z.custom<NewServizio>(),
}),
)
.mutation(async ({ input }) => {
return await addServizio(input.data);
}),
2025-08-29 16:18:32 +02:00
addServizioAnnunci: adminProcedure
2025-08-28 18:27:07 +02:00
.input(
z.object({
2025-08-29 16:18:32 +02:00
annunci: z.array(zAnnuncioId),
2025-08-28 18:27:07 +02:00
servizioId: zServizioId,
}),
)
.mutation(async ({ input }) => {
2025-08-29 16:18:32 +02:00
return await addServizioAnnunci({
annunci: input.annunci,
2025-08-28 18:27:07 +02:00
servizioId: input.servizioId,
});
}),
deleteServizio: adminProcedure
.input(
z.object({
servizioId: zServizioId,
}),
)
.mutation(async ({ input }) => {
return await deleteServizio(input.servizioId);
}),
2025-08-04 17:45:44 +02:00
2025-08-29 16:18:32 +02:00
getAcquistoData: protectedProcedure
2025-08-28 18:27:07 +02:00
.input(
z.object({
2025-08-29 16:18:32 +02:00
servizioId: zServizioId,
2025-08-28 18:27:07 +02:00
}),
)
.query(async ({ input }) => {
2025-08-29 16:18:32 +02:00
return await getDataPerAcquisto(input.servizioId);
2025-08-28 18:27:07 +02:00
}),
2025-08-29 16:18:32 +02:00
getAllServizioAnnunci: protectedProcedure
2025-08-28 18:27:07 +02:00
.input(
z.object({
2025-08-29 16:18:32 +02:00
userId: zUserId,
2025-08-28 18:27:07 +02:00
}),
)
.query(async ({ input }) => {
2025-08-29 16:18:32 +02:00
return await getAllServizioAnnunci(input.userId);
2025-08-28 18:27:07 +02:00
}),
getServizioData: protectedProcedure
.input(
z.object({
servizioId: zServizioId,
}),
)
.query(async ({ input }) => {
return await getServizioDataById(input.servizioId);
}),
2025-08-29 16:18:32 +02:00
getAnnunciAvailableToAdd: adminProcedure
2025-08-28 18:27:07 +02:00
.input(
z.object({
servizioId: zServizioId,
}),
)
.query(async ({ input }) => {
2025-08-29 16:18:32 +02:00
return await getAnnunciAvailableToAdd(input.servizioId);
2025-08-28 18:27:07 +02:00
}),
2025-08-04 17:45:44 +02:00
2025-08-29 16:18:32 +02:00
getCompatibileAnnunci: protectedProcedure
2025-08-28 18:27:07 +02:00
.input(
z.object({
servizioId: zServizioId,
adminOverride: z.boolean().optional(),
2025-08-28 18:27:07 +02:00
}),
)
.query(async ({ input }) => {
return await getCompatibileAnnunci({ ...input });
2025-08-28 18:27:07 +02:00
}),
2025-08-29 16:18:32 +02:00
getOrdineById: protectedProcedure
2025-08-28 18:27:07 +02:00
.input(
z.object({
2025-08-29 16:18:32 +02:00
ordineId: zOrdineId,
2025-08-28 18:27:07 +02:00
}),
)
.query(async ({ input }) => {
2025-08-29 16:18:32 +02:00
return await getOrdineById(input.ordineId);
2025-08-28 18:27:07 +02:00
}),
getOrdineRicevutaData: overridableProcedure
2025-08-28 18:27:07 +02:00
.input(
z.object({
2025-08-29 16:18:32 +02:00
ordineId: zOrdineId,
2025-08-28 18:27:07 +02:00
}),
)
.query(async ({ input }) => {
return await getOrdineRicevutaData({ ordineId: input.ordineId });
2025-08-28 18:27:07 +02:00
}),
2025-08-29 16:18:32 +02:00
getOrdini: protectedProcedure
2025-08-28 18:27:07 +02:00
.input(
z.object({
2025-08-29 16:18:32 +02:00
userId: zUserId.optional(),
2025-08-28 18:27:07 +02:00
}),
)
2025-08-29 16:18:32 +02:00
.query(async ({ input }) => {
return await getOrdini(input.userId);
2025-08-28 18:27:07 +02:00
}),
isOnboardAvailable: protectedProcedure
2025-08-28 18:27:07 +02:00
.input(
z.object({
servizioId: zServizioId,
}),
)
.output(
z.discriminatedUnion("available", [
z.object({
available: z.literal(true),
userId: zUserId,
}),
z.object({
available: z.literal(false),
userId: z.null(),
}),
]),
)
2025-08-29 16:18:32 +02:00
.query(async ({ input }) => {
const servizio = await getServizioById(input.servizioId);
if (
!servizio ||
servizio.isOkAcconto ||
servizio.isInterrotto ||
servizio.decorrenza !== null
) {
return { available: false, userId: null };
}
return { available: true, userId: servizio.user_id };
2025-08-28 18:27:07 +02:00
}),
2025-08-29 16:18:32 +02:00
getServizioAnnuncio: protectedProcedure
2025-08-28 18:27:07 +02:00
.input(
z.object({
annuncioId: zAnnuncioId,
2025-08-29 16:18:32 +02:00
servizioId: zServizioId,
2025-08-28 18:27:07 +02:00
}),
)
2025-08-29 16:18:32 +02:00
.query(async ({ input }) => {
return await getServizioAnnuncio({
2025-08-28 18:27:07 +02:00
annuncioId: input.annuncioId,
2025-08-29 16:18:32 +02:00
servizioId: input.servizioId,
2025-08-28 18:27:07 +02:00
});
}),
2025-08-29 16:18:32 +02:00
getUserServizi: adminProcedure
2025-08-28 18:27:07 +02:00
.input(
z.object({
userId: zUserId,
}),
)
2025-08-29 16:18:32 +02:00
.query(async ({ input }) => {
return await getServiziByUserId(input.userId);
2025-08-28 18:27:07 +02:00
}),
2025-08-29 16:18:32 +02:00
interruzioneServizio: protectedProcedure
2025-08-28 18:27:07 +02:00
.input(
z.object({
servizioId: zServizioId,
}),
)
.mutation(async ({ input }) => {
2025-08-29 16:18:32 +02:00
return await interruzioneServizio(input.servizioId);
2025-08-28 18:27:07 +02:00
}),
2025-08-29 16:18:32 +02:00
processServizioOnboard: protectedProcedure
2025-08-28 18:27:07 +02:00
.input(
z.object({
2025-08-29 16:18:32 +02:00
anagrafica: z.custom<UsersAnagraficaUpdate>(),
servizio: z.custom<ServizioUpdate>(),
servizioId: zServizioId,
user: z.custom<
Pick<Users, "nome" | "cognome" | "email" | "telefono" | "username">
>(),
userId: zUserId,
2025-08-28 18:27:07 +02:00
}),
)
2025-08-29 16:18:32 +02:00
.mutation(async ({ input }) => {
return await processServizioOnboard({
2025-08-29 16:18:32 +02:00
...input,
});
2025-08-28 18:27:07 +02:00
}),
attivazioneSenzaPagamento: protectedProcedure
2025-08-28 18:27:07 +02:00
.input(
z.object({
2025-08-29 16:18:32 +02:00
ordineId: zOrdineId,
2025-08-28 18:27:07 +02:00
}),
)
.mutation(async ({ input }) => {
return await attivaServizio_SaltaPagamentoAcconto(input.ordineId);
2025-08-28 18:27:07 +02:00
}),
2025-08-04 17:45:44 +02:00
removeAnnuncioServizio: protectedProcedure
2025-08-28 18:27:07 +02:00
.input(
z.object({
annuncioId: zAnnuncioId,
2025-08-29 16:18:32 +02:00
servizioId: zServizioId,
2025-08-28 18:27:07 +02:00
}),
)
.mutation(async ({ input }) => {
return await deleteServizioAnnuncio(input.servizioId, input.annuncioId);
2025-08-29 16:18:32 +02:00
}),
removeOrder: adminProcedure
2025-08-29 16:18:32 +02:00
.input(
z.object({
ordineId: zOrdineId,
2025-08-29 16:18:32 +02:00
}),
)
.mutation(async ({ input }) => {
return await removeOrder(input.ordineId);
2025-08-28 18:27:07 +02:00
}),
getPacksSolution: protectedProcedure
.input(
z.object({
servizioId: zServizioId,
}),
)
.query(async ({ input }) => {
return await getPacksPerServizio(input.servizioId);
}),
2025-08-29 16:18:32 +02:00
unlockServizioContatti: protectedProcedure
2025-08-28 18:27:07 +02:00
.input(
z.object({
2025-08-29 16:18:32 +02:00
annuncioId: zAnnuncioId,
2025-08-28 18:27:07 +02:00
servizioId: zServizioId,
}),
)
2025-08-29 16:18:32 +02:00
.mutation(async ({ input }) => {
return await SbloccaContatti({
annuncioId: input.annuncioId,
servizioId: input.servizioId,
});
2025-08-28 18:27:07 +02:00
}),
createOrdine: adminProcedure
.input(
z.object({
data: z.custom<NewOrdini>(),
}),
)
.mutation(async ({ input }) => {
return await createOrdine(input.data);
}),
2025-08-29 16:18:32 +02:00
updateOrder: adminProcedure
2025-08-28 18:27:07 +02:00
.input(
z.object({
2025-08-29 16:18:32 +02:00
data: z.custom<OrdiniUpdate>(),
ordineId: zOrdineId,
2025-08-28 18:27:07 +02:00
}),
)
2025-08-29 16:18:32 +02:00
.mutation(async ({ input }) => {
return await updateOrder({
data: input.data,
ordineId: input.ordineId,
});
2025-08-28 18:27:07 +02:00
}),
2025-08-29 16:18:32 +02:00
updateServizio: protectedProcedure
2025-08-28 18:27:07 +02:00
.input(
z.object({
2025-08-29 16:18:32 +02:00
data: z.custom<ServizioUpdate>(),
servizioId: zServizioId,
2025-08-28 18:27:07 +02:00
}),
)
.mutation(async ({ input }) => {
2025-08-29 16:18:32 +02:00
return await updateServizio({
data: input.data,
servizioId: input.servizioId,
});
2025-08-28 18:27:07 +02:00
}),
2025-08-29 16:18:32 +02:00
updateServizioAnnunci: protectedProcedure
2025-08-28 18:27:07 +02:00
.input(
z.object({
2025-08-29 16:18:32 +02:00
annuncioId: zAnnuncioId,
data: z.custom<ServizioAnnunciUpdate>(),
servizioId: zServizioId,
2025-08-28 18:27:07 +02:00
}),
)
.mutation(async ({ input }) => {
2025-08-29 16:18:32 +02:00
return await updateServizioAnnuncio({
annuncioId: input.annuncioId,
2025-08-28 18:27:07 +02:00
data: input.data,
2025-08-29 16:18:32 +02:00
servizioId: input.servizioId,
2025-08-28 18:27:07 +02:00
});
}),
adminUpdateConfermaStatus: adminProcedure
2025-08-28 18:27:07 +02:00
.input(
z.object({
2025-08-29 16:18:32 +02:00
annuncioId: zAnnuncioId,
servizioId: zServizioId,
status: z.boolean(),
2025-08-28 18:27:07 +02:00
}),
)
2025-08-29 16:18:32 +02:00
.mutation(async ({ input }) => {
return await adminUpdateConfermaStatus({
annuncioId: input.annuncioId,
servizioId: input.servizioId,
status: input.status,
});
}),
userSendConfermaIntent: protectedProcedure
.input(
z.object({
annuncioId: zAnnuncioId,
servizioId: zServizioId,
}),
)
.mutation(async ({ input }) => {
return await userSendConfermaIntent({
annuncioId: input.annuncioId,
servizioId: input.servizioId,
});
}),
userAcceptConfermaImmobile: protectedProcedure
.input(
z.object({
annuncioId: zAnnuncioId,
servizioId: zServizioId,
}),
)
.mutation(async ({ input }) => {
return await userAccettaConferma({
2025-08-29 16:18:32 +02:00
annuncioId: input.annuncioId,
servizioId: input.servizioId,
});
2025-08-28 18:27:07 +02:00
}),
2025-08-04 17:45:44 +02:00
});