From 4f3b7c00c9e0429d5c48b09b427edda6b3a7723d Mon Sep 17 00:00:00 2001 From: Kevin Barnoin Date: Fri, 6 Oct 2023 11:13:34 +0200 Subject: [PATCH 01/10] feat: return null if no geocoords --- server/src/services/etablissement.service.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server/src/services/etablissement.service.ts b/server/src/services/etablissement.service.ts index 8128f3b686..0b3ed0cbf9 100644 --- a/server/src/services/etablissement.service.ts +++ b/server/src/services/etablissement.service.ts @@ -299,18 +299,18 @@ export const getEtablissementFromCatalogue = async (siret: string): Promise} */ -export const getGeoCoordinates = async (adresse: string): Promise => { +export const getGeoCoordinates = async (adresse: string): Promise => { try { const response: AxiosResponse = await axios.get(`https://api-adresse.data.gouv.fr/search/?q=${adresse}`) // eslint-disable-next-line no-unsafe-optional-chaining const [firstFeature] = response.data?.features if (!firstFeature) { - return "NOT FOUND" + return null } return firstFeature.geometry.coordinates.reverse().join(",") } catch (error: any) { sentryCaptureException(error) - return "NOT FOUND" + return null } } /** From d5d03094b8bd0b12525436ed577d87285dc68d69 Mon Sep 17 00:00:00 2001 From: Kevin Barnoin Date: Fri, 6 Oct 2023 11:14:17 +0200 Subject: [PATCH 02/10] feat: add script --- server/src/commands.ts | 9 ++++++++- server/src/jobs/jobs.ts | 3 +++ .../formulaire/misc/recoverGeocoordinates.ts | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 server/src/jobs/lba_recruteur/formulaire/misc/recoverGeocoordinates.ts diff --git a/server/src/commands.ts b/server/src/commands.ts index 1be30f42eb..0293d538b8 100644 --- a/server/src/commands.ts +++ b/server/src/commands.ts @@ -8,7 +8,7 @@ import { closeElasticSearch } from "./common/esClient" import { logger } from "./common/logger" import { sleep } from "./common/utils/asyncUtils" import config from "./config" -import { initSentryProcessor, closeSentry } from "./http/sentry" +import { closeSentry, initSentryProcessor } from "./http/sentry" import server from "./http/server" import { addJob, processor } from "./jobs/jobs_actions" @@ -161,6 +161,13 @@ program.command("migrations:status").description("Check migrations status").acti program.command("migrations:create").description("Run migrations create").requiredOption("-d, --description ", "description").action(createJobAction("migrations:create")) +// Temporaire, one shot à executer en recette et prod +program + .command("migration:get-missing-geocoords") + .description("Récupération des geocoordonnées manquautes") + .option("-q, --queued", "Run job asynchronously", false) + .action(createJobAction("migration:get-missing-geocoords")) + // Temporaire, one shot à executer en recette et prod program .command("migration:remove-version-key-from-all-collections") diff --git a/server/src/jobs/jobs.ts b/server/src/jobs/jobs.ts index ccf37d71c5..ff4fc4552c 100644 --- a/server/src/jobs/jobs.ts +++ b/server/src/jobs/jobs.ts @@ -21,6 +21,7 @@ import { resetApiKey } from "./lba_recruteur/api/resetApiKey" import { annuleFormulaire } from "./lba_recruteur/formulaire/annuleFormulaire" import { createUserFromCLI } from "./lba_recruteur/formulaire/createUser" import { exportPE } from "./lba_recruteur/formulaire/misc/exportPE" +import { recoverMissingGeocoordinates } from "./lba_recruteur/formulaire/misc/recoverGeocoordinates" import { removeIsDelegatedFromJobs } from "./lba_recruteur/formulaire/misc/removeIsDelegatedFromJobs" import { removeVersionKeyFromAllCollections } from "./lba_recruteur/formulaire/misc/removeVersionKeyFromAllCollections" import { relanceFormulaire } from "./lba_recruteur/formulaire/relanceFormulaire" @@ -74,6 +75,8 @@ export async function runJob(job: IInternalJobs): Promise { return CRONS[job.name].handler() } switch (job.name) { + case "migration:get-missing-geocoords": // Temporaire, doit tourner en recette et production + return recoverMissingGeocoordinates() case "migration:remove-version-key-from-all-collections": // Temporaire, doit tourner en recette et production return removeVersionKeyFromAllCollections() case "migration:remove-delegated-from-jobs": // Temporaire, doit tourner en recette et production diff --git a/server/src/jobs/lba_recruteur/formulaire/misc/recoverGeocoordinates.ts b/server/src/jobs/lba_recruteur/formulaire/misc/recoverGeocoordinates.ts new file mode 100644 index 0000000000..bcbb6c1b05 --- /dev/null +++ b/server/src/jobs/lba_recruteur/formulaire/misc/recoverGeocoordinates.ts @@ -0,0 +1,16 @@ +import { logger } from "../../../../common/logger" +import { Recruiter } from "../../../../common/model" +import { asyncForEach, sleep } from "../../../../common/utils/asyncUtils" +import { getGeoCoordinates } from "../../../../services/etablissement.service" + +export const recoverMissingGeocoordinates = async () => { + const recruiters = await Recruiter.find({ geo_coordinates: null }) + + await asyncForEach(recruiters, async (recruiter) => { + await sleep(1000) + const geocoord = await getGeoCoordinates(`${recruiter.address_detail.l4} ${recruiter.address_detail.l6}`) + logger.info(`${recruiter.establishment_siret} - geocoord: ${geocoord} - adresse: ${recruiter.address_detail.l4} ${recruiter.address_detail.l6} `) + recruiter.geo_coordinates = geocoord + await recruiter.save() + }) +} From b17f8b5f225f7513b6a1a2e8d5d5c7f2b46be2e0 Mon Sep 17 00:00:00 2001 From: Kevin Barnoin Date: Fri, 6 Oct 2023 11:13:34 +0200 Subject: [PATCH 03/10] feat: return null if no geocoords --- server/src/services/etablissement.service.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server/src/services/etablissement.service.ts b/server/src/services/etablissement.service.ts index 8128f3b686..0b3ed0cbf9 100644 --- a/server/src/services/etablissement.service.ts +++ b/server/src/services/etablissement.service.ts @@ -299,18 +299,18 @@ export const getEtablissementFromCatalogue = async (siret: string): Promise} */ -export const getGeoCoordinates = async (adresse: string): Promise => { +export const getGeoCoordinates = async (adresse: string): Promise => { try { const response: AxiosResponse = await axios.get(`https://api-adresse.data.gouv.fr/search/?q=${adresse}`) // eslint-disable-next-line no-unsafe-optional-chaining const [firstFeature] = response.data?.features if (!firstFeature) { - return "NOT FOUND" + return null } return firstFeature.geometry.coordinates.reverse().join(",") } catch (error: any) { sentryCaptureException(error) - return "NOT FOUND" + return null } } /** From 196398c8a6e03089c32a6ed9ec78554efc8f029d Mon Sep 17 00:00:00 2001 From: Kevin Barnoin Date: Fri, 6 Oct 2023 11:14:17 +0200 Subject: [PATCH 04/10] feat: add script --- server/src/commands.ts | 9 ++++++++- server/src/jobs/jobs.ts | 3 +++ .../formulaire/misc/recoverGeocoordinates.ts | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 server/src/jobs/lba_recruteur/formulaire/misc/recoverGeocoordinates.ts diff --git a/server/src/commands.ts b/server/src/commands.ts index 1be30f42eb..0293d538b8 100644 --- a/server/src/commands.ts +++ b/server/src/commands.ts @@ -8,7 +8,7 @@ import { closeElasticSearch } from "./common/esClient" import { logger } from "./common/logger" import { sleep } from "./common/utils/asyncUtils" import config from "./config" -import { initSentryProcessor, closeSentry } from "./http/sentry" +import { closeSentry, initSentryProcessor } from "./http/sentry" import server from "./http/server" import { addJob, processor } from "./jobs/jobs_actions" @@ -161,6 +161,13 @@ program.command("migrations:status").description("Check migrations status").acti program.command("migrations:create").description("Run migrations create").requiredOption("-d, --description ", "description").action(createJobAction("migrations:create")) +// Temporaire, one shot à executer en recette et prod +program + .command("migration:get-missing-geocoords") + .description("Récupération des geocoordonnées manquautes") + .option("-q, --queued", "Run job asynchronously", false) + .action(createJobAction("migration:get-missing-geocoords")) + // Temporaire, one shot à executer en recette et prod program .command("migration:remove-version-key-from-all-collections") diff --git a/server/src/jobs/jobs.ts b/server/src/jobs/jobs.ts index ccf37d71c5..ff4fc4552c 100644 --- a/server/src/jobs/jobs.ts +++ b/server/src/jobs/jobs.ts @@ -21,6 +21,7 @@ import { resetApiKey } from "./lba_recruteur/api/resetApiKey" import { annuleFormulaire } from "./lba_recruteur/formulaire/annuleFormulaire" import { createUserFromCLI } from "./lba_recruteur/formulaire/createUser" import { exportPE } from "./lba_recruteur/formulaire/misc/exportPE" +import { recoverMissingGeocoordinates } from "./lba_recruteur/formulaire/misc/recoverGeocoordinates" import { removeIsDelegatedFromJobs } from "./lba_recruteur/formulaire/misc/removeIsDelegatedFromJobs" import { removeVersionKeyFromAllCollections } from "./lba_recruteur/formulaire/misc/removeVersionKeyFromAllCollections" import { relanceFormulaire } from "./lba_recruteur/formulaire/relanceFormulaire" @@ -74,6 +75,8 @@ export async function runJob(job: IInternalJobs): Promise { return CRONS[job.name].handler() } switch (job.name) { + case "migration:get-missing-geocoords": // Temporaire, doit tourner en recette et production + return recoverMissingGeocoordinates() case "migration:remove-version-key-from-all-collections": // Temporaire, doit tourner en recette et production return removeVersionKeyFromAllCollections() case "migration:remove-delegated-from-jobs": // Temporaire, doit tourner en recette et production diff --git a/server/src/jobs/lba_recruteur/formulaire/misc/recoverGeocoordinates.ts b/server/src/jobs/lba_recruteur/formulaire/misc/recoverGeocoordinates.ts new file mode 100644 index 0000000000..bcbb6c1b05 --- /dev/null +++ b/server/src/jobs/lba_recruteur/formulaire/misc/recoverGeocoordinates.ts @@ -0,0 +1,16 @@ +import { logger } from "../../../../common/logger" +import { Recruiter } from "../../../../common/model" +import { asyncForEach, sleep } from "../../../../common/utils/asyncUtils" +import { getGeoCoordinates } from "../../../../services/etablissement.service" + +export const recoverMissingGeocoordinates = async () => { + const recruiters = await Recruiter.find({ geo_coordinates: null }) + + await asyncForEach(recruiters, async (recruiter) => { + await sleep(1000) + const geocoord = await getGeoCoordinates(`${recruiter.address_detail.l4} ${recruiter.address_detail.l6}`) + logger.info(`${recruiter.establishment_siret} - geocoord: ${geocoord} - adresse: ${recruiter.address_detail.l4} ${recruiter.address_detail.l6} `) + recruiter.geo_coordinates = geocoord + await recruiter.save() + }) +} From 63f7e7712dc0cc1e0df38936ec7a0503ff47b255 Mon Sep 17 00:00:00 2001 From: Kevin Barnoin Date: Fri, 6 Oct 2023 11:24:36 +0200 Subject: [PATCH 05/10] feat: geocoord nullish --- shared/routes/recruiters.routes.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared/routes/recruiters.routes.ts b/shared/routes/recruiters.routes.ts index 52d7c5d652..dbe8c8e88b 100644 --- a/shared/routes/recruiters.routes.ts +++ b/shared/routes/recruiters.routes.ts @@ -65,7 +65,7 @@ export const zRecruiterRoutes = { naf_label: z.string().nullish(), establishment_size: z.string().nullish(), establishment_creation_date: z.date().nullish(), - geo_coordinates: z.string(), + geo_coordinates: z.string().nullish(), }) .strict(), }, From 76acd176c8f78a46e4d2d381743d08d820e9c8a1 Mon Sep 17 00:00:00 2001 From: Kevin Barnoin Date: Fri, 6 Oct 2023 11:36:18 +0200 Subject: [PATCH 06/10] feat: update recovery script & add to jobs --- server/src/commands.ts | 6 ++++++ server/src/jobs/jobs.ts | 3 +++ .../misc/updateAddressDetailOnRecruitersCollection.ts | 7 +++---- .../misc/updateAddressDetailOnUserrecrutersCollection.ts | 7 +++---- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/server/src/commands.ts b/server/src/commands.ts index 0293d538b8..4cb84d7b3b 100644 --- a/server/src/commands.ts +++ b/server/src/commands.ts @@ -161,6 +161,12 @@ program.command("migrations:status").description("Check migrations status").acti program.command("migrations:create").description("Run migrations create").requiredOption("-d, --description ", "description").action(createJobAction("migrations:create")) +program + .command("recruiters:get-missing-address-detail") + .description("Récupération des address_detail manquauts dans la collection Recruiters") + .option("-q, --queued", "Run job asynchronously", false) + .action(createJobAction("recruiters:get-missing-address-detail")) + // Temporaire, one shot à executer en recette et prod program .command("migration:get-missing-geocoords") diff --git a/server/src/jobs/jobs.ts b/server/src/jobs/jobs.ts index ff4fc4552c..ac528c7269 100644 --- a/server/src/jobs/jobs.ts +++ b/server/src/jobs/jobs.ts @@ -24,6 +24,7 @@ import { exportPE } from "./lba_recruteur/formulaire/misc/exportPE" import { recoverMissingGeocoordinates } from "./lba_recruteur/formulaire/misc/recoverGeocoordinates" import { removeIsDelegatedFromJobs } from "./lba_recruteur/formulaire/misc/removeIsDelegatedFromJobs" import { removeVersionKeyFromAllCollections } from "./lba_recruteur/formulaire/misc/removeVersionKeyFromAllCollections" +import { updateAddressDetailOnRecruitersCollection } from "./lba_recruteur/formulaire/misc/updateAddressDetailOnRecruitersCollection" import { relanceFormulaire } from "./lba_recruteur/formulaire/relanceFormulaire" import { generateIndexes } from "./lba_recruteur/indexes/generateIndexes" import { relanceOpco } from "./lba_recruteur/opco/relanceOpco" @@ -75,6 +76,8 @@ export async function runJob(job: IInternalJobs): Promise { return CRONS[job.name].handler() } switch (job.name) { + case "recruiters:get-missing-address-detail": + return updateAddressDetailOnRecruitersCollection() case "migration:get-missing-geocoords": // Temporaire, doit tourner en recette et production return recoverMissingGeocoordinates() case "migration:remove-version-key-from-all-collections": // Temporaire, doit tourner en recette et production diff --git a/server/src/jobs/lba_recruteur/formulaire/misc/updateAddressDetailOnRecruitersCollection.ts b/server/src/jobs/lba_recruteur/formulaire/misc/updateAddressDetailOnRecruitersCollection.ts index f22051cf90..e37a9adb85 100644 --- a/server/src/jobs/lba_recruteur/formulaire/misc/updateAddressDetailOnRecruitersCollection.ts +++ b/server/src/jobs/lba_recruteur/formulaire/misc/updateAddressDetailOnRecruitersCollection.ts @@ -2,11 +2,10 @@ import { logger } from "../../../../common/logger.js" import { Recruiter } from "../../../../common/model/index.js" import { asyncForEach, delay } from "../../../../common/utils/asyncUtils.js" import { getEtablissementFromGouv } from "../../../../services/etablissement.service.js" -import { runScript } from "../../../scriptWrapper.js" -runScript(async () => { +export const updateAddressDetailOnRecruitersCollection = async () => { logger.info("Start update user adresse detail") - const etablissements = await Recruiter.find({ address_detail: { $eq: null } }) + const etablissements = await Recruiter.find({ address_detail: null }) logger.info(`${etablissements.length} entries to update...`) @@ -41,4 +40,4 @@ runScript(async () => { } }) logger.info("End update user adresse detail") -}) +} diff --git a/server/src/jobs/lba_recruteur/formulaire/misc/updateAddressDetailOnUserrecrutersCollection.ts b/server/src/jobs/lba_recruteur/formulaire/misc/updateAddressDetailOnUserrecrutersCollection.ts index 91f8fad828..9db325e743 100644 --- a/server/src/jobs/lba_recruteur/formulaire/misc/updateAddressDetailOnUserrecrutersCollection.ts +++ b/server/src/jobs/lba_recruteur/formulaire/misc/updateAddressDetailOnUserrecrutersCollection.ts @@ -3,11 +3,10 @@ import { Recruiter, UserRecruteur } from "../../../../common/model/index.js" import { asyncForEach, delay } from "../../../../common/utils/asyncUtils.js" import { CFA, ENTREPRISE } from "../../../../services/constant.service.js" import { getEtablissementFromGouv } from "../../../../services/etablissement.service.js" -import { runScript } from "../../../scriptWrapper.js" -runScript(async () => { +export const updateAddressDetailOnUserrecrutersCollection = async () => { logger.info("Start update user adresse detail") - const users = await UserRecruteur.find({ type: { $in: [ENTREPRISE, CFA] }, address_detail: { $eq: null } }) + const users = await UserRecruteur.find({ type: { $in: [ENTREPRISE, CFA] }, address_detail: null }) logger.info(`${users.length} entries to update...`) @@ -56,4 +55,4 @@ runScript(async () => { } }) logger.info("End update user adresse detail") -}) +} From f7ea2f66d5b74f536a398909b4bdd1afb9c000ac Mon Sep 17 00:00:00 2001 From: Kevin Barnoin Date: Fri, 6 Oct 2023 11:37:37 +0200 Subject: [PATCH 07/10] feat: adapt script --- .../formulaire/misc/recoverGeocoordinates.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/server/src/jobs/lba_recruteur/formulaire/misc/recoverGeocoordinates.ts b/server/src/jobs/lba_recruteur/formulaire/misc/recoverGeocoordinates.ts index bcbb6c1b05..ab662aedeb 100644 --- a/server/src/jobs/lba_recruteur/formulaire/misc/recoverGeocoordinates.ts +++ b/server/src/jobs/lba_recruteur/formulaire/misc/recoverGeocoordinates.ts @@ -4,12 +4,21 @@ import { asyncForEach, sleep } from "../../../../common/utils/asyncUtils" import { getGeoCoordinates } from "../../../../services/etablissement.service" export const recoverMissingGeocoordinates = async () => { - const recruiters = await Recruiter.find({ geo_coordinates: null }) + const recruiters = await Recruiter.find({ geo_coordinates: "NOT FOUND" }) await asyncForEach(recruiters, async (recruiter) => { await sleep(1000) - const geocoord = await getGeoCoordinates(`${recruiter.address_detail.l4} ${recruiter.address_detail.l6}`) - logger.info(`${recruiter.establishment_siret} - geocoord: ${geocoord} - adresse: ${recruiter.address_detail.l4} ${recruiter.address_detail.l6} `) + let geocoord + if (recruiter.address_detail.l4) { + // if address data is in API address V2 + geocoord = await getGeoCoordinates(`${recruiter.address_detail.l4} ${recruiter.address_detail.l6}`) + } else { + // else API address V3 + geocoord = await getGeoCoordinates(`${recruiter.address_detail.acheminement_postal.l4} ${recruiter.address_detail.acheminement_postal.l6}`) + } + logger.info( + `${recruiter.establishment_siret} - geocoord: ${geocoord} - adresse: ${recruiter.address_detail.acheminement_postal.l4} ${recruiter.address_detail.acheminement_postal.l6} ` + ) recruiter.geo_coordinates = geocoord await recruiter.save() }) From 415c11d94f6258e177f1241489c136b130bab9fe Mon Sep 17 00:00:00 2001 From: Kevin Barnoin Date: Fri, 6 Oct 2023 12:01:20 +0200 Subject: [PATCH 08/10] fix: extend proper interface address v2 --- shared/models/address.model.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared/models/address.model.ts b/shared/models/address.model.ts index db4cfcfa39..edc404c74c 100644 --- a/shared/models/address.model.ts +++ b/shared/models/address.model.ts @@ -55,7 +55,7 @@ export const ZAdresseCFA = z .strict() .openapi("AdresseCFA") -const ZAdresseV2 = ZAdresseCFA.extend({ +const ZAdresseV2 = ZAcheminementPostal.extend({ numero_voie: z.string(), type_voie: z.string(), nom_voie: z.string(), From 82a37b25426e08e8df919160fd9f8f51777dcb86 Mon Sep 17 00:00:00 2001 From: Kevin Barnoin Date: Fri, 6 Oct 2023 12:14:06 +0200 Subject: [PATCH 09/10] feat: update script --- .../formulaire/misc/recoverGeocoordinates.ts | 37 +++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/server/src/jobs/lba_recruteur/formulaire/misc/recoverGeocoordinates.ts b/server/src/jobs/lba_recruteur/formulaire/misc/recoverGeocoordinates.ts index ab662aedeb..86ba9bb900 100644 --- a/server/src/jobs/lba_recruteur/formulaire/misc/recoverGeocoordinates.ts +++ b/server/src/jobs/lba_recruteur/formulaire/misc/recoverGeocoordinates.ts @@ -1,13 +1,39 @@ +import { ENTREPRISE } from "shared/constants/recruteur" + import { logger } from "../../../../common/logger" -import { Recruiter } from "../../../../common/model" +import { Recruiter, UserRecruteur } from "../../../../common/model" import { asyncForEach, sleep } from "../../../../common/utils/asyncUtils" import { getGeoCoordinates } from "../../../../services/etablissement.service" -export const recoverMissingGeocoordinates = async () => { +const recoverMissingGeocoordinatesUserRecruteur = async () => { + const users = await UserRecruteur.find({ geo_coordinates: "NOT FOUND", type: ENTREPRISE }) + + await asyncForEach(users, async (user) => { + if (!user.address_detail) return + await sleep(500) + + let geocoord + if ("l4" in user.address_detail) { + // if address data is in API address V2 + geocoord = await getGeoCoordinates(`${user.address_detail.l4} ${user.address_detail.l6}`) + logger.info(`${user.establishment_siret} - geocoord: ${geocoord} - adresse: ${user.address_detail.l4} ${user.address_detail.l6} `) + } else { + // else API address V3 + geocoord = await getGeoCoordinates(`${user.address_detail?.acheminement_postal?.l4} ${user.address_detail?.acheminement_postal?.l6}`) + logger.info(`${user.establishment_siret} - geocoord: ${geocoord} - adresse: ${user.address_detail?.acheminement_postal?.l4} ${user.address_detail?.acheminement_postal?.l6} `) + } + user.geo_coordinates = geocoord + await user.save() + }) +} + +const recoverMissingGeocoordinatesRecruiters = async () => { const recruiters = await Recruiter.find({ geo_coordinates: "NOT FOUND" }) await asyncForEach(recruiters, async (recruiter) => { - await sleep(1000) + if (!recruiter.address_detail) return + await sleep(500) + let geocoord if (recruiter.address_detail.l4) { // if address data is in API address V2 @@ -23,3 +49,8 @@ export const recoverMissingGeocoordinates = async () => { await recruiter.save() }) } + +export const recoverMissingGeocoordinates = async () => { + await recoverMissingGeocoordinatesRecruiters() + await recoverMissingGeocoordinatesUserRecruteur() +} From 5c094437ef5dd00b5fe449f43fd74538e2850bd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Auricoste?= Date: Wed, 18 Oct 2023 15:55:32 +0200 Subject: [PATCH 10/10] fix: typing script recoverGeocoordinates --- .../formulaire/misc/recoverGeocoordinates.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/server/src/jobs/lba_recruteur/formulaire/misc/recoverGeocoordinates.ts b/server/src/jobs/lba_recruteur/formulaire/misc/recoverGeocoordinates.ts index 86ba9bb900..79fd82ddf7 100644 --- a/server/src/jobs/lba_recruteur/formulaire/misc/recoverGeocoordinates.ts +++ b/server/src/jobs/lba_recruteur/formulaire/misc/recoverGeocoordinates.ts @@ -3,7 +3,7 @@ import { ENTREPRISE } from "shared/constants/recruteur" import { logger } from "../../../../common/logger" import { Recruiter, UserRecruteur } from "../../../../common/model" import { asyncForEach, sleep } from "../../../../common/utils/asyncUtils" -import { getGeoCoordinates } from "../../../../services/etablissement.service" +import { GeoCoord, getGeoCoordinates } from "../../../../services/etablissement.service" const recoverMissingGeocoordinatesUserRecruteur = async () => { const users = await UserRecruteur.find({ geo_coordinates: "NOT FOUND", type: ENTREPRISE }) @@ -12,7 +12,7 @@ const recoverMissingGeocoordinatesUserRecruteur = async () => { if (!user.address_detail) return await sleep(500) - let geocoord + let geocoord: GeoCoord | null if ("l4" in user.address_detail) { // if address data is in API address V2 geocoord = await getGeoCoordinates(`${user.address_detail.l4} ${user.address_detail.l6}`) @@ -22,7 +22,7 @@ const recoverMissingGeocoordinatesUserRecruteur = async () => { geocoord = await getGeoCoordinates(`${user.address_detail?.acheminement_postal?.l4} ${user.address_detail?.acheminement_postal?.l6}`) logger.info(`${user.establishment_siret} - geocoord: ${geocoord} - adresse: ${user.address_detail?.acheminement_postal?.l4} ${user.address_detail?.acheminement_postal?.l6} `) } - user.geo_coordinates = geocoord + user.geo_coordinates = geocoord ? `${geocoord.latitude},${geocoord.longitude}` : null await user.save() }) } @@ -34,7 +34,7 @@ const recoverMissingGeocoordinatesRecruiters = async () => { if (!recruiter.address_detail) return await sleep(500) - let geocoord + let geocoord: GeoCoord | null if (recruiter.address_detail.l4) { // if address data is in API address V2 geocoord = await getGeoCoordinates(`${recruiter.address_detail.l4} ${recruiter.address_detail.l6}`) @@ -45,7 +45,7 @@ const recoverMissingGeocoordinatesRecruiters = async () => { logger.info( `${recruiter.establishment_siret} - geocoord: ${geocoord} - adresse: ${recruiter.address_detail.acheminement_postal.l4} ${recruiter.address_detail.acheminement_postal.l6} ` ) - recruiter.geo_coordinates = geocoord + recruiter.geo_coordinates = geocoord ? `${geocoord.latitude},${geocoord.longitude}` : null await recruiter.save() }) }