From b91db11f8f2645dc41727e17f4e652c94c209029 Mon Sep 17 00:00:00 2001 From: Kevin Barnoin Date: Mon, 21 Aug 2023 20:18:42 +0200 Subject: [PATCH] feat: update logic to fill new fields (#545) * feat: update logic to fill new fields * fix: missing await * feat: add query limit & optional chaining * feat: split process into a function + cli * fix: remove import --- server/src/cli.ts | 8 +++++++ .../updateFormationCatalogue.ts | 24 +++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 server/src/jobs/formationsCatalogue/updateFormationCatalogue.ts diff --git a/server/src/cli.ts b/server/src/cli.ts index 9d6baa4fe1..a5e067d7e9 100644 --- a/server/src/cli.ts +++ b/server/src/cli.ts @@ -36,6 +36,7 @@ import { checkAwaitingCompaniesValidation } from "./jobs/lba_recruteur/user/misc import updateBrevoBlockedEmails from "./jobs/updateBrevoBlockedEmails/updateBrevoBlockedEmails.js" import { importReferentielOnisep } from "./jobs/rdv/importReferentielOnisep.js" import updateReferentielRncpRomes from "./jobs/referentielRncpRome/updateReferentielRncpRomes.js" +import { updateFormationCatalogue } from "./jobs/formationsCatalogue/updateFormationCatalogue.js" cli.addHelpText("after", null) @@ -262,6 +263,13 @@ cli runScript(() => importCatalogueFormationJob()) }) +cli + .command("sync-catalogue-trainings-extra-data") + .description("Mise à jour des champs spécifiques de la collection formations catalogue") + .action(() => { + runScript((components) => updateFormationCatalogue(components)) + }) + cli .command("sync-sib-blocked") .option("-all-addresses, [AllAddresses]", "pour récupérer toutes les adresses bloquées", false) diff --git a/server/src/jobs/formationsCatalogue/updateFormationCatalogue.ts b/server/src/jobs/formationsCatalogue/updateFormationCatalogue.ts new file mode 100644 index 0000000000..d09cc09048 --- /dev/null +++ b/server/src/jobs/formationsCatalogue/updateFormationCatalogue.ts @@ -0,0 +1,24 @@ +import { logger } from "../../common/logger.js" +import { FormationCatalogue } from "../../common/model/index.js" +import { IFormationCatalogue } from "../../common/model/schema/formationCatalogue/formationCatalogue.types.js" +import { asyncForEach } from "../../common/utils/asyncUtils.js" +import { getFormationsFromCatalogueMe } from "../../services/catalogue.service.js" + +export const updateFormationCatalogue = async ({ db }) => { + logger.info("--- update formation catalogue data --- start") + const formations = await FormationCatalogue.find({ $and: [{ affelnet_statut: null }, { parcoursup_id: null }] }).lean() + + logger.info(`${formations.length} à contrôler...`) + + await asyncForEach(formations, async (formation: IFormationCatalogue) => { + const formationME = await getFormationsFromCatalogueMe({ + limit: 1, + query: { cle_ministere_educatif: formation.cle_ministere_educatif }, + select: { parcoursup_id: 1, affelnet_statut: 1 }, + }) + const { parcoursup_id, affelnet_statut } = formationME[0] + + await db.collection("formationcatalogues").updateOne({ cle_ministere_educatif: formation.cle_ministere_educatif }, { $set: { parcoursup_id, affelnet_statut } }) + }) + logger.info("--- update formation catalogue data --- end") +}