Newer
Older
import { Op } from 'sequelize';
import { profiles, status } from '../enums/index.enum';
import Paginator from '../interfaces/paginator.interface';
import { User } from '../models/users.model';
const listPending = async (limit: number, offset: number,
search: string): Promise<Paginator<User>> => {
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
if (limit >= 1 && offset >= 0) {
if (search && search !== '') {
options = {
where: {
status: status.pending,
[Op.or]: [
{ name: { [Op.substring]: search } },
{ email: { [Op.substring]: search } },
],
},
limit,
offset,
};
} else {
options = {
where: {
status: status.pending,
},
limit,
offset,
};
}
}
return User.findAndCountAll({
attributes: [
'id', 'name', 'email', 'organization', 'type', 'status', 'active', 'createdAt',
],
order: [
['createdAt', 'ASC'],
],
...options,
});
};
const listApproved = async (limit: number, offset: number,
search: string): Promise<Paginator<User>> => {
let options = {};
if (limit >= 1 && offset >= 0) {
if (search && search !== '') {
options = {
where: {
status: status.approved,
[Op.or]: [
{ name: { [Op.substring]: search } },
{ email: { [Op.substring]: search } },
],
},
limit,
offset,
};
} else {
options = {
where: {
status: status.approved,
},
limit,
offset,
};
}
}
return User.findAndCountAll({
attributes: [
'id', 'name', 'email', 'organization', 'type', 'status', 'active', 'createdAt',
],
order: [
['createdAt', 'ASC'],
],
...options,
});
};
const listAll = async (limit: number, offset: number): Promise<Paginator<User>> => {
let options = {};
if (limit >= 1 && offset >= 0) {
options = {
limit,
offset,
};
}
return User.findAndCountAll({
attributes: [
'id', 'name', 'email', 'organization', 'type', 'status', 'active', 'createdAt',
],
...options,
});
};
const create = async (userDTO: UserCreateDTO): Promise<User> => User.findOne({
where: {
email: userDTO.email,
},
}).then(async (user: User) => {
if (user) {
// se hace el checkeo antes porque luego se encripta
if (userDTO.password.length >= 6) {
const newUser: User = await User.create({
organization: userDTO.organization,
password: bcrypt.hashSync(userDTO.password, 10),
type: profiles.client,
status: status.pending,
createdBy: 1,
createdAt: new Date(),
}).catch((error: Error) => {
console.log(error);
throw new Error('create 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,
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 {
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
return user.update({
password: bcrypt.hashSync(userDTO.password, 10),
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 approve = async (userId: number): 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: status.approved,
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 cancel = async (userId: number): 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({
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
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 {
listAll,
listPending,
listApproved,