35 lines
1.2 KiB
JavaScript
35 lines
1.2 KiB
JavaScript
const helperPath = '../../helper';
|
|
const { encriptar } = require(`${helperPath}/encriptar`);
|
|
const validar = require(`${helperPath}/validar`);
|
|
const Operador = require(`../../db/tablas/Operador`);
|
|
|
|
const update = async (body) => {
|
|
const idOperador = validar.validarNumeroEntero(
|
|
body.idOperador,
|
|
'id operador',
|
|
true
|
|
);
|
|
const password = body.password
|
|
? validar.validarAlfanumerico(body.password, 'contraseña', false, 20)
|
|
: '';
|
|
let update = {};
|
|
|
|
return Operador.findOne({ where: { idOperador } })
|
|
.then((res) => {
|
|
if (!res) throw new Error('No existe este operador.');
|
|
if (res.idTipoUsuario != 2)
|
|
throw new Error(
|
|
'Este usuario no es de tipo operador, no se pueda actualizar su información.'
|
|
);
|
|
if (body.activo === 'desactivar') update.activo = false;
|
|
else if (body.activo === 'activar') update.activo = true;
|
|
if (password) update.password = encriptar(password);
|
|
if (validar.validarObjetoVacio(update))
|
|
throw new Error('No se mando nada para actualizar.');
|
|
return Operador.update(update, { where: { idOperador } });
|
|
})
|
|
.then(() => ({ message: 'Se actualizó correctamente al operador.' }));
|
|
};
|
|
|
|
module.exports = update;
|