Skip to content

Commit

Permalink
refactor(identite): extraxt getOrganizationInfo usecase
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasduteil committed Dec 24, 2024
1 parent 26b881e commit 4bd4375
Show file tree
Hide file tree
Showing 30 changed files with 762 additions and 564 deletions.
5 changes: 2 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/identite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"devDependencies": {
"@electric-sql/pglite": "^0.2.15",
"@gouvfr-lasuite/proconnect.core": "^0.2.0",
"@gouvfr-lasuite/proconnect.insee": "^0.2.0",
"@tsconfig/node22": "^22.0.0",
"@types/mocha": "^10.0.10",
"@types/node": "^22.10.2",
Expand Down
138 changes: 138 additions & 0 deletions packages/identite/src/organization/get-organization-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
//

import type { OrganizationInfo } from "#src/types";
import type {
FindBySirenHandler,
FindBySiretHandler,
} from "@gouvfr-lasuite/proconnect.insee/entreprises";
import {
formatAdresseEtablissement,
formatEnseigne,
formatNomComplet,
libelleFromCategoriesJuridiques,
libelleFromCodeEffectif,
libelleFromCodeNaf,
} from "@gouvfr-lasuite/proconnect.insee/formatters";
import type { InseeEtablissement } from "@gouvfr-lasuite/proconnect.insee/types";

//

export class InseeNotFoundError extends Error {}

export class InvalidSiretError extends Error {}

//
type FactoryDependencies = {
findBySiret: FindBySiretHandler;
findBySiren: FindBySirenHandler;
};

export function getOrganizationInfoFactory(ctx: FactoryDependencies) {
const { findBySiren, findBySiret } = ctx;

const siret = [/^\d{14}$/, findBySiret] as const;
const siren = [/^\d{9}$/, findBySiren] as const;

const strategies = [siret, siren];

return async function getOrganizationInfo(
siretOrSiren: string,
): Promise<OrganizationInfo> {
const [_, finder] =
strategies.find(([pattern]) => pattern.test(siretOrSiren)) ?? [];

if (!finder) {
throw new InvalidSiretError();
}

let etablissement = await finder(siretOrSiren);

const { statutDiffusionEtablissement } = etablissement;

if (statutDiffusionEtablissement === "N") {
throw new InseeNotFoundError();
}

return etablissementToOrganizationInfo(etablissement);
};
}

function etablissementToOrganizationInfo(
etablissement: InseeEtablissement,
): OrganizationInfo {
const {
adresseEtablissement,
anneeEffectifsEtablissement,
periodesEtablissement,
siret: siretFromInseeApi,
statutDiffusionEtablissement,
trancheEffectifsEtablissement,
uniteLegale,
} = etablissement;

const {
categorieJuridiqueUniteLegale,
denominationUniteLegale,
sigleUniteLegale,
nomUniteLegale,
nomUsageUniteLegale,
prenomUsuelUniteLegale,
trancheEffectifsUniteLegale,
} = uniteLegale;

// get last period to obtain most recent data
const {
activitePrincipaleEtablissement,
enseigne1Etablissement,
enseigne2Etablissement,
enseigne3Etablissement,
etatAdministratifEtablissement,
} = periodesEtablissement[0];

const { codePostalEtablissement, codeCommuneEtablissement } =
adresseEtablissement;

const enseigne = formatEnseigne(
enseigne1Etablissement,
enseigne2Etablissement,
enseigne3Etablissement,
);

const nomComplet = formatNomComplet({
denominationUniteLegale,
prenomUsuelUniteLegale,
nomUniteLegale,
nomUsageUniteLegale,
sigleUniteLegale,
});

const organizationLabel = `${nomComplet}${enseigne ? ` - ${enseigne}` : ""}`;

return {
siret: siretFromInseeApi,
libelle: organizationLabel,
nomComplet,
enseigne,
trancheEffectifs: trancheEffectifsEtablissement,
trancheEffectifsUniteLegale,
libelleTrancheEffectif:
libelleFromCodeEffectif(
trancheEffectifsEtablissement,
anneeEffectifsEtablissement,
) ?? "",
etatAdministratif: etatAdministratifEtablissement,
estActive: etatAdministratifEtablissement === "A",
statutDiffusion: statutDiffusionEtablissement,
estDiffusible: statutDiffusionEtablissement === "O",
adresse: formatAdresseEtablissement(adresseEtablissement),
codePostal: codePostalEtablissement,
codeOfficielGeographique: codeCommuneEtablissement,
activitePrincipale: activitePrincipaleEtablissement,
libelleActivitePrincipale: libelleFromCodeNaf(
activitePrincipaleEtablissement,
),
categorieJuridique: `${categorieJuridiqueUniteLegale}`,
libelleCategorieJuridique:
libelleFromCategoriesJuridiques(categorieJuridiqueUniteLegale) ?? "",
};
}
1 change: 1 addition & 0 deletions packages/identite/src/organization/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//

export * from "./get-organization-info.js";
export * from "./upsert.js";
1 change: 0 additions & 1 deletion packages/identite/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
export * from "./contexts.js";
export * from "./organization-info.js";
export * from "./organization.js";
export * from "./tranche-effectifs.js";
export * from "./user.js";
2 changes: 1 addition & 1 deletion packages/identite/src/types/organization-info.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//

import type { TrancheEffectifs } from "./tranche-effectifs.js";
import type { TrancheEffectifs } from "@gouvfr-lasuite/proconnect.insee/types";

//

Expand Down
2 changes: 1 addition & 1 deletion packages/identite/src/types/organization.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//

import type { TrancheEffectifs } from "./tranche-effectifs.js";
import type { TrancheEffectifs } from "@gouvfr-lasuite/proconnect.insee/types";

//

Expand Down
4 changes: 2 additions & 2 deletions packages/insee/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"type": "module",
"imports": {
"#src/*": {
"types": "./src/*",
"default": "./dist/*"
"types": "./dist/*/index.d.ts",
"default": "./dist/*/index.js"
}
},
"exports": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//

export type CategoriesJuridique = keyof typeof categoriesJuridiques;
export const categoriesJuridiques = {
1: "Entrepreneur individuel",
2: "Groupement de droit privé non doté de la personnalité morale",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { TrancheEffectifs } from "@gouvfr-lasuite/proconnect.identite/types";
//

import type { TrancheEffectifs } from "#src/types/tranche-effectifs.js";

//

export const codesEffectifs: { [K in NonNullable<TrancheEffectifs>]: string } =
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//

export type CodeNaf = keyof typeof codesNaf;

export const codesNaf = {
"01.11Z":
"Culture de céréales (à l’exception du riz), de légumineuses et de graines oléagineuses",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//

export type CodeVoie = keyof typeof codesVoies;
export const codesVoies = {
AIRE: "Aire",
ALL: "Allée",
Expand Down
6 changes: 6 additions & 0 deletions packages/insee/src/data/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//

export * from "./categories-juridiques.js";
export * from "./codes-effectifs.js";
export * from "./codes-naf.js";
export * from "./codes-voie.js";
Loading

0 comments on commit 4bd4375

Please sign in to comment.