Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: geocoordinates script #643

Merged
merged 15 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion server/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -161,6 +161,19 @@ program.command("migrations:status").description("Check migrations status").acti

program.command("migrations:create").description("Run migrations create").requiredOption("-d, --description <string>", "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")
.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")
Expand Down
6 changes: 6 additions & 0 deletions server/src/jobs/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ 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 { 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"
Expand Down Expand Up @@ -74,6 +76,10 @@ export async function runJob(job: IInternalJobs): Promise<number> {
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
return removeVersionKeyFromAllCollections()
case "migration:remove-delegated-from-jobs": // Temporaire, doit tourner en recette et production
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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: "NOT FOUND" })

await asyncForEach(recruiters, async (recruiter) => {
await sleep(1000)
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()
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -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...`)

Expand Down Expand Up @@ -41,4 +40,4 @@ runScript(async () => {
}
})
logger.info("End update user adresse detail")
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -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...`)

Expand Down Expand Up @@ -56,4 +55,4 @@ runScript(async () => {
}
})
logger.info("End update user adresse detail")
})
}
6 changes: 3 additions & 3 deletions server/src/services/etablissement.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,18 +299,18 @@ export const getEtablissementFromCatalogue = async (siret: string): Promise<IEta
* @param {String} adresse
* @returns {Promise<string>}
*/
export const getGeoCoordinates = async (adresse: string): Promise<string> => {
export const getGeoCoordinates = async (adresse: string): Promise<string | null> => {
try {
const response: AxiosResponse<IAPIAdresse> = 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
}
}
/**
Expand Down
2 changes: 1 addition & 1 deletion shared/routes/recruiters.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
Expand Down