-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanagementRouter.ts
32 lines (26 loc) · 1.22 KB
/
managementRouter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import express, { Response } from 'express';
import { z } from 'zod';
import { isSuperUserCheck } from '../middleware/userRoleMiddleware';
import { getAllUsersList, getUserById, updateUserLock } from '../services/managementService';
import { ParticipantRequest } from '../services/participantsService';
const handleGetAllUsers = async (req: ParticipantRequest, res: Response) => {
const userList = await getAllUsersList();
return res.status(200).json(userList);
};
const handleChangeUserLock = async (req: ParticipantRequest, res: Response) => {
const { userId } = z.object({ userId: z.coerce.number() }).parse(req.params);
const { isLocked } = z.object({ isLocked: z.boolean() }).parse(req.body);
const user = await getUserById(userId);
if (req.auth?.payload?.email === user?.email) {
res.status(403).send([{ message: 'You cannot lock yourself.' }]);
return;
}
await updateUserLock(req, userId, isLocked);
return res.status(200).end();
};
export function createManagementRouter() {
const managementRouter = express.Router();
managementRouter.get('/users', isSuperUserCheck, handleGetAllUsers);
managementRouter.patch('/:userId/changeLock', isSuperUserCheck, handleChangeUserLock);
return managementRouter;
}