Skip to content

Commit

Permalink
feat: enable join non diffusible organization
Browse files Browse the repository at this point in the history
  • Loading branch information
rdubigny committed Jul 28, 2023
1 parent 85e542b commit e33dee8
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,8 @@ document.addEventListener("DOMContentLoaded", function() {
var libelle = response.organizationInfo.libelle;
var adresse = response.organizationInfo.adresse;
var libelleActivitePrincipale = response.organizationInfo.libelleActivitePrincipale;
var estDiffusible = response.organizationInfo.estDiffusible;
var estActive = response.organizationInfo.estActive;
if (!estDiffusible) {
organizationAlertElement.style.display = "block";
organizationAlertElement.innerHTML = "Cet établissement est non-diffusible. Merci de le rendre diffusible pour pouvoir vous créer un compte. <a href='https://annuaire-entreprises.data.gouv.fr/etablissement/" + siret + "?mtm_campaign=moncomptepro'>Plus d'info.</a>";
} else if (estActive) {
if (estActive) {
organizationInfoElement.style.display = "block";
organizationInfoLibelleElement.innerHTML = libelle;
organizationInfoAdresseElement.innerHTML = adresse;
Expand Down
7 changes: 3 additions & 4 deletions src/connectors/api-sirene/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ export const formatNomComplet = ({
nomUsageUniteLegale,
sigleUniteLegale,
}) => {
const formattedFirstName = formatFirstNames([prenomUsuelUniteLegale]);
const formattedName = formatNameFull(nomUniteLegale, nomUsageUniteLegale);
return `${capitalize(denominationUniteLegale) ||
`${formatFirstNames([prenomUsuelUniteLegale])} ${formatNameFull(
nomUniteLegale,
nomUsageUniteLegale
)}` ||
[formattedFirstName, formattedName].filter(e => !!e).join(' ') ||
'Nom inconnu'}${sigleUniteLegale ? ` (${sigleUniteLegale})` : ''}`;
};

Expand Down
107 changes: 101 additions & 6 deletions src/connectors/api-sirene/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
INSEE_CONSUMER_KEY,
INSEE_CONSUMER_SECRET,
} from '../../env';
import { cloneDeep, set } from 'lodash';

type ApiInseeResponse = {
etablissement: {
Expand Down Expand Up @@ -152,6 +153,95 @@ type ApiInseeResponse = {
};
};

const hideNonDiffusibleData = (
etablissement: ApiInseeResponse['etablissement']
): ApiInseeResponse['etablissement'] => {
const hiddenEtablissement = cloneDeep(etablissement);
set(hiddenEtablissement, 'uniteLegale.denominationUniteLegale', null);
set(hiddenEtablissement, 'uniteLegale.sigleUniteLegale', null);
set(hiddenEtablissement, 'uniteLegale.denominationUsuelle1UniteLegale', null);
set(hiddenEtablissement, 'uniteLegale.denominationUsuelle2UniteLegale', null);
set(hiddenEtablissement, 'uniteLegale.denominationUsuelle3UniteLegale', null);
set(hiddenEtablissement, 'uniteLegale.sexeUniteLegale', null);
set(hiddenEtablissement, 'uniteLegale.nomUniteLegale', null);
set(hiddenEtablissement, 'uniteLegale.nomUsageUniteLegale', null);
set(hiddenEtablissement, 'uniteLegale.prenom1UniteLegale', null);
set(hiddenEtablissement, 'uniteLegale.prenom2UniteLegale', null);
set(hiddenEtablissement, 'uniteLegale.prenom3UniteLegale', null);
set(hiddenEtablissement, 'uniteLegale.prenom4UniteLegale', null);
set(hiddenEtablissement, 'uniteLegale.prenomUsuelUniteLegale', null);
set(hiddenEtablissement, 'uniteLegale.pseudonymeUniteLegale', null);
set(
hiddenEtablissement,
'adresseEtablissement.complementAdresseEtablissement',
null
);
set(
hiddenEtablissement,
'adresseEtablissement.numeroVoieEtablissement',
null
);
set(
hiddenEtablissement,
'adresseEtablissement.indiceRepetitionEtablissement',
null
);
set(hiddenEtablissement, 'adresseEtablissement.typeVoieEtablissement', null);
set(
hiddenEtablissement,
'adresseEtablissement.libelleVoieEtablissement',
null
);
set(
hiddenEtablissement,
'adresse2Etablissement.complementAdresse2Etablissement',
null
);
set(
hiddenEtablissement,
'adresse2Etablissement.numeroVoie2Etablissement',
null
);
set(
hiddenEtablissement,
'adresse2Etablissement.indiceRepetition2Etablissement',
null
);
set(
hiddenEtablissement,
'adresse2Etablissement.typeVoie2Etablissement',
null
);
set(
hiddenEtablissement,
'adresse2Etablissement.libelleVoie2Etablissement',
null
);

set(
hiddenEtablissement,
'periodesEtablissement.0.enseigne1Etablissement',
null
);
set(
hiddenEtablissement,
'periodesEtablissement.0.enseigne2Etablissement',
null
);
set(
hiddenEtablissement,
'periodesEtablissement.0.enseigne3Etablissement',
null
);
set(
hiddenEtablissement,
'periodesEtablissement.0.denominationUsuelleEtablissement',
null
);

return hiddenEtablissement;
};

export const getOrganizationInfo = async (
siret: string
): Promise<OrganizationInfo> => {
Expand All @@ -171,7 +261,7 @@ export const getOrganizationInfo = async (
}
);

const {
let {
data: { etablissement },
}: AxiosResponse<ApiInseeResponse> = await axios.get(
`https://api.insee.fr/entreprises/sirene/V3/siret/${siret}`,
Expand All @@ -181,20 +271,25 @@ export const getOrganizationInfo = async (
}
);

const { statutDiffusionEtablissement } = etablissement;

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

if (statutDiffusionEtablissement === 'P') {
etablissement = hideNonDiffusibleData(etablissement);
}

const {
siret: siretFromInseeApi,
trancheEffectifsEtablissement,
anneeEffectifsEtablissement,
adresseEtablissement,
statutDiffusionEtablissement,
periodesEtablissement,
uniteLegale,
} = etablissement;

if (statutDiffusionEtablissement !== 'O') {
throw new InseeNotFoundError();
}

const {
categorieJuridiqueUniteLegale,
denominationUniteLegale,
Expand Down
2 changes: 1 addition & 1 deletion src/views/user/join-organization.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,5 @@
</div>
</div>
</div>
<script src="/assets/fetch-organization-info_20230120.js"></script>
<script src="/assets/fetch-organization-info_20230728.js"></script>
<script src="/assets/submit-tile-forms.js"></script>
28 changes: 23 additions & 5 deletions test/api-sirene.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,32 @@ describe('getOrganizationInfo', () => {
});
});

it('should throw for partially non diffusible établissement', async () => {
it('should show partial data for partially non diffusible établissement', async () => {
nock('https://api.insee.fr')
.get('/entreprises/sirene/V3/siret/94957325700019')
.reply(200, partiallyNonDiffusible);
await assert.isRejected(
getOrganizationInfo('94957325700019'),
InseeNotFoundError
);

await assert.eventually.deepEqual(getOrganizationInfo('94957325700019'), {
siret: '94957325700019',
libelle: 'Nom inconnu',
nomComplet: 'Nom inconnu',
enseigne: '',
trancheEffectifs: null,
trancheEffectifsUniteLegale: null,
libelleTrancheEffectif: null,
etatAdministratif: 'A',
estActive: true,
statutDiffusion: 'P',
estDiffusible: false,
adresse: '06220 Vallauris',
codePostal: '06220',
codeOfficielGeographique: '06155',
activitePrincipale: '62.02A',
libelleActivitePrincipale:
'62.02A - Conseil en systèmes et logiciels informatiques',
categorieJuridique: '1000',
libelleCategorieJuridique: 'Entrepreneur individuel',
});
});

it('should throw for totally non diffusible établissement', async () => {
Expand Down

0 comments on commit e33dee8

Please sign in to comment.