-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers.router.ts
More file actions
114 lines (104 loc) · 3.36 KB
/
users.router.ts
File metadata and controls
114 lines (104 loc) · 3.36 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { NextFunction, Request, Router } from 'express';
import {
createJwtForUser,
findFilteredPosts,
findFilteredVotes,
findFilteredComments,
getPostFilterOptionsFromReqQuery,
getVoteFilterOptionsFromReqQuery,
getCommentFilterOptionsFromReqQuery,
} from '../../../lib/helpers';
import { AuthResponse, NewUserInput } from '../../../types';
import { Prisma } from '../../../../prisma/generated/client';
import {
authValidator,
adminValidator,
optionalAuthValidator,
createAdminOrOwnerValidator,
} from '../../../middlewares/validators';
import userSchema, {
secretSchema,
usernameSchema,
fullnameSchema,
passwordSchema,
} from './user.schema';
import usersService from './users.service';
export const usersRouter = Router();
usersRouter.get('/', authValidator, adminValidator, async (req, res) => {
const users = await usersService.getAllUsers();
res.json(users);
});
usersRouter.get(
'/:idOrUsername',
optionalAuthValidator,
async (req, res, next) => {
const param = req.params.idOrUsername;
const user = await usersService.findUserByIdOrByUsernameOrThrow(param);
if (param === user.id) {
res.json(user);
} else {
const nextWrapper: NextFunction = (x: unknown) => {
if (x) next(x);
else res.json(user);
};
await createAdminOrOwnerValidator(() => user.id)(req, res, nextWrapper);
}
}
);
usersRouter.get('/:id/posts', optionalAuthValidator, async (req, res) => {
const authorId = req.params.id;
const filters = getPostFilterOptionsFromReqQuery(req);
const userPosts = await findFilteredPosts(filters, { authorId });
res.json(userPosts);
});
usersRouter.get('/:id/comments', optionalAuthValidator, async (req, res) => {
const authorId = req.params.id;
const filters = getCommentFilterOptionsFromReqQuery(req);
const comments = await findFilteredComments(filters, { authorId });
res.json(comments);
});
usersRouter.get('/:id/votes', optionalAuthValidator, async (req, res) => {
const userId = req.params.id;
const filters = getVoteFilterOptionsFromReqQuery(req);
const votes = await findFilteredVotes(filters, { userId });
res.json(votes);
});
usersRouter.post('/', async (req, res) => {
const parsedNewUser = userSchema.parse(req.body);
const createdUser = await usersService.createUser(parsedNewUser);
const signupRes: AuthResponse = {
token: createJwtForUser(createdUser),
user: createdUser,
};
res.status(201).json(signupRes);
});
usersRouter.patch(
'/:id',
authValidator,
createAdminOrOwnerValidator((req) => req.params.id),
async (req: Request<{ id: string }, unknown, NewUserInput>, res) => {
const { username, fullname, password, confirm, secret } = req.body;
const data: Prisma.UserUpdateInput = {};
if (username) data.username = usernameSchema.parse(username);
if (fullname) data.fullname = fullnameSchema.parse(fullname);
if (password) {
data.password = passwordSchema.parse({
password: password,
confirm,
}).password;
}
if (secret && secretSchema.parse(secret)) data.isAdmin = true;
await usersService.updateUser(req.params.id, data);
res.status(204).end();
}
);
usersRouter.delete(
'/:id',
authValidator,
createAdminOrOwnerValidator((req) => req.params.id),
async (req, res) => {
await usersService.deleteUser(req.params.id);
res.status(204).end();
}
);
export default usersRouter;