refactor: rename getServizioAnnuncio to getServizioContratto and update related logic in servizio router

This commit is contained in:
Marco Pedone 2026-04-09 16:03:32 +02:00
parent a3d3e8f5bd
commit 73675480f6
3 changed files with 78 additions and 95 deletions

View file

@ -1,39 +1,31 @@
import type { GetServerSideProps } from "next"; import type { GetServerSideProps } from "next";
import Link from "next/link"; import Link from "next/link";
import { AllegatoIframe } from "~/components/allegato-iframe"; import { AllegatoIframe } from "~/components/allegato-iframe";
import { DocNotFoundPage } from "~/components/doc_not_found";
import { AreaRiservataLayout } from "~/components/Layout"; import { AreaRiservataLayout } from "~/components/Layout";
import { LoadingPage } from "~/components/loading"; import { LoadingPage } from "~/components/loading";
import { Status500 } from "~/components/status-page";
import { getStorageUrl } from "~/lib/storage_utils"; import { getStorageUrl } from "~/lib/storage_utils";
import { redirectTo500 } from "~/lib/utils"; import { redirectTo500 } from "~/lib/utils";
import type { NextPageWithLayout } from "~/pages/_app"; import type { NextPageWithLayout } from "~/pages/_app";
import { useTranslation } from "~/providers/I18nProvider"; import { useTranslation } from "~/providers/I18nProvider";
import type { AnnunciId } from "~/schemas/public/Annunci";
import type { ServizioServizioId } from "~/schemas/public/Servizio";
import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper"; import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper";
import { zAnnuncioId, zServizioId } from "~/server/utils/zod_types"; import { zAnnuncioId, zServizioId } from "~/server/utils/zod_types";
import { api } from "~/utils/api"; import { api } from "~/utils/api";
type ContrattoViewerProps = { type ContrattoViewerProps = {
servizioId: ServizioServizioId; storageId: string;
annuncioId: AnnunciId;
}; };
/** /**
* Pagina di visualizzazione del contratto di registrazione: /servizio/contratto-registrazione/[servizioId]/[annuncioId] * Pagina di visualizzazione del contratto di registrazione: /servizio/contratto-registrazione/[servizioId]/[annuncioId]
*/ */
const ContrattoViewer: NextPageWithLayout<ContrattoViewerProps> = ({ const ContrattoViewer: NextPageWithLayout<ContrattoViewerProps> = ({
servizioId, storageId,
annuncioId,
}: ContrattoViewerProps) => { }: ContrattoViewerProps) => {
const { data, isLoading } = api.servizio.getServizioAnnuncio.useQuery({ const { data: fileInfos, isLoading } = api.storage.getFileInfos.useQuery({
annuncioId, storageId,
servizioId,
}); });
const { t } = useTranslation(); const { t } = useTranslation();
if (isLoading) return <LoadingPage />; if (isLoading) return <LoadingPage />;
if (!data) return <Status500 />; if (!fileInfos) return <p>{t.file_section.error}</p>;
if (!data.doc_registrazione_ref) return <DocNotFoundPage />;
return ( return (
<div className="mx-2 flex flex-1 overflow-auto"> <div className="mx-2 flex flex-1 overflow-auto">
<div className="mx-auto flex grow sm:grow-0"> <div className="mx-auto flex grow sm:grow-0">
@ -44,23 +36,10 @@ const ContrattoViewer: NextPageWithLayout<ContrattoViewerProps> = ({
<p className="text-center"> <p className="text-center">
{t.servizio.contratto_view.ricevuta_cta} {t.servizio.contratto_view.ricevuta_cta}
</p> </p>
<FileSection storageId={data.doc_registrazione_ref} /> <AllegatoIframe
</div> allegato={fileInfos}
</div> className="h-full max-h-[70vh]"
</div> />
);
};
const FileSection = ({ storageId }: { storageId: string }) => {
const { data: fileInfos, isLoading } = api.storage.getFileInfos.useQuery({
storageId,
});
const { t } = useTranslation();
if (isLoading) return <LoadingPage />;
if (!fileInfos) return <p>{t.file_section.error}</p>;
return (
<>
<AllegatoIframe allegato={fileInfos} className="h-full max-h-[70vh]" />
<Link <Link
className="underline underline-offset-1 after:content-['_↗']" className="underline underline-offset-1 after:content-['_↗']"
@ -71,7 +50,9 @@ const FileSection = ({ storageId }: { storageId: string }) => {
> >
{t.file_section.download} {t.file_section.download}
</Link> </Link>
</> </div>
</div>
</div>
); );
}; };
@ -108,15 +89,23 @@ export const getServerSideProps = (async (context) => {
const helper = await TrpcAuthedFetchingIstance({ access_token }); const helper = await TrpcAuthedFetchingIstance({ access_token });
if (helper) { if (helper) {
await helper.trpc.servizio.getServizioAnnuncio.prefetch({ const storageId = await helper.trpc.servizio.getServizioRegistrazione.fetch(
{
annuncioId: parsedAnnuncioId.data, annuncioId: parsedAnnuncioId.data,
servizioId: parsedServizioId.data, servizioId: parsedServizioId.data,
},
);
if (!storageId) {
return redirectTo500;
}
await helper.trpc.storage.getFileInfos.prefetch({
storageId,
}); });
return { return {
props: { props: {
annuncioId: parsedAnnuncioId.data, storageId,
servizioId: parsedServizioId.data,
trpcState: helper.trpc.dehydrate(), trpcState: helper.trpc.dehydrate(),
}, },
}; };

View file

@ -1,53 +1,25 @@
import type { GetServerSideProps } from "next"; import type { GetServerSideProps } from "next";
import Link from "next/link"; import Link from "next/link";
import { AllegatoIframe } from "~/components/allegato-iframe"; import { AllegatoIframe } from "~/components/allegato-iframe";
import { DocNotFoundPage } from "~/components/doc_not_found";
import { AreaRiservataLayout } from "~/components/Layout"; import { AreaRiservataLayout } from "~/components/Layout";
import { LoadingPage } from "~/components/loading"; import { LoadingPage } from "~/components/loading";
import { Status500 } from "~/components/status-page";
import { getStorageUrl } from "~/lib/storage_utils"; import { getStorageUrl } from "~/lib/storage_utils";
import { redirectTo500 } from "~/lib/utils"; import { redirectTo500 } from "~/lib/utils";
import type { NextPageWithLayout } from "~/pages/_app"; import type { NextPageWithLayout } from "~/pages/_app";
import { useTranslation } from "~/providers/I18nProvider"; import { useTranslation } from "~/providers/I18nProvider";
import type { AnnunciId } from "~/schemas/public/Annunci";
import type { ServizioServizioId } from "~/schemas/public/Servizio";
import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper"; import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper";
import { zAnnuncioId, zServizioId } from "~/server/utils/zod_types"; import { zAnnuncioId, zServizioId } from "~/server/utils/zod_types";
import { api } from "~/utils/api"; import { api } from "~/utils/api";
type ContrattoViewerProps = { type ContrattoViewerProps = {
servizioId: ServizioServizioId; storageId: string;
annuncioId: AnnunciId;
}; };
/** /**
* Pagina di visualizzazione del contratto: /servizio/contratto-registrazione/[servizioId]/[annuncioId] * Pagina di visualizzazione del contratto: /servizio/contratto-registrazione/[servizioId]/[annuncioId]
*/ */
const ContrattoViewer: NextPageWithLayout<ContrattoViewerProps> = ({ const ContrattoViewer: NextPageWithLayout<ContrattoViewerProps> = ({
servizioId, storageId,
annuncioId,
}: ContrattoViewerProps) => { }: ContrattoViewerProps) => {
const { data, isLoading } = api.servizio.getServizioAnnuncio.useQuery({
annuncioId,
servizioId,
});
const { t } = useTranslation();
if (isLoading) return <LoadingPage />;
if (!data) return <Status500 />;
if (!data.doc_contratto_ref) return <DocNotFoundPage />;
return (
<div className="mx-2 flex flex-1 overflow-auto">
<div className="mx-auto flex grow sm:grow-0">
<div className="flex flex-col items-center space-y-4 py-4">
<h1 className="font-bold text-xl">{t.servizio.contratto.titolo}</h1>
<p className="text-center">{t.servizio.contratto.descrizione}</p>
<FileSection storageId={data.doc_contratto_ref} />
</div>
</div>
</div>
);
};
const FileSection = ({ storageId }: { storageId: string }) => {
const { data: fileInfos, isLoading } = api.storage.getFileInfos.useQuery({ const { data: fileInfos, isLoading } = api.storage.getFileInfos.useQuery({
storageId, storageId,
}); });
@ -55,8 +27,15 @@ const FileSection = ({ storageId }: { storageId: string }) => {
if (isLoading) return <LoadingPage />; if (isLoading) return <LoadingPage />;
if (!fileInfos) return <p>{t.file_section.error}</p>; if (!fileInfos) return <p>{t.file_section.error}</p>;
return ( return (
<> <div className="mx-2 flex flex-1 overflow-auto">
<AllegatoIframe allegato={fileInfos} className="h-full max-h-[70vh]" /> <div className="mx-auto flex grow sm:grow-0">
<div className="flex flex-col items-center space-y-4 py-4">
<h1 className="font-bold text-xl">{t.servizio.contratto.titolo}</h1>
<p className="text-center">{t.servizio.contratto.descrizione}</p>
<AllegatoIframe
allegato={fileInfos}
className="h-full max-h-[70vh]"
/>
<Link <Link
className="underline underline-offset-1 after:content-['_↗']" className="underline underline-offset-1 after:content-['_↗']"
@ -67,7 +46,9 @@ const FileSection = ({ storageId }: { storageId: string }) => {
> >
{t.file_section.download} {t.file_section.download}
</Link> </Link>
</> </div>
</div>
</div>
); );
}; };
@ -104,15 +85,21 @@ export const getServerSideProps = (async (context) => {
const helper = await TrpcAuthedFetchingIstance({ access_token }); const helper = await TrpcAuthedFetchingIstance({ access_token });
if (helper) { if (helper) {
await helper.trpc.servizio.getServizioAnnuncio.prefetch({ const storageId = await helper.trpc.servizio.getServizioContratto.fetch({
annuncioId: parsedAnnuncioId.data, annuncioId: parsedAnnuncioId.data,
servizioId: parsedServizioId.data, servizioId: parsedServizioId.data,
}); });
if (!storageId) {
return redirectTo500;
}
await helper.trpc.storage.getFileInfos.prefetch({
storageId,
});
return { return {
props: { props: {
annuncioId: parsedAnnuncioId.data, storageId,
servizioId: parsedServizioId.data,
trpcState: helper.trpc.dehydrate(), trpcState: helper.trpc.dehydrate(),
}, },
}; };

View file

@ -205,8 +205,7 @@ export const servizioRouter = createTRPCRouter({
} }
return { available: true, userId: servizio.user_id }; return { available: true, userId: servizio.user_id };
}), }),
getServizioContratto: protectedProcedure
getServizioAnnuncio: protectedProcedure
.input( .input(
z.object({ z.object({
annuncioId: zAnnuncioId, annuncioId: zAnnuncioId,
@ -214,21 +213,29 @@ export const servizioRouter = createTRPCRouter({
}), }),
) )
.query(async ({ input }) => { .query(async ({ input }) => {
return await getServizioAnnuncio({ const sa = await getServizioAnnuncio({
annuncioId: input.annuncioId, annuncioId: input.annuncioId,
servizioId: input.servizioId, servizioId: input.servizioId,
}); });
}),
getUserServizi: adminProcedure return sa.doc_contratto_ref;
}),
getServizioRegistrazione: protectedProcedure
.input( .input(
z.object({ z.object({
userId: zUserId, annuncioId: zAnnuncioId,
servizioId: zServizioId,
}), }),
) )
.query(async ({ input }) => { .query(async ({ input }) => {
return await getServiziByUserId(input.userId); const sa = await getServizioAnnuncio({
annuncioId: input.annuncioId,
servizioId: input.servizioId,
});
return sa.doc_registrazione_ref;
}), }),
interruzioneServizio: protectedProcedure interruzioneServizio: protectedProcedure
.input( .input(
z.object({ z.object({