Skip to content

Commit 1ae8c0b

Browse files
authored
Merge pull request #90 from ungdev/dev
feat(shotgun & 404 page & minimized routes)
2 parents e534d9a + c05d5b8 commit 1ae8c0b

17 files changed

Lines changed: 462 additions & 203 deletions

File tree

Lines changed: 103 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,166 +1,209 @@
11
import { Request, Response } from "express";
2-
import { Accepted, Error, Ok, Unauthorized } from "../utils/responses";
2+
import { Accepted, Conflict, Error, Ok, Teapot, Unauthorized } from "../utils/responses";
33
import * as event_service from "../services/event.service";
44
import * as team_service from "../services/team.service";
55
import { Event } from "../schemas/Basic/event.schema";
6+
import { shotgun_password } from "../utils/secret";
7+
8+
type AuthenticatedRequest = Request & { user?: { userId?: number } };
69

710

811
export const checkShotgunStatus = async (req: Request, res: Response) => {
9-
try{
12+
try {
1013
const status = await event_service.getEventsStatus();
11-
Ok(res, ({data: status?.shotgun_open }));
1214

13-
}catch(error){
14-
Error(res, {msg :"Error while catching shotgun status :" + error})
15+
Ok(res, ({ data: { status: Boolean(status?.shotgun_open), password: Boolean(status?.shotgun_open) ? shotgun_password : "" } }));
16+
17+
} catch (error) {
18+
Error(res, { msg: "Error while catching shotgun status :" + error })
1519
}
1620
};
1721

1822
export const checkPreRegisterStatus = async (req: Request, res: Response) => {
19-
try{
23+
try {
2024
const status = await event_service.getEventsStatus();
21-
Ok(res, ({data: status?.pre_registration_open}));
25+
Ok(res, ({ data: status?.pre_registration_open }));
2226

23-
}catch(error){
24-
Error(res, {msg :"Error while catching shotgun status :" + error})
27+
} catch (error) {
28+
Error(res, { msg: "Error while catching pre-registration status :" + error })
2529
}
2630
};
2731

2832
export const checkSDIStatus = async (req: Request, res: Response) => {
29-
try{
33+
try {
3034
const status = await event_service.getEventsStatus();
31-
Ok(res, ({data: status?.sdi_open}));
35+
Ok(res, ({ data: status?.sdi_open }));
3236

33-
}catch(error){
34-
Error(res, {msg :"Error while catching SDI status :" + error})
37+
} catch (error) {
38+
Error(res, { msg: "Error while catching SDI status :" + error })
3539
}
3640
};
3741

3842
export const checkWEIStatus = async (req: Request, res: Response) => {
39-
try{
43+
try {
4044
const status = await event_service.getEventsStatus();
41-
Ok(res, ({data: status?.wei_open}));
45+
Ok(res, ({ data: status?.wei_open }));
4246

43-
}catch(error){
44-
Error(res, {msg :"Error while catching WEI status :" + error})
47+
} catch (error) {
48+
Error(res, { msg: "Error while catching WEI status :" + error })
4549
}
4650
};
4751

4852
export const checkFoodStatus = async (req: Request, res: Response) => {
49-
try{
53+
try {
5054
const status = await event_service.getEventsStatus();
51-
Ok(res, ({data: status?.food_open}));
55+
Ok(res, ({ data: status?.food_open }));
5256

53-
}catch(error){
54-
Error(res, {msg :"Error while catching Food status :" + error})
57+
} catch (error) {
58+
Error(res, { msg: "Error while catching Food status :" + error })
5559
}
5660
};
5761

5862
export const checkChallStatus = async (req: Request, res: Response) => {
59-
try{
63+
try {
6064
const status = await event_service.getEventsStatus();
61-
Ok(res, ({data: status?.chall_open}));
65+
Ok(res, ({ data: status?.chall_open }));
66+
67+
} catch (error) {
68+
Error(res, { msg: "Error while catching Challenge status :" + error })
69+
}
70+
};
71+
72+
export const getShotgunAttempts = async (req: Request, res: Response) => {
73+
try {
74+
const shotgunAttempts = await event_service.getAllTeamShotguns();
75+
const shotgunAttemptsWithLeaders = await Promise.all(
76+
shotgunAttempts.map(async (attempt) => {
77+
if (!attempt.teamId) {
78+
return { ...attempt, leaderCount: 0 };
79+
}
80+
81+
const teamUsers = await team_service.getTeamUsers(attempt.teamId);
82+
const leaderCount = teamUsers.filter((user) => user.permission !== "Nouveau").length;
83+
84+
return { ...attempt, leaderCount };
85+
})
86+
);
6287

63-
}catch(error){
64-
Error(res, {msg :"Error while catching Challenge status :" + error})
88+
Ok(res, { data: shotgunAttemptsWithLeaders });
89+
} catch (error) {
90+
Error(res, { msg: "Erreur lors de la récupération des tentatives shotgun : " + error });
6591
}
6692
};
6793

6894

6995
export const shotgunAttempt = async (req: Request, res: Response) => {
7096

71-
const userId = req.user.userId;
72-
97+
const { password } = req.body as { password?: string };
98+
99+
const userId = (req as AuthenticatedRequest).user?.userId;
100+
101+
if (!userId) {
102+
Unauthorized(res, { msg: "Utilisateur non authentifié." });
103+
return;
104+
}
105+
106+
if (!shotgun_password) {
107+
Error(res, { msg: "Mot de passe shotgun non configuré côté serveur." });
108+
return;
109+
}
110+
111+
if (password !== shotgun_password) {
112+
Teapot(res, { msg: "Le mot de passe shotgun est incorrect." });
113+
return;
114+
}
115+
73116
const status = await event_service.getEventsStatus();
74117
if (!status?.shotgun_open) {
75-
Unauthorized(res, { msg: "Le shotgun est fermé." });
76-
return;
118+
Unauthorized(res, { msg: "Le shotgun est fermé." });
119+
return;
77120
}
78-
try{
121+
try {
79122
const userTeam = await team_service.getUserTeam(userId)
80123

81-
if(!userTeam){
124+
if (!userTeam) {
82125
Error(res, { msg: "Erreur : Tu n'as pas d'équipe !" });
83126
return;
84127
}
85128

86129
const alreadyShotgun = await event_service.alreadyShotgun(userTeam)
87130

88-
if(alreadyShotgun){
89-
Accepted(res, { msg: "Votre équipe est déjà dans le shotgun." });
131+
if (alreadyShotgun) {
132+
Conflict(res, { msg: "Votre équipe est déjà dans le shotgun." });
90133
return;
91134
}
92135

93136
await event_service.validateShotgun(userTeam);
94-
Ok(res, { msg: "Shotgun validé !"});
137+
Ok(res, { msg: "Shotgun validé !" });
95138
return;
96-
}catch(error){
97-
Error(res, {msg :"Erreur pendant le shotguns : "+ error});
139+
} catch (error) {
140+
Error(res, { msg: "Erreur pendant le shotguns : " + error });
98141
return;
99142
}
100143
};
101144

102145
export const togglePreRegistration = async (req: Request, res: Response) => {
103146
const { preRegistrationOpen } = req.body;
104-
147+
105148
try {
106-
const result = await event_service.updatepreRegistrationStatus(preRegistrationOpen);
107-
Ok(res, { msg: "Paramètres mis à jour.", data : result});
149+
const result = await event_service.updatepreRegistrationStatus(preRegistrationOpen);
150+
Ok(res, { msg: "Paramètres mis à jour.", data: result });
108151
} catch (error) {
109-
Error(res, { msg: "Erreur lors de la mise à jour." });
152+
Error(res, { msg: "Erreur lors de la mise à jour." });
110153
}
111154
};
112155

113156
export const toggleShotgun = async (req: Request, res: Response) => {
114157
const { shotgunOpen } = req.body;
115-
158+
116159
try {
117-
const result = await event_service.updateShotgunStatus(shotgunOpen);
118-
Ok(res,{ msg: "Paramètres mis à jour.", data: result });
160+
const result = await event_service.updateShotgunStatus(shotgunOpen);
161+
Ok(res, { msg: "Paramètres mis à jour.", data: result });
119162
} catch (error) {
120-
Error(res,{ msg: "Erreur lors de la mise à jour." });
163+
Error(res, { msg: "Erreur lors de la mise à jour." });
121164
}
122165
};
123166

124167
export const toggleSDI = async (req: Request, res: Response) => {
125168
const { sdiOpen } = req.body;
126-
169+
127170
try {
128-
const result = await event_service.updateSDIStatus(sdiOpen);
129-
Ok(res,{ msg: "Paramètres mis à jour.", data: result });
171+
const result = await event_service.updateSDIStatus(sdiOpen);
172+
Ok(res, { msg: "Paramètres mis à jour.", data: result });
130173
} catch (error) {
131-
Error(res,{ msg: "Erreur lors de la mise à jour." });
174+
Error(res, { msg: "Erreur lors de la mise à jour." });
132175
}
133176
};
134177

135178
export const toggleWEI = async (req: Request, res: Response) => {
136179
const { weiOpen } = req.body;
137-
180+
138181
try {
139-
const result = await event_service.updateWEIStatus(weiOpen);
140-
Ok(res,{ msg: "Paramètres mis à jour.", data: result });
182+
const result = await event_service.updateWEIStatus(weiOpen);
183+
Ok(res, { msg: "Paramètres mis à jour.", data: result });
141184
} catch (error) {
142-
Error(res,{ msg: "Erreur lors de la mise à jour." });
185+
Error(res, { msg: "Erreur lors de la mise à jour." });
143186
}
144187
};
145188

146189
export const toggleFood = async (req: Request, res: Response) => {
147190
const { foodOpen } = req.body;
148-
191+
149192
try {
150-
const result = await event_service.updateFoodStatus(foodOpen);
151-
Ok(res,{ msg: "Paramètres mis à jour.", data: result });
193+
const result = await event_service.updateFoodStatus(foodOpen);
194+
Ok(res, { msg: "Paramètres mis à jour.", data: result });
152195
} catch (error) {
153-
Error(res,{ msg: "Erreur lors de la mise à jour." });
196+
Error(res, { msg: "Erreur lors de la mise à jour." });
154197
}
155198
};
156199

157200
export const toggleChall = async (req: Request, res: Response) => {
158201
const { challOpen } = req.body;
159-
202+
160203
try {
161-
const result = await event_service.updateChallStatus(challOpen);
162-
Ok(res,{ msg: "Paramètres mis à jour.", data: result });
204+
const result = await event_service.updateChallStatus(challOpen);
205+
Ok(res, { msg: "Paramètres mis à jour.", data: result });
163206
} catch (error) {
164-
Error(res,{ msg: "Erreur lors de la mise à jour." });
207+
Error(res, { msg: "Erreur lors de la mise à jour." });
165208
}
166209
};

backend/src/routes/event.routes.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@ import { authenticateUser } from '../middlewares/auth.middleware';
66
const eventRouter = express.Router();
77

88
// User routes
9-
eventRouter.get("/user/shotgunstatus",checkRole("Student",[]), eventController.checkShotgunStatus);
10-
eventRouter.get("/user/preregisterstatus",checkRole("Student",[]), eventController.checkPreRegisterStatus);
9+
eventRouter.get("/user/shotgunstatus", checkRole("Student", []), eventController.checkShotgunStatus);
10+
eventRouter.get("/user/preregisterstatus", checkRole("Student", []), eventController.checkPreRegisterStatus);
1111
eventRouter.get("/user/sdistatus", eventController.checkSDIStatus);
1212
eventRouter.get("/user/weistatus", eventController.checkWEIStatus);
1313
eventRouter.get("/user/foodstatus", eventController.checkFoodStatus);
1414
eventRouter.get("/user/challstatus", eventController.checkChallStatus);
15-
eventRouter.post("/user/shotgunattempt",checkRole("Student",[]), eventController.shotgunAttempt);
15+
eventRouter.post("/user/shotgunattempt", checkRole("Student", []), eventController.shotgunAttempt);
1616

1717

1818
// Admin routes
19-
eventRouter.post("/admin/shotguntoggle",checkRole("Admin",[]),eventController.toggleShotgun);
20-
eventRouter.post("/admin/preregistrationtoggle",checkRole("Admin",[]), eventController.togglePreRegistration);
21-
eventRouter.post("/admin/sditoggle",checkRole("Admin",[]),eventController.toggleSDI);
22-
eventRouter.post("/admin/weitoggle",checkRole("Admin",[]), eventController.toggleWEI);
23-
eventRouter.post("/admin/foodtoggle",checkRole("Admin",[]), eventController.toggleFood);
24-
eventRouter.post("/admin/challtoggle",checkRole("Admin",[]), eventController.toggleChall);
19+
eventRouter.post("/admin/shotguntoggle", checkRole("Admin", []), eventController.toggleShotgun);
20+
eventRouter.get("/admin/shotgunattempts", checkRole("Admin", ["Respo CE"]), eventController.getShotgunAttempts);
21+
eventRouter.post("/admin/preregistrationtoggle", checkRole("Admin", []), eventController.togglePreRegistration);
22+
eventRouter.post("/admin/sditoggle", checkRole("Admin", []), eventController.toggleSDI);
23+
eventRouter.post("/admin/weitoggle", checkRole("Admin", []), eventController.toggleWEI);
24+
eventRouter.post("/admin/foodtoggle", checkRole("Admin", []), eventController.toggleFood);
25+
eventRouter.post("/admin/challtoggle", checkRole("Admin", []), eventController.toggleChall);
2526

2627
export default eventRouter;

0 commit comments

Comments
 (0)