From 829aa5e35499c2caa2b3227a6975526ecec8db1e Mon Sep 17 00:00:00 2001 From: Lemuel Marquez Date: Mon, 31 May 2021 15:45:04 -0500 Subject: [PATCH] regreso con numero inventario --- .../Prestamo/regresarNumeroInventario.js | 46 +++++++++++++++++++ server/routes/Prestamo.js | 15 ++++++ 2 files changed, 61 insertions(+) create mode 100644 server/controller/Prestamo/regresarNumeroInventario.js diff --git a/server/controller/Prestamo/regresarNumeroInventario.js b/server/controller/Prestamo/regresarNumeroInventario.js new file mode 100644 index 0000000..68144ea --- /dev/null +++ b/server/controller/Prestamo/regresarNumeroInventario.js @@ -0,0 +1,46 @@ +const moment = require('moment'); +const { validarId } = require('../../helper/validar'); +const dbPath = '../../db/tablas'; +const Prestamo = require(`${dbPath}/Prestamo`); +const Equipo = require(`${dbPath}/Equipo`); +const Operador = require(`${dbPath}/Operador`); + +const regresarNumeroInventario = async (body) => { + const numeroInventario = validarAlfanumerico( + body.numeroInventario, + 'El numero de inventario', + 20 + ); + const idOperadorRegreso = validarId(body.idOperadorRegreso); + let idPrestamo; + const ahora = moment(); + + return Operador.findOne({ where: { idOperador: idOperadorRegreso } }) + .then((res) => { + if (!res) throw new Error('No existe este operador.'); + return Prestamo.findOne({ + where: { activo: true }, + include: [{ model: Equipo, where: { numeroInventario } }], + }); + }) + .then((res) => { + if (!res) + throw new Error('No existe un prestamo activo con este equipo.'); + idPrestamo = res.idPrestamo; + return Equipo.update( + { enUso: false, apartado: false }, + { where: { idEquipo: res.Equipo.idEquipo } } + ); + }) + .then((res) => { + Prestamo.update( + { idOperadorRegreso, horaEntrega: ahora.format(), activo: false }, + { where: { idPrestamo } } + ); + }) + .then((res) => ({ + message: 'Se ha regresado correctamente el equio de computo.', + })); +}; + +module.exports = regresarNumeroInventario; diff --git a/server/routes/Prestamo.js b/server/routes/Prestamo.js index ad555d5..c3d42ad 100644 --- a/server/routes/Prestamo.js +++ b/server/routes/Prestamo.js @@ -10,6 +10,7 @@ const historialUsuario = require(`${controllerPath}/historialUsuario`); const pedir = require(`${controllerPath}/pedir`); const recoger = require(`${controllerPath}/recoger`); const regresar = require(`${controllerPath}/regresar`); +const regresarNumeroInventario = require(`${controllerPath}/regresarNumeroInventario`); const prestamo = require(`${controllerPath}/prestamo`); const prestamoUsuario = require(`${controllerPath}/prestamoUsuario`); const cancelar = require(`${controllerPath}/cancelar`); @@ -57,6 +58,20 @@ app.put( } ); +app.put( + `${route}/regresar_numero_inventario`, + // verificaToken, + (req, res) => { + return regresarNumeroInventario(req.body) + .then((data) => { + res.status(200).json(data); + }) + .catch((err) => { + res.status(400).json({ message: err.message }); + }); + } +); + app.put( `${route}/cancelar`, // verificaToken,