Skip to content

Commit

Permalink
feat(api,lib): 75 - maj jeunes avec motif de depart NOT_DONE status p…
Browse files Browse the repository at this point in the history
…hase1 j-8 (#4692)
  • Loading branch information
Eric013 authored Jan 24, 2025
1 parent 77addde commit ff86b5f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
19 changes: 14 additions & 5 deletions api/src/crons/autoValidatePhase1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
try {
Expand All @@ -21,18 +21,27 @@ export const handler = async (): Promise<void> => {
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++;
});

Expand Down
13 changes: 12 additions & 1 deletion packages/lib/src/domains/sejours/phase1/contants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

0 comments on commit ff86b5f

Please sign in to comment.