Files
pcpuma_acatlan_api/server/controller/Operador/update.js
T

35 lines
1.2 KiB
JavaScript
Raw Normal View History

2021-06-21 01:05:55 -05:00
const helperPath = '../../helper';
const { encriptar } = require(`${helperPath}/encriptar`);
const validar = require(`${helperPath}/validar`);
2021-09-20 00:21:36 -05:00
const Operador = require(`../../db/tablas/Operador`);
2021-05-04 01:08:19 -05:00
2021-05-26 15:47:24 -05:00
const update = async (body) => {
2022-01-03 16:47:25 -06:00
const idOperador = validar.validarNumeroEntero(
body.idOperador,
'id operador',
true
);
2022-01-24 15:58:34 -06:00
const password = body.password
? validar.validarAlfanumerico(body.password, 'contraseña', false, 20)
: '';
2021-05-26 15:47:24 -05:00
let update = {};
2022-02-01 09:48:07 -06:00
return Operador.findOne({ where: { idOperador } })
2021-05-26 15:47:24 -05:00
.then((res) => {
2021-09-20 00:21:36 -05:00
if (!res) throw new Error('No existe este operador.');
2022-02-01 09:48:07 -06:00
if (res.idTipoUsuario != 2)
throw new Error(
'Este usuario no es de tipo operador, no se pueda actualizar su información.'
);
2022-01-24 15:58:34 -06:00
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.');
2021-05-26 15:47:24 -05:00
return Operador.update(update, { where: { idOperador } });
})
2022-02-01 09:48:07 -06:00
.then(() => ({ message: 'Se actualizó correctamente al operador.' }));
2021-05-26 15:47:24 -05:00
};
2021-05-04 01:08:19 -05:00
module.exports = update;