From 48d882bbd2fb782eb8f20ea1d9af1f3a725a4c01 Mon Sep 17 00:00:00 2001 From: xXpuma99Xx <51341582+xXpuma99Xx@users.noreply.github.com> Date: Tue, 22 Feb 2022 15:16:46 -0600 Subject: [PATCH] listo equipos consecutivos --- server/controller/Prestamo/entregar.js | 5 ++- server/controller/Prestamo/pedir.js | 44 ++++++++++++++++++++++++-- server/db/tablas/Equipo.js | 5 +++ 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/server/controller/Prestamo/entregar.js b/server/controller/Prestamo/entregar.js index b5b4ae3..983ed4d 100644 --- a/server/controller/Prestamo/entregar.js +++ b/server/controller/Prestamo/entregar.js @@ -53,7 +53,10 @@ const entregar = async (body) => { if (ahora > moment(`${ahora.format('YYYY-MM-DD')} 17:45`)) update.horaFin = moment(`${ahora.format('YYYY-MM-DD')} 19:45`); else update.horaFin = ahora.add(2, 'h').format(); - return Equipo.update({ idStatus: 2 }, { where: { idEquipo } }); + return Equipo.update( + { idStatus: 2, prestado: true }, + { where: { idEquipo } } + ); }) .then(() => Prestamo.update(update, { where: { idPrestamo } })) .then(() => diff --git a/server/controller/Prestamo/pedir.js b/server/controller/Prestamo/pedir.js index af1799a..b486710 100644 --- a/server/controller/Prestamo/pedir.js +++ b/server/controller/Prestamo/pedir.js @@ -84,7 +84,7 @@ const pedir = async (body) => { 'Tienes un préstamo activo. No puedes solicitar otro equipo de cómputo.' ); } - return Equipo.findOne({ + return Equipo.count({ where: whereEquipo, include: [ { @@ -92,7 +92,45 @@ const pedir = async (body) => { where: { idTipoCarrito, idModulo, activo: true }, }, ], - order: [['updatedAt']], + }); + }) + .then((res) => { + if (res === 0) + throw new Error( + 'No existe un equipo de cómputo con las características solicitadas. Intenta elegir otro tipo de equipo.' + ); + return Equipo.count({ + where: { ...whereEquipo, prestado: false }, + include: [ + { + model: Carrito, + where: { idTipoCarrito, idModulo, activo: true }, + }, + ], + }); + }) + .then(async (res) => { + if (res === 0) + await Equipo.update( + { prestado: false }, + { + where: { ...whereEquipo, prestado: true }, + include: [ + { + model: Carrito, + where: { idTipoCarrito, idModulo, activo: true }, + }, + ], + } + ); + return Equipo.findOne({ + where: { ...whereEquipo, prestado: false }, + include: [ + { + model: Carrito, + where: { idTipoCarrito, idModulo, activo: true }, + }, + ], }); }) .then((res) => { @@ -110,7 +148,7 @@ const pedir = async (body) => { idEquipo, }) ) - .then((res) => { + .then(() => { io.getIO().emit('actualizar', { idUsuario }); return { message: diff --git a/server/db/tablas/Equipo.js b/server/db/tablas/Equipo.js index 12b22c4..6bee9f5 100644 --- a/server/db/tablas/Equipo.js +++ b/server/db/tablas/Equipo.js @@ -26,6 +26,11 @@ Equipo.init( type: DataTypes.STRING(15), allowNull: false, }, + prestado: { + type: DataTypes.BOOLEAN, + allowNull: false, + defaultValue: false, + }, }, { sequelize,