Skip to content

Commit

Permalink
feat(cron): amélioration de l'autovalidation de la phase 1 avec filtr…
Browse files Browse the repository at this point in the history
…age des jeunes validés
  • Loading branch information
naustra committed Nov 14, 2024
1 parent a378ece commit 5fb2bb5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
18 changes: 9 additions & 9 deletions api/src/crons/autoValidatePhase1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ export const handler = async (): Promise<void> => {
if (!onGoingCohorts.length) return;

for (const cohort of onGoingCohorts) {
let daysToValidate = cohort.daysToValidate;
if (!daysToValidate) {
console.warn(`Cohort ${cohort.name} has no daysToValidate`);
daysToValidate = 8;
}
if (differenceInDays(now, cohort.dateStart) !== cohort.daysToValidate) continue;

const youngs = await YoungModel.find({ cohortId: cohort._id });
const youngs = await YoungModel.find({ cohortId: cohort._id, status: "VALIDATED", statusPhase1: "AFFECTED" });

let nbYoungs = 0;
for await (const young of youngs) {
for (const young of youngs) {
const bus = await LigneBusModel.findById(young.ligneId);
const sessionPhase1 = await CohortModel.findById(young.sessionPhase1Id);
const dateStart = getDepartureDate(young, sessionPhase1, cohort, { bus });

let daysToValidate = cohort.daysToValidate;
if (!daysToValidate) {
console.warn(`Cohort ${cohort.name} has no daysToValidate`);
daysToValidate = 8;
}
if (differenceInDays(now, dateStart) !== cohort.daysToValidate) continue;

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`,
Expand Down
11 changes: 4 additions & 7 deletions api/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -729,15 +729,12 @@ async function addingDayToDate(days, dateStart) {
return formattedValidationDate;
}

async function autoValidationSessionPhase1Young({ young, sessionPhase1, cohort = null, user }) {
async function autoValidationSessionPhase1Young({ young, sessionPhase1, user }) {
let cohortWithOldRules = ["2021", "2022", "Février 2023 - C", "Avril 2023 - A", "Avril 2023 - B", "Juin 2023"];
let youngCohort = cohort;
if (!cohort) {
youngCohort = await CohortModel.findOne({ name: young.cohort });
}
const youngCohort = await CohortModel.findOne({ name: young.cohort });

// ! Freeze validation for legacy cohort
if (cohortWithOldRules.includes(young.cohort) || cohortWithOldRules.includes(youngCohort.name)) return;
if (cohortWithOldRules.includes(youngCohort.name)) return;

const { daysToValidate: daysToValidate, dateStart: dateStartCohort } = await getCohortDateInfo(sessionPhase1.cohort);

Expand All @@ -762,7 +759,7 @@ async function updateStatusPhase1(young, validationDateWithDays, user) {
// Cette constante nous permet de vérifier si un jeune était présent au début du séjour (exception pour cette cohorte : pas besoin de JDM)(basé sur son grade)
// ! Si null, on considère que le statut de pointage n'est pas encore connu (présent/absent)
const isCohesionStayValid = young.cohesionStayPresence === "true";
// Cette constante nour permet de vérifier si la date de départ d'un jeune permet de valider sa phase 1 (basé sur son grade)
// Cette constante pour permet de vérifier si la date de départ d'un jeune permet de valider sa phase 1 (basé sur son grade)
const isDepartureDateValid = now >= validationDate && (!young?.departSejourAt || young?.departSejourAt >= validationDate);
// On valide la phase 1 si toutes les condition sont réunis. Une exception : le jeune a été exclu.
if (isValidationDatePassed) {
Expand Down

0 comments on commit 5fb2bb5

Please sign in to comment.