Skip to content
Snippets Groups Projects
Commit 9d618624 authored by Ignacio Otero's avatar Ignacio Otero
Browse files

Cambios

parent c0311f99
No related branches found
No related tags found
No related merge requests found
MYSQL_USERNAME=root
MYSQL_PASSWORD=password
MYSQL_PASSWORD=mysql
MYSQL_DB=repp_users
\ No newline at end of file
......@@ -18,6 +18,10 @@ const up = (queryInterface, Sequelize) => {
allowNull: false,
unique: true
},
organization: {
type: Sequelize.STRING,
allowNull: false,
},
password: {
type: Sequelize.STRING,
allowNull: false
......
......@@ -48,7 +48,17 @@ const password = async (req: Request, res: Response): Promise<Response> => {
const approve = async (req: Request, res: Response): Promise<Response> => {
try {
const user: User = await UserService.approve(Number(req.params.id), req.body);
const user: User = await UserService.approve(Number(req.params.id));
return res.status(200).send(user);
} catch (error) {
const e = error as Error;
return res.status(400).json({ error: e.message });
}
};
const cancel = async (req: Request, res: Response): Promise<Response> => {
try {
const user: User = await UserService.cancel(Number(req.params.id));
return res.status(200).send(user);
} catch (error) {
const e = error as Error;
......@@ -83,4 +93,7 @@ router.route('/:id/approve')
router.route('/:id/active')
.patch(active);
router.route('/:id/cancel')
.put(cancel);
export default router;
export interface UserCreateDTO {
name: string;
email: string;
organization: string;
password: string;
repeat: string;
type: number;
status: number;
}
export interface UserLoginDTO {
email: string;
password: string;
}
export interface UserDTO {
id: number;
name: string;
email: string;
organization: string;
type: number;
status: number;
active: boolean;
createdAt: Date;
}
......@@ -3,7 +3,7 @@ import { profiles, status } from '../enums/index.enum';
import Paginator from '../interfaces/paginator.interface';
import { User } from '../models/users.model';
import { UserCreateDTO } from '../DTOs/UserDTO';
import { UserCreateDTO, UserDTO } from '../DTOs/UserDTO';
const list = async (limit: number, offset: number): Promise<Paginator<User>> => {
let options = {};
......@@ -15,10 +15,10 @@ const list = async (limit: number, offset: number): Promise<Paginator<User>> =>
}
return User.findAndCountAll({
attributes: [
'id', 'name', 'email', 'type', 'active',
'id', 'name', 'email', 'organization', 'type', 'status', 'active', 'createdAt',
],
order: [
['name', 'ASC'],
['createdAt', 'ASC'],
],
...options,
});
......@@ -30,28 +30,31 @@ const create = async (userDTO: UserCreateDTO): Promise<User> => User.findOne({
},
}).then(async (user: User) => {
if (user) {
throw new Error('email in use');
throw new Error('email is taken');
} else {
const { password, repeat } = userDTO;
if (password === repeat) {
return User.create({
// se hace el checkeo antes porque luego se encripta
if (userDTO.password.length >= 6) {
const newUser: User = await 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,
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');
});
newUser.toJSON();
return newUser;
}
throw new Error('passwords doesn\'t match');
throw new Error('password too short');
}
}).catch((error: Error) => {
console.log(error);
throw new Error('find user error');
throw error;
});
const update = async (userId: number, userDTO: UserCreateDTO): Promise<User> => User.findOne({
......@@ -74,7 +77,7 @@ const update = async (userId: number, userDTO: UserCreateDTO): Promise<User> =>
return user.update({
name: userDTO.name,
email: userDTO.email,
type: userDTO.type || profiles.unassigned,
organization: userDTO.organization,
updatedAt: new Date(),
}).catch((error: Error) => {
console.log(error);
......@@ -99,25 +102,46 @@ const password = async (userId: number, userDTO: UserCreateDTO): Promise<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');
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 approve = async (userId: number, userDTO: UserCreateDTO): Promise<User> => User.findOne({
const cancel = async (userId: number): Promise<User> => User.findOne({
attributes: [
'id', 'name',
'email', 'type',
......@@ -131,7 +155,7 @@ const approve = async (userId: number, userDTO: UserCreateDTO): Promise<User> =>
throw new Error('user not found');
} else {
return user.update({
status: userDTO.status,
status: status.pending,
updatedAt: new Date(),
}).catch((error: Error) => {
console.log(error);
......@@ -169,5 +193,6 @@ export default {
update,
password,
approve,
cancel,
active,
};
......@@ -8,11 +8,27 @@ export const up = (queryInterface: any, Sequelize: any) => queryInterface.create
name: {
type: Sequelize.STRING,
allowNull: true,
validate: {
notEmpty: true,
len: [3, 40],
},
},
email: {
type: Sequelize.STRING,
allowNull: false,
unique: true,
validate: {
isEmail: true,
max: 60,
},
},
organization: {
type: Sequelize.STRING,
allowNull: false,
validate: {
notEmpty: true,
max: 50,
},
},
password: {
type: Sequelize.STRING,
......
import { Sequelize, DataTypes, Model } from 'sequelize';
import { status } from '../enums/index.enum';
import { UserDTO } from '../DTOs/UserDTO';
import * as environments from '../config/config';
const current: string = process.env.NODE_ENV || 'development';
......@@ -8,7 +9,18 @@ const sequelize: Sequelize = new Sequelize(
config.database, config.username, config.password, config,
);
export class User extends Model {}
export class User extends Model {
toJSON(): UserDTO {
const values = { ...this.get() };
delete values.password;
delete values.updatedAt;
delete values.createdBy;
delete values.updatedBy;
delete values.deletedAt;
return values;
}
}
User.init({
id: {
......@@ -20,11 +32,27 @@ User.init({
name: {
type: DataTypes.STRING(255),
allowNull: true,
validate: {
notEmpty: true,
len: [3, 40],
},
},
email: {
type: DataTypes.STRING(255),
allowNull: false,
unique: true,
validate: {
isEmail: true,
max: 60,
},
},
organization: {
type: DataTypes.STRING(255),
allowNull: false,
validate: {
notEmpty: true,
max: 50,
},
},
password: {
type: DataTypes.STRING(255),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment