Skip to content

Commit

Permalink
chore: revert async data component
Browse files Browse the repository at this point in the history
prefer the use of async client call

# Conflicts:
#	app/(header-default)/documents/[slug]/_components/actes-section.tsx
#	app/(header-default)/documents/[slug]/page.tsx
  • Loading branch information
johangirod committed Jun 3, 2024
1 parent febbcaa commit 44ea3e9
Show file tree
Hide file tree
Showing 14 changed files with 447 additions and 86 deletions.
100 changes: 100 additions & 0 deletions app/(header-default)/documents/[slug]/_components/actes-section.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
'use client';
import routes from '#clients/routes';
import { Warning } from '#components-ui/alerts';
import ButtonLink from '#components-ui/button';
import ShowMore from '#components-ui/show-more';
import { AsyncDataSectionClient } from '#components/section/data-section/client';
import { FullTable } from '#components/table/full';
import { EAdministration } from '#models/administrations/EAdministration';
import {
IUniteLegale,
isAssociation,
isServicePublic,
} from '#models/core/types';
import { IActesRNE } from '#models/immatriculation';
import { formatDateLong } from '#utils/helpers';
import useFetchRNEDocuments from 'hooks/fetch/RNE-documents';

const NoDocument = () => (
<>Aucun document n’a été retrouvé dans le RNE pour cette entreprise.</>
);

export default function AgentActesSection({
uniteLegale,
}: {
uniteLegale: IUniteLegale;
}) {
const documents = useFetchRNEDocuments(uniteLegale);
return (
<AsyncDataSectionClient
title={'Actes et statuts'}
id={'actes'}
isProtected
sources={[EAdministration.INPI]}
data={documents}
notFoundInfo={
<>
{(isAssociation(uniteLegale) || isServicePublic(uniteLegale)) && (
<>
<Warning full>
Les associations et les services publics ne sont pas
immatriculés au RNE.
</Warning>
<br />
</>
)}
<NoDocument />
</>
}
>
{(documents) => (
<>
<p>
Cette entreprise possède {documents.actes.length} document(s) au
RNE. Chaque document peut contenir un ou plusieurs actes :
</p>
{documents.actes.length >= 5 ? (
<ShowMore
label={`Voir les ${
documents.actes.length - 5
} documents supplémentaires`}
>
<ActesTable actes={documents.actes} />
</ShowMore>
) : (
<ActesTable actes={documents.actes} />
)}
</>
)}
</AsyncDataSectionClient>
);
}

