listo
This commit is contained in:
@@ -1,13 +1,8 @@
|
||||
const { Op } = require('sequelize');
|
||||
const validar = require('../../helper/validar');
|
||||
const Usuario = require('../../db/tablas/Usuario');
|
||||
|
||||
const responsableUpdate = async (body) => {
|
||||
let isObjectEmpty = (obj) => {
|
||||
for (let key in obj) {
|
||||
if (obj.hasOwnProperty(key)) return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
let idUsuario = validar.validarId(body.idUsuario);
|
||||
let dataUpdate = {};
|
||||
|
||||
@@ -18,24 +13,20 @@ const responsableUpdate = async (body) => {
|
||||
if (res.idTipoUsuario !== 2)
|
||||
throw new Error('No es un usuaior de tipo responasble.');
|
||||
if (body.correo) {
|
||||
let yaUsado = await Usuario.findOne({
|
||||
where: { usuario: body.correo },
|
||||
}).then((res) => res);
|
||||
const yaUsado = await Usuario.findOne({
|
||||
where: { usuario: body.correo, idUsuario: { [Op.not]: idUsuario } },
|
||||
});
|
||||
|
||||
if (body.correo === res.usuario)
|
||||
throw new Error('El responable ya tiene ese correo asignado.');
|
||||
if (yaUsado)
|
||||
throw new Error(
|
||||
'No se puede asignar este correo a esta cuenta porque esta siendo ocupado por otro resposanble.'
|
||||
);
|
||||
dataUpdate.usuario = validar.validarCorreo(body.correo);
|
||||
}
|
||||
if (body.nombre) {
|
||||
if (body.nombre === res.nombre)
|
||||
throw new Error('El responable ya tiene ese nombre asignado.');
|
||||
if (body.nombre)
|
||||
dataUpdate.nombre = validar.validarTexto(body.nombre, 'el nombre', 70);
|
||||
}
|
||||
if (isObjectEmpty(dataUpdate))
|
||||
|
||||
if (validar.isObjectEmpty(dataUpdate))
|
||||
throw new Error('No se a envio nada para actualizar.');
|
||||
return Usuario.update(dataUpdate, { where: { idUsuario } });
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user