43 lines
1.5 KiB
JavaScript
43 lines
1.5 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.validarId(body.idEquipo);
|
|
const idStatus = body.idStatus ? validar.validarId(body.idStatus) : null;
|
|
const idCarrito = body.idCarrito ? validar.validarId(body.idCarrito) : null;
|
|
const idPrograma = body.idPrograma
|
|
? validar.validarId(body.idPrograma)
|
|
: null;
|
|
let update = {};
|
|
|
|
if (idStatus)
|
|
await Status.findOne({ where: { idStatus } }).then((res) => {
|
|
if (!res) throw new Error('No existe este status.');
|
|
update.idStatus = idStatus;
|
|
});
|
|
if (idCarrito)
|
|
await Carrito.findOne({ where: { idCarrito } }).then((res) => {
|
|
if (!res) throw new Error('No existe este carrito.');
|
|
update.idCarrito = idCarrito;
|
|
});
|
|
if (idPrograma)
|
|
await Programa.findOne({ where: { idPrograma } }).then((res) => {
|
|
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((res) => ({ message: 'Se actualizó correctamente el equipo.' }));
|
|
};
|
|
|
|
module.exports = update;
|