type IActesTableProps = {
actes: IActesRNE['actes'];
};
function ActesTable({ actes }: IActesTableProps) {
return (
<FullTable
head={['Date de dépôt', 'Acte(s) contenu(s)', 'Lien']}
body={actes.map((a) => [
formatDateLong(a.dateDepot),
a.actes && (
<ul>
{(a?.actes || []).map((acteName) => (
<li key={acteName}>{acteName}</li>
))}
</ul>
),
<ButtonLink
target="_blank"
alt
small
to={`${routes.api.rne.documents.download}${a.id}?type=acte`}
>
Télécharger
</ButtonLink>,
])}
/>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use client';

import ButtonLink from '#components-ui/button';
import { Icon } from '#components-ui/icon/wrapper';
import { AsyncDataSectionClient } from '#components/section/data-section/client';
import { EAdministration } from '#models/administrations/EAdministration';
import { IUniteLegale } from '#models/core/types';
import useFetchCarteProfessionnelleTP from 'hooks/fetch/carte-professionnelle-TP';

export default function CarteProfessionnelleTPSection({
uniteLegale,
}: {
uniteLegale: IUniteLegale;
}) {
const carteProfessionnelleTravauxPublics =
useFetchCarteProfessionnelleTP(uniteLegale);

return (
<AsyncDataSectionClient
title="Carte professionnelle travaux publics"
id="carte-professionnelle-travaux-publics"
isProtected
notFoundInfo={
'Aucune carte professionnelle trouvée pour cette entreprise'
}
sources={[EAdministration.FNTP]}
data={carteProfessionnelleTravauxPublics}
>
{(data) => (
<>
<p>
Cette entreprise possède une{' '}
<a
href="https://www.fntp.fr/tout-savoir-sur-la-carte-professionnelle-tp"
aria-label="En savoir plus sur la carte professionnelle d’entrepreneur de travaux publics, nouvelle fenêtre"
target="_blank"
rel="noreferrer"
>
carte professionnelle d’entrepreneur de travaux publics
</a>
, délivrée par la FNTP.
</p>

<div className="layout-center">
<ButtonLink
target="_blank"
ariaLabel="Télécharger le justificatif de la carte professionnelle travaux publics, téléchargement dans une nouvelle fenêtre"
to={`${data.documentUrl}`}
>
<Icon slug="download">Télécharger le justificatif</Icon>
</ButtonLink>
</div>
</>
)}
</AsyncDataSectionClient>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use client';
import Conformite from '#components/espace-agent-components/conformite';
import { AsyncDataSectionClient } from '#components/section/data-section/client';
import { TwoColumnTable } from '#components/table/simple';
import { EAdministration } from '#models/administrations/EAdministration';
import { IUniteLegale } from '#models/core/types';
import useFetchConformite from 'hooks/fetch/conformite';

function ConformiteSection({ uniteLegale }: { uniteLegale: IUniteLegale }) {
const conformite = useFetchConformite(uniteLegale);

return (
<AsyncDataSectionClient
title={'Conformité'}
id={'conformite'}
isProtected
sources={[
EAdministration.DGFIP,
EAdministration.URSSAF,
EAdministration.MSA,
]}
data={conformite}
>
{(conformite) => (
<TwoColumnTable
body={[
['Conformité fiscale', <Conformite data={conformite?.fiscale} />],
[
'Conformité sociale',
<>
<Conformite
data={conformite?.vigilance}
administration="Urssaf"
/>
<br />
<Conformite data={conformite?.msa} administration="MSA" />
</>,
],
]}
/>
)}
</AsyncDataSectionClient>
);
}

export default ConformiteSection;
46 changes: 19 additions & 27 deletions app/(header-default)/documents/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { Metadata } from 'next';
import { HorizontalSeparator } from '#components-ui/horizontal-separator';
import { CarteProfessionnelleTravauxPublicsSection } from '#components/carte-professionnelle-travaux-publics-section';
import { PrintNever } from '#components-ui/print-visibility';
import AgentWallDocuments from '#components/espace-agent-components/agent-wall/document';
import ConformiteSection from '#components/espace-agent-components/conformite-section';
import ActesSection from '#components/espace-agent-components/documents/actes-walled';
import Title from '#components/title-section';
import { FICHE } from '#components/title-section/tabs';
import { isAPI404 } from '#models/api-not-responding';
import { getCarteProfessionnelleTravauxPublic } from '#models/espace-agent/carte-professionnelle-travaux-publics';
import { EScope, hasRights } from '#models/user/rights';
import {
uniteLegalePageDescription,
Expand All @@ -17,6 +14,8 @@ import extractParamsAppRouter, {
AppRouterProps,
} from '#utils/server-side-helper/app/extract-params';
import getSession from '#utils/server-side-helper/app/get-session';
import AgentActesSection from './_components/actes-section';
import CarteProfessionnelleTPSection from './_components/carte-professionnelle-TP-section';

export const generateMetadata = async (
props: AppRouterProps
Expand Down Expand Up @@ -46,15 +45,6 @@ const UniteLegaleDocumentPage = async (props: AppRouterProps) => {
const { slug, isBot } = extractParamsAppRouter(props);
const uniteLegale = await cachedGetUniteLegale(slug, isBot);

const carteProfessionnelleTravauxPublics = hasRights(
session,
EScope.carteProfessionnelleTravauxPublics
)
? await getCarteProfessionnelleTravauxPublic(
uniteLegale.siren,
session?.user
)
: null;
return (
<>
<div className="content-container">
Expand All @@ -63,21 +53,23 @@ const UniteLegaleDocumentPage = async (props: AppRouterProps) => {
ficheType={FICHE.DOCUMENTS}
session={session}
/>
{hasRights(session, EScope.conformite) && (
<>
<ConformiteSection uniteLegale={uniteLegale} session={session} />
<HorizontalSeparator />
</>
)}
<ActesSection uniteLegale={uniteLegale} session={session} />
{carteProfessionnelleTravauxPublics &&
!isAPI404(carteProfessionnelleTravauxPublics) && (
<CarteProfessionnelleTravauxPublicsSection
carteProfessionnelleTravauxPublics={
carteProfessionnelleTravauxPublics
}
<PrintNever>
{hasRights(session, EScope.conformite) && (
<ConformiteSection uniteLegale={uniteLegale} />

Check failure on line 58 in app/(header-default)/documents/[slug]/page.tsx

View workflow job for this annotation

GitHub Actions / Type check

Property 'session' is missing in type '{ uniteLegale: IUniteLegale; }' but required in type 'IProps'.
)}
{!hasRights(session, EScope.documentsRne) ? (
<AgentWallDocuments

Check failure on line 61 in app/(header-default)/documents/[slug]/page.tsx

View workflow job for this annotation

GitHub Actions / Type check

Property 'condition' is missing in type '{ title: string; id: string; uniteLegale: IUniteLegale; }' but required in type '{ title: string; id: string; uniteLegale: IUniteLegale; condition: boolean; }'.
title="Actes et statuts"
id="actes"
uniteLegale={uniteLegale}
/>
) : (
<AgentActesSection uniteLegale={uniteLegale} />
)}
{hasRights(session, EScope.carteProfessionnelleTravauxPublics) && (
<CarteProfessionnelleTPSection uniteLegale={uniteLegale} />
)}
</PrintNever>
</div>
</>
);
Expand Down
22 changes: 22 additions & 0 deletions app/api/data-fetching/espace-agent/RNE-documents/[slug]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { EAdministration } from '#models/administrations/EAdministration';
import { getDocumentsRNEProtected } from '#models/immatriculation/rne';
import { EScope } from '#models/user/rights';
import { verifySiren } from '#utils/helpers';
import { ProtectedAPIRoute } from '../../_helper';

export async function GET(
_request: Request,
{ params }: { params: { slug: string } }
) {
const slug = params.slug;
return ProtectedAPIRoute(
'RNEDocuments',
slug,
EAdministration.INPI,
EScope.documentsRne,
async () => {
const siren = verifySiren(slug as string);
return getDocumentsRNEProtected(siren);
}
);
}
37 changes: 37 additions & 0 deletions app/api/data-fetching/espace-agent/_helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { HttpForbiddenError } from '#clients/exceptions';
import { EAdministration } from '#models/administrations/EAdministration';
import { FetchRessourceException } from '#models/exceptions';
import { EScope, hasRights } from '#models/user/rights';
import { logFatalErrorInSentry } from '#utils/sentry';
import getSession from '#utils/server-side-helper/app/get-session';

export async function ProtectedAPIRoute<T>(
routeLabel: string,
slug: string,
administration: EAdministration,
scope: EScope,
run: (agentSiret: string) => Promise<T>
) {
const session = await getSession();
try {
if (!hasRights(session, scope)) {
throw new HttpForbiddenError('Unauthorized account');
}

const agentSiret = session?.user?.siret || 'Inconnu';

const data = await run(agentSiret);
return Response.json(data, { status: 200 });
} catch (e: any) {
const message = `Failed to get donnees ${routeLabel}`;
logFatalErrorInSentry(
new FetchRessourceException({
ressource: routeLabel,
context: { slug },
cause: e,
administration,
})
);
return Response.json({ message }, { status: e.status || 500 });
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { EAdministration } from '#models/administrations/EAdministration';
import { getCarteProfessionnelleTravauxPublic } from '#models/espace-agent/carte-professionnelle-travaux-publics';
import { EScope } from '#models/user/rights';
import { Siret, extractSirenFromSiret, verifySiret } from '#utils/helpers';
import { ProtectedAPIRoute } from '../../_helper';

export async function GET(
_request: Request,
{ params }: { params: { slug: string } }
) {
const slug = params.slug;
return ProtectedAPIRoute(
'CarteProfessionnelleTravauxPublics',
slug,
EAdministration.FNTP,
EScope.conformite,
async (agentSiret: string) => {
const siret = verifySiret(slug as string);
const siren = extractSirenFromSiret(siret);

return await getCarteProfessionnelleTravauxPublic(
siren,
agentSiret as Siret
);
}
);
}
24 changes: 24 additions & 0 deletions app/api/data-fetching/espace-agent/conformite/[slug]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { EAdministration } from '#models/administrations/EAdministration';
import { getConformiteEntreprise } from '#models/espace-agent/conformite';
import { EScope } from '#models/user/rights';
import { extractSirenFromSiret, verifySiret } from '#utils/helpers';
import { ProtectedAPIRoute } from '../../_helper';

export async function GET(
_request: Request,
{ params }: { params: { slug: string } }
) {
const slug = params.slug;
return ProtectedAPIRoute(
'DonneesConformite',
slug,
EAdministration.DINUM,
EScope.conformite,
async (agentSiret: string) => {
const siret = verifySiret(slug as string);
const siren = extractSirenFromSiret(siret);

return await getConformiteEntreprise(siren, siret, agentSiret);
}
);
}
Loading

0 comments on commit 44ea3e9

Please sign in to comment.