Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
nicobret committed Jan 30, 2025
1 parent 5048b49 commit 2ecd17a
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 25 deletions.
14 changes: 10 additions & 4 deletions admin/src/scenes/volontaires/useFilterLabels.ts
Original file line number Diff line number Diff line change
@@ -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<any> {
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<FilterLabelDto> {
const { ok, code, data } = await buildRequest<FilterLabelRoutes["Get"]>({
path: "/filter-label/{listType}",
method: "GET",
params: { listType },
})();
if (!ok) throw new Error(code);
if (!data) throw new Error("No data");
return data;
}

Expand Down
16 changes: 6 additions & 10 deletions admin/src/scenes/volontaires/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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é";
},
},
{
Expand All @@ -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é";
},
},
{
Expand All @@ -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é";
},
},
{
Expand Down Expand Up @@ -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;
},
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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;
29 changes: 29 additions & 0 deletions api/src/filterLabel/filterLabelRepository.ts
Original file line number Diff line number Diff line change
@@ -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");
}
11 changes: 11 additions & 0 deletions api/src/filterLabel/filterLabelService.ts
Original file line number Diff line number Diff line change
@@ -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 };
}
2 changes: 1 addition & 1 deletion api/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
1 change: 1 addition & 0 deletions packages/lib/src/dto/filterLabelDto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type FilterLabelDto = Record<string, string>;
1 change: 1 addition & 0 deletions packages/lib/src/dto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from "./contactDto";
export * from "./taskDto";
export * from "./phase1";
export * from "./referentiel/ReferentielTaskDto";
export * from "./filterLabelDto";
13 changes: 13 additions & 0 deletions packages/lib/src/routes/filterLabel/index.ts
Original file line number Diff line number Diff line change
@@ -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<FilterLabelDto>;
}

export type FilterLabelRoutes = {
Get: GetFilterLabelRoute;
};
1 change: 1 addition & 0 deletions packages/lib/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

0 comments on commit 2ecd17a

Please sign in to comment.