Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: POC RNF #751

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions clients/api-rnf/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import routes from '#clients/routes';
import { IETATADMINSTRATIF } from '#models/etat-administratif';
import { IFondation } from '#models/fondations';
import { IdRnf } from '#utils/helpers/id-rnf';
import { httpGet } from '#utils/network';

type IRNFResponse = {
createdAt: string;
updatedAt: string;
rnfId: string; //"075-FDD-00003-01"
type: string;
department: string;
title: string;
dissolvedAt: string | null;
phone: string;
email: string;
address: {
createdAt: string;
updatedAt: string;
label: string;
type: string;
streetAddress: string;
streetNumber: string;
streetName: string;
postalCode: string;
cityName: string;
cityCode: string;
departmentName: string;
departmentCode: string;
regionName: string;
regionCode: string;
};
status: unknown;
persons: unknown[];
};

/**
* Call RNF API using an Id RNF
* @param idRnf
*/
const clientRNF = async (idRnf: IdRnf, useCache = true) => {
const url = `${routes.rnf}${idRnf}`;
const response = await httpGet<IRNFResponse>(url, { useCache });

return mapToDomainObject(idRnf, response);
};

const mapToDomainObject = (
idRnf: IdRnf,
response: IRNFResponse
): IFondation => {
const {
title,
dissolvedAt,
address,
department,
type,
createdAt,
phone,
email,
} = response;

return {
idRnf,
nomComplet: title,
etatAdministratif: !dissolvedAt
? IETATADMINSTRATIF.ACTIF
: IETATADMINSTRATIF.CESSEE,
adresse: address.label,
departement: department,
type,
dateCreation: createdAt.split('T')[0],
dateFermeture: (dissolvedAt || '').split('T')[0],
telephone: phone,
email,
};
};

