Skip to content

Commit

Permalink
add type and year properties
Browse files Browse the repository at this point in the history
  • Loading branch information
nicobret committed Nov 5, 2024
1 parent df111fd commit 9467aa8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
12 changes: 7 additions & 5 deletions api/src/cohortGroup/cohortGroupController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import express from "express";
import { RouteRequest, RouteResponse } from "../controllers/request";
import { capture } from "../sentry";
import { CohortGroupRoutes, ERRORS, ROLES } from "snu-lib";
import { COHORT_TYPE_LIST, CohortGroupRoutes, ERRORS, ROLES } from "snu-lib";
import { accessControlMiddleware } from "../middlewares/accessControlMiddleware";
import { CohortGroupModel } from "../models/cohortGroup";
import Joi from "joi";
Expand All @@ -25,13 +25,15 @@ router.post(
requestValidatorMiddleware({
body: Joi.object({
name: Joi.string().required(),
type: Joi.string().valid(COHORT_TYPE_LIST),
year: Joi.number(),
}),
}),
accessControlMiddleware([ROLES.ADMIN]),
async (req: RouteRequest<CohortGroupRoutes["PostCohortGroupRoute"]>, res: RouteResponse<CohortGroupRoutes["PostCohortGroupRoute"]>) => {
try {
const { name } = req.body;
const data = new CohortGroupModel({ name });
const { name, type, year } = req.body;
const data = new CohortGroupModel({ name, type, year });
await data.save({ fromUser: req.user });
return res.json({ ok: true, data });
} catch (error) {
Expand All @@ -55,12 +57,12 @@ router.put(
async (req: RouteRequest<CohortGroupRoutes["PutCohortGroupRoute"]>, res: RouteResponse<CohortGroupRoutes["PutCohortGroupRoute"]>) => {
try {
const { id } = req.params;
const { name } = req.body;
const { name, type, year } = req.body;
const data = await CohortGroupModel.findById(id);
if (!data) {
return res.status(404).send({ ok: false, code: ERRORS.NOT_FOUND });
}
data.name = name;
data.set({ name, type, year });
await data.save({ fromUser: req.user });
return res.json({ ok: true, data });
} catch (error) {
Expand Down
15 changes: 14 additions & 1 deletion packages/lib/src/mongoSchema/cohortGroup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Schema, InferSchemaType } from "mongoose";

import { InterfaceExtended } from "..";
import { COHORT_TYPE_LIST, InterfaceExtended } from "..";

export const CohortGroupSchema = {
name: {
Expand All @@ -10,6 +10,19 @@ export const CohortGroupSchema = {
description: "Nom du groupe de cohortes. Exp: CLE 2024, HTS 2025.",
},
},
type: {
type: String,
enum: COHORT_TYPE_LIST,
documentation: {
description: "Type de cohortes : CLE ou VOLONTAIRE.",
},
},
year: {
type: Number,
documentation: {
description: "Année de la cohorte.",
},
},
createdAt: { type: Date, default: Date.now },
updatedAt: { type: Date, default: Date.now },
};
Expand Down
4 changes: 2 additions & 2 deletions packages/lib/src/routes/cohortGroup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BasicRoute, RouteResponseBody } from "..";
interface PostCohortGroupRoute extends BasicRoute {
method: "POST";
path: "/cohort-group";
payload: { name: string };
payload: { name: string; type: string; year: number };
response: RouteResponseBody<CohortGroupType>;
}

Expand All @@ -18,7 +18,7 @@ interface PutCohortGroupRoute extends BasicRoute {
method: "PUT";
path: "/cohort-group/:id";
params: { id: string };
payload: { name: string };
payload: { name: string; type: string; year: number };
response: RouteResponseBody<CohortGroupType>;
}

Expand Down

0 comments on commit 9467aa8

Please sign in to comment.