diff --git a/server/src/db/migrations/202407030000000-migrate-rename-bonnesboites.ts b/server/src/db/migrations/202407030000000-migrate-rename-bonnesboites.ts index 69809c0be1..f7d2c31415 100644 --- a/server/src/db/migrations/202407030000000-migrate-rename-bonnesboites.ts +++ b/server/src/db/migrations/202407030000000-migrate-rename-bonnesboites.ts @@ -5,9 +5,19 @@ import { logger } from "@/common/logger" export const up = async (db: Db) => { logger.info("202407030000000-migrate-rename-bonnesboites started") - await db.collection("bonnesboites").rename("recruteurslba") - await db.collection("unsubscribedbonnesboites").rename("unsubscribedrecruteurslba") - await db.collection("bonnesboiteslegacies").rename("recruteurslbalegacies") + const renameCollectionIfNecessary = async (db, oldName, newName) => { + const collections = await db.listCollections({ name: newName }).toArray() + if (collections.length === 0) { + await db.collection(oldName).rename(newName) + console.log(`Renamed collection ${oldName} to ${newName}`) + } else { + console.log(`Collection ${newName} already exists, skipping rename of ${oldName}`) + } + } + + await renameCollectionIfNecessary(db, "bonnesboites", "recruteurslba") + await renameCollectionIfNecessary(db, "unsubscribedbonnesboites", "unsubscribedrecruteurslba") + await renameCollectionIfNecessary(db, "bonnesboiteslegacies", "recruteurslbalegacies") logger.info("202407030000000-migrate-rename-bonnesboites ended") } diff --git a/server/src/services/trainingLinks.service.ts b/server/src/services/trainingLinks.service.ts index 3a593c1f3e..7fe8adda24 100644 --- a/server/src/services/trainingLinks.service.ts +++ b/server/src/services/trainingLinks.service.ts @@ -55,6 +55,21 @@ const getFormations = ( const getTrainingsFromParameters = async (wish: IWish): Promise => { let formations + let query: any = { $or: [] } + + if (wish.cfd) { + query.$or.push({ cfd: wish.cfd }) + } + if (wish.rncp) { + query.$or.push({ rncp_code: wish.rncp }) + } + if (wish.mef) { + query.$or.push({ "bcn_mefs_10.mef10": wish.mef }) + } + + if (!query.$or.length) { + query = undefined + } // search by cle ME if (wish.cle_ministere_educatif) { formations = await getFormations({ cle_ministere_educatif: wish.cle_ministere_educatif }) @@ -71,17 +86,14 @@ const getTrainingsFromParameters = async (wish: IWish): Promise => { const postCode = wish.code_insee || wish.code_postal let wLat, wLon if (postCode) { - const responseApiAdresse = await apiGeoAdresse.search(postCode) + let responseApiAdresse = await apiGeoAdresse.search(postCode) + + if (!responseApiAdresse || !responseApiAdresse.features.length) { + const generalPostCode = postCode.replace(/\d{3}$/, "000") + responseApiAdresse = await apiGeoAdresse.search(generalPostCode) + } + if (responseApiAdresse && responseApiAdresse.features.length) { ;[wLon, wLat] = responseApiAdresse.features[0].geometry.coordinates } @@ -198,7 +216,11 @@ const getLBALink = async (wish: IWish): Promise => { return buildEmploiUrl({ params: { ...(romes.length ? { romes } : {}), lat, lon, radius: "60", ...utmData } }) } else { // No formations found, use user coordinates if available - return buildEmploiUrl({ params: { ...(romes.length ? { romes } : {}), lat: wLat ?? undefined, lon: wLon ?? undefined, radius: "60", ...utmData } }) + if (romes.length) { + return buildEmploiUrl({ params: { romes, lat: wLat ?? undefined, lon: wLon ?? undefined, radius: "60", ...utmData } }) + } else { + return buildEmploiUrl({ baseUrl: config.publicUrl, params: { ...utmData } }) + } } }