Skip to content

Commit

Permalink
feat: la création de dossier apprenant ne dépend plus de la création …
Browse files Browse the repository at this point in the history
…de formation, faite dans un job à part
  • Loading branch information
stephane-ruhlmann committed Nov 8, 2022
1 parent 77b7028 commit bf59ca5
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 142 deletions.
2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"remove:apprentis-nonRecus-depuis": "node -r dotenv/config src/jobs/remove-data/statuts-apprentis-nonRecus-depuis/index.js",
"remove:dossiersApprenants-nonRecus-depuis": "node -r dotenv/config src/jobs/remove-data/dossiers-apprenants-nonRecus-depuis/index.js",
"purge:events": "node -r dotenv/config src/jobs/remove-data/purgeEvents/index.js",
"formation:retrieve-from-cfd": "node -r dotenv/config src/jobs/formation-retrieve-from-cfd/index.js",
"seed:formations": "node -r dotenv/config src/jobs/seed/seedFormations.js",
"create:effectifsApprenants-collection": "node -r dotenv/config src/jobs/create-effectifs-apprenants-collection/index.js",
"dossiersApprenants:retrieve-networks": "node -r dotenv/config src/jobs/dossiersApprenants-retrieve-networks/index.js",
"support:identify-network-duplicates": "node -r dotenv/config src/jobs/support/identify-network-duplicates/index.js",
Expand Down
11 changes: 0 additions & 11 deletions server/src/common/components/dossiersApprenants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ const { DossierApprenantModel, CfaModel } = require("../model");
const omit = require("lodash.omit");
const { DUPLICATE_TYPE_CODES } = require("../constants/dossierApprenantConstants");
const { asyncForEach } = require("../../common/utils/asyncUtils");
const { validateCfd } = require("../domain/cfd");
const { escapeRegExp } = require("../utils/regexUtils");
const { isEqual } = require("date-fns");
const { existsFormation, createFormation, getFormationWithCfd } = require("./formations")();
const { DossierApprenant } = require("../factory/dossierApprenant");
const { faker } = require("@faker-js/faker/locale/fr");

Expand Down Expand Up @@ -138,22 +136,13 @@ const createDossierApprenant = async (itemToCreate) => {
// if dossier apprenant établissement has a VALID uai try to retrieve information in Referentiel CFAs
const etablissementInReferentielCfaFromUai = await CfaModel.findOne({ uai: itemToCreate.uai_etablissement });

// if dossier apprenant has a valid cfd, check if it exists in db and create it otherwise
if (validateCfd(itemToCreate.formation_cfd) && !(await existsFormation(itemToCreate.formation_cfd))) {
await createFormation(itemToCreate.formation_cfd);
}

const formationInfo = await getFormationWithCfd(itemToCreate.formation_cfd);

const dossierApprenantEntity = DossierApprenant.create({
ine_apprenant: itemToCreate.ine_apprenant,
nom_apprenant: itemToCreate.nom_apprenant.toUpperCase(),
prenom_apprenant: itemToCreate.prenom_apprenant.toUpperCase(),
email_contact: itemToCreate.email_contact,
formation_cfd: itemToCreate.formation_cfd,
libelle_long_formation: itemToCreate.libelle_long_formation,
niveau_formation: formationInfo?.niveau,
niveau_formation_libelle: formationInfo?.niveau_libelle,
uai_etablissement: itemToCreate.uai_etablissement,
siret_etablissement: itemToCreate.siret_etablissement,
nom_etablissement: itemToCreate.nom_etablissement,
Expand Down
1 change: 0 additions & 1 deletion server/src/common/constants/jobsConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const JOB_NAMES = {
seedRandomizedSample: "seed-randomized-sample",
identifyUaisInCatalog: "identify-uais-types-catalog",
identifyNetworkDuplicates: "identify-network-duplicates",
formationRetrieveFromCfd: "formation-retrieve-from-cfd",
dossiersApprenantsRetrieveNetworks: "dossiersApprenants-retrieve-networks",
dossiersApprenantsRetrieveNiveaux: "dossiersApprenants-retrieve-niveaux",
dossiersApprenantsRetrieveFormateurGestionnairesCatalog: "dossiersApprenants-retrieve-formateur-gestionnaire-catalog",
Expand Down
44 changes: 0 additions & 44 deletions server/src/jobs/dossiersApprenants-retrieve-niveaux/index.js

This file was deleted.

39 changes: 0 additions & 39 deletions server/src/jobs/formation-retrieve-from-cfd/index.js

This file was deleted.

56 changes: 56 additions & 0 deletions server/src/jobs/seed/seedFormations.js
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");
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ const assert = require("assert").strict;
const MockDate = require("mockdate");
const { addDays, isEqual } = require("date-fns");
const dossiersApprenants = require("../../../../src/common/components/dossiersApprenants");
const { DossierApprenantModel, CfaModel, FormationModel } = require("../../../../src/common/model");
const { DossierApprenantModel, CfaModel } = require("../../../../src/common/model");
const { createRandomDossierApprenant, getRandomUaiEtablissement } = require("../../../data/randomizedSample");
const {
CODES_STATUT_APPRENANT,
DUPLICATE_TYPE_CODES,
} = require("../../../../src/common/constants/dossierApprenantConstants");
const { RESEAUX_CFAS } = require("../../../../src/common/constants/networksConstants");
const { Formation } = require("../../../../src/common/factory/formation");

describe(__filename, () => {
let fakeNowDate;
Expand Down Expand Up @@ -1022,50 +1021,6 @@ describe(__filename, () => {
assert.equal(etablissement_reseaux[0], RESEAUX_CFAS.ANASUP.nomReseau);
assert.equal(etablissement_reseaux[1], RESEAUX_CFAS.BTP_CFA.nomReseau);
});

it("Vérifie qu'à la création d'un statut avec un CFD valide on crée la formation correspondante si elle n'existe pas", async () => {
const { createDossierApprenant } = await dossiersApprenants();

// Create statut
const cfd = "01022104";
const statutWithValidCfd = { ...createRandomDossierApprenant(), formation_cfd: cfd };
const createdStatut = await createDossierApprenant(statutWithValidCfd);

assert.ok(createdStatut);
// Check that formation was created
const foundFormations = await FormationModel.find();
assert.equal(foundFormations.length, 1);
assert.equal(foundFormations[0].cfd, cfd);
});

it("Vérifie qu'à la création d'un statut avec un CFD valide on ne crée pas de formation si elle existe", async () => {
const { createDossierApprenant } = await dossiersApprenants();

// Create Formation
const formationSeed = {
cfd: "01022103",
rncps: ["RNCP31811"],
libelle: "EMPLOYE TRAITEUR (CAP)",
cfd_start_date: new Date("2021-08-31").toISOString(),
cfd_end_date: new Date("2022-08-31").toISOString(),
};
const formation = Formation.create(formationSeed);
await new FormationModel(formation).save();

// Create statut
const statutWithValidCfd = {
...createRandomDossierApprenant(),
formation_cfd: formationSeed.cfd,
};
const createdStatut = await createDossierApprenant(statutWithValidCfd);
assert.ok(createdStatut);

// Find formations
const foundFormations = await FormationModel.find();

assert.equal(foundFormations.length, 1);
assert.equal(foundFormations[0].cfd, createdStatut.formation_cfd);
});
});

describe("getDuplicatesList", () => {
Expand Down

0 comments on commit bf59ca5

Please sign in to comment.