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

View file

@ -1,53 +1,25 @@
import type { GetServerSideProps } from "next";
import Link from "next/link";
import { AllegatoIframe } from "~/components/allegato-iframe";
import { DocNotFoundPage } from "~/components/doc_not_found";
import { AreaRiservataLayout } from "~/components/Layout";
import { LoadingPage } from "~/components/loading";
import { Status500 } from "~/components/status-page";
import { getStorageUrl } from "~/lib/storage_utils";
import { redirectTo500 } from "~/lib/utils";
import type { NextPageWithLayout } from "~/pages/_app";
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 { zAnnuncioId, zServizioId } from "~/server/utils/zod_types";
import { api } from "~/utils/api";
type ContrattoViewerProps = {
servizioId: ServizioServizioId;
annuncioId: AnnunciId;
storageId: string;
};
/**
* Pagina di visualizzazione del contratto: /servizio/contratto-registrazione/[servizioId]/[annuncioId]
*/
const ContrattoViewer: NextPageWithLayout<ContrattoViewerProps> = ({
servizioId,
annuncioId,
storageId,
}: 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({
storageId,
});
@ -55,19 +27,28 @@ const FileSection = ({ storageId }: { storageId: string }) => {
if (isLoading) return <LoadingPage />;
if (!fileInfos) return <p>{t.file_section.error}</p>;
return (
<>
<AllegatoIframe allegato={fileInfos} className="h-full max-h-[70vh]" />
<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>
<AllegatoIframe
allegato={fileInfos}
className="h-full max-h-[70vh]"
/>
<Link
className="underline underline-offset-1 after:content-['_↗']"
href={getStorageUrl({
storageId: fileInfos.id,
params: { mode: "download" },
})}
>
{t.file_section.download}
</Link>
</>
<Link
className="underline underline-offset-1 after:content-['_↗']"
href={getStorageUrl({
storageId: fileInfos.id,
params: { mode: "download" },
})}
>
{t.file_section.download}
</Link>
</div>
</div>
</div>
);
};
@ -104,15 +85,21 @@ export const getServerSideProps = (async (context) => {
const helper = await TrpcAuthedFetchingIstance({ access_token });
if (helper) {
await helper.trpc.servizio.getServizioAnnuncio.prefetch({
const storageId = await helper.trpc.servizio.getServizioContratto.fetch({
annuncioId: parsedAnnuncioId.data,
servizioId: parsedServizioId.data,
});
if (!storageId) {
return redirectTo500;
}
await helper.trpc.storage.getFileInfos.prefetch({
storageId,
});
return {
props: {
annuncioId: parsedAnnuncioId.data,
servizioId: parsedServizioId.data,
storageId,
trpcState: helper.trpc.dehydrate(),
},
};

View file

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