Newer
Older
import { Op, where } from 'sequelize';
import { profiles, status } from '../enums/index.enum';
import Paginator from '../interfaces/paginator.interface';
import { User } from '../models/users.model';
import { UserCreateDTO, UserLoginDTO } from '../DTOs/UserDTO';
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
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,
});
};
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
const listClients = 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,
type: profiles.client,
[Op.or]: [
{ name: { [Op.substring]: search } },
{ email: { [Op.substring]: search } },
],
},
limit,
offset,
};
} else {
options = {
where: {
status: status.approved,
type: profiles.client,
},
limit,
offset,
};
}
}
return User.findAndCountAll({
attributes: [
'id', 'name', 'email', 'organization', 'type', 'status', 'active', 'createdAt',
],
order: [
['createdAt', 'ASC'],
],
...options,
});
};
const listAdmins = 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,
type: profiles.administrator,
[Op.or]: [
{ name: { [Op.substring]: search } },
{ email: { [Op.substring]: search } },
],
},
limit,
offset,
};
} else {
options = {
where: {
status: status.approved,
type: profiles.administrator,
},
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 {
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
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({
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
type: profiles.client,
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 giveAdminPermission = 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({
type: profiles.administrator,
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 removeAdminPermission = 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({
type: profiles.client,
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
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');
});
const login = async (userDTO: UserLoginDTO): Promise<User> => User.findOne({
attributes: [
'id', 'name', 'email', 'organization', 'password',
'type', 'status', 'active', 'createdAt',
],
where: {
email: userDTO.email,
status: status.approved,
active: true,
},
}).then((user: User) => {
if (!user) {
throw new Error('user not found');
} else if (user && bcrypt.compareSync(userDTO.password, String(user.get('password')))) {
return user;
} else {
console.log('auth failed, credentials:', userDTO);
throw new Error('auth failed');
}
}).catch((error: Error) => {
console.log(error);
console.log('credentials:', userDTO);
throw new Error('find user error');
});
const listUsersById = async (ids: number[]): Promise<User[]> => {
const users = User.findAll({
attributes: [
'id', 'name', 'email', 'organization', 'type',
],
where: { id: { [Op.in]: ids } },
});
return users;
};
listAll,
listPending,
listApproved,
listClients,
listAdmins,
giveAdminPermission,
removeAdminPermission,