Skip to content

Commit

Permalink
add cohort route types
Browse files Browse the repository at this point in the history
  • Loading branch information
achorein committed Sep 17, 2024
1 parent 0dec1ce commit 42965e7
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 36 deletions.
17 changes: 11 additions & 6 deletions admin/src/scenes/phase0/components/ChangeCohortPen.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
34 changes: 34 additions & 0 deletions admin/src/services/cohortService.ts
Original file line number Diff line number Diff line change
@@ -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<CohortsRoutes["GetEligibility"]>({
path: "/cohort-session/eligibility/2023/{id}",
method: "POST",
params: { id },
payload,
query,
})();
if (!ok) {
throw new Error(code);
}
return cohorts;
},
};

export { CohortService };
8 changes: 4 additions & 4 deletions api/src/cohort/cohortService.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { COHORT_TYPE, ERRORS, CohortType } from "snu-lib";
import { CohortDocument, CohortModel } from "../models";

const isInscriptionOpenOnSomeCohorts = async (): Promise<Boolean> => {
const isInscriptionOpenOnSomeCohorts = async (): Promise<boolean> => {
const cohorts = await CohortModel.find({ type: COHORT_TYPE.VOLONTAIRE });
return cohorts.some((cohort) => cohort.isInscriptionOpen);
};

const isReInscriptionOpenOnSomeCohorts = async (): Promise<Boolean> => {
const isReInscriptionOpenOnSomeCohorts = async (): Promise<boolean> => {
const cohorts = await CohortModel.find({ type: COHORT_TYPE.VOLONTAIRE });
return cohorts.some((cohort) => cohort.isReInscriptionOpen);
};

export const isInscriptionOpen = async (cohortName: String | undefined): Promise<Boolean> => {
export const isInscriptionOpen = async (cohortName: String | undefined): Promise<boolean> => {
if (cohortName) {
const cohort = await CohortModel.findOne({ name: cohortName });
if (!cohort) return false;
Expand All @@ -20,7 +20,7 @@ export const isInscriptionOpen = async (cohortName: String | undefined): Promise
return isInscriptionOpenOnSomeCohorts();
};

export const isReInscriptionOpen = async (cohortName?: String): Promise<Boolean> => {
export const isReInscriptionOpen = async (cohortName?: String): Promise<boolean> => {
if (cohortName) {
const cohort = await CohortModel.findOne({ name: cohortName });
if (!cohort) return false;
Expand Down
35 changes: 18 additions & 17 deletions api/src/controllers/cohort-session.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand Down Expand Up @@ -78,36 +81,34 @@ router.post("/eligibility/2023/:id?", passport.authenticate("referent"), async f
}
});

router.get("/isReInscriptionOpen", async (req: UserRequest, res: Response) => {
router.get("/isReInscriptionOpen", async (req: RouteRequest<CohortsRoutes["GetIsReincriptionOpen"]>, res: RouteResponse<CohortsRoutes["GetIsReincriptionOpen"]>) => {
try {
const data = await isReInscriptionOpen();
const isOpen = await isReInscriptionOpen();

return res.send({
return res.json({
ok: true,
data,
data: isOpen,
});
} catch (error) {
capture(error);
return res.status(500).send({ ok: false, code: ERRORS.SERVER_ERROR });
}
});

router.get("/isInscriptionOpen", async (req: UserRequest, res: Response) => {
const { error, value } = Joi.object({
router.get("/isInscriptionOpen", async (req: RouteRequest<CohortsRoutes["GetIsIncriptionOpen"]>, res: RouteResponse<CohortsRoutes["GetIsIncriptionOpen"]>) => {
const { error, value: query } = Joi.object<CohortsRoutes["GetIsIncriptionOpen"]["query"]>({
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);
Expand Down
4 changes: 2 additions & 2 deletions api/src/utils/cohort.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions packages/lib/src/routes/cohort/get.ts
Original file line number Diff line number Diff line change
@@ -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<CohortDto>;
response: RouteResponseBody<CohortType>;
}
33 changes: 33 additions & 0 deletions packages/lib/src/routes/cohort/getEligibility.ts
Original file line number Diff line number Diff line change
@@ -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;
}
>
>;
}
10 changes: 10 additions & 0 deletions packages/lib/src/routes/cohort/getIsIncriptionOpen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { BasicRoute, RouteResponseBody } from "..";

export interface GetIsIncriptionOpenRoute extends BasicRoute {
method: "GET";
path: "/cohort-session/isInscriptionOpen";
query: {
sessionName?: string;
};
response: RouteResponseBody<boolean>;
}
7 changes: 7 additions & 0 deletions packages/lib/src/routes/cohort/getIsReIncriptionOpen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { BasicRoute, RouteResponseBody } from "..";

export interface GetIsReincriptionOpenRoute extends BasicRoute {
method: "GET";
path: "/cohort-session/isReInscriptionOpen";
response: RouteResponseBody<boolean>;
}
10 changes: 6 additions & 4 deletions packages/lib/src/routes/cohort/index.ts
Original file line number Diff line number Diff line change
@@ -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;
};
2 changes: 1 addition & 1 deletion packages/lib/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export interface BasicRoute {
params?: Record<string, number | string | null | undefined>;
payload?: Record<string, any>;
query?: Record<string, any>;
response?: RouteResponseBody<Record<string, any> | Record<string, any>[]>;
response?: RouteResponseBody<Record<string, any> | Record<string, any>[] | boolean>;
method: "GET" | "POST" | "DELETE" | "PUT";
}

Expand Down

0 comments on commit 42965e7

Please sign in to comment.