Files

65 lines
2.1 KiB
JavaScript
Raw Permalink Normal View History

2021-05-04 01:08:19 -05:00
const validar = require('../../helper/validar');
const dbPath = '../../db/tablas';
2021-05-27 10:58:49 -05:00
const Carrito = require(`${dbPath}/Carrito`);
2021-09-20 00:21:36 -05:00
const Equipo = require(`${dbPath}/Equipo`);
const Motivo = require(`${dbPath}/Motivo`);
2021-05-27 10:58:49 -05:00
const Programa = require(`${dbPath}/Programa`);
2021-05-04 01:08:19 -05:00
const Status = require(`${dbPath}/Status`);
2021-05-26 19:14:11 -05:00
const update = async (body) => {
2022-01-03 16:47:25 -06:00
const idEquipo = validar.validarNumeroEntero(
body.idEquipo,
'id equipo',
true
);
const idStatus = body.idStatus
? validar.validarNumeroEntero(body.idStatus, 'id status', true)
2022-02-13 19:10:21 -06:00
: '';
2022-01-03 16:47:25 -06:00
const idCarrito = body.idCarrito
? validar.validarNumeroEntero(body.idCarrito, 'id carrito', true)
2022-02-13 19:10:21 -06:00
: '';
2021-05-27 10:58:49 -05:00
const idPrograma = body.idPrograma
2022-01-03 16:47:25 -06:00
? validar.validarNumeroEntero(body.idPrograma, 'id programa', true)
2022-02-13 19:10:21 -06:00
: '';
const motivo = body.motivo
? validar.validarAlfanumerico(body.motivo, 'motivo', true, 500)
: '';
2021-05-27 10:58:49 -05:00
let update = {};
2021-05-26 19:14:11 -05:00
2022-01-24 15:58:34 -06:00
return Status.findOne({ where: { idStatus } })
.then((res) => {
if (idStatus) {
if (!res) throw new Error('No existe este status.');
if (!motivo)
throw new Error('No se mando el motivo de cambio de status.');
2022-01-24 15:58:34 -06:00
update.idStatus = idStatus;
}
2022-03-21 20:48:29 -06:00
2022-01-24 15:58:34 -06:00
return Carrito.findOne({ where: { idCarrito } });
})
.then((res) => {
if (idCarrito) {
if (!res) throw new Error('No existe este carrito.');
update.idCarrito = idCarrito;
}
return Programa.findOne({ where: { idPrograma } });
})
.then((res) => {
if (idPrograma) {
if (!res) throw new Error('No existe este programa.');
update.idPrograma = idPrograma;
}
if (validar.validarObjetoVacio(update))
2022-02-13 13:34:54 -06:00
throw new Error('No se envió nada para actualizar.');
2022-01-24 15:58:34 -06:00
return Equipo.findOne({ where: { idEquipo } });
})
2022-03-21 20:48:29 -06:00
.then(async (res) => {
2021-05-26 19:14:11 -05:00
if (!res) throw new Error('No existe este equipo.');
if (idStatus) await Motivo.create({ motivo, idEquipo, idStatus });
2021-05-27 10:58:49 -05:00
return Equipo.update(update, { where: { idEquipo } });
2021-05-26 19:14:11 -05:00
})
2022-02-01 06:44:34 -06:00
.then(() => ({ message: 'Se actualizó correctamente el equipo.' }));
2021-05-26 19:14:11 -05:00
};
2021-05-04 01:08:19 -05:00
module.exports = update;