refactor: update servizio handling to include expiration logic and improve pack retrieval process
This commit is contained in:
parent
e692f9f1cb
commit
6f83ca3c34
4 changed files with 151 additions and 103 deletions
|
|
@ -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 (
|
||||
<ServizioCard
|
||||
content={
|
||||
<>
|
||||
<p className="font-medium text-lg">Servizio Scaduto</p>
|
||||
<p>
|
||||
Non è possibile riutilizzare questo servizio, poiché sono passati
|
||||
più di 60 giorni dalla data di decorrenza senza che sia stato
|
||||
effettuato il saldo.
|
||||
</p>
|
||||
</>
|
||||
}
|
||||
header={
|
||||
<>
|
||||
<ServizioActions />
|
||||
</>
|
||||
}
|
||||
headerClassName="pb-0"
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
const annuncioConfermato = servizio.annunci.filter(
|
||||
(a) => a.accettato_conferma_at !== null,
|
||||
);
|
||||
|
|
|
|||
|
|
@ -184,37 +184,46 @@ const ServizioPacksInfos = () => {
|
|||
});
|
||||
if (!packs) return null;
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
<div className="w-full space-y-2">
|
||||
<h3 className="font-semibold text-lg">Prezzi:</h3>
|
||||
<div className="flex w-full flex-col gap-4 md:flex-row">
|
||||
{packs.acconto && (
|
||||
<div className="flex flex-1 flex-col gap-2 rounded-md bg-secondary/50 p-2">
|
||||
<span className="font-medium">{packs.acconto.nome_it}</span>
|
||||
<span className="text-muted-foreground text-sm">
|
||||
{packs.acconto.desc_it}
|
||||
</span>
|
||||
<span className="font-semibold text-lg">
|
||||
{formatCurrency(packs.acconto.prezzo_cent / 100)}
|
||||
</span>
|
||||
<span className="text-muted-foreground text-sm">
|
||||
COD: {packs.acconto.idprezziario}
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
{packs.saldo && (
|
||||
<div className="flex flex-1 flex-col gap-2 rounded-md bg-secondary/50 p-2">
|
||||
<span className="font-medium">{packs.saldo.nome_it}</span>
|
||||
<span className="text-muted-foreground text-sm">
|
||||
{packs.saldo.desc_it}
|
||||
</span>
|
||||
<span className="font-semibold text-lg">
|
||||
{formatCurrency(packs.saldo.prezzo_cent / 100)}
|
||||
</span>
|
||||
<span className="text-muted-foreground text-sm">
|
||||
COD: {packs.saldo.idprezziario}
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
<div className="flex flex-1 flex-col gap-2 rounded-md bg-secondary/50 p-2">
|
||||
{packs.acconto.success ? (
|
||||
<>
|
||||
<span className="font-medium">{packs.acconto.pack.nome_it}</span>
|
||||
<span className="text-muted-foreground text-sm">
|
||||
{packs.acconto.pack.desc_it}
|
||||
</span>
|
||||
<span className="font-semibold text-lg">
|
||||
{formatCurrency(packs.acconto.pack.prezzo_cent / 100)}
|
||||
</span>
|
||||
<span className="text-muted-foreground text-sm">
|
||||
COD: {packs.acconto.pack.idprezziario}
|
||||
</span>
|
||||
</>
|
||||
) : (
|
||||
<span className="font-medium">{packs.acconto.message}</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="flex flex-1 flex-col gap-2 rounded-md bg-secondary/50 p-2">
|
||||
{packs.saldo.success ? (
|
||||
<>
|
||||
<span className="font-medium">{packs.saldo.pack.nome_it}</span>
|
||||
<span className="text-muted-foreground text-sm">
|
||||
{packs.saldo.pack.desc_it}
|
||||
</span>
|
||||
<span className="font-semibold text-lg">
|
||||
{formatCurrency(packs.saldo.pack.prezzo_cent / 100)}
|
||||
</span>
|
||||
<span className="text-muted-foreground text-sm">
|
||||
COD: {packs.saldo.pack.idprezziario}
|
||||
</span>
|
||||
</>
|
||||
) : (
|
||||
<span className="font-medium">{packs.saldo.message}</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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<GetPackResult | GetPackError> => {
|
||||
type ServizioPacks = {
|
||||
acconto: GetPackResult | GetPackError;
|
||||
saldo: GetPackResult | GetPackError;
|
||||
};
|
||||
export const getPacksPerServizio = async (
|
||||
servizioId: ServizioServizioId,
|
||||
): Promise<ServizioPacks> => {
|
||||
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<GetPackResult | GetPackError> => {
|
||||
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<GetPackResult | GetPackError> => {
|
||||
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 ({
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue