Skip to content

Commit

Permalink
chore: refactor all api routes
Browse files Browse the repository at this point in the history
  • Loading branch information
johangirod committed Jun 17, 2024
1 parent e786e4d commit b32b9ad
Show file tree
Hide file tree
Showing 57 changed files with 366 additions and 977 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import BreakPageForPrint from '#components-ui/print-break-page';
import { IUniteLegale } from '#models/core/types';
import { EScope, hasRights } from '#models/user/rights';
import { ISession } from '#models/user/session';
import useFetchRNEImmatriculation from 'hooks/fetch/RNE-immatriculation';
import { useAPIRouteData } from 'hooks/fetch/use-API-route-data';
import BeneficiairesSection from './beneficiaires';
import DirigeantsProtectedSection from './protected-dirigeants';
import DirigeantsSection from './rne-dirigeants';
Expand All @@ -17,7 +17,7 @@ export function DirigeantInformation({
uniteLegale: IUniteLegale;
session: ISession | null;
}) {
const immatriculationRNE = useFetchRNEImmatriculation(uniteLegale);
const immatriculationRNE = useAPIRouteData('rne', uniteLegale.siren);

return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '#models/api-not-responding';
import { IUniteLegale } from '#models/core/types';
import { IDirigeant, IImmatriculationRNE } from '#models/immatriculation';
import useFetchRCSMandataires from 'hooks/fetch/RCS-mandataires';
import { useAPIRouteData } from 'hooks/fetch/use-API-route-data';
import { DirigeantContent } from './dirigeant-content';
import DirigeantsSection from './rne-dirigeants';

Expand Down Expand Up @@ -72,7 +72,10 @@ function DirigeantsProtectedSection({
uniteLegale,
immatriculationRNE,
}: IProps) {
const mandatairesRCS = useFetchRCSMandataires(uniteLegale);
const mandatairesRCS = useAPIRouteData(
'espace-agent/rcs-mandataires',
uniteLegale.siren
);

if (!isAPILoading(mandatairesRCS)) {
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from '#models/core/types';
import { IActesRNE } from '#models/immatriculation';
import { formatDateLong } from '#utils/helpers';
import useFetchRNEDocuments from 'hooks/fetch/RNE-documents';
import { useAPIRouteData } from 'hooks/fetch/use-API-route-data';

const NoDocument = () => (
<>Aucun document n’a été retrouvé dans le RNE pour cette entreprise.</>
Expand All @@ -24,7 +24,10 @@ export default function AgentActesSection({
}: {
uniteLegale: IUniteLegale;
}) {
const documents = useFetchRNEDocuments(uniteLegale);
const documents = useAPIRouteData(
'espace-agent/rne/documents',
uniteLegale.siren
);
return (
<AsyncDataSectionClient
title={'Actes et statuts'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ 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';
import { useAPIRouteData } from 'hooks/fetch/use-API-route-data';

function ConformiteSection({ uniteLegale }: { uniteLegale: IUniteLegale }) {
const conformite = useFetchConformite(uniteLegale);
const conformite = useAPIRouteData(
'espace-agent/conformite',
uniteLegale.siege.siret
);

return (
<AsyncDataSectionClient
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
'use client';

import useFetchRNEImmatriculation from 'hooks/fetch/RNE-immatriculation';
import { Loader } from '#components-ui/loader';
import { isAPILoading } from '#models/api-loading';
import { IUniteLegale } from '#models/core/types';
import { ISession } from '#models/user/session';
import { useAPIRouteData } from 'hooks/fetch/use-API-route-data';
import { IJustificatifs, ImmatriculationsSection } from './container';

const Immatriculations: React.FC<{
uniteLegale: IUniteLegale;
immatriculationJOAFE: IJustificatifs['immatriculationJOAFE'];
session: ISession | null;
}> = ({ uniteLegale, immatriculationJOAFE, session }) => {
const immatriculationRNE = useFetchRNEImmatriculation(uniteLegale);
const immatriculationRNE = useAPIRouteData('rne', uniteLegale.siren);

return (
<>
Expand Down
5 changes: 5 additions & 0 deletions app/api/data-fetching/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# data-fetching folder

This foder contains the data fetching routes called from the frontend app.

Link between routes and model is done automatically with the `routes-handlers` module.
Route protection is done with the `routes-scopes` module.

The two are in different files to tree-shake the unused code for client (only routes-scopes is used in the front).
76 changes: 12 additions & 64 deletions app/api/data-fetching/[...slug]/route.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import { Exception } from '#models/exceptions';
import { hasRights } from '#models/user/rights';
import { ISession } from '#models/user/session';
import logErrorInSentry, { logInfoInSentry } from '#utils/sentry';
import { withAntiScrapping } from '#utils/server-side-helper/app/with-anti-bot';
import { APIHandler, APIPath, APIRoutesScope } from '../routesHandler';
import { APIPath, APIRoutesHandlers } from '../routes-handlers';
import { APIRoutesScopes } from '../routes-scopes';
import {
APIRouteError,
getRouteAndSlug,
withHandleError,
withIgnoreBot,
} from '../utils';

async function getRoute(
_request: Request,
params: { params: { slug: Array<string> } },
/* The `session` parameter in the `getRoute` function is an object of type `ISession` representing
the user session. It contains information about the current user's session, such as user details,
permissions, and any other relevant data needed to process the request. The `session` object is
used to check if the user has the necessary rights or scopes to access a specific API route before
handling the request. */
session: ISession
) {
const { slug, route } = getRouteAndSlug(params);

if (!(route in APIHandler)) {
if (!(route in APIRoutesHandlers)) {
throw new APIRouteError('API route not found', { route, slug }, 404);
}
const handler = APIHandler[route as APIPath];
const scope = APIRoutesScope[route as APIPath];
const handler = APIRoutesHandlers[route as APIPath];
const scope = APIRoutesScopes[route as APIPath];
if (!hasRights(session, scope)) {
throw new APIRouteError(
'User does not have the required scope for this API route',
Expand All @@ -35,55 +34,4 @@ async function getRoute(
return new Response(JSON.stringify(response), { status: 200 });
}

export const GET = withHandleError(withAntiScrapping(getRoute));

function withHandleError(handler: any) {
return async function (request: Request, params: any) {
try {
return await handler(request, params);
} catch (e: any) {
if (e instanceof APIRouteError) {
logInfoInSentry(e);
return new Response(e.message, { status: e.status });
}

let routeAndSlug;
try {
routeAndSlug = getRouteAndSlug(params);
} catch (e) {
routeAndSlug = { route: '', slug: '' };
}
const error = new APIRouteError(
'Internal Server Error',
routeAndSlug,
500,
e
);
logErrorInSentry(error);
return new Response(error.message, { status: error.status });
}
};
}

function getRouteAndSlug(params: { params: { slug: Array<string> } }) {
const slug = params.params.slug.at(-1) as string;
const route = params.params.slug.slice(0, -1).join('/');
return { route, slug };
}

class APIRouteError extends Exception {
constructor(
message: string,
context: { route: string; slug: string },
public status: 404 | 403 | 500,
cause?: any
) {
super({
name: 'APIRouteError',
message,
context: { page: context.route, slug: context.slug },
cause,
});
this.name = 'APIRouteError';
}
}
export const GET = withHandleError(withIgnoreBot(getRoute));
30 changes: 0 additions & 30 deletions app/api/data-fetching/association/[slug]/route.ts

This file was deleted.

36 changes: 0 additions & 36 deletions app/api/data-fetching/espace-agent/_helper.ts

This file was deleted.

This file was deleted.

24 changes: 0 additions & 24 deletions app/api/data-fetching/espace-agent/conformite/[slug]/route.ts

This file was deleted.

22 changes: 0 additions & 22 deletions app/api/data-fetching/espace-agent/opqibi/[slug]/route.ts

This file was deleted.

22 changes: 0 additions & 22 deletions app/api/data-fetching/espace-agent/qualibat/[slug]/route.ts

This file was deleted.

22 changes: 0 additions & 22 deletions app/api/data-fetching/espace-agent/qualifelec/[slug]/route.ts

This file was deleted.

Loading

0 comments on commit b32b9ad

Please sign in to comment.