-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(identite): extraxt getOrganizationInfo usecase
- Loading branch information
1 parent
26b881e
commit 4bd4375
Showing
30 changed files
with
762 additions
and
564 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
packages/identite/src/organization/get-organization-info.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) ?? "", | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
// | ||
|
||
export * from "./get-organization-info.js"; | ||
export * from "./upsert.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
...ctors/api-sirene/categories-juridiques.ts → ...s/insee/src/data/categories-juridiques.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 5 additions & 1 deletion
6
src/connectors/api-sirene/codes-effectifs.ts → packages/insee/src/data/codes-effectifs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/connectors/api-sirene/codes-naf.ts → packages/insee/src/data/codes-naf.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/connectors/api-sirene/codes-voie.ts → packages/insee/src/data/codes-voie.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
Oops, something went wrong.