querries traen solo lo que necesitan
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
const validar = require('../../helper/validar');
|
||||
const { crearToken } = require('../../middleware/autentificacion');
|
||||
// const validarPath = '../../db/tablas';
|
||||
const validar = require('../../helper/validar');
|
||||
const { comparar } = require('../../helper/encriptar');
|
||||
const dbPath = '../../db/tablas';
|
||||
const Usuario = require(`${dbPath}/Usuario`);
|
||||
@@ -21,20 +22,26 @@ const login = async (body) => {
|
||||
|
||||
return Usuario.findOne({
|
||||
where: { usuario },
|
||||
include: [{ model: TipoUsuario }],
|
||||
}).then((res) => {
|
||||
if (!res) throw new Error('No existe este usuario.');
|
||||
if (!res.activo) throw new Error('Este usuario no esta activo.');
|
||||
if (comparar(password, res.password)) {
|
||||
delete res.dataValues.password;
|
||||
delete res.dataValues.activo;
|
||||
return {
|
||||
Usuario: res,
|
||||
token: crearToken(res.idUsuario),
|
||||
};
|
||||
}
|
||||
throw new Error('La contraseña es incorrecta');
|
||||
});
|
||||
attributes: ['idUsuario', 'activo', 'password'],
|
||||
})
|
||||
.then((res) => {
|
||||
if (!res) throw new Error('No existe este usuario.');
|
||||
if (!res.activo) throw new Error('Este usuario no esta activo.');
|
||||
if (!comparar(password, res.password))
|
||||
throw new Error('La contraseña es incorrecta');
|
||||
return Usuario.findOne({
|
||||
where: { idUsuario: res.idUsuario },
|
||||
include: [{ model: TipoUsuario }],
|
||||
attributes: ['idUsuario', 'usuario', 'nombre'],
|
||||
});
|
||||
})
|
||||
.then((res) => ({
|
||||
Usuario: res,
|
||||
token: crearToken({
|
||||
idUsuario: res.idUsuario,
|
||||
idTipoUsuario: res.TipoUsuario.idTipoUsuario,
|
||||
}),
|
||||
}));
|
||||
};
|
||||
|
||||
module.exports = login;
|
||||
|
||||
@@ -6,10 +6,7 @@ const responsable = async (body) => {
|
||||
|
||||
return Usuario.findOne({
|
||||
where: { idUsuario },
|
||||
}).then((res) => {
|
||||
delete res.dataValues.password;
|
||||
delete res.dataValues.idTipoUsuario;
|
||||
return res;
|
||||
attributes: ['idUsuario', 'usuario', 'nombre', 'activo'],
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -26,15 +26,10 @@ const responsables = async (body) => {
|
||||
{ idTipoUsuario: 2 },
|
||||
],
|
||||
},
|
||||
attributes: ['idUsuario', 'usuario', 'nombre', 'activo'],
|
||||
limit: 25,
|
||||
offset: 25 * (pagina - 1),
|
||||
}).then((res) => {
|
||||
for (let i = 0; i < res.rows.length; i++) {
|
||||
delete res.rows[i].dataValues.password;
|
||||
delete res.rows[i].dataValues.idTipoUsuario;
|
||||
}
|
||||
return { count: res.count, responsables: res.rows };
|
||||
});
|
||||
}).then((res) => ({ count: res.count, responsables: res.rows }));
|
||||
};
|
||||
|
||||
module.exports = responsables;
|
||||
|
||||
Reference in New Issue
Block a user