diff --git a/apps/infoalloggi/src/components/servizio/main.tsx b/apps/infoalloggi/src/components/servizio/main.tsx index 314d0c7..9c3af63 100644 --- a/apps/infoalloggi/src/components/servizio/main.tsx +++ b/apps/infoalloggi/src/components/servizio/main.tsx @@ -1,3 +1,4 @@ +import { add } from "date-fns"; import { ArrowRight, Package, PackageCheck } from "lucide-react"; import Link from "next/link"; import { useRouter } from "next/router"; @@ -172,6 +173,36 @@ export const ServizioContent = () => { /> ); } + + if ( + new Date() > + add(servizio.decorrenza, { + days: 60, + }) && + !servizio.isOkSaldo + ) { + return ( + +

Servizio Scaduto

+

+ Non è possibile riutilizzare questo servizio, poiché sono passati + più di 60 giorni dalla data di decorrenza senza che sia stato + effettuato il saldo. +

+ + } + header={ + <> + + + } + headerClassName="pb-0" + /> + ); + } + const annuncioConfermato = servizio.annunci.filter( (a) => a.accettato_conferma_at !== null, ); diff --git a/apps/infoalloggi/src/components/servizio/servizio_actions.tsx b/apps/infoalloggi/src/components/servizio/servizio_actions.tsx index dfd7260..2cd0164 100644 --- a/apps/infoalloggi/src/components/servizio/servizio_actions.tsx +++ b/apps/infoalloggi/src/components/servizio/servizio_actions.tsx @@ -184,37 +184,46 @@ const ServizioPacksInfos = () => { }); 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} - -
- )} +
+ {packs.acconto.success ? ( + <> + {packs.acconto.pack.nome_it} + + {packs.acconto.pack.desc_it} + + + {formatCurrency(packs.acconto.pack.prezzo_cent / 100)} + + + COD: {packs.acconto.pack.idprezziario} + + + ) : ( + {packs.acconto.message} + )} +
+ +
+ {packs.saldo.success ? ( + <> + {packs.saldo.pack.nome_it} + + {packs.saldo.pack.desc_it} + + + {formatCurrency(packs.saldo.pack.prezzo_cent / 100)} + + + COD: {packs.saldo.pack.idprezziario} + + + ) : ( + {packs.saldo.message} + )} +
); diff --git a/apps/infoalloggi/src/server/api/routers/servizio.ts b/apps/infoalloggi/src/server/api/routers/servizio.ts index 528a8bf..746aaef 100644 --- a/apps/infoalloggi/src/server/api/routers/servizio.ts +++ b/apps/infoalloggi/src/server/api/routers/servizio.ts @@ -29,7 +29,7 @@ import { getCompatibileAnnunci, getDataPerAcquisto, getOrdineRicevutaData, - getPackPerServizio, + getPacksPerServizio, getPreOnboardData, getServiziByUserId, getServizioAnnuncio, @@ -51,7 +51,6 @@ import { userAccettaConferma, userSendConfermaIntent, } from "~/server/controllers/servizio.controller"; -import { getPrezziarioByIdHandler } from "~/server/services/prezziario.service"; import { zAnnuncioId, zOrdineId, @@ -349,25 +348,7 @@ export const servizioRouter = createTRPCRouter({ }), ) .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, - }; + return await getPacksPerServizio(input.servizioId); }), unlockServizioContatti: protectedProcedure .input( diff --git a/apps/infoalloggi/src/server/controllers/servizio.controller.ts b/apps/infoalloggi/src/server/controllers/servizio.controller.ts index 6e3014c..14648a5 100644 --- a/apps/infoalloggi/src/server/controllers/servizio.controller.ts +++ b/apps/infoalloggi/src/server/controllers/servizio.controller.ts @@ -43,6 +43,7 @@ import { NewMail, type NewMailProps } from "~/server/services/mailer"; import { getUser } from "~/server/services/user.service"; import { withImages, withVideos } from "~/utils/kysely-helper"; import { GetUserInterests } from "../services/interests.service"; +import { getPrezziarioByIdHandler } from "../services/prezziario.service"; import type { AnnuncioRicerca } from "./annunci.controller"; export const addServizio = async (data: NewServizio) => { @@ -507,72 +508,98 @@ const ConsulenzaPriceMapping = { }; type GetPackError = { success: false; message: string }; -type GetPackResult = { success: true; packId: PrezziarioIdprezziario }; +type GetPackResult = { success: true; pack: Prezziario }; -export const getPackPerServizio = async ({ - stage, - servizioId, -}: { - stage: "acconto" | "saldo"; - servizioId: ServizioServizioId; -}): Promise => { +type ServizioPacks = { + acconto: GetPackResult | GetPackError; + saldo: GetPackResult | GetPackError; +}; +export const getPacksPerServizio = async ( + servizioId: ServizioServizioId, +): Promise => { const servizio = await getServizioById(servizioId); if (!servizio) { - return { success: false, message: "Servizio not found" }; + throw new TRPCError({ + code: "NOT_FOUND", + message: "Servizio non trovato", + }); } - switch (stage) { - case "acconto": { - switch (servizio.tipologia) { - case TipologiaPosizioneEnum.Transitorio: { + + const getAcconto = async (): Promise => { + switch (servizio.tipologia) { + case TipologiaPosizioneEnum.Transitorio: { + const pack = await getPrezziarioByIdHandler({ + id: "ACCONTO_BD" as PrezziarioIdprezziario, + }); + + if (!pack) { + return { success: false, message: "Pack non trovato" }; + } + return { + success: true, + pack, + }; + } + + case TipologiaPosizioneEnum.Stabile: { + const pack = await getPrezziarioByIdHandler({ + id: "ACCONTO_CA" as PrezziarioIdprezziario, + }); + + if (!pack) { + return { success: false, message: "Pack non trovato" }; + } + return { + success: true, + pack, + }; + } + default: + return { success: false, message: "Seleziona tipologia" }; + } + }; + + const getSaldo = async (): Promise => { + switch (servizio.tipologia) { + case TipologiaPosizioneEnum.Transitorio: { + const mpp = + servizio.permanenza && SaldoTransitorioMapping[servizio.permanenza]; + if (!servizio.permanenza || !mpp) { return { - success: true, - packId: "ACCONTO_BD" as PrezziarioIdprezziario, + success: false, + message: "Permanenza non selezionata", }; } + const pack = await getPrezziarioByIdHandler({ id: mpp }); + if (!pack) { + return { success: false, message: "Pack non trovato" }; + } - case TipologiaPosizioneEnum.Stabile: { - return { - success: true, - packId: "ACCONTO_CA" as PrezziarioIdprezziario, - }; - } - default: - return { success: false, message: "Invalid tipologia" }; + return { success: true, pack }; } - } - 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: { + const pack = await getPrezziarioByIdHandler({ + id: (servizio.budget >= 500 + ? "SALDOCA500+" + : "SALDOCA500") as PrezziarioIdprezziario, + }); + if (!pack) { + return { success: false, message: "Pack non trovato" }; } - 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" }; + return { + success: true, + pack, + }; } + default: + return { success: false, message: "Seleziona tipologia" }; } + }; - default: - return { success: false, message: "Invalid stage" }; - } + return { + acconto: await getAcconto(), + saldo: await getSaldo(), + }; }; type TipoEnum = keyof typeof ConsulenzaPriceMapping; export const setupSaldoConsulenzaServizio = async ({