From 42965e7efed042df2208e83ccc1e10ab30e83a81 Mon Sep 17 00:00:00 2001 From: Anselme Date: Tue, 17 Sep 2024 12:09:49 +0200 Subject: [PATCH] add cohort route types --- .../phase0/components/ChangeCohortPen.jsx | 17 +++++---- admin/src/services/cohortService.ts | 34 ++++++++++++++++++ api/src/cohort/cohortService.ts | 8 ++--- api/src/controllers/cohort-session.ts | 35 ++++++++++--------- api/src/utils/cohort.ts | 4 +-- packages/lib/src/routes/cohort/get.ts | 4 +-- .../lib/src/routes/cohort/getEligibility.ts | 33 +++++++++++++++++ .../src/routes/cohort/getIsIncriptionOpen.ts | 10 ++++++ .../routes/cohort/getIsReIncriptionOpen.ts | 7 ++++ packages/lib/src/routes/cohort/index.ts | 10 +++--- packages/lib/src/routes/index.ts | 2 +- 11 files changed, 128 insertions(+), 36 deletions(-) create mode 100644 admin/src/services/cohortService.ts create mode 100644 packages/lib/src/routes/cohort/getEligibility.ts create mode 100644 packages/lib/src/routes/cohort/getIsIncriptionOpen.ts create mode 100644 packages/lib/src/routes/cohort/getIsReIncriptionOpen.ts diff --git a/admin/src/scenes/phase0/components/ChangeCohortPen.jsx b/admin/src/scenes/phase0/components/ChangeCohortPen.jsx index 3085842e24..4bca78bf1e 100644 --- a/admin/src/scenes/phase0/components/ChangeCohortPen.jsx +++ b/admin/src/scenes/phase0/components/ChangeCohortPen.jsx @@ -3,15 +3,19 @@ import { IoRepeat } from "react-icons/io5"; import { HiUsers, HiCheckCircle, HiExclamationCircle, HiOutlineXCircle } from "react-icons/hi"; import { useSelector } from "react-redux"; import { toastr } from "react-redux-toastr"; + import { ROLES, translateStatusClasse, translateInscriptionStatus, YOUNG_SOURCE, STATUS_CLASSE, COHORT_TYPE, YOUNG_STATUS_PHASE1 } from "snu-lib"; import { ProfilePic } from "@snu/ds"; import { Badge, ModalConfirmation, Select, InputText, Button } from "@snu/ds/admin"; + +import { capture } from "@/sentry"; +import downloadPDF from "@/utils/download-pdf"; import Pencil from "@/assets/icons/Pencil"; -import UploadedFileIcon from "@/assets/icons/UploadedFileIcon"; import api from "@/services/api"; +import { CohortService } from "@/services/cohortService"; + +import UploadedFileIcon from "@/assets/icons/UploadedFileIcon"; import Loader from "@/components/Loader"; -import { capture } from "@/sentry"; -import downloadPDF from "@/utils/download-pdf"; export function ChangeCohortPen({ young, onChange }) { const user = useSelector((state) => state.Auth.user); @@ -31,9 +35,10 @@ export function ChangeCohortPen({ young, onChange }) { // setOptions(isEligibleForCohortToCome && young.cohort !== "à venir" ? [cohortToCome] : []); // return; // } - const { data } = await api.post(`/cohort-session/eligibility/2023/${young._id}`); - if (Array.isArray(data)) { - const cohorts = data.map((c) => ({ name: c.name, goal: c.goalReached, isEligible: c.isEligible, type: c.type })).filter((c) => c.name !== young.cohort); + // const { data } = await api.post(`/cohort-session/eligibility/2023/${young._id}`); + const eligibilities = await CohortService.getEligibility(young._id); + if (Array.isArray(eligibilities)) { + const cohorts = eligibilities.map((c) => ({ name: c.name, goal: c.goalReached, isEligible: c.isEligible, type: c.type })).filter((c) => c.name !== young.cohort); // TODO: rajouter un flag hidden pour les cohort non visible cohorts.push({ name: "à venir", type: "VOLONTAIRE" }); setOptions(cohorts); diff --git a/admin/src/services/cohortService.ts b/admin/src/services/cohortService.ts new file mode 100644 index 0000000000..80cc341f36 --- /dev/null +++ b/admin/src/services/cohortService.ts @@ -0,0 +1,34 @@ +import { CohortsRoutes } from "snu-lib"; + +import { buildRequest } from "@/utils/buildRequest"; + +const CohortService = { + getEligibility: async ( + id: CohortsRoutes["GetEligibility"]["params"]["id"], + { + payload, + query, + }: { + payload: CohortsRoutes["GetEligibility"]["payload"]; + query: CohortsRoutes["GetEligibility"]["query"]; + }, + ) => { + const { + ok, + code, + data: cohorts, + } = await buildRequest({ + path: "/cohort-session/eligibility/2023/{id}", + method: "POST", + params: { id }, + payload, + query, + })(); + if (!ok) { + throw new Error(code); + } + return cohorts; + }, +}; + +export { CohortService }; diff --git a/api/src/cohort/cohortService.ts b/api/src/cohort/cohortService.ts index f0245bbe29..ef352ddf80 100644 --- a/api/src/cohort/cohortService.ts +++ b/api/src/cohort/cohortService.ts @@ -1,17 +1,17 @@ import { COHORT_TYPE, ERRORS, CohortType } from "snu-lib"; import { CohortDocument, CohortModel } from "../models"; -const isInscriptionOpenOnSomeCohorts = async (): Promise => { +const isInscriptionOpenOnSomeCohorts = async (): Promise => { const cohorts = await CohortModel.find({ type: COHORT_TYPE.VOLONTAIRE }); return cohorts.some((cohort) => cohort.isInscriptionOpen); }; -const isReInscriptionOpenOnSomeCohorts = async (): Promise => { +const isReInscriptionOpenOnSomeCohorts = async (): Promise => { const cohorts = await CohortModel.find({ type: COHORT_TYPE.VOLONTAIRE }); return cohorts.some((cohort) => cohort.isReInscriptionOpen); }; -export const isInscriptionOpen = async (cohortName: String | undefined): Promise => { +export const isInscriptionOpen = async (cohortName: String | undefined): Promise => { if (cohortName) { const cohort = await CohortModel.findOne({ name: cohortName }); if (!cohort) return false; @@ -20,7 +20,7 @@ export const isInscriptionOpen = async (cohortName: String | undefined): Promise return isInscriptionOpenOnSomeCohorts(); }; -export const isReInscriptionOpen = async (cohortName?: String): Promise => { +export const isReInscriptionOpen = async (cohortName?: String): Promise => { if (cohortName) { const cohort = await CohortModel.findOne({ name: cohortName }); if (!cohort) return false; diff --git a/api/src/controllers/cohort-session.ts b/api/src/controllers/cohort-session.ts index 3df8eab02c..8023943c44 100644 --- a/api/src/controllers/cohort-session.ts +++ b/api/src/controllers/cohort-session.ts @@ -1,15 +1,18 @@ import express, { Response } from "express"; +import passport from "passport"; import Joi from "joi"; +import config from "config"; + +import { CohortsRoutes, ROLES, YoungType } from "snu-lib"; + import { capture } from "../sentry"; import { ERRORS } from "../utils"; -import { getFilteredSessions, getAllSessions, getFilteredSessionsForCLE, CohortDocumentWithPlaces } from "../utils/cohort"; +import { YoungModel } from "../models"; +import { RouteRequest, RouteResponse, UserRequest } from "./request"; import { validateId } from "../utils/validator"; -import { YoungModel, YoungType } from "../models"; -import passport from "passport"; -import { ROLES } from "snu-lib"; -import config from "config"; + +import { getFilteredSessions, getAllSessions, getFilteredSessionsForCLE, CohortDocumentWithPlaces } from "../utils/cohort"; import { isReInscriptionOpen, isInscriptionOpen } from "../cohort/cohortService"; -import { UserRequest } from "./request"; const router = express.Router(); @@ -78,13 +81,13 @@ router.post("/eligibility/2023/:id?", passport.authenticate("referent"), async f } }); -router.get("/isReInscriptionOpen", async (req: UserRequest, res: Response) => { +router.get("/isReInscriptionOpen", async (req: RouteRequest, res: RouteResponse) => { try { - const data = await isReInscriptionOpen(); + const isOpen = await isReInscriptionOpen(); - return res.send({ + return res.json({ ok: true, - data, + data: isOpen, }); } catch (error) { capture(error); @@ -92,22 +95,20 @@ router.get("/isReInscriptionOpen", async (req: UserRequest, res: Response) => { } }); -router.get("/isInscriptionOpen", async (req: UserRequest, res: Response) => { - const { error, value } = Joi.object({ +router.get("/isInscriptionOpen", async (req: RouteRequest, res: RouteResponse) => { + const { error, value: query } = Joi.object({ sessionName: Joi.string(), }) .unknown() .validate(req.query, { stripUnknown: true }); - if (error) return res.status(400).send({ ok: false, code: ERRORS.INVALID_PARAMS }); - const { sessionName } = value; try { - const data = await isInscriptionOpen(sessionName); + const isOpen = await isInscriptionOpen(query.sessionName); - return res.send({ + return res.json({ ok: true, - data, + data: isOpen, }); } catch (error) { capture(error); diff --git a/api/src/utils/cohort.ts b/api/src/utils/cohort.ts index 500c50f000..309694bb61 100644 --- a/api/src/utils/cohort.ts +++ b/api/src/utils/cohort.ts @@ -1,5 +1,5 @@ -import { YOUNG_STATUS, getRegionForEligibility, regionsListDROMS, COHORT_TYPE, getDepartmentForEligibility } from "snu-lib"; -import { YoungModel, CohortModel, InscriptionGoalModel, CohortDocument, YoungType } from "../models"; +import { YOUNG_STATUS, getRegionForEligibility, regionsListDROMS, COHORT_TYPE, getDepartmentForEligibility, YoungType } from "snu-lib"; +import { YoungModel, CohortModel, InscriptionGoalModel, CohortDocument } from "../models"; export type CohortDocumentWithPlaces = CohortDocument<{ numberOfCandidates?: number; diff --git a/packages/lib/src/routes/cohort/get.ts b/packages/lib/src/routes/cohort/get.ts index 7c2a0ce15a..bdba5fa5b3 100644 --- a/packages/lib/src/routes/cohort/get.ts +++ b/packages/lib/src/routes/cohort/get.ts @@ -1,9 +1,9 @@ -import { CohortDto } from "../../dto"; +import { CohortType } from "../../mongoSchema"; import { BasicRoute, RouteResponseBody } from ".."; export interface GetOneCohortRoute extends BasicRoute { method: "GET"; path: "/cohort/{id}"; params: { id: string }; - response: RouteResponseBody; + response: RouteResponseBody; } diff --git a/packages/lib/src/routes/cohort/getEligibility.ts b/packages/lib/src/routes/cohort/getEligibility.ts new file mode 100644 index 0000000000..d67715d470 --- /dev/null +++ b/packages/lib/src/routes/cohort/getEligibility.ts @@ -0,0 +1,33 @@ +import { BasicRoute, RouteResponseBody } from ".."; +import { CohortType } from "../../mongoSchema"; + +export interface GetEligibilityRoute extends BasicRoute { + method: "POST"; + path: "/cohort-session/eligibility/2023/{id}"; + params: { id?: string }; + payload?: { + schoolDepartment?: string; + department: string; + region: string; + schoolRegion?: string; + birthdateAt: Date; + grade: string; + status: string; + zip?: string; + }; + query?: { + getAllSessions?: boolean; + }; + response: RouteResponseBody< + Array< + CohortType & { + numberOfCandidates?: number; + numberOfValidated?: number; + goal?: number; + goalReached?: boolean; + isFull?: boolean; + isEligible?: boolean; + } + > + >; +} diff --git a/packages/lib/src/routes/cohort/getIsIncriptionOpen.ts b/packages/lib/src/routes/cohort/getIsIncriptionOpen.ts new file mode 100644 index 0000000000..114c309296 --- /dev/null +++ b/packages/lib/src/routes/cohort/getIsIncriptionOpen.ts @@ -0,0 +1,10 @@ +import { BasicRoute, RouteResponseBody } from ".."; + +export interface GetIsIncriptionOpenRoute extends BasicRoute { + method: "GET"; + path: "/cohort-session/isInscriptionOpen"; + query: { + sessionName?: string; + }; + response: RouteResponseBody; +} diff --git a/packages/lib/src/routes/cohort/getIsReIncriptionOpen.ts b/packages/lib/src/routes/cohort/getIsReIncriptionOpen.ts new file mode 100644 index 0000000000..16f127110a --- /dev/null +++ b/packages/lib/src/routes/cohort/getIsReIncriptionOpen.ts @@ -0,0 +1,7 @@ +import { BasicRoute, RouteResponseBody } from ".."; + +export interface GetIsReincriptionOpenRoute extends BasicRoute { + method: "GET"; + path: "/cohort-session/isReInscriptionOpen"; + response: RouteResponseBody; +} diff --git a/packages/lib/src/routes/cohort/index.ts b/packages/lib/src/routes/cohort/index.ts index 41293857ed..c361c2845e 100644 --- a/packages/lib/src/routes/cohort/index.ts +++ b/packages/lib/src/routes/cohort/index.ts @@ -1,9 +1,11 @@ import { GetOneCohortRoute } from "./get"; +import { GetEligibilityRoute } from "./getEligibility"; +import { GetIsIncriptionOpenRoute } from "./getIsIncriptionOpen"; +import { GetIsReincriptionOpenRoute } from "./getIsReIncriptionOpen"; export type CohortsRoutes = { - // Get: GetCohortRoute; - // Create: CreateCohortRoute, GetOne: GetOneCohortRoute; - // Update: UpdateCohortRoute, - // Delete: DeleteCohortRoute, + GetEligibility: GetEligibilityRoute; + GetIsIncriptionOpen: GetIsIncriptionOpenRoute; + GetIsReincriptionOpen: GetIsReincriptionOpenRoute; }; diff --git a/packages/lib/src/routes/index.ts b/packages/lib/src/routes/index.ts index 0f55422cb3..161ab1f9d4 100644 --- a/packages/lib/src/routes/index.ts +++ b/packages/lib/src/routes/index.ts @@ -3,7 +3,7 @@ export interface BasicRoute { params?: Record; payload?: Record; query?: Record; - response?: RouteResponseBody | Record[]>; + response?: RouteResponseBody | Record[] | boolean>; method: "GET" | "POST" | "DELETE" | "PUT"; }