-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: la création de dossier apprenant ne dépend plus de la création …
…de formation, faite dans un job à part
- Loading branch information
1 parent
77b7028
commit bf59ca5
Showing
7 changed files
with
58 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 0 additions & 44 deletions
44
server/src/jobs/dossiersApprenants-retrieve-niveaux/index.js
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const { runScript } = require("../scriptWrapper"); | ||
const logger = require("../../common/logger"); | ||
const { asyncForEach } = require("../../common/utils/asyncUtils"); | ||
const { sleep } = require("../../common/utils/miscUtils"); | ||
|
||
const SLEEP_TIME_BETWEEN_CREATION = 100; // 100ms to avoid flooding TCO and LBA APIs | ||
|
||
/* | ||
Ce script récupère tous les CFDs valides présents dans la collection DossierApprenant, crée une formation en base | ||
pour chacun si elle n'existe pas et ajoute les infos de la formation aux dossiers apprenants correspondant | ||
*/ | ||
runScript(async ({ db, formations }) => { | ||
let createdFormationsCount = 0; | ||
let notCreatedFormationsCount = 0; | ||
let dossiersApprenantUpdatedCount = 0; | ||
|
||
// get all CFDs from dossiers apprenants collection | ||
const allCfds = await db.collection("dossiersApprenants").distinct("formation_cfd"); | ||
logger.info(allCfds.length, "distinct CFD found in collection DossierApprenant"); | ||
|
||
// filter out CFD for which we already have a formation in db | ||
const formationsCfdToCreate = []; | ||
await asyncForEach(allCfds, async (cfd) => { | ||
const formationExistsInDb = await formations.existsFormation(cfd); | ||
if (!formationExistsInDb) formationsCfdToCreate.push(cfd); | ||
}); | ||
|
||
logger.info(formationsCfdToCreate.length, "formations should be created"); | ||
|
||
// create a formation for every "new" CFD and update related dossiers apprenants | ||
await asyncForEach(formationsCfdToCreate, async (cfd) => { | ||
try { | ||
const createdFormation = await formations.createFormation(cfd); | ||
createdFormationsCount++; | ||
|
||
const { result: dossierApprenantsUpdateResult } = await db.collection("dossiersApprenants").updateMany( | ||
{ formation_cfd: cfd }, | ||
{ | ||
$set: { | ||
niveau_formation: createdFormation.niveau, | ||
niveau_formation_libelle: createdFormation.niveau_libelle, | ||
}, | ||
} | ||
); | ||
dossiersApprenantUpdatedCount += dossierApprenantsUpdateResult.nModified; | ||
} catch (err) { | ||
logger.error("error while creating formation for CFD", cfd, err); | ||
notCreatedFormationsCount++; | ||
} | ||
await sleep(SLEEP_TIME_BETWEEN_CREATION); | ||
}); | ||
|
||
logger.info(`${createdFormationsCount} formations created in DB`); | ||
logger.warn(`${notCreatedFormationsCount} formations could not be created`); | ||
logger.info(`${dossiersApprenantUpdatedCount} dossiers apprenants updated with formation info`); | ||
}, "seed:formations"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters