Skip to content

Commit

Permalink
Lbac 1762 fix rome detail (#835)
Browse files Browse the repository at this point in the history
* fix: reprise de rome_detail + fix appel à l'api rome

* fix: rome_detail

* fix: remove log
  • Loading branch information
remy-auricoste authored Nov 21, 2023
1 parent 9c4fde0 commit 38f8d56
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
5 changes: 5 additions & 0 deletions server/src/common/apis/Pe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ export const getPeReferentiels = async (referentiel: string) => {
}
}

/**
* @deprecated use getRomeDetailsFromDB instead
* @param romeCode
* @returns
*/
export const getRomeDetailsFromAPI = async (romeCode: string): Promise<IRomeDetailsFromAPI | null | undefined> => {
tokenRomePE = await getPeAccessToken("ROME", tokenRomePE)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Boom from "boom"
import { TRAINING_RYTHM } from "shared/constants/recruteur"
import { z } from "zod"

Expand All @@ -7,6 +8,7 @@ import { asyncForEach } from "@/common/utils/asyncUtils"
import { sentryCaptureException } from "@/common/utils/sentryUtils"
import { notifyToSlack } from "@/common/utils/slackUtils"
import { updateOffre } from "@/services/formulaire.service"
import { getRomeDetailsFromDB } from "@/services/rome.service"

const fixDates = async () => {
const subject = "Fix data validations pour recruiters : delegations.cfa_read_company_detail_at"
Expand Down Expand Up @@ -46,6 +48,47 @@ const fixDates = async () => {
return stats
}

const fixRomeDetails = async () => {
const subject = "Fix data validations pour recruiters : rome_detail"
const recruiters = await Recruiter.find(
{
"jobs.rome_detail": { $type: "string" },
},
undefined,
{ runValidators: false, rawResult: true }
).lean()
const stats = { success: 0, failure: 0 }
logger.info(`${subject}: ${recruiters.length} recruteurs à mettre à jour...`)
await asyncForEach(recruiters, async (recruiter) => {
try {
logger.info("treating recruiter", recruiter._id)
const { jobs } = recruiter
await asyncForEach(jobs, async (job) => {
const { rome_detail, rome_code } = job
if (rome_detail && typeof rome_detail === "string") {
const romeData = await getRomeDetailsFromDB(rome_code[0])
if (!romeData) {
throw Boom.internal(`could not find rome infos for rome=${rome_code}`)
}
job.rome_detail = romeData.fiche_metier
}
await updateOffre(job._id, { ...job })
})
stats.success++
} catch (err) {
logger.error(err)
sentryCaptureException(err)
stats.failure++
}
})
await notifyToSlack({
subject,
message: `${stats.failure} erreurs. ${stats.success} mises à jour`,
error: stats.failure > 0,
})
return stats
}

const fixJobRythm = async () => {
const subject = "Fix data validations : job_rythm"
const recruiters = await Recruiter.find({
Expand Down Expand Up @@ -86,6 +129,7 @@ const fixJobRythm = async () => {
}

export const fixRecruiterDataValidation = async () => {
await fixRomeDetails()
await fixDates()
await fixJobRythm()
}
6 changes: 3 additions & 3 deletions server/src/services/formulaire.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { FilterQuery, ModelUpdateOptions, UpdateQuery } from "mongoose"
import { IDelegation, IJob, IJobWritable, IRecruiter, IUserRecruteur, JOB_STATUS } from "shared"
import { ETAT_UTILISATEUR, RECRUITER_STATUS } from "shared/constants/recruteur"

import { getRomeDetailsFromAPI } from "@/common/apis/Pe"
import { getStaticFilePath } from "@/common/utils/getStaticFilePath"

import { Recruiter, UnsubscribeOF } from "../common/model/index"
Expand All @@ -16,6 +15,7 @@ import { getCatalogueEtablissements, getCatalogueFormations } from "./catalogue.
import dayjs from "./dayjs.service"
import { getEtablissement, sendEmailConfirmationEntreprise } from "./etablissement.service"
import mailer from "./mailer.service"
import { getRomeDetailsFromDB } from "./rome.service"
import { getUser, getUserStatus } from "./userRecruteur.service"

interface IFormulaireExtended extends IRecruiter {
Expand Down Expand Up @@ -95,15 +95,15 @@ export const createJob = async ({ job, id }: { job: IJobWritable; id: string }):
jobPartial.job_status = user && isUserAwaiting ? JOB_STATUS.EN_ATTENTE : JOB_STATUS.ACTIVE
// get user activation state if not managed by a CFA
const codeRome = job.rome_code[0]
const romeData = await getRomeDetailsFromAPI(codeRome)
const romeData = await getRomeDetailsFromDB(codeRome)
if (!romeData) {
throw Boom.internal(`could not find rome infos for rome=${codeRome}`)
}
const creationDate = new Date()
const { job_start_date = creationDate } = job
const updatedJob: Partial<IJob> = Object.assign(job, {
job_start_date,
rome_detail: romeData,
rome_detail: romeData.fiche_metier,
job_creation_date: creationDate,
job_expiration_date: addExpirationPeriod(creationDate).toDate(),
job_update_date: creationDate,
Expand Down

0 comments on commit 38f8d56

Please sign in to comment.