Newer
Older
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import bcrypt from 'bcrypt';
import { profiles, status } from '../enums/index.enum';
import Paginator from '../interfaces/paginator.interface';
import { User } from '../models/users.model';
import { UserCreateDTO } from '../DTOs/UserDTO';
const list = async (limit: number, offset: number): Promise<Paginator<User>> => {
let options = {};
if (limit && offset) {
options = {
limit,
offset,
};
}
return User.findAndCountAll({
attributes: [
'id', 'name', 'email', 'type', 'active',
],
order: [
['name', 'ASC'],
],
...options,
});
};
const create = async (userDTO: UserCreateDTO): Promise<User> => User.findOne({
where: {
email: userDTO.email,
},
}).then(async (user: User) => {
if (user) {
throw new Error('email in use');
} else {
const { password, repeat } = userDTO;
if (password === repeat) {
return User.create({
name: userDTO.name,
email: userDTO.email,
password: bcrypt.hashSync(userDTO.password || '1234', 10),
type: userDTO.type || profiles.unassigned,
status: userDTO.status || status.approved,
createdBy: 1,
createdAt: new Date(),
}).catch((error: Error) => {
console.log(error);
throw new Error('create user error');
});
}
throw new Error('passwords doesn\'t match');
}
}).catch((error: Error) => {
console.log(error);
throw new Error('find user error');
});
const update = async (userId: number, userDTO: UserCreateDTO): Promise<User> => User.findOne({
attributes: [
'id', 'name', 'email',
],
where: {
id: userId,
},
}).then(async (user: User) => {
if (!user) {
throw new Error('user not found');
} else {
const emailUser: User = await User.findOne({
where: {
email: userDTO.email,
},
});
if (!emailUser || emailUser.get('id') === user.get('id')) {
return user.update({
name: userDTO.name,
email: userDTO.email,
type: userDTO.type || profiles.unassigned,
updatedAt: new Date(),
}).catch((error: Error) => {
console.log(error);
throw new Error('user update error');
});
}
throw new Error('email in use');
}
}).catch((error: Error) => {
console.log(error);
throw new Error('find user error');
});
const password = async (userId: number, userDTO: UserCreateDTO): Promise<User> => User.findOne({
attributes: [
'id', 'name', 'email',
],
where: {
id: userId,
},
}).then(async (user: User) => {
if (!user) {
throw new Error('user not found');
} else {
const userPassword = userDTO.password;
const { repeat } = userDTO;
if (userPassword === repeat) {
return user.update({
password: bcrypt.hashSync(userDTO.password, 10),
updatedAt: new Date(),
}).catch((error: Error) => {
console.log(error);
throw new Error('user update error');
});
}
throw new Error('passwords doesn\'t match');
}
}).catch((error: Error) => {
console.log(error);
throw new Error('find user error');
});
const approve = async (userId: number, userDTO: UserCreateDTO): Promise<User> => User.findOne({
attributes: [
'id', 'name',
'email', 'type',
'createdAt',
],
where: {
id: userId,
},
}).then(async (user: User) => {
if (!user) {
throw new Error('user not found');
} else {
return user.update({
status: userDTO.status,
updatedAt: new Date(),
}).catch((error: Error) => {
console.log(error);
throw new Error('user update error');
});
}
}).catch((error: Error) => {
console.log(error);
throw new Error('find user error');
});
const active = async (userId: number): Promise<User> => User.findOne({
where: {
id: userId,
},
}).then(async (user: User) => {
if (!user) {
throw new Error('user not found');
} else {
return user.update({
active: !user.get('active'),
updatedAt: new Date(),
}).catch((error: Error) => {
throw new Error('user update error');
});
}
}).catch((error: Error) => {
console.log(error);
throw new Error('find user error');
});
export default {
list,
create,
update,
password,
approve,
active,
};