From 2ecd17ae613922a9b0af3e04b7c5100bf0106bc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Bret=C3=A9cher?= Date: Thu, 30 Jan 2025 11:53:21 +0100 Subject: [PATCH] up --- .../src/scenes/volontaires/useFilterLabels.ts | 14 ++++++--- admin/src/scenes/volontaires/utils/index.js | 16 ++++------ .../filterLabelController.ts} | 15 ++++------ api/src/filterLabel/filterLabelRepository.ts | 29 +++++++++++++++++++ api/src/filterLabel/filterLabelService.ts | 11 +++++++ api/src/routes.ts | 2 +- packages/lib/src/dto/filterLabelDto.ts | 1 + packages/lib/src/dto/index.ts | 1 + packages/lib/src/routes/filterLabel/index.ts | 13 +++++++++ packages/lib/src/routes/index.ts | 1 + 10 files changed, 78 insertions(+), 25 deletions(-) rename api/src/{controllers/filter-label.ts => filterLabel/filterLabelController.ts} (54%) create mode 100644 api/src/filterLabel/filterLabelRepository.ts create mode 100644 api/src/filterLabel/filterLabelService.ts create mode 100644 packages/lib/src/dto/filterLabelDto.ts create mode 100644 packages/lib/src/routes/filterLabel/index.ts diff --git a/admin/src/scenes/volontaires/useFilterLabels.ts b/admin/src/scenes/volontaires/useFilterLabels.ts index 9eb2d0d46b..f84b4a4f8e 100644 --- a/admin/src/scenes/volontaires/useFilterLabels.ts +++ b/admin/src/scenes/volontaires/useFilterLabels.ts @@ -1,9 +1,15 @@ -import API from "@/services/api"; +import { buildRequest } from "@/utils/buildRequest"; import { useQuery } from "@tanstack/react-query"; +import { FilterLabelDto, FilterLabelRoutes } from "snu-lib"; -async function getFilterLabels(listType: string): Promise { - const { ok, data } = await API.get(`/filter-label/${listType}`); - if (!ok) throw new Error("Impossible de récupérer les labels de filtres."); +async function getFilterLabels(listType: string): Promise { + const { ok, code, data } = await buildRequest({ + path: "/filter-label/{listType}", + method: "GET", + params: { listType }, + })(); + if (!ok) throw new Error(code); + if (!data) throw new Error("No data"); return data; } diff --git a/admin/src/scenes/volontaires/utils/index.js b/admin/src/scenes/volontaires/utils/index.js index c179ff5d4d..212415c8bd 100644 --- a/admin/src/scenes/volontaires/utils/index.js +++ b/admin/src/scenes/volontaires/utils/index.js @@ -26,6 +26,7 @@ import { formatPhoneE164 } from "../../../utils/formatPhoneE164"; import { youngCheckinField } from "@/utils"; export const getFilterArray = (user, bus, session, classes, etablissements) => { + console.log("🚀 ~ getFilterArray ~ classes:", classes); return [ { title: "Cohorte", name: "cohort", parentGroup: "Général", missingLabel: "Non renseigné", sort: (e) => orderCohort(e) }, { title: "Cohorte d'origine", name: "originalCohort", parentGroup: "Général", missingLabel: "Non renseigné", sort: orderCohort }, @@ -181,10 +182,9 @@ export const getFilterArray = (user, bus, session, classes, etablissements) => { parentGroup: "Phase 1", missingLabel: "Non renseigné", translate: (item) => { + console.log("🚀 ~ getFilterArray ~ item:", typeof item); if (item === "N/A" || !classes.length) return item; - const res = classes.find((option) => option._id.toString() === item); - if (!res) return "N/A - Supprimé"; - return res?.uniqueKeyAndId; + return classes[item] || "N/A - Supprimé"; }, }, { @@ -194,9 +194,7 @@ export const getFilterArray = (user, bus, session, classes, etablissements) => { missingLabel: "Non renseigné", translate: (item) => { if (item === "N/A" || !etablissements.length) return item; - const res = etablissements.find((option) => option._id.toString() === item); - if (!res) return "N/A - Supprimé"; - return res?.name; + return etablissements[item] || "N/A - Supprimé"; }, }, { @@ -206,9 +204,7 @@ export const getFilterArray = (user, bus, session, classes, etablissements) => { missingLabel: "Non renseigné", translate: (item) => { if (item === "N/A" || !session.length) return item; - const res = session.find((option) => option._id.toString() === item); - if (!res) return "N/A - Supprimé"; - return res?.codeCentre; + return session[item] || "N/A - Supprimé"; }, }, { @@ -274,7 +270,7 @@ export const getFilterArray = (user, bus, session, classes, etablissements) => { missingLabel: "Non renseigné", translate: (item) => { if (item === "N/A" || !bus?.length) return item; - return bus.find((option) => option._id.toString() === item)?.busId || item; + return bus[item] || item; }, }, { diff --git a/api/src/controllers/filter-label.ts b/api/src/filterLabel/filterLabelController.ts similarity index 54% rename from api/src/controllers/filter-label.ts rename to api/src/filterLabel/filterLabelController.ts index 66c0c92f64..613c8dc45b 100644 --- a/api/src/controllers/filter-label.ts +++ b/api/src/filterLabel/filterLabelController.ts @@ -2,14 +2,12 @@ import Joi from "joi"; import { ERRORS } from "snu-lib"; import passport from "passport"; import express, { Response } from "express"; -import { ClasseModel, EtablissementModel, LigneBusModel, SessionPhase1Model } from "../models"; import { capture } from "../sentry"; -import { UserRequest } from "./request"; +import { UserRequest } from "../controllers/request"; +import { getLabelVolontaires, listTypes } from "./filterLabelService"; const router = express.Router(); -const listTypes = { INSCRIPTION: "inscription", VOLONTAIRES: "volontaires" }; - const validator = Joi.string().valid(listTypes.INSCRIPTION, listTypes.VOLONTAIRES).required(); router.get("/:list", passport.authenticate("referent", { session: false, failWithError: true }), async (req: UserRequest, res: Response) => { @@ -20,14 +18,11 @@ router.get("/:list", passport.authenticate("referent", { session: false, failWit } if (value === listTypes.VOLONTAIRES) { - const sessions = await SessionPhase1Model.find({}, { codeCentre: 1 }); - const bus = await LigneBusModel.find({}, { busId: 1 }); - const classes = await ClasseModel.find({}, { uniqueKeyAndId: 1 }); - // console.log("🚀 ~ router.get ~ classes:", classes); - const etablissements = await EtablissementModel.find({}, { name: 1 }); - const data = { sessions, bus, classes, etablissements }; + const data = await getLabelVolontaires(); return res.status(200).send({ ok: true, data }); } + + // TODO: getLabelInscriptions }); module.exports = router; diff --git a/api/src/filterLabel/filterLabelRepository.ts b/api/src/filterLabel/filterLabelRepository.ts new file mode 100644 index 0000000000..d35638a581 --- /dev/null +++ b/api/src/filterLabel/filterLabelRepository.ts @@ -0,0 +1,29 @@ +import { ClasseModel, EtablissementModel, LigneBusModel, SessionPhase1Model } from "../models"; + +function formatLabel(data, key: string) { + return data.reduce((acc, cur) => { + acc[cur._id] = cur[key]; + return acc; + }, {}); +} + +export async function getBus() { + const data = await LigneBusModel.find({}, { busId: 1 }); + return formatLabel(data, "busId"); +} + +export async function getClasses() { + // Pourquoi ça renvoit les patches ça ? + const data = await ClasseModel.find({}, { uniqueKeyAndId: 1 }); + return formatLabel(data, "uniqueKeyAndId"); +} + +export async function getEtablissements() { + const data = await EtablissementModel.find({}, { name: 1 }); + return formatLabel(data, "name"); +} + +export async function getSessions() { + const data = await SessionPhase1Model.find({}, { codeCentre: 1 }); + return formatLabel(data, "codeCentre"); +} diff --git a/api/src/filterLabel/filterLabelService.ts b/api/src/filterLabel/filterLabelService.ts new file mode 100644 index 0000000000..c0a71fbd1c --- /dev/null +++ b/api/src/filterLabel/filterLabelService.ts @@ -0,0 +1,11 @@ +import { getBus, getClasses, getEtablissements, getSessions } from "./filterLabelRepository"; + +export const listTypes = { INSCRIPTION: "inscription", VOLONTAIRES: "volontaires" }; + +export async function getLabelVolontaires() { + const sessions = await getSessions(); + const bus = await getBus(); + const classes = await getClasses(); + const etablissements = await getEtablissements(); + return { sessions, bus, classes, etablissements }; +} diff --git a/api/src/routes.ts b/api/src/routes.ts index 11abe76fa0..3def966146 100644 --- a/api/src/routes.ts +++ b/api/src/routes.ts @@ -40,7 +40,7 @@ export function injectRoutes(app) { app.use("/SNUpport", require("./controllers/SNUpport")); app.use("/cle", require("./cle").default); app.use("/preinscription", require("./preinscription/preinscriptionController")); - app.use("/filter-label", require("./controllers/filter-label")); + app.use("/filter-label", require("./filterLabel/filterLabelController")); //services app.use("/jeveuxaider", require("./services/jeveuxaider")); diff --git a/packages/lib/src/dto/filterLabelDto.ts b/packages/lib/src/dto/filterLabelDto.ts new file mode 100644 index 0000000000..b3538c824a --- /dev/null +++ b/packages/lib/src/dto/filterLabelDto.ts @@ -0,0 +1 @@ +export type FilterLabelDto = Record; diff --git a/packages/lib/src/dto/index.ts b/packages/lib/src/dto/index.ts index e0bda87314..c7cdf00651 100644 --- a/packages/lib/src/dto/index.ts +++ b/packages/lib/src/dto/index.ts @@ -10,3 +10,4 @@ export * from "./contactDto"; export * from "./taskDto"; export * from "./phase1"; export * from "./referentiel/ReferentielTaskDto"; +export * from "./filterLabelDto"; diff --git a/packages/lib/src/routes/filterLabel/index.ts b/packages/lib/src/routes/filterLabel/index.ts new file mode 100644 index 0000000000..ce5f3da442 --- /dev/null +++ b/packages/lib/src/routes/filterLabel/index.ts @@ -0,0 +1,13 @@ +import { FilterLabelDto } from "src/dto/filterLabelDto"; +import { BasicRoute, RouteResponseBody } from ".."; + +interface GetFilterLabelRoute extends BasicRoute { + method: "GET"; + path: "/filter-label/{listType}"; + params: { listType: string }; + response: RouteResponseBody; +} + +export type FilterLabelRoutes = { + Get: GetFilterLabelRoute; +}; diff --git a/packages/lib/src/routes/index.ts b/packages/lib/src/routes/index.ts index 1f133b8cd4..ecefffe341 100644 --- a/packages/lib/src/routes/index.ts +++ b/packages/lib/src/routes/index.ts @@ -38,5 +38,6 @@ export type { PreinscriptionRoutes } from "./preinscription"; export type { AffectationRoutes } from "./phase1/affectation"; export type { ReferentielRoutes } from "./referentiel"; export type { ReferentRoutes } from "./iam/referent"; +export type { FilterLabelRoutes } from "./filterLabel"; export * from "./phase1";