Skip to content

Commit

Permalink
feat: add dirigeant field to mapping file
Browse files Browse the repository at this point in the history
  • Loading branch information
ImenOuidou committed Dec 3, 2023
1 parent 07996ba commit 6415926
Showing 1 changed file with 113 additions and 23 deletions.
136 changes: 113 additions & 23 deletions src/n8n/utils/elastic/enterprise.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
export type BceEtablissement = {
eta_nic: string;
eta_siren: string;
dirigeants:
| {
nom: string;
prenoms: string;
}[]
| string;
eta_siret: string;
eta_statutDiffusionEtablissement: string;
eta_dateCreationEtablissement: string;
Expand Down Expand Up @@ -155,13 +161,42 @@ export type Enterprise = {
export const mappings = {
properties: {
siren: { type: "keyword" },
dirigeants: {
type: "nested",
properties: {
nom: {
type: "text",
analyzer: "french_indexing",
fields: {
keyword: {
type: "keyword",
},
},
},
prenoms: {
type: "text",
analyzer: "french_indexing",
fields: {
keyword: {
type: "keyword",
},
},
},
},
},

siret: { type: "keyword" },
siretRank: { type: "rank_feature" },

raisonSociale: {
type: "text",
analyzer: "french_indexing",
similarity: "bm25_no_norm_length",
type: "text",
fields: {
keyword: {
type: "keyword",
},
},
},
denominationUsuelleUniteLegale: {
analyzer: "french_indexing",
Expand Down Expand Up @@ -214,25 +249,24 @@ const getTrancheEffectif = (effectif: string): string => {
if (numEffectif > 4999) return "51";

return "00";
}
};

const removeEmpty = (document: any): object =>
Object.keys(document)
.reduce((res: any, key: string) => {
const value = document[key];
Object.keys(document).reduce((res: any, key: string) => {
const value = document[key];

if (value !== undefined && value !== null && value !== "") {
res[key] = value
}
if (value !== undefined && value !== null && value !== "") {
res[key] = value;
}

return res;
}, {});
return res;
}, {});

const getRank = (effectif: string) => {
const effectifNumber = +effectif;

return isNaN(effectifNumber) ? 0.1 : Math.max(effectifNumber, 0.1);
}
};

export const mapEnterprise = (enterprise: BceEtablissement) => {
const naming = Array.from(
Expand All @@ -255,19 +289,20 @@ export const mapEnterprise = (enterprise: BceEtablissement) => {
)
.filter((t) => t)
.join(" ");

const codeActivitePrincipale = [
enterprise.eta_activitePrincipaleEtablissement,
enterprise.ent_activitePrincipaleUniteLegale,
]
.find((s) => !s.startsWith("00.00")); // 00.00Z is a temporary code
].find((s) => !s?.startsWith("00.00")); // 00.00Z is a temporary code

const adresseEtablissement = [
enterprise.eta_numeroVoieEtablissement,
enterprise.eta_indiceRepetitionEtablissement,
enterprise.eta_typeVoieEtablissement,
enterprise.eta_libelleVoieEtablissement,
].join(' ').trim().replace(/ /, ' ');
]
.join(" ")
.trim()
.replace(/ /, " ");

const departementEtablissement = enterprise.eta_codePostalEtablissement.slice(
0,
Expand All @@ -277,17 +312,67 @@ export const mapEnterprise = (enterprise: BceEtablissement) => {
const domaineActivite = codeActivitePrincipale?.slice(0, 2);

// ranking feature cannot be 0
const trancheEffectifsUniteLegale = getTrancheEffectif(enterprise.ent_trancheEffectifsUniteLegale);
const trancheEffectifsEtablissement = getTrancheEffectif(enterprise.eff_EFF_TOTAL);
const trancheEffectifsUniteLegale = getTrancheEffectif(
enterprise.ent_trancheEffectifsUniteLegale
);
const trancheEffectifsEtablissement = getTrancheEffectif(
enterprise.eff_EFF_TOTAL
);
let dirigeants = [];

// Check if 'dirigeants' exists and is a non-empty string

if (
typeof enterprise?.dirigeants === "string" &&
enterprise?.dirigeants?.trim() !== ""
) {
try {
dirigeants = JSON.parse(enterprise.dirigeants);
} catch (error) {
console.error(
"Error parsing dirigeants:",
error,
"String:",
enterprise.dirigeants
);
// Set dirigeants to an empty array if parsing fails
dirigeants = [];
}
}

// Safe mapping
const dirigeantsMapped = Array.isArray(dirigeants)
? dirigeants.map((d) => ({
nom: d.nom,
prenom: d.prenoms,
}))
: [];

// Now you can safely use map on dirigeants

const document = {
siren: enterprise.ent_siren,
siret: enterprise.eta_siret,
siretRank: enterprise.eta_siret,
dirigeants: dirigeantsMapped,
categorieEntreprise: enterprise?.ent_categorieEntreprise,
etablissements: enterprise?.Nombre_Eta,
pseudo: enterprise?.ent_pseudonymeUniteLegale,

categorieJuridiqueUniteLegale:
enterprise?.ent_categorieJuridiqueUniteLegale,
activitePrincipaleUniteLegale:
enterprise?.ent_activitePrincipaleUniteLegale,
activitePrincipaleEtablissement:
enterprise?.eta_activitePrincipaleEtablissement,

nomenclatureActivitePrincipaleUniteLegale:
enterprise?.ent_nomenclatureActivitePrincipaleUniteLegale,

naming,
denominationUniteLegale: enterprise.ent_denominationUniteLegale,
denominationUsuelleUniteLegale: enterprise.ent_denominationUsuelle1UniteLegale,
denominationUsuelleUniteLegale:
enterprise.ent_denominationUsuelle1UniteLegale,
prenomUniteLegale: enterprise.ent_prenom1UniteLegale,
nomUniteLegale: enterprise.ent_nomUniteLegale,
enseigneEtablissement: enterprise.eta_enseigne1Etablissement,
Expand All @@ -302,19 +387,24 @@ export const mapEnterprise = (enterprise: BceEtablissement) => {
domaineActivite,

adresseEtablissement: adresseEtablissement,
complementAdresseEtablissement: enterprise.eta_complementAdresseEtablissement,
complementAdresseEtablissement:
enterprise.eta_complementAdresseEtablissement,
codePostalEtablissement: enterprise.eta_codePostalEtablissement,
libelleCommuneEtablissement: enterprise.eta_libelleCommuneEtablissement,
codeCommuneEtablissement: enterprise.eta_codeCommuneEtablissement || enterprise.eta_codeCommune2Etablissement,
codeCommuneEtablissement:
enterprise.eta_codeCommuneEtablissement ||
enterprise.eta_codeCommune2Etablissement,
departementEtablissement,

etatAdministratifEtablissement: enterprise.eta_etatAdministratifEtablissement,
etatAdministratifEtablissement:
enterprise.eta_etatAdministratifEtablissement,
etablissementSiege: enterprise.eta_etablissementSiege === "true",

caractereEmployeurEtablissementRank: enterprise.eta_caractereEmployeurEtablissement === "O" ? 1 : 0.1,
caractereEmployeurEtablissementRank:
enterprise.eta_caractereEmployeurEtablissement === "O" ? 1 : 0.1,

etablissementsUniteLegaleRank: getRank(enterprise.Nombre_Eta),
statutDiffusionEtablissement: enterprise.eta_statutDiffusionEtablissement
statutDiffusionEtablissement: enterprise.eta_statutDiffusionEtablissement,
};

return removeEmpty(document);
Expand Down

0 comments on commit 6415926

Please sign in to comment.