-
Notifications
You must be signed in to change notification settings - Fork 7
/
user.ts
115 lines (103 loc) · 3.42 KB
/
user.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
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
115
import { ApiResponse } from '../models/ApiResponse';
import { UserDAO } from '../models/DAO/UserDAO';
import { User } from '../models/User';
const user = (app: { [key: string]: any }): void => {
app.get('/users', (req: { [key: string]: any }, res: { [key: string]: any }) => {
const apiResponse = new ApiResponse();
new UserDAO()
.getAllUsersFrom(req.company, req.permission)
.then((users: User[]) => {
apiResponse.responseText = JSON.stringify(users.map((user: User) => user.toJson()));
})
.catch((err) => {
apiResponse.statusCode = 500;
apiResponse.responseText = err.message;
apiResponse.errorMessage = err.message;
})
.finally(() => {
res.status(apiResponse.statusCode).send(apiResponse.jsonResponse);
});
});
app.get('/user', (req: { [key: string]: any }, res: { [key: string]: any }) => {
const apiResponse = new ApiResponse();
const user = {
permission: req.permission,
agency: req.agency,
company: req.company,
email: req.email,
};
apiResponse.statusCode = 200;
apiResponse.responseText = JSON.stringify(user);
res.status(apiResponse.statusCode).send(apiResponse.jsonResponse);
});
app.post('/user/changepass', (req: { [key: string]: any }, res: { [key: string]: any }) => {
const apiResponse = new ApiResponse();
new UserDAO(req.email, req.body.password)
.getUser()
.then((user: User) => {
user.password = req.body.newPassword;
return new UserDAO().changePassword(user);
})
.then((result: boolean) => {
if (result) {
apiResponse.statusCode = 200;
apiResponse.responseText = 'Senha alterada com sucesso!';
} else {
throw new Error('Erro ao modificar a senha!');
}
})
.catch((err) => {
apiResponse.statusCode = 500;
apiResponse.responseText = 'Email e/ou senha incorreto(s)!';
apiResponse.errorMessage = err.message;
})
.finally(() => {
res.status(apiResponse.statusCode).send(apiResponse.jsonResponse);
});
});
app.post('/user/:id/deactivate', (req: { [key: string]: any }, res: { [key: string]: any }) => {
const apiResponse = new ApiResponse();
const targetUserId = req.params.id;
new UserDAO()
.deactivateUser(targetUserId, req.permission)
.then((result: boolean) => {
if (result) {
apiResponse.statusCode = 200;
apiResponse.responseText = 'Usuário desativado com sucesso!';
} else {
throw new Error('Erro ao desativar usuário!');
}
})
.catch((err) => {
apiResponse.statusCode = 500;
apiResponse.responseText = 'Email e/ou senha incorreto(s)!';
apiResponse.errorMessage = err.message;
})
.finally(() => {
res.status(apiResponse.statusCode).send(apiResponse.jsonResponse);
});
});
app.post('/user/:id/reactivate', (req: { [key: string]: any }, res: { [key: string]: any }) => {
const apiResponse = new ApiResponse();
const targetUserId = req.params.id;
new UserDAO()
.reactivateUser(targetUserId, req.permission)
.then((result: boolean) => {
if (result) {
apiResponse.statusCode = 200;
apiResponse.responseText = 'Usuário re-ativado com sucesso!';
} else {
throw new Error('Erro ao re-ativar usuário!');
}
})
.catch((err) => {
apiResponse.statusCode = 500;
apiResponse.responseText = 'Email e/ou senha incorreto(s)!';
apiResponse.errorMessage = err.message;
})
.finally(() => {
res.status(apiResponse.statusCode).send(apiResponse.jsonResponse);
});
});
};
export default user;