From b376de48bbdea266ae7ab0eb7f6fe8f9b2d2cce2 Mon Sep 17 00:00:00 2001 From: Marco Pedone Date: Wed, 17 Dec 2025 14:14:30 +0100 Subject: [PATCH] refactor: streamline ServizioContent layout and enhance ServizioInfos with pack details retrieval --- .../src/components/servizio/main.tsx | 40 ++-------- .../components/servizio/servizio_actions.tsx | 55 ++++++++++++- apps/infoalloggi/src/lib/annuncio_details.ts | 4 - .../src/server/api/routers/prezziario.ts | 2 +- .../src/server/api/routers/servizio.ts | 28 +++++++ .../server/controllers/servizio.controller.ts | 80 +++++++++++++++++-- .../src/server/services/prezziario.service.ts | 25 +++--- 7 files changed, 173 insertions(+), 61 deletions(-) diff --git a/apps/infoalloggi/src/components/servizio/main.tsx b/apps/infoalloggi/src/components/servizio/main.tsx index 78a4dca..36ee222 100644 --- a/apps/infoalloggi/src/components/servizio/main.tsx +++ b/apps/infoalloggi/src/components/servizio/main.tsx @@ -203,16 +203,9 @@ export const ServizioContent = () => { ); } - const annunciSelezionati = servizio.annunci.filter( - (a) => a.user_confirmed_at === null, - ); - - const annunciInConferma = servizio.annunci.filter( - (a) => a.user_confirmed_at !== null, - ); - return ( {annuncioConfermato.length > 0 && ( @@ -252,8 +245,8 @@ export const ServizioContent = () => { )}
-
- {annunciInConferma.map((data) => { +
+ {servizio.annunci.map((data) => { return ( { servizioId={servizio.servizio_id} userId={userId} > - + ); })}
- {annunciSelezionati.length > 0 && ( -
-

- Annunci selezionati: -

-
- {annunciSelezionati.map((data) => { - return ( - - - - ); - })} -
-
- )} - {/* {annunciSelezionati.length > 0 && ( - - )} */} +
diff --git a/apps/infoalloggi/src/components/servizio/servizio_actions.tsx b/apps/infoalloggi/src/components/servizio/servizio_actions.tsx index c0fc5fb..86e3e85 100644 --- a/apps/infoalloggi/src/components/servizio/servizio_actions.tsx +++ b/apps/infoalloggi/src/components/servizio/servizio_actions.tsx @@ -55,7 +55,7 @@ import { FormEditServizio, } from "~/forms/FormEditServizioAdmin"; import { FormNewServizio, type FormValues } from "~/forms/FormNewServizio"; -import { cn } from "~/lib/utils"; +import { cn, formatCurrency } from "~/lib/utils"; import { useServizio } from "~/providers/ServizioProvider"; import type { AnnunciId } from "~/schemas/public/Annunci"; import type { ServizioServizioId } from "~/schemas/public/Servizio"; @@ -94,10 +94,13 @@ const ServizioInfos = () => { add(servizio.decorrenza, { days: 60 }), ).toLocaleDateString("it-IT"); return ( - + {open ? ( - )} - +
@@ -149,6 +152,7 @@ const ServizioInfos = () => {
+
); @@ -172,6 +176,49 @@ const ServizioBasicActions = ({ className }: { className?: string }) => { } return null; }; + +const ServizioPacksInfos = () => { + const { servizio } = useServizio(); + const { data: packs } = api.servizio.getPacksSolution.useQuery({ + servizioId: servizio.servizio_id, + }); + if (!packs) return null; + return ( +
+

Prezzi:

+
+ {packs.acconto && ( +
+ {packs.acconto.nome_it} + + {packs.acconto.desc_it} + + + {formatCurrency(packs.acconto.prezzo_cent / 100)} + + + COD: {packs.acconto.idprezziario} + +
+ )} + {packs.saldo && ( +
+ {packs.saldo.nome_it} + + {packs.saldo.desc_it} + + + {formatCurrency(packs.saldo.prezzo_cent / 100)} + + + COD: {packs.saldo.idprezziario} + +
+ )} +
+
+ ); +}; export const AddAnnuncio = ({ servizioId, userId, diff --git a/apps/infoalloggi/src/lib/annuncio_details.ts b/apps/infoalloggi/src/lib/annuncio_details.ts index 1415855..f94bd13 100644 --- a/apps/infoalloggi/src/lib/annuncio_details.ts +++ b/apps/infoalloggi/src/lib/annuncio_details.ts @@ -14,10 +14,6 @@ export const handleConsegna = ({ aggiornamento: string; }) => { const currentMonth = new Date().getMonth() + 1; - console.log("handleConsegna", { - consegna: consegna, - currentMonth: currentMonth, - }); if (consegna !== null) { if (consegna !== currentMonth) { return `${mesi[consegna]}`; diff --git a/apps/infoalloggi/src/server/api/routers/prezziario.ts b/apps/infoalloggi/src/server/api/routers/prezziario.ts index a001064..37c2351 100644 --- a/apps/infoalloggi/src/server/api/routers/prezziario.ts +++ b/apps/infoalloggi/src/server/api/routers/prezziario.ts @@ -85,7 +85,7 @@ export const prezziarioRouter = createTRPCRouter({ getPrezzoPerServizio: publicProcedure .input(z.object({ idprezziario: zPrezziarioId })) .query(async ({ input }) => { - return await getPrezziarioByIdHandler({ db, input }); + return await getPrezziarioByIdHandler({ id: input.idprezziario }); }), updatePrezziario: adminProcedure .input( diff --git a/apps/infoalloggi/src/server/api/routers/servizio.ts b/apps/infoalloggi/src/server/api/routers/servizio.ts index 83799ea..528a8bf 100644 --- a/apps/infoalloggi/src/server/api/routers/servizio.ts +++ b/apps/infoalloggi/src/server/api/routers/servizio.ts @@ -29,6 +29,7 @@ import { getCompatibileAnnunci, getDataPerAcquisto, getOrdineRicevutaData, + getPackPerServizio, getPreOnboardData, getServiziByUserId, getServizioAnnuncio, @@ -50,6 +51,7 @@ import { userAccettaConferma, userSendConfermaIntent, } from "~/server/controllers/servizio.controller"; +import { getPrezziarioByIdHandler } from "~/server/services/prezziario.service"; import { zAnnuncioId, zOrdineId, @@ -340,7 +342,33 @@ export const servizioRouter = createTRPCRouter({ servizioId: input.servizioId, }); }), + getPacksSolution: protectedProcedure + .input( + z.object({ + servizioId: zServizioId, + }), + ) + .query(async ({ input }) => { + const accontoId = await getPackPerServizio({ + stage: "acconto", + servizioId: input.servizioId, + }); + const saldoId = await getPackPerServizio({ + stage: "saldo", + servizioId: input.servizioId, + }); + const acconto = accontoId.success + ? await getPrezziarioByIdHandler({ id: accontoId.packId }) + : null; + const saldo = saldoId.success + ? await getPrezziarioByIdHandler({ id: saldoId.packId }) + : null; + return { + acconto, + saldo, + }; + }), unlockServizioContatti: protectedProcedure .input( z.object({ diff --git a/apps/infoalloggi/src/server/controllers/servizio.controller.ts b/apps/infoalloggi/src/server/controllers/servizio.controller.ts index 98e18f1..6e3014c 100644 --- a/apps/infoalloggi/src/server/controllers/servizio.controller.ts +++ b/apps/infoalloggi/src/server/controllers/servizio.controller.ts @@ -506,6 +506,74 @@ const ConsulenzaPriceMapping = { Transitorio: "CONS_TRAN" as PrezziarioIdprezziario, }; +type GetPackError = { success: false; message: string }; +type GetPackResult = { success: true; packId: PrezziarioIdprezziario }; + +export const getPackPerServizio = async ({ + stage, + servizioId, +}: { + stage: "acconto" | "saldo"; + servizioId: ServizioServizioId; +}): Promise => { + const servizio = await getServizioById(servizioId); + if (!servizio) { + return { success: false, message: "Servizio not found" }; + } + switch (stage) { + case "acconto": { + switch (servizio.tipologia) { + case TipologiaPosizioneEnum.Transitorio: { + return { + success: true, + packId: "ACCONTO_BD" as PrezziarioIdprezziario, + }; + } + + case TipologiaPosizioneEnum.Stabile: { + return { + success: true, + packId: "ACCONTO_CA" as PrezziarioIdprezziario, + }; + } + default: + return { success: false, message: "Invalid tipologia" }; + } + } + case "saldo": { + switch (servizio.tipologia) { + case TipologiaPosizioneEnum.Transitorio: { + const mpp = + servizio.permanenza && SaldoTransitorioMapping[servizio.permanenza]; + if (!servizio.permanenza || !mpp) { + return { + success: false, + message: "Permanenza not set for transitorio servizio", + }; + } + return { success: true, packId: mpp }; + } + case TipologiaPosizioneEnum.Stabile: { + if (servizio.budget >= 500) { + return { + success: true, + packId: "SALDOCA500+" as PrezziarioIdprezziario, + }; + } + return { + success: true, + packId: "SALDOCA500" as PrezziarioIdprezziario, + }; + } + default: + return { success: false, message: "Invalid tipologia" }; + } + } + + default: + return { success: false, message: "Invalid stage" }; + } +}; type TipoEnum = keyof typeof ConsulenzaPriceMapping; export const setupSaldoConsulenzaServizio = async ({ servizioId, @@ -711,12 +779,12 @@ export const getAllServizioAnnunci = async (userId: UsersId) => { //ignora annunci che non sono disponibili .where("annunci.web", "=", true) .where("annunci.stato", "!=", "Sospeso") - // .orderBy("servizio_annunci.user_confirmed_at", (ob) => - // ob.asc().nullsLast(), - // ) - // .orderBy("servizio_annunci.open_contatti_at", (ob) => - // ob.asc().nullsFirst(), - // ) + .orderBy("servizio_annunci.user_confirmed_at", (ob) => + ob.asc().nullsLast(), + ) + .orderBy("servizio_annunci.open_contatti_at", (ob) => + ob.asc().nullsFirst(), + ) .orderBy("servizio_annunci.created_at", "asc") .whereRef( "servizio_annunci.servizio_id", diff --git a/apps/infoalloggi/src/server/services/prezziario.service.ts b/apps/infoalloggi/src/server/services/prezziario.service.ts index b7a3eb8..d8a0e06 100644 --- a/apps/infoalloggi/src/server/services/prezziario.service.ts +++ b/apps/infoalloggi/src/server/services/prezziario.service.ts @@ -26,19 +26,22 @@ export const getPrezziario = async () => { }; export const getPrezziarioByIdHandler = async ({ - db, - input, + id, }: { - db: Querier; - input: { - idprezziario: PrezziarioIdprezziario; - }; + id: PrezziarioIdprezziario; }) => { - return await db - .selectFrom("prezziario") - .selectAll() - .where("prezziario.idprezziario", "=", input.idprezziario) - .executeTakeFirst(); + try { + return await db + .selectFrom("prezziario") + .selectAll() + .where("prezziario.idprezziario", "=", id) + .executeTakeFirst(); + } catch (error) { + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: `Error getting prezziario by id: ${(error as Error).message}`, + }); + } }; export const addPrezziario = async ({