Skip to content

Commit

Permalink
refactor: etablissement fetching logic
Browse files Browse the repository at this point in the history
  • Loading branch information
XavierJp committed Aug 14, 2024
1 parent 468dd23 commit 5d773dd
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 51 deletions.
18 changes: 9 additions & 9 deletions clients/sirene-insee/siren.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ type TmpUniteLegale = {
denomination: string;
denominationUsuelle: string;
sigle: string;
allSiegesSiret: Siret[];
};
};

Expand Down Expand Up @@ -114,7 +113,8 @@ const clientUniteLegaleInsee = async (

const etablissementsList = allEtablissements?.list || [siege];
etablissementsList.forEach(
(e) => (e.ancienSiege = tmpUniteLegale.allSiegesSiret.indexOf(e.siret) > -1)
(e) =>
(e.ancienSiege = uniteLegale.anciensSiegesSirets.indexOf(e.siret) > -1)
);

const etablissements = createEtablissementsList(
Expand Down Expand Up @@ -241,6 +241,13 @@ const mapToDomainObject = (
siren,
oldSiren: originalSiren,
siege,
anciensSiegesSirets: Array.from(
new Set(
periodesUniteLegale
.map((e) => (siren + e.nicSiegeUniteLegale) as Siret)
.filter((e) => e !== siege.siret)
)
),
natureJuridique: categorieJuridiqueUniteLegale || '',
libelleNatureJuridique: libelleFromCategoriesJuridiques(
categorieJuridiqueUniteLegale
Expand Down Expand Up @@ -280,13 +287,6 @@ const mapToDomainObject = (
denominationUsuelle: denominationUsuelleUniteLegale,
denomination: denominationUniteLegale || names || 'Nom inconnu',
sigle: sigleUniteLegale,
allSiegesSiret: Array.from(
new Set(
periodesUniteLegale.map(
(e) => (siren + e.nicSiegeUniteLegale) as Siret
)
)
),
},
};
};
Expand Down
88 changes: 66 additions & 22 deletions models/core/etablissement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ import {
verifySiret,
} from '#utils/helpers';
import { isProtectedSiren } from '#utils/helpers/is-protected-siren-or-siret';
import logErrorInSentry, { logFatalErrorInSentry } from '#utils/sentry';
import logErrorInSentry, {
logFatalErrorInSentry,
logWarningInSentry,
} from '#utils/sentry';
import getSession from '#utils/server-side-helper/app/get-session';
import { shouldUseInsee } from '.';
import { EAdministration } from '../administrations/EAdministration';
import {
APINotRespondingFactory,
IAPINotRespondingError,
isAPI404,
isAPINotResponding,
} from '../api-not-responding';
import { FetchRessourceException, IExceptionContext } from '../exceptions';
Expand Down Expand Up @@ -81,6 +85,9 @@ const fetchFromClients = async (
);

if (!useInsee) {
if (isAPI404(etablissementRechercheEntreprise)) {
throw new SiretNotFoundError(siret);
}
if (isAPINotResponding(etablissementRechercheEntreprise)) {
throw new HttpServerError('Recherche failed, return 500');
}
Expand All @@ -92,35 +99,57 @@ const fetchFromClients = async (
useCache,
});

/**
* Nowhere to be found
*/
if (
isAPI404(etablissementRechercheEntreprise) &&
isAPI404(etablissementInsee)
) {
throw new SiretNotFoundError(siret);
}

if (isAPINotResponding(etablissementInsee)) {
if (isAPINotResponding(etablissementRechercheEntreprise)) {
const etablissmentInseeFallbacked = await fetchEtablissmentFromInsee(
siret,
{
useFallback: true,
useCache,
}
);
if (isAPINotResponding(etablissmentInseeFallbacked)) {
throw new HttpServerError('Sirene Insee fallback failed, return 500');
}
return etablissmentInseeFallbacked;
/***
* Sirene Insee failed
*/
if (isAPI404(etablissementRechercheEntreprise)) {
throw new SiretNotFoundError(siret);
} else if (isAPINotResponding(etablissementRechercheEntreprise)) {
throw new HttpServerError('Both API failed');
} else {
return etablissementRechercheEntreprise;
}
} else {
if (isAPINotResponding(etablissementRechercheEntreprise)) {
/**
* Sirene succeed but siret is not in recherhce or recherche failed
*/
if (
isAPINotResponding(etablissementRechercheEntreprise) ||
isAPI404(etablissementRechercheEntreprise)
) {
logWarningInSentry(
new FetchRessourceException({
ressource: 'UniteLegaleRecherche',
administration: EAdministration.DINUM,
message: 'Fail to find siret in recherche API',
context: {
siret,
},
})
);
return etablissementInsee;
} else {
return {
...etablissementInsee,
complements: {
...etablissementInsee?.complements,
...etablissementRechercheEntreprise.complements,
},
};
}
}

// default case
return {
...etablissementInsee,
complements: {
...etablissementInsee?.complements,
...etablissementRechercheEntreprise.complements,
},
};
};

const fetchEtablissmentFromInsee = async (
Expand All @@ -137,6 +166,13 @@ const fetchEtablissmentFromInsee = async (
throw new SiretNotFoundError(siret);
}

if (!options.useFallback) {
return await clientEtablissementInsee(siret, {
...options,
useFallback: true,
});
}

logErrorInSentry(
new FetchEtablissementException({
message: 'Fail to fetch from INSEE API',
Expand Down Expand Up @@ -211,20 +247,28 @@ const getEtablissementWithUniteLegaleFromSlug = async (
const etablissement = await getEtablissementFromSlug(slug, {
isBot,
});

const uniteLegale = await getUniteLegaleFromSlug(etablissement.siren, {
isBot,
});

// only for insee as recherche already holds ancienSiege at etablissement level
if (uniteLegale.anciensSiegesSirets.indexOf(etablissement.siret)) {
etablissement.ancienSiege = true;
}

return { etablissement, uniteLegale };
};

/**
* Download Etablissement and the Latitude/longitude
*
*/
const getEtablissementWithLatLongFromSlug = async (
slug: string
): Promise<IEtablissement> => {
const etablissement = await getEtablissementFromSlug(slug);

if (!etablissement.latitude || !etablissement.longitude) {
const { lat, long } = await getGeoLoc(etablissement);
etablissement.latitude = lat;
Expand Down
3 changes: 3 additions & 0 deletions models/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ export interface IUniteLegale extends IEtablissementsList {
oldSiren: Siren;
tva: ITVAIntracommunautaire | null;
siege: IEtablissement;
// only used to pass information from unitelegale to the etablissement in insee response. Prefer etablissement.ancienSiege
anciensSiegesSirets: Siret[];
natureJuridique: string;
libelleNatureJuridique: string;
activitePrincipale: string;
Expand Down Expand Up @@ -124,6 +126,7 @@ export const createDefaultUniteLegale = (siren: Siren): IUniteLegale => {
oldSiren: siren,
siege,
tva: null,
anciensSiegesSirets: [],
statutDiffusion: ISTATUTDIFFUSION.DIFFUSIBLE,
etatAdministratif: IETATADMINSTRATIF.INCONNU,
nomComplet: '',
Expand Down
50 changes: 30 additions & 20 deletions models/core/unite-legale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,34 +161,37 @@ class UniteLegaleBuilder {
return uniteLegaleRechercheEntreprise;
}

/**
* Siren in Sirene API, not in stocks
*/
if (
isAPI404(uniteLegaleRechercheEntreprise) &&
!isAPINotResponding(uniteLegaleInsee)
) {
return uniteLegaleInsee;
}

/***
* Sirene Insee failed
*/
if (isAPINotResponding(uniteLegaleInsee)) {
/***
* Sirene Insee failed
*/
if (isAPI404(uniteLegaleRechercheEntreprise)) {
throw new SirenNotFoundError(this._siren);
} else if (isAPINotResponding(uniteLegaleRechercheEntreprise)) {
throw new HttpServerError('Both API failed');
} else {
return uniteLegaleRechercheEntreprise;
}
}

/**
* Recherche failed
*/
if (isAPINotResponding(uniteLegaleRechercheEntreprise)) {
return uniteLegaleInsee;
} else {
/**
* Sirene succeed but siren not in recherhce or recherche failed
*/
if (
isAPINotResponding(uniteLegaleRechercheEntreprise) ||
isAPI404(uniteLegaleRechercheEntreprise)
) {
logWarningInSentry(
new FetchRessourceException({
ressource: 'UniteLegaleRecherche',
administration: EAdministration.DINUM,
message: 'Fail to find siren in recherche API',
context: {
siren: this._siren,
},
})
);
return uniteLegaleInsee;
}
}

/**
Expand Down Expand Up @@ -295,6 +298,13 @@ const fetchUniteLegaleFromInsee = async (
return APINotRespondingFactory(EAdministration.INSEE, 404);
}

if (!inseeOptions.useFallback) {
return await clientUniteLegaleInsee(siren, page, {
...inseeOptions,
useFallback: true,
});
}

logWarningInSentry(
new FetchRessourceException({
ressource: 'UniteLegaleInsee',
Expand Down

0 comments on commit 5d773dd

Please sign in to comment.