Skip to content

Commit

Permalink
refactor: idcc using metadata file provided by API (#802)
Browse files Browse the repository at this point in the history
Co-authored-by: Johan Girod <[email protected]>
  • Loading branch information
XavierJp and johangirod authored Dec 11, 2023
1 parent 77fcc01 commit 092715a
Show file tree
Hide file tree
Showing 14 changed files with 239 additions and 3,942 deletions.
13 changes: 10 additions & 3 deletions clients/matomo/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import routes from '#clients/routes';
import constants from '#models/constants';
import { FetchRessourceException } from '#models/exceptions';
import { readFromGrist } from '#utils/integrations/grist';
import { httpGet } from '#utils/network';
Expand Down Expand Up @@ -145,9 +146,15 @@ export const clientMatomoStats = async (): Promise<IMatomoStats> => {
matomoEventsCategory,
npsRecords,
] = await Promise.all([
httpGet<IMatomoMonthlyStat[]>(createPageViewUrl()),
httpGet<IMatomoEventStat[]>(createCopyPasteEventUrl()),
httpGet<IMatomoEventStat[][]>(createEventsCategoryUrl()),
httpGet<IMatomoMonthlyStat[]>(createPageViewUrl(), {
timeout: constants.timeout.XXL,
}),
httpGet<IMatomoEventStat[]>(createCopyPasteEventUrl(), {
timeout: constants.timeout.XXL,
}),
httpGet<IMatomoEventStat[][]>(createEventsCategoryUrl(), {
timeout: constants.timeout.XXL,
}),
getNpsRecords(),
]);

Expand Down
64 changes: 64 additions & 0 deletions clients/recherche-entreprise/idcc-metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { HttpNotFound, HttpServerError } from '#clients/exceptions';
import routes from '#clients/routes';
import { ICCWithMetadata } from '#models/conventions-collectives-list';
import { httpGet } from '#utils/network';

type IIdccMetadata = {
[idcc: string]: {
'titre de la convention': string; //Convention collective na… des cabinets d'avocats"
id_kali: string; //KALICONT000005635185"
cc_ti: string; //IDCC"
nature: string; //CONVENTION COLLECTIVE NATIONALE"
etat: string; //VIGUEUR_ETEN"
debut: string; //1979-03-01 00:00:00"
fin: string | null;
url: string; //https://www.legifrance.g…/id/KALICONT000005635185"
};
};

class IdCCMetadataClient {
private _idccMetadata = null as {
[idcc: string]: ICCWithMetadata;
} | null;

get = async (idcc: string) => {
if (!this._idccMetadata) {
const response = await httpGet<IIdccMetadata>(
routes.conventionsCollectives.metadata
);

this._idccMetadata = this.mapToDomainObject(response);
}

if (Object.values(this._idccMetadata).length === 0) {
throw new HttpServerError('Empty Idcc metadata list');
}

if (idcc in this._idccMetadata) {
return this._idccMetadata[idcc] || {};
} else {
throw new HttpNotFound('Convention collective not found');
}
};

mapToDomainObject = (response: IIdccMetadata) => {
return Object.entries(response).reduce(
(idccMetadatas, [idcc, metadata]) => {
const { id_kali, url, nature, etat } = metadata;
idccMetadatas[idcc] = {
idKali: id_kali,
legifrance: url,
title: metadata['titre de la convention'],
nature,
etat,
idcc,
};
return idccMetadatas;
},
{} as { [idcc: string]: ICCWithMetadata }
);
};
}
const client = new IdCCMetadataClient();

export const clientIdccMetadata = async (idcc: string) => client.get(idcc);
26 changes: 11 additions & 15 deletions clients/recherche-entreprise/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import { HttpNotFound } from '#clients/exceptions';
import routes from '#clients/routes';
import stubClientWithSnapshots from '#clients/stub-client-with-snaphots';
import constants from '#models/constants';
import { IConventionsCollectives } from '#models/conventions-collectives-list';
import { createEtablissementsList } from '#models/etablissements-list';
import { IETATADMINSTRATIF, estActif } from '#models/etat-administratif';
import { IEtatCivil, IPersonneMorale } from '#models/immatriculation';
import {
IConventionCollective,
IEtablissement,
NotEnoughParamsException,
createDefaultEtablissement,
createDefaultUniteLegale,
} from '#models/index';
import { ISearchResult, ISearchResults } from '#models/search';
import { ISearchResults } from '#models/search';
import SearchFilterParams from '#models/search-filter-params';
import {
extractNicFromSiret,
Expand All @@ -23,7 +23,6 @@ import {
verifySiret,
} from '#utils/helpers';
import {
getConventionCollectives,
libelleFromCategoriesJuridiques,
libelleFromCodeNAFWithoutNomenclature,
} from '#utils/helpers/formatting/labels';
Expand Down Expand Up @@ -120,10 +119,7 @@ const mapToDomainObjectNew = (
};
};

const mapToUniteLegale = (
result: IResult,
pageEtablissements: number
): ISearchResult => {
const mapToUniteLegale = (result: IResult, pageEtablissements: number) => {
const {
nature_juridique,
siege,
Expand Down Expand Up @@ -245,8 +241,14 @@ const mapToUniteLegale = (
colter,
dateCreation: parseDateCreationInsee(date_creation),
dateDerniereMiseAJour: date_mise_a_jour,
conventionsCollectives: etablissementsList.all.flatMap(
(e) => e.conventionsCollectives
conventionsCollectives: etablissements.reduce(
(idccSiretPair, { siret, liste_idcc }) => {
(liste_idcc || []).forEach((idcc) => {
idccSiretPair[idcc] = [...(idccSiretPair[idcc] || []), siret];
});
return idccSiretPair;
},
{} as IConventionsCollectives
),
};
};
Expand Down Expand Up @@ -312,7 +314,6 @@ const mapToEtablissement = (
activite_principale = '',
date_creation = '',
date_debut_activite = '',
liste_idcc,
tranche_effectif_salarie = '',
annee_tranche_effectif_salarie = '',
} = etablissement;
Expand Down Expand Up @@ -352,11 +353,6 @@ const mapToEtablissement = (
activitePrincipale: activite_principale,
dateCreation: parseDateCreationInsee(date_creation),
dateDebutActivite: date_debut_activite,
conventionsCollectives: (liste_idcc || [])
.map((idcc) => {
return getConventionCollectives(idcc, siret);
})
.filter((cc): cc is IConventionCollective => !!cc),
};
};

Expand Down
2 changes: 2 additions & 0 deletions clients/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const routes = {
},
},
conventionsCollectives: {
metadata:
'https://recherche-entreprises.api.gouv.fr/metadata/conventions_collectives',
site: 'https://code.travail.gouv.fr/outils/convention-collective',
details: 'https://code.travail.gouv.fr/convention-collective/',
},
Expand Down
140 changes: 86 additions & 54 deletions components/conventions-collectives-section/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@ import ButtonLink from '#components-ui/button';
import FAQLink from '#components-ui/faq-link';
import { Tag } from '#components-ui/tag';
import { MTPEI } from '#components/administrations';
import { Section } from '#components/section';
import { DataSection } from '#components/section/data-section';
import { FullTable } from '#components/table/full';
import { EAdministration } from '#models/administrations';
import { formatSiret } from '#utils/helpers';
import { IUniteLegale } from '#models/index';
import { IAPINotRespondingError } from '#models/api-not-responding';
import { ICCWithMetadata } from '#models/conventions-collectives-list';
import { capitalize, formatSiret } from '#utils/helpers';

const ConventionsCollectivesSection: React.FC<{
uniteLegale: IUniteLegale;
}> = ({ uniteLegale }) => {
const conventionsCollectives = uniteLegale?.conventionsCollectives || [];

const plural = conventionsCollectives.length > 0 ? 's' : '';

ccWithMetadata: ICCWithMetadata[] | IAPINotRespondingError;
}> = ({ ccWithMetadata }) => {
return (
<Section title="Conventions collectives" sources={[EAdministration.MTPEI]}>
{conventionsCollectives.length === 0 ? (
<DataSection
title="Conventions collectives"
sources={[EAdministration.MTPEI]}
notFoundInfo={
<div>
Cette structure n’a pas de{' '}
<a
Expand All @@ -31,50 +30,83 @@ const ConventionsCollectivesSection: React.FC<{
</a>{' '}
auprès du <MTPEI />.
</div>
) : (
<>
Cette structure possède {conventionsCollectives.length}{' '}
<FAQLink
to="/faq/convention-collective"
tooltipLabel={`convention${plural} collective${plural}`}
>
Qu’est-ce qu’une convention collective ?
</FAQLink>{' '}
enregistrée{plural}.
<p>
Pour en savoir plus sur une convention collective en particulier,
consultez{' '}
<a
rel="noreferrer noopener"
target="_blank"
href="https://code.travail.gouv.fr/outils/convention-collective"
}
data={ccWithMetadata}
>
{(ccWithMetadata) => {
const plural = ccWithMetadata.length > 0 ? 's' : '';
return (
<>
Cette structure possède {ccWithMetadata.length}{' '}
<FAQLink
to="/faq/convention-collective"
tooltipLabel={`convention${plural} collective${plural}`}
>
le site du Code du Travail Numérique.
</a>
</p>
<FullTable
head={['SIRET', 'N°IDCC', 'Détails', 'Explications']}
body={conventionsCollectives.map((convention) => [
<a href={`/etablissement/${convention.siret}`}>
{formatSiret(convention.siret)}
</a>,
<Tag>IDCC {convention.idcc}</Tag>,
<i className="font-small">{convention.title}</i>,
convention.CdTN ? (
<ButtonLink
target="_blank"
to={`${routes.conventionsCollectives.details}${convention.idcc}`}
alt
small
>
⇢&nbsp;Consulter
</ButtonLink>
) : null,
])}
/>
</>
)}
</Section>
Qu’est-ce qu’une convention collective ?
</FAQLink>{' '}
enregistrée{plural}.
<p>
Pour en savoir plus sur une convention collective en particulier,
consultez{' '}
<a
rel="noreferrer noopener"
target="_blank"
href="https://code.travail.gouv.fr/outils/convention-collective"
>
le site du Code du Travail Numérique.
</a>
</p>
{ccWithMetadata && ccWithMetadata.length > 0 && (
<FullTable
head={['N°IDCC', 'Détails', 'Etablissement(s)', 'Explications']}
body={ccWithMetadata.map(
({ idcc, sirets = [], nature, title }) => [
<Tag>IDCC {idcc}</Tag>,
<>
{nature && (
<>
<b className="font-small">{capitalize(nature)}</b>
<br />
</>
)}
<span className="font-small">
{title || <i>Non renseigné</i>}
</span>
</>,
<ul>
{(sirets || []).map((siret) => (
<li>
<a href={`/etablissement/${siret}`}>
{formatSiret(siret)}
</a>
</li>
))}
</ul>,
<>
{idcc === '9999' ? (
<i>Non renseigné</i>
) : (
<ButtonLink
target="_blank"
to={`${routes.conventionsCollectives.details}${idcc}`}
alt
aria-label={`Convention collective ${
title || idcc
}, consulter les informations`}
small
>
⇢&nbsp;Consulter
</ButtonLink>
)}
</>,
]
)}
/>
)}
</>
);
}}
</DataSection>
);
};
export default ConventionsCollectivesSection;
3 changes: 2 additions & 1 deletion components/title-section/tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ export const Tabs: React.FC<{
label: 'Conventions collectives',
pathPrefix: '/divers/',
noFollow: false,
shouldDisplay: (uniteLegale.conventionsCollectives || []).length > 0,
shouldDisplay:
Object.keys(uniteLegale.conventionsCollectives || {}).length > 0,
width: '130px',
},
];
Expand Down
4 changes: 2 additions & 2 deletions components/unite-legale-section/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ const UniteLegaleSection: React.FC<{
}> = ({ uniteLegale, session }) => {
const hasLabelsAndCertificates = checkHasLabelsAndCertificates(uniteLegale);

const conventionsCollectives = Array.from(
new Set((uniteLegale.conventionsCollectives || []).map((cc) => cc.idcc))
const conventionsCollectives = Object.keys(
uniteLegale.conventionsCollectives || {}
);

const data = [
Expand Down
Loading

0 comments on commit 092715a

Please sign in to comment.