export { clientRNF };
1 change: 1 addition & 0 deletions clients/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const routes = {
'https://geo.api.gouv.fr/departements?fields=code&format=json&zone=metro,drom,com',
region: 'https://geo.api.gouv.fr/regions?limit=3',
},
rnf: 'https://rnf.dso.numerique-interieur.com/api/foundations/',
journalOfficielAssociations: {
ods: {
metadata:
Expand Down
17 changes: 17 additions & 0 deletions components-ui/badge/frequent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ export const AssociationBadge = ({
backgroundColor="#e5d2f9"
/>
);

export const FondationBadge = ({
small = false,
isSelected = false,
onClick,
}: IPartialBadgeProps) => (
<Badge
small={small}
onClick={onClick}
icon="communityFill"
isSelected={isSelected}
label="Fondation"
fontColor="#3d0d71"
backgroundColor="#ffb8e3"
/>
);

export const EntrepriseIndividuelleBadge = ({
small = false,
isSelected = false,
Expand Down
43 changes: 43 additions & 0 deletions models/fondations/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { clientRNF } from '#clients/api-rnf';
import { HttpNotFound } from '#clients/exceptions';
import { IETATADMINSTRATIF } from '#models/etat-administratif';
import { IdRnf, verifyIdRnf } from '#utils/helpers/id-rnf';
import logErrorInSentry from '#utils/sentry';
import { NotAnIdRnfError, RnfNotFoundError } from '..';

export type IFondation = {
idRnf: IdRnf;
etatAdministratif: IETATADMINSTRATIF;
nomComplet: string;
adresse: string;
departement: string;
type: string;
dateCreation: string;
dateFermeture: string;
telephone: string;
email: string;
};

/**
* Get a fondation if it exists
* @param slug
* @returns
*/
export const getFondationFromSlug = async (slug: string) => {
try {
const idRnf = verifyIdRnf(slug);
return await clientRNF(idRnf);
} catch (e: any) {
if (e instanceof HttpNotFound) {
throw new RnfNotFoundError(slug);
} else if (e instanceof NotAnIdRnfError) {
throw e;
}

logErrorInSentry(e, {
details: slug,
errorName: 'Error in API RNF',
});
throw e;
}
};
4 changes: 2 additions & 2 deletions models/immatriculation/joafe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { IdRna, Siren, verifyIdRna } from '#utils/helpers';
import logErrorInSentry from '#utils/sentry';
import { IImmatriculation } from '.';
import { NotAValidIdRnaError } from '..';
import { NotAnIdRnaError } from '..';

export interface IImmatriculationJOAFE extends IImmatriculation {
siren: Siren;
Expand Down Expand Up @@ -41,7 +41,7 @@ export const getImmatriculationJOAFE = async (
siteLink: annonceCreation.path,
} as IImmatriculationJOAFE;
} catch (e: any) {
if (e instanceof HttpNotFound || e instanceof NotAValidIdRnaError) {
if (e instanceof HttpNotFound || e instanceof NotAnIdRnaError) {
return APINotRespondingFactory(EAdministration.DILA, 404);
}

Expand Down
26 changes: 25 additions & 1 deletion models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,31 @@ export class NotASiretError extends Error {
/**
* This is not a valid IdRna
*/
export class NotAValidIdRnaError extends Error {
export class NotAnIdRnaError extends Error {
constructor(public message: string) {
super();
}
Comment on lines +322 to +324
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can omit the constructor...

}

/**
* This is not a valid IdRnf
*/
export class NotAnIdRnfError extends Error {
constructor(public message: string) {
super();
}
}

/**
* This is a valid siren but it was not found
*/
export class RnfNotFoundError extends Error {
constructor(public message: string) {
super();
}
}

export class IsLikelyAnIdRnfException extends Error {
constructor(public message: string) {
super();
}
Expand Down
32 changes: 26 additions & 6 deletions models/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import {
escapeTerm,
isLikelyASiretOrSiren,
} from '#utils/helpers';
import { isIdRnfValid } from '#utils/helpers/id-rnf';
import { isProtectedSiren } from '#utils/helpers/is-protected-siren-or-siret';
import { logWarningInSentry } from '#utils/sentry';
import {
IEtablissement,
IsLikelyASirenOrSiretException,
IUniteLegale,
IsLikelyASirenOrSiretException,
IsLikelyAnIdRnfException,
NotEnoughParamsException,
SearchEngineError,
} from '.';
Expand Down Expand Up @@ -42,17 +44,32 @@ const noResults = {
notEnoughParams: false,
};

/**
* Checks if term looks like a siren, siret or rnf
* @param term
*/
const formatChecks = (term: string) => {
const likelyASiretOrSiren = isLikelyASiretOrSiren(term);

if (likelyASiretOrSiren) {
throw new IsLikelyASirenOrSiretException(term);
}

const isLikelyAnIdRnf = isIdRnfValid(term);

if (isLikelyAnIdRnf) {
throw new IsLikelyAnIdRnfException(term);
}
};

const search = async (
searchTerm: string,
page: number,
searchFilterParams: SearchFilterParams
) => {
const cleanedTerm = cleanSearchTerm(searchTerm);
const likelyASiretOrSiren = isLikelyASiretOrSiren(cleanedTerm);

if (likelyASiretOrSiren) {
throw new IsLikelyASirenOrSiretException(cleanedTerm);
}
formatChecks(cleanedTerm);

try {
const escapedSearchTerm = escapeTerm(searchTerm);
Expand All @@ -69,7 +86,10 @@ const search = async (
return { ...noResults, badParams: true };
}

if (e instanceof IsLikelyASirenOrSiretException) {
if (
e instanceof IsLikelyASirenOrSiretException ||
e instanceof IsLikelyAnIdRnfException
) {
throw e;
}
if (e instanceof HttpNotFound) {
Expand Down
115 changes: 115 additions & 0 deletions pages/fondation/[slug].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { GetServerSideProps } from 'next';
import { FondationBadge } from '#components-ui/badge/frequent';
import { HorizontalSeparator } from '#components-ui/horizontal-separator';
import IsActiveTag from '#components-ui/is-active-tag';
import Meta from '#components/meta';
import { DataSection } from '#components/section/data-section';
import { TwoColumnTable } from '#components/table/simple';
import { EAdministration } from '#models/administrations';
import { IFondation, getFondationFromSlug } from '#models/fondations';
import { ISTATUTDIFFUSION } from '#models/statut-diffusion';
import { formatDate, formatIntFr } from '#utils/helpers';
import { libelleFromDepartement } from '#utils/helpers/formatting/labels';
import extractParamsFromContext from '#utils/server-side-props-helper/extract-params-from-context';
import {
IPropsWithMetadata,
postServerSideProps,
} from '#utils/server-side-props-helper/post-server-side-props';
import { NextPageWithLayout } from 'pages/_app';

interface IProps extends IPropsWithMetadata {
fondation: IFondation;
}

const FondationPage: NextPageWithLayout<IProps> = ({ fondation }) => (
<>
<Meta
title={fondation.nomComplet}
noIndex={true}
canonical={`https://annuaire-entreprises.data.gouv.fr/fondation/${fondation.idRnf}`}
/>

<h1>{fondation.nomComplet}</h1>
<div className="fondation-sub-title">
<FondationBadge />
<span className="siren">&nbsp;‣&nbsp;{formatIntFr(fondation.idRnf)}</span>
<span>
<IsActiveTag
etatAdministratif={fondation.etatAdministratif}
statutDiffusion={ISTATUTDIFFUSION.DIFFUSIBLE}
/>
</span>
</div>

<div className="content-container">
<DataSection
title="Répertoire National des fondations"
sources={[EAdministration.MI]}
data={fondation}
>
{(fondation) => (
<>
<TwoColumnTable
body={[
['N°RNF', fondation.idRnf],
['Nom', fondation.nomComplet],
[
'Département',
`${libelleFromDepartement(fondation.departement)} (${
fondation.departement
})`,
],
['Adresse', fondation.adresse],
['Date de création', formatDate(fondation.dateCreation)],
...(fondation.dateFermeture
? [['Date de fermeture', formatDate(fondation.dateCreation)]]
: []),
[
'Téléphone',
fondation.telephone ? (
<a href={`tel:${fondation.telephone}`}>
{fondation.telephone}
</a>
) : (
''
),
],
[
'Email',
fondation.email ? (
<a href={`mailto:${fondation.email}`}>{fondation.email}</a>
) : (
''
),
],
]}
/>
</>
)}
</DataSection>
</div>
<HorizontalSeparator />
<style jsx>{`
.fondation-sub-title {
display: flex;
align-items: center;
}
`}</style>
</>
);

export const getServerSideProps: GetServerSideProps = postServerSideProps(
async (context) => {
const { slug } = extractParamsFromContext(context, true);

const fondation = await getFondationFromSlug(slug);

return {
props: {
fondation,
},
};
}
);

export default FondationPage;
Loading