57 lines
1.8 KiB
JavaScript
57 lines
1.8 KiB
JavaScript
const validar = require('../../helper/validar');
|
|
const dbPath = '../../db/tablas';
|
|
const Carrito = require(`${dbPath}/Carrito`);
|
|
const Equipo = require(`${dbPath}/Equipo`);
|
|
const Programa = require(`${dbPath}/Programa`);
|
|
const Status = require(`${dbPath}/Status`);
|
|
|
|
const update = async (body) => {
|
|
const idEquipo = validar.validarNumeroEntero(
|
|
body.idEquipo,
|
|
'id equipo',
|
|
true
|
|
);
|
|
const idStatus = body.idStatus
|
|
? validar.validarNumeroEntero(body.idStatus, 'id status', true)
|
|
: null;
|
|
const idCarrito = body.idCarrito
|
|
? validar.validarNumeroEntero(body.idCarrito, 'id carrito', true)
|
|
: null;
|
|
const idPrograma = body.idPrograma
|
|
? validar.validarNumeroEntero(body.idPrograma, 'id programa', true)
|
|
: null;
|
|
let update = {};
|
|
|
|
return Status.findOne({ where: { idStatus } })
|
|
.then((res) => {
|
|
if (idStatus) {
|
|
if (!res) throw new Error('No existe este status.');
|
|
update.idStatus = idStatus;
|
|
}
|
|
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))
|
|
throw new Error('No se envio nada para actualizar.');
|
|
return Equipo.findOne({ where: { idEquipo } });
|
|
})
|
|
.then((res) => {
|
|
if (!res) throw new Error('No existe este equipo.');
|
|
return Equipo.update(update, { where: { idEquipo } });
|
|
})
|
|
.then(() => ({ message: 'Se actualizó correctamente el equipo.' }));
|
|
};
|
|
|
|
module.exports = update;
|