Newer
Older
import bcrypt from 'bcrypt';
import { profiles, status } from '../enums/index.enum';
import Paginator from '../interfaces/paginator.interface';
import { User } from '../models/users.model';
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', '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 {
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
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({
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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,