api: update user and drop old create-app
Parents:
cc08a065 file(s) changed
- api/src/controllers/users.ts +35 -2
- api/src/createApp.ts +3 -0
- api/src/index.ts +1 -1
- api/src/models/user.ts +24 -4
- api/src/routes/users.ts +1 -0
api/src/controllers/users.ts
@@ -1,6 +1,9 @@
1 1 import { Request, Response, NextFunction } from 'express';
2 2 import {
3 3 createUser,
4 + findUserById,
5 + findUserByEmail,
6 + updateUser,
4 7 generateToken
5 8 } from '@app/models/user';
6 9
@@ -11,11 +14,11 @@
11 14 export const UsersController = {
12 15 create: async (req: Request, res: Response, next: NextFunction) => {
13 16 try {
14 - const { email, password, first_name: firstName, last_name: lastName } = req.body;
17 + const { email, password, firstName, lastName } = req.body;
15 18
16 19 // Validate required fields
17 20 const requiredValidation = validateRequiredFields(req.body, [
18 - 'email', 'password', 'first_name'
21 + 'email', 'password', 'firstName'
19 22 ]);
20 23
21 24 if (!requiredValidation.valid) {
@@ -32,6 +35,36 @@
32 35 const jwtToken = generateToken(user.id, user.email);
33 36
34 37 res.status(201).json({ token: jwtToken });
38 + } catch (err) {
39 + next(err);
40 + }
41 + },
42 +
43 + update: async (req: Request, res: Response, next: NextFunction) => {
44 + try {
45 + const { id } = req.params
46 + const { email, password, firstName, lastName } = req.body;
47 +
48 + const user = await findUserById(Number(id));
49 +
50 + if (!user) {
51 + return res.status(404).json({
52 + error: "Usuário não encontrado"
53 + })
54 + }
55 +
56 + if (email || password || firstName || lastName) {
57 + const response = await updateUser(user, {
58 + email, firstName, lastName
59 + })
60 + }
61 +
62 + const updated = await findUserById(Number(id))
63 +
64 + // TODO: Drop encrypted password & token here
65 + return res.status(200).json({
66 + user: updated
67 + })
35 68 } catch (err) {
36 69 next(err);
37 70 }
api/src/createApp.ts
@@ -1,5 +1,6 @@
1 1 import express from 'express';
2 2 import cors from 'cors';
3 + import bodyParser from 'body-parser';
3 4 import morgan from 'morgan';
4 5 import userRouter from '@app/routes/users';
5 6 import authRouter from '@app/routes/auth';
@@ -16,6 +17,7 @@ credentials: true
16 17 }));
17 18
18 19 app.use(express.json());
20 + app.use(bodyParser.json());
19 21
20 22 // Only use morgan in non-test environments
21 23 if (process.env.NODE_ENV !== 'test') {
@@ -33,4 +35,4 @@
33 35 app.use(errorHandler); // always last
34 36
35 37 return app;
36 - }
38 + }
api/src/index.ts
@@ -1,4 +1,4 @@
1 - import { createApp } from '@app/create-app';
1 + import { createApp } from '@app/createApp';
2 2
3 3 const port = process.env.PORT || 3000;
4 4
api/src/models/user.ts
@@ -16,6 +16,12 @@ } from "@app/utils/validators";
16 16
17 17 import { InvalidEmailError, ShortPasswordError } from '@app/lib/errors/UserErrors'
18 18
19 + type UserOptions = {
20 + email: string;
21 + firstName: string;
22 + lastName: string;
23 + };
24 +
19 25 const createUser = async (email: string, firstName: string, lastName: string, password: string) => {
20 26 // Validate email format
21 27 if (!isValidEmail(email)) {
@@ -26,7 +32,7 @@ // Validate password strength
26 32 if (!isValidPassword(password)) {
27 33 throw new ShortPasswordError()
28 34 }
29 -
35 +
30 36 const encryptedPassword = bcrypt.hashSync(password, 10);
31 37
32 38 try {
@@ -41,14 +47,19 @@ });
41 47
42 48 return user;
43 49 } catch (err: any) {
44 - console.log(err)
45 - throw new Error("Something was off when creating user")
50 + throw err;
46 51 }
47 52 }
48 53
49 54 const generateToken = (userId: number, email: string) => {
50 55 return createJWT(userId, email);
51 56 }
57 +
58 + const findUserById = async (id: number) => {
59 + return await prisma.user.findUnique({
60 + where: { id }
61 + });
62 + };
52 63
53 64 const findUserByEmail = async (email: string) => {
54 65 return await prisma.user.findUnique({
@@ -134,11 +145,20 @@
134 145 return { success: true };
135 146 };
136 147
148 + const updateUser = async (user: any, data: UserOptions) => {
149 + return await prisma.user.update({
150 + where: { id: user.id },
151 + data
152 + });
153 + }
154 +
137 155 export {
138 156 createUser,
157 + findUserById,
139 158 generateToken,
140 159 findUserByEmail,
141 160 authenticateUser,
142 161 initiatePasswordReset,
143 - resetPassword
162 + resetPassword,
163 + updateUser
144 164 }
api/src/routes/users.ts
@@ -4,5 +4,6 @@
4 4 const router = Router();
5 5
6 6 router.post('/', UsersController.create);
7 + router.put('/:id', UsersController.update);
7 8
8 9 export default router;