From ec56ec85308ce684ac1d5c1102ef4d7b80d7a66f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Bret=C3=A9cher?= Date: Wed, 29 Jan 2025 14:11:10 +0100 Subject: [PATCH 1/2] up --- packages/lib/src/sessions.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/packages/lib/src/sessions.ts b/packages/lib/src/sessions.ts index 87296582e1..d8dd6fd773 100644 --- a/packages/lib/src/sessions.ts +++ b/packages/lib/src/sessions.ts @@ -1,5 +1,5 @@ import { regionsListDROMS } from "./region-and-departments"; -import { COHORT_STATUS, YOUNG_STATUS } from "./constants/constants"; +import { COHORT_STATUS, YOUNG_STATUS, YOUNG_STATUS_PHASE1 } from "./constants/constants"; import { getZonedDate } from "./utils/date"; import { EtablissementDto } from "./dto"; import { format } from "date-fns"; @@ -140,12 +140,10 @@ function hasAccessToReinscription(young: YoungType) { return young.cohort === "à venir"; } -//@todo : for browser apps better logic in app isYoungCanApplyToPhase2Missions (also takes into account timezone) -function canApplyToPhase2(young, cohort) { - if (young.statusPhase2OpenedAt && new Date(young.statusPhase2OpenedAt) < new Date()) return true; - const now = new Date(); - const dateEnd = getCohortEndDate(cohort); - return ["DONE", "EXEMPTED"].includes(young.statusPhase1) && now >= dateEnd; +function canApplyToPhase2(young: YoungType, cohort: CohortType) { + const statusCheck = young.statusPhase1 === YOUNG_STATUS_PHASE1.DONE || young.statusPhase1 === YOUNG_STATUS_PHASE1.EXEMPTED || !!young.statusPhase2OpenedAt; + const dateCheck = new Date() > getCohortEndDate(cohort) || (young.statusPhase2OpenedAt && new Date() > new Date(young.statusPhase2OpenedAt)); + return statusCheck && dateCheck; } export { From 17f3acb1cd3b53a63ed40a8d1a4ced075aa29a42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Bret=C3=A9cher?= Date: Wed, 29 Jan 2025 16:12:13 +0100 Subject: [PATCH 2/2] fix --- api/src/controllers/mission.js | 13 +++++++------ packages/lib/src/sessions.ts | 7 ++++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/api/src/controllers/mission.js b/api/src/controllers/mission.js index 798ec90a3b..005447d0d4 100644 --- a/api/src/controllers/mission.js +++ b/api/src/controllers/mission.js @@ -301,14 +301,15 @@ router.get("/:id", passport.authenticate(["referent", "young"], { session: false const application = await ApplicationModel.findOne({ missionId: checkedId, youngId: req.user._id }); const cohort = await CohortModel.findById(req.user.cohortId); if (!cohort) return res.status(404).send({ ok: false, code: ERRORS.NOT_FOUND }); + const data = { + ...serializeMission(mission), + tutor: missionTutor, + application: application ? serializeApplication(application) : null, + ...(await getAuthorizationToApply(mission, req.user, cohort)), + }; return res.status(200).send({ ok: true, - data: { - ...serializeMission(mission), - tutor: missionTutor, - application: application ? serializeApplication(application) : null, - ...(await getAuthorizationToApply(mission, req.user, cohort)), - }, + data, }); } return res.status(200).send({ ok: true, data: { ...serializeMission(mission), tutor: missionTutor } }); diff --git a/packages/lib/src/sessions.ts b/packages/lib/src/sessions.ts index d8dd6fd773..8dc1016dbb 100644 --- a/packages/lib/src/sessions.ts +++ b/packages/lib/src/sessions.ts @@ -141,9 +141,10 @@ function hasAccessToReinscription(young: YoungType) { } function canApplyToPhase2(young: YoungType, cohort: CohortType) { - const statusCheck = young.statusPhase1 === YOUNG_STATUS_PHASE1.DONE || young.statusPhase1 === YOUNG_STATUS_PHASE1.EXEMPTED || !!young.statusPhase2OpenedAt; - const dateCheck = new Date() > getCohortEndDate(cohort) || (young.statusPhase2OpenedAt && new Date() > new Date(young.statusPhase2OpenedAt)); - return statusCheck && dateCheck; + const hasStartedPhase2 = young.statusPhase2OpenedAt && new Date() > new Date(young.statusPhase2OpenedAt); + const cohortHasStarted = new Date() < getCohortStartDate(cohort); + const cohortHasEnded = new Date() > getCohortEndDate(cohort); + return hasStartedPhase2 && !cohortHasStarted && cohortHasEnded; } export {