From ff86b5f1f1e2e520885c4fde43a543e4e8d390f9 Mon Sep 17 00:00:00 2001 From: Sanchez Eric Date: Fri, 24 Jan 2025 11:47:26 +0100 Subject: [PATCH] feat(api,lib): 75 - maj jeunes avec motif de depart NOT_DONE status phase1 j-8 (#4692) --- api/src/crons/autoValidatePhase1.ts | 19 ++++++++++++++----- .../src/domains/sejours/phase1/contants.ts | 13 ++++++++++++- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/api/src/crons/autoValidatePhase1.ts b/api/src/crons/autoValidatePhase1.ts index 134bc7eb36..504de7a945 100644 --- a/api/src/crons/autoValidatePhase1.ts +++ b/api/src/crons/autoValidatePhase1.ts @@ -4,7 +4,7 @@ import { updateStatusPhase1 } from "../utils"; import { capture } from "../sentry"; import slack from "../slack"; import { CohortModel, LigneBusModel, YoungModel } from "../models"; -import { getDepartureDate, YOUNG_STATUS, YOUNG_STATUS_PHASE1 } from "snu-lib"; +import { getDepartureDate, YOUNG_STATUS, YOUNG_STATUS_PHASE1, DEPART_SEJOUR_MOTIFS_NOT_DONE } from "snu-lib"; export const handler = async (): Promise => { try { @@ -21,18 +21,27 @@ export const handler = async (): Promise => { for (const cohort of onGoingCohorts) { const cursor = await YoungModel.find({ cohortId: cohort._id, status: YOUNG_STATUS.VALIDATED, statusPhase1: YOUNG_STATUS_PHASE1.AFFECTED }).cursor(); + const autoValidationUser = { + firstName: `[CRON] Autovalidation de la phase 1 après ${cohort.daysToValidate} jours après le départ`, + }; + let nbYoungs = 0; await cursor.eachAsync(async (young) => { const bus = await LigneBusModel.findById(young.ligneId); const sessionPhase1 = await CohortModel.findById(young.sessionPhase1Id); const dateStart = getDepartureDate(young, sessionPhase1, cohort, { bus }); - if (differenceInDays(now, dateStart) !== cohort.daysToValidate) return; + if (differenceInDays(now, dateStart) <= cohort.daysToValidate) { + const isMotifNotDone = young.departSejourMotif && DEPART_SEJOUR_MOTIFS_NOT_DONE.includes(young.departSejourMotif as (typeof DEPART_SEJOUR_MOTIFS_NOT_DONE)[number]); + + if (isMotifNotDone) { + young.set({ statusPhase1: YOUNG_STATUS_PHASE1.NOT_DONE }); + await young.save({ fromUser: autoValidationUser }); + } + } const validationDateWithDays = addDays(new Date(dateStart), cohort.daysToValidate).toISOString(); - const modified = await updateStatusPhase1(young, validationDateWithDays, { - firstName: `[CRON] Autovalidation de la phase 1 après ${cohort.daysToValidate} jours après le départ`, - }); + const modified = await updateStatusPhase1(young, validationDateWithDays, autoValidationUser); if (modified) nbYoungs++; }); diff --git a/packages/lib/src/domains/sejours/phase1/contants.ts b/packages/lib/src/domains/sejours/phase1/contants.ts index e498f16e18..8598e1e235 100644 --- a/packages/lib/src/domains/sejours/phase1/contants.ts +++ b/packages/lib/src/domains/sejours/phase1/contants.ts @@ -15,4 +15,15 @@ const TRANSPORT_CONVOCATION_SUBTRACT_MINUTES = { const TRANSPORT_MODES_LIST = Object.values(TRANSPORT_MODES); -export { TRANSPORT_MODES_LIST, TRANSPORT_MODES, TRANSPORT_CONVOCATION_SUBTRACT_MINUTES_DEFAULT, TRANSPORT_CONVOCATION_SUBTRACT_MINUTES }; +const DEPART_SEJOUR_MOTIFS = { + EXCLUSION: "Exclusion", + FORCE_MAJEURE: "Cas de force majeure pour le volontaire", + ANNULATION: "Annulation du séjour ou mesure d’éviction sanitaire", + AUTRE: "Autre", +} as const; + +const DEPART_SEJOUR_MOTIFS_LIST = Object.values(DEPART_SEJOUR_MOTIFS); +const DEPART_SEJOUR_MOTIFS_NOT_DONE = [DEPART_SEJOUR_MOTIFS.EXCLUSION, DEPART_SEJOUR_MOTIFS.FORCE_MAJEURE, DEPART_SEJOUR_MOTIFS.AUTRE]; +export type DepartSejourMotif = typeof DEPART_SEJOUR_MOTIFS_LIST[number]; + +export { TRANSPORT_MODES_LIST, TRANSPORT_MODES, TRANSPORT_CONVOCATION_SUBTRACT_MINUTES_DEFAULT, TRANSPORT_CONVOCATION_SUBTRACT_MINUTES, DEPART_SEJOUR_MOTIFS, DEPART_SEJOUR_MOTIFS_LIST, DEPART_SEJOUR_MOTIFS_NOT_DONE };