From c229865523d351a26a06916b67909fcdc750e6f2 Mon Sep 17 00:00:00 2001 From: Antoine Bigard Date: Wed, 18 Oct 2023 09:10:24 +0200 Subject: [PATCH 01/10] feat: update algo --- server/package.json | 1 + server/src/services/trainingLinks.service.ts | 146 +++++++++---------- shared/routes/trainingLinks.routes.ts | 1 - yarn.lock | 8 + 4 files changed, 82 insertions(+), 74 deletions(-) diff --git a/server/package.json b/server/package.json index b8d3b416ae..47f8a1e5f5 100644 --- a/server/package.json +++ b/server/package.json @@ -58,6 +58,7 @@ "fastify-type-provider-zod": "^1.1.9", "form-data": "^4.0.0", "fs-extra": "^9.1.0", + "geolib": "^3.3.4", "got": "^12.5.2", "iconv-lite": "^0.6.3", "is-semver": "^1.0.11", diff --git a/server/src/services/trainingLinks.service.ts b/server/src/services/trainingLinks.service.ts index b8d2df5efc..caca1a22c6 100644 --- a/server/src/services/trainingLinks.service.ts +++ b/server/src/services/trainingLinks.service.ts @@ -1,5 +1,9 @@ import { URL } from "url" +// eslint-disable-next-line import/no-extraneous-dependencies +import getDistance from "geolib/es/getDistance" +import { IFormationCatalogue } from "shared/models" + import { EligibleTrainingsForAppointment, FormationCatalogue } from "../common/model/index" import apiGeoAdresse from "../common/utils/apiGeoAdresse" import { asyncForEach } from "../common/utils/asyncUtils" @@ -27,8 +31,9 @@ interface ILinks { const utmData = { utm_source: "lba", utm_medium: "email", utm_campaign: "promotion-emploi-jeunes-voeux" } -const buildEmploiUrl = ({ baseUrl = `${config.publicUrl}/recherche-emploi`, params }: { baseUrl?: string; params: Record }) => { +const buildEmploiUrl = ({ baseUrl = `${config.publicUrl}/recherche-emploi`, params }: { baseUrl?: string; params: Record }) => { const url = new URL(baseUrl) + // @ts-expect-error Object.entries(params).forEach(([key, value]) => url.searchParams.append(key, value)) return url.toString() } @@ -37,7 +42,7 @@ const buildEmploiUrl = ({ baseUrl = `${config.publicUrl}/recherche-emploi`, para * @description local function to get the formation related to the query * @param {Object} query * @param {string} filter - * @returns {Promise} + * @returns {Promise} */ const getFormation = ( query: object, @@ -46,45 +51,45 @@ const getFormation = ( rome_codes: 1, _id: 0, } -) => FormationCatalogue.findOne(query, filter) +) => FormationCatalogue.find(query, filter) /** * @description get formation according to the available parameters passed to the API endpoint * @param {object} wish wish data - * @returns {Promise} + * @returns {Promise} */ -const getTrainingFromParameters = async (wish: IWish) => { - let formation +const getTrainingsFromParameters = async (wish: IWish): Promise => { + let formations // search by cle ME if (wish.cle_ministere_educatif) { - formation = await getFormation({ cle_ministere_educatif: wish.cle_ministere_educatif }) + formations = await getFormation({ cle_ministere_educatif: wish.cle_ministere_educatif }) } - if (!formation) { + if (!formations && formations.length) { // search by uai_lieu_formation if (wish.uai_lieu_formation) { - formation = await getFormation({ $or: [{ cfd: wish.cfd }, { rncp_code: wish.rncp }, { "bcn_mefs_10.mef10": wish.mef }], uai_formation: wish.uai_lieu_formation }) + formations = await getFormation({ $or: [{ cfd: wish.cfd }, { rncp_code: wish.rncp }, { "bcn_mefs_10.mef10": wish.mef }], uai_formation: wish.uai_lieu_formation }) } } - if (!formation) { + if (!formations && formations.length) { // search by uai_formateur if (wish.uai_formateur) { - formation = await getFormation({ $or: [{ cfd: wish.cfd }, { rncp_code: wish.rncp }, { "bcn_mefs_10.mef10": wish.mef }], etablissement_formateur_uai: wish.uai_formateur }) + formations = await getFormation({ $or: [{ cfd: wish.cfd }, { rncp_code: wish.rncp }, { "bcn_mefs_10.mef10": wish.mef }], etablissement_formateur_uai: wish.uai_formateur }) } } - if (!formation) { + if (!formations && formations.length) { // search by uai_formateur_responsable if (wish.uai_formateur_responsable) { - formation = await getFormation({ + formations = await getFormation({ $or: [{ cfd: wish.cfd }, { rncp_code: wish.rncp }, { "bcn_mefs_10.mef10": wish.mef }], etablissement_gestionnaire_uai: wish.uai_formateur_responsable, }) } } - return formation + return formations } /** @@ -125,76 +130,71 @@ const getPrdvLink = async (wish: IWish): Promise => { * @returns {Promise} LBA link */ const getLBALink = async (wish: IWish): Promise => { - let formation + // get related trainings from catalogue + const formations = await getTrainingsFromParameters(wish) - // get related training from catalogue - formation = await getTrainingFromParameters(wish) + if (formations.length === 0 || !formations) { + return buildEmploiUrl({ params: utmData }) + } - if (formation) { - const [lat, lon] = formation.lieu_formation_geo_coordonnees.split(",") - return buildEmploiUrl({ params: { romes: formation.rome_codes, lat, lon, radius: "60", ...utmData } }) - } else { - // identify rome codes - let romes - formation = await FormationCatalogue.find( - { - $or: [ - { - rncp_code: wish.rncp, - }, - { - cfd: wish.cfd ? wish.cfd : undefined, - }, - { - "bcn_mefs_10.mef10": wish.mef, - }, - ], - }, - { - rome_codes: 1, - _id: 0, + let [formation] = formations + if (formations.length > 1) { + const postCode = wish.code_insee || wish.code_postal + let wLat, wLon + if (postCode) { + const responseApiAdresse = await apiGeoAdresse.searchPostcodeOnly(postCode) + if (responseApiAdresse && responseApiAdresse.features.length) { + ;[wLon, wLat] = responseApiAdresse.features[0].geometry.coordinates } - ).limit(5) - // recover all romes codes into a single array - if (formation.length) { - romes = [...new Set(formation.flatMap(({ rome_codes }) => rome_codes))] - } else { - return buildEmploiUrl({ params: utmData }) + let distance = 9999 + for (const [i, iFormation] of formations.entries()) { + if (iFormation.lieu_formation_geo_coordonnees) { + const [fLat, fLon] = iFormation.lieu_formation_geo_coordonnees.split(",") + const fDist = getDistance({ latitude: wLat, longitude: wLon }, { latitude: fLat, longitude: fLon }) + if (fDist < distance) { + distance = fDist + formation = formations[i] + } + } + } } + } - if (!romes.length) { - return buildEmploiUrl({ params: utmData }) + if (formation.lieu_formation_geo_coordonnees) { + const [lat, lon] = formation.lieu_formation_geo_coordonnees.split(",") + if (formation.rome_codes && formation.rome_codes.length) { + return buildEmploiUrl({ params: { romes: formation.rome_codes, lat: (lat && lon) ?? undefined, lon: (lat && lon) ?? undefined, radius: "60", ...utmData } }) } else { - // identify location - let lat, lon - - if (wish.uai) { - formation = await getFormation( - { - etablissement_formateur_uai: wish.uai, - }, - { - lieu_formation_geo_coordonnees: 1, - _id: 0, - } - ) - - const postCode = wish.code_insee || wish.code_postal - if (formation) { - ;[lat, lon] = formation.lieu_formation_geo_coordonnees.split(",") - } else if (postCode) { - // KBA 20230817 : might be modified using INSEE postcode. - const responseApiAdresse = await apiGeoAdresse.searchPostcodeOnly(postCode) - if (responseApiAdresse && responseApiAdresse.features.length) { - ;[lon, lat] = responseApiAdresse.features[0].geometry.coordinates - } + const tmpFormations = await FormationCatalogue.find( + { + $or: [ + { + rncp_code: wish.rncp, + }, + { + cfd: wish.cfd ? wish.cfd : undefined, + }, + { + "bcn_mefs_10.mef10": wish.mef, + }, + ], + }, + { + rome_codes: 1, + _id: 0, + } + ).limit(5) + if (tmpFormations.length) { + const romes = [...new Set(formations.flatMap(({ rome_codes }) => rome_codes))] as string[] + if (romes.length) { + return buildEmploiUrl({ params: { romes: romes, lat: (lat && lon) ?? undefined, lon: (lat && lon) ?? undefined, radius: "60", ...utmData } }) } } - - return buildEmploiUrl({ params: { romes: romes, lat: (lat && lon) ?? undefined, lon: (lat && lon) ?? undefined, radius: "60", ...utmData } }) } } + + return buildEmploiUrl({ params: utmData }) } /** diff --git a/shared/routes/trainingLinks.routes.ts b/shared/routes/trainingLinks.routes.ts index 6c334ea89b..3c44fcc65d 100644 --- a/shared/routes/trainingLinks.routes.ts +++ b/shared/routes/trainingLinks.routes.ts @@ -19,7 +19,6 @@ export const zTrainingLinksRoutes = { cfd: z.string().nullable().optional(), rncp: z.string().nullable().optional(), code_postal: z.string().nullable().optional(), - uai: z.string().nullable().optional(), uai_lieu_formation: z.string().nullable().optional(), uai_formateur: z.string().nullable().optional(), uai_formateur_responsable: z.string().nullable().optional(), diff --git a/yarn.lock b/yarn.lock index 441ea06f96..0b56530292 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9568,6 +9568,13 @@ __metadata: languageName: node linkType: hard +"geolib@npm:^3.3.4": + version: 3.3.4 + resolution: "geolib@npm:3.3.4" + checksum: b5ca8090effd384fec168a957d89416577a92dcb63aedf361a60794e07051e6f2a98a3c2db64de2dd9114ac22c63fbf9ec67b2f61dbc300292040cbd4387942b + languageName: node + linkType: hard + "get-caller-file@npm:^2.0.5": version: 2.0.5 resolution: "get-caller-file@npm:2.0.5" @@ -17413,6 +17420,7 @@ __metadata: fastify-type-provider-zod: ^1.1.9 form-data: ^4.0.0 fs-extra: ^9.1.0 + geolib: ^3.3.4 got: ^12.5.2 iconv-lite: ^0.6.3 is-semver: ^1.0.11 From 2199c72615b3b851954d231a19b25504fb309a89 Mon Sep 17 00:00:00 2001 From: Antoine Bigard Date: Wed, 18 Oct 2023 10:13:36 +0200 Subject: [PATCH 02/10] feat: handle geo coord case --- server/src/services/trainingLinks.service.ts | 74 +++++++++++--------- 1 file changed, 40 insertions(+), 34 deletions(-) diff --git a/server/src/services/trainingLinks.service.ts b/server/src/services/trainingLinks.service.ts index caca1a22c6..d3ad3aef4d 100644 --- a/server/src/services/trainingLinks.service.ts +++ b/server/src/services/trainingLinks.service.ts @@ -138,15 +138,18 @@ const getLBALink = async (wish: IWish): Promise => { } let [formation] = formations - if (formations.length > 1) { - const postCode = wish.code_insee || wish.code_postal - let wLat, wLon - if (postCode) { - const responseApiAdresse = await apiGeoAdresse.searchPostcodeOnly(postCode) - if (responseApiAdresse && responseApiAdresse.features.length) { - ;[wLon, wLat] = responseApiAdresse.features[0].geometry.coordinates - } + const postCode = wish.code_insee || wish.code_postal + let wLat, wLon + if (postCode) { + const responseApiAdresse = await apiGeoAdresse.searchPostcodeOnly(postCode) + if (responseApiAdresse && responseApiAdresse.features.length) { + ;[wLon, wLat] = responseApiAdresse.features[0].geometry.coordinates + } + } + + if (formations.length > 1) { + if (wLat && wLon) { let distance = 9999 for (const [i, iFormation] of formations.entries()) { if (iFormation.lieu_formation_geo_coordonnees) { @@ -161,36 +164,39 @@ const getLBALink = async (wish: IWish): Promise => { } } + let lat, lon if (formation.lieu_formation_geo_coordonnees) { - const [lat, lon] = formation.lieu_formation_geo_coordonnees.split(",") - if (formation.rome_codes && formation.rome_codes.length) { - return buildEmploiUrl({ params: { romes: formation.rome_codes, lat: (lat && lon) ?? undefined, lon: (lat && lon) ?? undefined, radius: "60", ...utmData } }) - } else { - const tmpFormations = await FormationCatalogue.find( + ;[lat, lon] = formation.lieu_formation_geo_coordonnees.split(",") + } else { + ;[lat, lon] = [wLat, wLon] + } + + if (formation.rome_codes && formation.rome_codes.length) { + return buildEmploiUrl({ params: { romes: formation.rome_codes, lat: (lat && lon) ?? undefined, lon: (lat && lon) ?? undefined, radius: "60", ...utmData } }) + } + const tmpFormations = await FormationCatalogue.find( + { + $or: [ { - $or: [ - { - rncp_code: wish.rncp, - }, - { - cfd: wish.cfd ? wish.cfd : undefined, - }, - { - "bcn_mefs_10.mef10": wish.mef, - }, - ], + rncp_code: wish.rncp, }, { - rome_codes: 1, - _id: 0, - } - ).limit(5) - if (tmpFormations.length) { - const romes = [...new Set(formations.flatMap(({ rome_codes }) => rome_codes))] as string[] - if (romes.length) { - return buildEmploiUrl({ params: { romes: romes, lat: (lat && lon) ?? undefined, lon: (lat && lon) ?? undefined, radius: "60", ...utmData } }) - } - } + cfd: wish.cfd ? wish.cfd : undefined, + }, + { + "bcn_mefs_10.mef10": wish.mef, + }, + ], + }, + { + rome_codes: 1, + _id: 0, + } + ).limit(5) + if (tmpFormations.length) { + const romes = [...new Set(formations.flatMap(({ rome_codes }) => rome_codes))] as string[] + if (romes.length) { + return buildEmploiUrl({ params: { romes: romes, lat: (lat && lon) ?? undefined, lon: (lat && lon) ?? undefined, radius: "60", ...utmData } }) } } From 7fa4771719e9ed4762406c2cfe9c384f2ca7a8a5 Mon Sep 17 00:00:00 2001 From: Antoine Bigard Date: Wed, 18 Oct 2023 10:45:39 +0200 Subject: [PATCH 03/10] fix: small --- server/src/services/trainingLinks.service.ts | 22 +++++++++----------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/server/src/services/trainingLinks.service.ts b/server/src/services/trainingLinks.service.ts index d3ad3aef4d..88c11089e9 100644 --- a/server/src/services/trainingLinks.service.ts +++ b/server/src/services/trainingLinks.service.ts @@ -148,17 +148,15 @@ const getLBALink = async (wish: IWish): Promise => { } } - if (formations.length > 1) { - if (wLat && wLon) { - let distance = 9999 - for (const [i, iFormation] of formations.entries()) { - if (iFormation.lieu_formation_geo_coordonnees) { - const [fLat, fLon] = iFormation.lieu_formation_geo_coordonnees.split(",") - const fDist = getDistance({ latitude: wLat, longitude: wLon }, { latitude: fLat, longitude: fLon }) - if (fDist < distance) { - distance = fDist - formation = formations[i] - } + if (formations.length > 1 && wLat && wLon) { + let distance = 9999 + for (const [i, iFormation] of formations.entries()) { + if (iFormation.lieu_formation_geo_coordonnees) { + const [fLat, fLon] = iFormation.lieu_formation_geo_coordonnees.split(",") + const fDist = getDistance({ latitude: wLat, longitude: wLon }, { latitude: fLat, longitude: fLon }) + if (fDist < distance) { + distance = fDist + formation = formations[i] } } } @@ -194,7 +192,7 @@ const getLBALink = async (wish: IWish): Promise => { } ).limit(5) if (tmpFormations.length) { - const romes = [...new Set(formations.flatMap(({ rome_codes }) => rome_codes))] as string[] + const romes = [...new Set(tmpFormations.flatMap(({ rome_codes }) => rome_codes))] as string[] if (romes.length) { return buildEmploiUrl({ params: { romes: romes, lat: (lat && lon) ?? undefined, lon: (lat && lon) ?? undefined, radius: "60", ...utmData } }) } From 85355dee0afbc91eff40ac7ac2974e0662f76be2 Mon Sep 17 00:00:00 2001 From: Antoine Bigard Date: Wed, 18 Oct 2023 11:23:33 +0200 Subject: [PATCH 04/10] fix: oops --- server/src/services/trainingLinks.service.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/server/src/services/trainingLinks.service.ts b/server/src/services/trainingLinks.service.ts index 88c11089e9..bf66c23e58 100644 --- a/server/src/services/trainingLinks.service.ts +++ b/server/src/services/trainingLinks.service.ts @@ -65,21 +65,21 @@ const getTrainingsFromParameters = async (wish: IWish): Promise => { // get related trainings from catalogue const formations = await getTrainingsFromParameters(wish) - if (formations.length === 0 || !formations) { + if (!formations || !formations.length) { return buildEmploiUrl({ params: utmData }) } From 32c0c58bac1e31685bc55a531d1d68fb734e70af Mon Sep 17 00:00:00 2001 From: Antoine Bigard Date: Wed, 18 Oct 2023 11:33:35 +0200 Subject: [PATCH 05/10] fix: rename --- server/src/services/trainingLinks.service.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/server/src/services/trainingLinks.service.ts b/server/src/services/trainingLinks.service.ts index bf66c23e58..5dfac615e6 100644 --- a/server/src/services/trainingLinks.service.ts +++ b/server/src/services/trainingLinks.service.ts @@ -44,7 +44,7 @@ const buildEmploiUrl = ({ baseUrl = `${config.publicUrl}/recherche-emploi`, para * @param {string} filter * @returns {Promise} */ -const getFormation = ( +const getFormations = ( query: object, filter: object = { lieu_formation_geo_coordonnees: 1, @@ -62,27 +62,27 @@ const getTrainingsFromParameters = async (wish: IWish): Promise Date: Wed, 18 Oct 2023 12:22:17 +0200 Subject: [PATCH 06/10] fix: lat lon --- server/src/services/trainingLinks.service.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/src/services/trainingLinks.service.ts b/server/src/services/trainingLinks.service.ts index 5dfac615e6..34476cd6d3 100644 --- a/server/src/services/trainingLinks.service.ts +++ b/server/src/services/trainingLinks.service.ts @@ -170,7 +170,7 @@ const getLBALink = async (wish: IWish): Promise => { } if (formation.rome_codes && formation.rome_codes.length) { - return buildEmploiUrl({ params: { romes: formation.rome_codes, lat: (lat && lon) ?? undefined, lon: (lat && lon) ?? undefined, radius: "60", ...utmData } }) + return buildEmploiUrl({ params: { romes: formation.rome_codes, lat: lat ?? undefined, lon: lon ?? undefined, radius: "60", ...utmData } }) } const tmpFormations = await FormationCatalogue.find( { @@ -194,7 +194,7 @@ const getLBALink = async (wish: IWish): Promise => { if (tmpFormations.length) { const romes = [...new Set(tmpFormations.flatMap(({ rome_codes }) => rome_codes))] as string[] if (romes.length) { - return buildEmploiUrl({ params: { romes: romes, lat: (lat && lon) ?? undefined, lon: (lat && lon) ?? undefined, radius: "60", ...utmData } }) + return buildEmploiUrl({ params: { romes: romes, lat: lat ?? undefined, lon: lon ?? undefined, radius: "60", ...utmData } }) } } From 54ffec76ba9b28d3afe7f49bf322151ee261b78e Mon Sep 17 00:00:00 2001 From: Antoine Bigard Date: Wed, 18 Oct 2023 12:35:53 +0200 Subject: [PATCH 07/10] fix: replace searchPostcodeOnly by search only --- server/src/services/trainingLinks.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/services/trainingLinks.service.ts b/server/src/services/trainingLinks.service.ts index 34476cd6d3..ce23a29f7c 100644 --- a/server/src/services/trainingLinks.service.ts +++ b/server/src/services/trainingLinks.service.ts @@ -142,7 +142,7 @@ const getLBALink = async (wish: IWish): Promise => { const postCode = wish.code_insee || wish.code_postal let wLat, wLon if (postCode) { - const responseApiAdresse = await apiGeoAdresse.searchPostcodeOnly(postCode) + const responseApiAdresse = await apiGeoAdresse.search(postCode) if (responseApiAdresse && responseApiAdresse.features.length) { ;[wLon, wLat] = responseApiAdresse.features[0].geometry.coordinates } From e7de07636c8f2bc9acf6fcbaac7c2733e0ded6e5 Mon Sep 17 00:00:00 2001 From: Antoine Bigard Date: Wed, 18 Oct 2023 12:41:28 +0200 Subject: [PATCH 08/10] fix: import --- server/src/services/trainingLinks.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/services/trainingLinks.service.ts b/server/src/services/trainingLinks.service.ts index ce23a29f7c..e6dd381e01 100644 --- a/server/src/services/trainingLinks.service.ts +++ b/server/src/services/trainingLinks.service.ts @@ -1,7 +1,7 @@ import { URL } from "url" // eslint-disable-next-line import/no-extraneous-dependencies -import getDistance from "geolib/es/getDistance" +import { getDistance } from "geolib" import { IFormationCatalogue } from "shared/models" import { EligibleTrainingsForAppointment, FormationCatalogue } from "../common/model/index" From 0c1933236b97a75cf2b58cec630fd62345874de8 Mon Sep 17 00:00:00 2001 From: Antoine Bigard Date: Wed, 18 Oct 2023 12:46:47 +0200 Subject: [PATCH 09/10] fix: min distance --- server/src/services/trainingLinks.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/services/trainingLinks.service.ts b/server/src/services/trainingLinks.service.ts index e6dd381e01..18b04621a5 100644 --- a/server/src/services/trainingLinks.service.ts +++ b/server/src/services/trainingLinks.service.ts @@ -149,7 +149,7 @@ const getLBALink = async (wish: IWish): Promise => { } if (formations.length > 1 && wLat && wLon) { - let distance = 9999 + let distance = 999999999 for (const [i, iFormation] of formations.entries()) { if (iFormation.lieu_formation_geo_coordonnees) { const [fLat, fLon] = iFormation.lieu_formation_geo_coordonnees.split(",") From 88297880e0750ac18ad5c7c5ca0ed38dd9163c89 Mon Sep 17 00:00:00 2001 From: Antoine Bigard Date: Wed, 18 Oct 2023 13:31:16 +0200 Subject: [PATCH 10/10] fix: review / recette --- server/src/services/trainingLinks.service.ts | 72 ++++++++++++-------- 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/server/src/services/trainingLinks.service.ts b/server/src/services/trainingLinks.service.ts index 18b04621a5..7173f4a9b0 100644 --- a/server/src/services/trainingLinks.service.ts +++ b/server/src/services/trainingLinks.service.ts @@ -33,8 +33,11 @@ const utmData = { utm_source: "lba", utm_medium: "email", utm_campaign: "promoti const buildEmploiUrl = ({ baseUrl = `${config.publicUrl}/recherche-emploi`, params }: { baseUrl?: string; params: Record }) => { const url = new URL(baseUrl) - // @ts-expect-error - Object.entries(params).forEach(([key, value]) => url.searchParams.append(key, value)) + + Object.entries(params).forEach(([key, value]) => { + // @ts-expect-error + if (value) url.searchParams.append(key, value) + }) return url.toString() } @@ -124,6 +127,33 @@ const getPrdvLink = async (wish: IWish): Promise => { return "" } +const getRomesGlobaux = async ({ rncp, cfd, mef }) => { + let romes = [] as string[] + const tmpFormations = await FormationCatalogue.find( + { + $or: [ + { + rncp_code: rncp, + }, + { + cfd: cfd ? cfd : undefined, + }, + { + "bcn_mefs_10.mef10": mef, + }, + ], + }, + { + rome_codes: 1, + _id: 0, + } + ).limit(5) + if (tmpFormations.length) { + romes = [...new Set(tmpFormations.flatMap(({ rome_codes }) => rome_codes))] as string[] + } + return romes +} + /** * @description get link LBA for a specific training * @param {object} wish wish data @@ -134,7 +164,8 @@ const getLBALink = async (wish: IWish): Promise => { const formations = await getTrainingsFromParameters(wish) if (!formations || !formations.length) { - return buildEmploiUrl({ params: utmData }) + const romes = await getRomesGlobaux({ rncp: wish.rncp, cfd: wish.cfd, mef: wish.mef }) + return buildEmploiUrl({ params: { ...(romes.length ? { romes: romes } : {}), ...utmData } }) } let [formation] = formations @@ -169,36 +200,19 @@ const getLBALink = async (wish: IWish): Promise => { ;[lat, lon] = [wLat, wLon] } - if (formation.rome_codes && formation.rome_codes.length) { - return buildEmploiUrl({ params: { romes: formation.rome_codes, lat: lat ?? undefined, lon: lon ?? undefined, radius: "60", ...utmData } }) + if (formations.length > 1 && !postCode) { + ;[lat, lon] = [undefined, undefined] } - const tmpFormations = await FormationCatalogue.find( - { - $or: [ - { - rncp_code: wish.rncp, - }, - { - cfd: wish.cfd ? wish.cfd : undefined, - }, - { - "bcn_mefs_10.mef10": wish.mef, - }, - ], - }, - { - rome_codes: 1, - _id: 0, - } - ).limit(5) - if (tmpFormations.length) { - const romes = [...new Set(tmpFormations.flatMap(({ rome_codes }) => rome_codes))] as string[] - if (romes.length) { - return buildEmploiUrl({ params: { romes: romes, lat: lat ?? undefined, lon: lon ?? undefined, radius: "60", ...utmData } }) + + if (formations.length === 1) { + if (formation.rome_codes && formation.rome_codes.length) { + return buildEmploiUrl({ params: { romes: formation.rome_codes, lat: lat ?? undefined, lon: lon ?? undefined, radius: "60", ...utmData } }) } } - return buildEmploiUrl({ params: utmData }) + const romes = await getRomesGlobaux({ rncp: wish.rncp, cfd: wish.cfd, mef: wish.mef }) + + return buildEmploiUrl({ params: { ...(romes.length ? { romes: romes } : {}), lat: lat ?? undefined, lon: lon ?? undefined, radius: "60", ...utmData } }) } /**