From 7b3b4e7e1a424e7df190de47fc38d455c1a8931c Mon Sep 17 00:00:00 2001 From: Lemuel Marquez Date: Thu, 27 May 2021 10:58:49 -0500 Subject: [PATCH] update equipo listo --- server/controller/Equipo/update.js | 32 ++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/server/controller/Equipo/update.js b/server/controller/Equipo/update.js index 9ffbf5e..283c91c 100644 --- a/server/controller/Equipo/update.js +++ b/server/controller/Equipo/update.js @@ -1,20 +1,40 @@ const validar = require('../../helper/validar'); const dbPath = '../../db/tablas'; const Equipo = require(`${dbPath}/Equipo`); +const Carrito = require(`${dbPath}/Carrito`); +const Programa = require(`${dbPath}/Programa`); const Status = require(`${dbPath}/Status`); const update = async (body) => { const idEquipo = validar.validarId(body.idEquipo); - const idStatus = validar.validarId(body.idStatus); + 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 Status.findOne({ where: { idStatus } }); - }) - .then((res) => { - if (!res) throw new Error('No existe este status.'); - return Equipo.update({ idStatus }, { where: { idEquipo } }); + return Equipo.update(update, { where: { idEquipo } }); }) .then((res) => ({ message: 'Se actualizó correctamente el equipo.' })); };