From 7681cd2ca32b4706479420a741ca60bfe27261c2 Mon Sep 17 00:00:00 2001 From: Nicolas KREMER Date: Wed, 24 Apr 2024 17:05:15 +0200 Subject: [PATCH] feat: mise a jour des indicateurs en contrat avant date inscription (#3651) --- server/package.json | 1 + .../actions/effectifs.statut.actions.ts | 27 ++++++++++++++----- yarn.lock | 8 ++++++ 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/server/package.json b/server/package.json index c226ff790..0c5bd5f9b 100644 --- a/server/package.json +++ b/server/package.json @@ -60,6 +60,7 @@ "jsonwebtoken": "8.5.1", "lil-http-terminator": "^1.2.3", "lodash-es": "4.17.21", + "lodash.clonedeep": "^4.5.0", "lodash.groupby": "4.6.0", "lodash.indexby": "3.1.1", "lodash.omit": "4.5.0", diff --git a/server/src/common/actions/effectifs.statut.actions.ts b/server/src/common/actions/effectifs.statut.actions.ts index c14487535..4a612c877 100644 --- a/server/src/common/actions/effectifs.statut.actions.ts +++ b/server/src/common/actions/effectifs.statut.actions.ts @@ -1,5 +1,6 @@ import { captureException } from "@sentry/node"; import Boom from "boom"; +import { cloneDeep } from "lodash-es"; import { MongoServerError, UpdateFilter } from "mongodb"; import { STATUT_APPRENANT, StatutApprenant } from "shared/constants"; import { IEffectif, IEffectifApprenant, IEffectifComputedStatut } from "shared/models/data/effectifs.model"; @@ -152,9 +153,6 @@ export function determineStatutsByContrats( const dateFin = effectif.formation.date_fin ? new Date(effectif.formation.date_fin) : currentDate; const effectiveDateFin = dateFin < currentDate ? dateFin : currentDate; - if (dateEntree) statuts.push({ valeur: STATUT_APPRENANT.INSCRIT, date: dateEntree }); - - let latestRuptureDate; let contracts = effectif.contrats ?.map((contract) => ({ @@ -163,6 +161,15 @@ export function determineStatutsByContrats( })) .sort((a, b) => a.dateDebut.getTime() - b.dateDebut.getTime()) || []; + const earliestContract = contracts[0]?.dateDebut; + + if (dateEntree && earliestContract && earliestContract < dateEntree) { + statuts.push({ valeur: STATUT_APPRENANT.INSCRIT, date: earliestContract }); + } else if (dateEntree) { + statuts.push({ valeur: STATUT_APPRENANT.INSCRIT, date: dateEntree }); + } + + let latestRuptureDate; contracts.forEach((contract, index) => { const { dateDebut, dateRupture } = contract; @@ -205,7 +212,9 @@ function determineNewStatutFromHistorique( throw new Error("La période de formation est nulle ou indéfinie"); } - const sortedHistoriqueStatut = historiqueStatut.sort( + const clonedHistoriqueStatut = cloneDeep(historiqueStatut); + + const sortedHistoriqueStatut = clonedHistoriqueStatut.sort( (a, b) => new Date(a.date_statut).getTime() - new Date(b.date_statut).getTime() ); @@ -213,11 +222,17 @@ function determineNewStatutFromHistorique( let inscriptionDate = startYear !== endYear ? new Date(`${startYear}-08-01T00:00:00Z`) : new Date(`${startYear}-01-01T00:00:00Z`); - if (sortedHistoriqueStatut[0].valeur_statut === 0) { - inscriptionDate = new Date(sortedHistoriqueStatut[0].date_statut); + if (sortedHistoriqueStatut[0].valeur_statut === 2) { + inscriptionDate = sortedHistoriqueStatut[0].date_statut; sortedHistoriqueStatut.shift(); } + let earliestStatutDate = new Date(sortedHistoriqueStatut[0].date_statut); + + if (earliestStatutDate < inscriptionDate) { + inscriptionDate = earliestStatutDate; + } + const parcours: { valeur: StatutApprenant; date: Date }[] = [ { valeur: STATUT_APPRENANT.INSCRIT, date: inscriptionDate }, ]; diff --git a/yarn.lock b/yarn.lock index acc3f359b..3500105e0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -12647,6 +12647,13 @@ __metadata: languageName: node linkType: hard +"lodash.clonedeep@npm:^4.5.0": + version: 4.5.0 + resolution: "lodash.clonedeep@npm:4.5.0" + checksum: 92c46f094b064e876a23c97f57f81fbffd5d760bf2d8a1c61d85db6d1e488c66b0384c943abee4f6af7debf5ad4e4282e74ff83177c9e63d8ff081a4837c3489 + languageName: node + linkType: hard + "lodash.debounce@npm:4.0.8, lodash.debounce@npm:^4.0.8": version: 4.0.8 resolution: "lodash.debounce@npm:4.0.8" @@ -17751,6 +17758,7 @@ __metadata: jsonwebtoken: 8.5.1 lil-http-terminator: ^1.2.3 lodash-es: 4.17.21 + lodash.clonedeep: ^4.5.0 lodash.groupby: 4.6.0 lodash.indexby: 3.1.1 lodash.omit: 4.5.0