From 1bd65011c428444baf7bfbe0d0d461b1a289e49e Mon Sep 17 00:00:00 2001 From: lemuel Date: Tue, 1 Feb 2022 09:48:07 -0600 Subject: [PATCH] =?UTF-8?q?mejora=20c=C3=B3digo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/controller/Operador/crear.js | 2 +- server/controller/Operador/login.js | 5 +---- server/controller/Operador/operadores.js | 9 +------- server/controller/Operador/update.js | 8 +++++-- server/controller/Prestamo/activos.js | 21 +++++++------------ .../controller/Prestamo/cancelarOperador.js | 15 ++++++------- server/controller/Prestamo/cancelarUsuario.js | 14 ++++++------- server/controller/Prestamo/entregar.js | 10 ++++----- server/controller/Prestamo/historial.js | 11 ++-------- server/controller/Prestamo/historialEquipo.js | 9 +------- .../controller/Prestamo/historialUsuario.js | 9 +------- server/controller/Prestamo/pedir.js | 4 ++-- server/controller/Prestamo/prestamo.js | 4 ++-- server/controller/Prestamo/prestamoUsuario.js | 1 - server/controller/Prestamo/regresar.js | 14 ++++++------- .../Prestamo/regresarNumeroInventario.js | 10 ++++----- .../controller/Prestamo/regresoInmediato.js | 15 ++++++------- .../regresoInmediatoNumeroInventario.js | 15 ++++++------- server/controller/Prestamo/status.js | 2 +- 19 files changed, 65 insertions(+), 113 deletions(-) diff --git a/server/controller/Operador/crear.js b/server/controller/Operador/crear.js index c1fde6f..1fd6f87 100644 --- a/server/controller/Operador/crear.js +++ b/server/controller/Operador/crear.js @@ -21,7 +21,7 @@ const crear = async (body) => { idTipoUsuario: 2, }); }) - .then((res) => ({ message: 'Se creo un nuevo operador correctamente.' })); + .then(() => ({ message: 'Se creo un nuevo operador correctamente.' })); }; module.exports = crear; diff --git a/server/controller/Operador/login.js b/server/controller/Operador/login.js index 6d31562..d9020fb 100644 --- a/server/controller/Operador/login.js +++ b/server/controller/Operador/login.js @@ -15,10 +15,7 @@ const login = async (body) => { 20 ); - return Operador.findOne({ - where: { operador }, - attributes: ['idOperador', 'password', 'activo'], - }) + return Operador.findOne({ where: { operador } }) .then((res) => { if (!res) throw new Error('No existe este operador.'); if (!res.activo) throw new Error('Este operador no esta activo.'); diff --git a/server/controller/Operador/operadores.js b/server/controller/Operador/operadores.js index 2dec3c8..5e41bf5 100644 --- a/server/controller/Operador/operadores.js +++ b/server/controller/Operador/operadores.js @@ -5,14 +5,7 @@ const Operador = require(`${dbPath}/Operador`); const TipoUsuario = require(`${dbPath}/TipoUsuario`); const operadores = async (body) => { - const pagina = validar.validarNumero( - body.pagina, - 'página', - false, - null, - true, - true - ); + const pagina = validar.validarNumeroEntero(body.pagina, 'página', false); const operador = body.operador ? validar.validarTexto(body.operador, 'operador', true, 20) : ''; diff --git a/server/controller/Operador/update.js b/server/controller/Operador/update.js index aeff626..e9bdf3a 100644 --- a/server/controller/Operador/update.js +++ b/server/controller/Operador/update.js @@ -14,9 +14,13 @@ const update = async (body) => { : ''; let update = {}; - return Operador.findOne({ where: { idOperador, idTipoUsuario: 2 } }) + return Operador.findOne({ where: { idOperador } }) .then((res) => { if (!res) throw new Error('No existe este operador.'); + if (res.idTipoUsuario != 2) + throw new Error( + 'Este usuario no es de tipo operador, no se pueda actualizar su información.' + ); if (body.activo === 'desactivar') update.activo = false; else if (body.activo === 'activar') update.activo = true; if (password) update.password = encriptar(password); @@ -24,7 +28,7 @@ const update = async (body) => { throw new Error('No se mando nada para actualizar.'); return Operador.update(update, { where: { idOperador } }); }) - .then((res) => ({ message: 'Se actualizó correctamente al operador.' })); + .then(() => ({ message: 'Se actualizó correctamente al operador.' })); }; module.exports = update; diff --git a/server/controller/Prestamo/activos.js b/server/controller/Prestamo/activos.js index d5441d0..91fc6ce 100644 --- a/server/controller/Prestamo/activos.js +++ b/server/controller/Prestamo/activos.js @@ -12,23 +12,19 @@ const TipoUsuario = require(`${dbPath}/TipoUsuario`); const Usuario = require(`${dbPath}/Usuario`); const activos = async (body) => { + const pagina = validar.validarNumeroEntero(body.pagina, 'página', false); const idModulo = validar.validarNumeroEntero( body.idModulo, 'id modulo', true ); - const pagina = validar.validarNumero( - body.pagina, - 'página', - false, - null, - true, - true - ); - const usuario = body.usuario ? validar.validarNumeroCuenta(body.usuario) : ''; const idPrestamo = body.idPrestamo ? validar.validarNumeroEntero(body.idPrestamo, 'id prestamo', true) - : ''; + : null; + const idTipoCarrito = body.idTipoCarrito + ? validar.validarNumeroEntero(body.idTipoCarrito, 'id tipo carrito', true) + : null; + const usuario = body.usuario ? validar.validarNumeroCuenta(body.usuario) : ''; const numeroInventario = body.numeroInventario ? validar.validarAlfanumerico( body.numeroInventario, @@ -37,9 +33,6 @@ const activos = async (body) => { 20 ) : ''; - const idTipoCarrito = body.idTipoCarrito - ? validar.validarNumeroEntero(body.idTipoCarrito, 'id tipo carrito', true) - : null; let whereCarrito = [{ idModulo }]; if (idTipoCarrito) whereCarrito.push({ idTipoCarrito }); @@ -48,10 +41,10 @@ const activos = async (body) => { return Prestamo.findAndCountAll({ where: { [Op.or]: [ + { activo: true }, { [Op.and]: [{ regresoInmediato: true }, { idOperadorRegreso: null }], }, - { activo: true }, ], idPrestamo: { [Op.like]: `%${idPrestamo}%` }, }, diff --git a/server/controller/Prestamo/cancelarOperador.js b/server/controller/Prestamo/cancelarOperador.js index 77d155e..756ce09 100644 --- a/server/controller/Prestamo/cancelarOperador.js +++ b/server/controller/Prestamo/cancelarOperador.js @@ -6,7 +6,7 @@ const Operador = require(`${dbPath}/Operador`); const Prestamo = require(`${dbPath}/Prestamo`); const cancelarOperador = async (body) => { - const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id prestamo', true); + const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id préstamo', true); const idOperadorCancelacion = validarNumeroEntero( body.idOperadorCancelacion, 'id operador cancelación', @@ -16,29 +16,26 @@ const cancelarOperador = async (body) => { return Operador.findOne({ where: { idOperador: idOperadorCancelacion } }) .then((res) => { if (!res) throw new Error('No existe este operador.'); - return Prestamo.findOne({ - where: { idPrestamo }, - include: [{ model: Equipo }], - }); + return Prestamo.findOne({ where: { idPrestamo } }); }) .then((res) => { if (!res) throw new Error('Este prestamo no existe.'); if (!res.activo) throw new Error( - 'Este prestamo ya no esta activo, ya no se puede cancelar.' + 'Este préstamo ya no esta activo, no se puede cancelar.' ); return Equipo.update( { idStatus: 4 }, - { where: { idEquipo: res.Equipo.idEquipo } } + { where: { idEquipo: res.idEquipo } } ); }) - .then((res) => + .then(() => Prestamo.update( { activo: false, idOperadorCancelacion }, { where: { idPrestamo } } ) ) - .then((res) => { + .then(() => { io.getIO().emit('actualizar', { idPrestamo }); return { message: 'Se canceló el préstamo correctamente.' }; }); diff --git a/server/controller/Prestamo/cancelarUsuario.js b/server/controller/Prestamo/cancelarUsuario.js index 2e1fb13..9336e92 100644 --- a/server/controller/Prestamo/cancelarUsuario.js +++ b/server/controller/Prestamo/cancelarUsuario.js @@ -5,34 +5,34 @@ const Prestamo = require(`${dbPath}/Prestamo`); const Equipo = require(`${dbPath}/Equipo`); const cancelarUsuario = async (body) => { - const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id prestamo', true); + const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id préstamo', true); return Prestamo.findOne({ where: { idPrestamo }, include: [{ model: Equipo }], }) .then((res) => { - if (!res) throw new Error('Este prestamo no existe.'); + if (!res) throw new Error('Este préstamo no existe.'); if (!res.activo) throw new Error( - 'Este prestamo ya no esta activo, ya no se puede cancelar.' + 'Este préstamo ya no esta activo, no se puede cancelar.' ); if (res.Equipo.idStatus === 2) throw new Error( - 'El equipo ya fue entregado al usuario, ya no se puede cancelar.' + 'El equipo ya fue entregado al usuario, no se puede cancelar.' ); return Equipo.update( { idStatus: 4 }, - { where: { idEquipo: res.Equipo.idEquipo } } + { where: { idEquipo: res.idEquipo } } ); }) - .then((res) => + .then(() => Prestamo.update( { activo: false, canceladoUsuario: true }, { where: { idPrestamo } } ) ) - .then((res) => { + .then(() => { io.getIO().emit('actualizar'); return { message: 'Se canceló el préstamo correctamente.' }; }); diff --git a/server/controller/Prestamo/entregar.js b/server/controller/Prestamo/entregar.js index cc47de0..abb4e22 100644 --- a/server/controller/Prestamo/entregar.js +++ b/server/controller/Prestamo/entregar.js @@ -35,17 +35,17 @@ const entregar = async (body) => { throw new Error('Ya se entrego el equipo al usuario.'); else if (moment(res.horaMaxRecoger) < ahora) await Equipo.update({ idStatus: 4 }, { where: { idEquipo } }) - .then((res) => + .then(() => Prestamo.update({ activo: false }, { where: { idPrestamo } }) ) - .then((res) => { + .then(() => { throw new Error( - 'Ya paso el tiempo limite para recoger el dispositivo.' + 'Ya paso el tiempo límite para recoger el equipo de cómputo.' ); }); return Equipo.update({ idStatus: 2 }, { where: { idEquipo } }); }) - .then((res) => { + .then(() => { Prestamo.update( { idOperadorEntrega, @@ -55,7 +55,7 @@ const entregar = async (body) => { { where: { idPrestamo } } ); }) - .then((res) => + .then(() => Equipo.findOne({ where: { idEquipo }, include: [ diff --git a/server/controller/Prestamo/historial.js b/server/controller/Prestamo/historial.js index 62d2d12..a795289 100644 --- a/server/controller/Prestamo/historial.js +++ b/server/controller/Prestamo/historial.js @@ -12,17 +12,10 @@ const TipoUsuario = require(`${dbPath}/TipoUsuario`); const Usuario = require(`${dbPath}/Usuario`); const historial = async (body) => { - const pagina = validar.validarNumero( - body.pagina, - 'página', - false, - null, - true, - true - ); + const pagina = validar.validarNumeroEntero(body.pagina, 'página', false); const usuario = body.usuario ? validar.validarNumeroCuenta(body.usuario) : ''; const idPrestamo = body.idPrestamo - ? validar.validarNumeroEntero(body.idPrestamo, 'id prestamo', true) + ? validar.validarNumeroEntero(body.idPrestamo, 'id préstamo', true) : ''; const numeroInventario = body.numeroInventario ? validar.validarAlfanumerico( diff --git a/server/controller/Prestamo/historialEquipo.js b/server/controller/Prestamo/historialEquipo.js index 97da81b..1de0ea2 100644 --- a/server/controller/Prestamo/historialEquipo.js +++ b/server/controller/Prestamo/historialEquipo.js @@ -8,19 +8,12 @@ const TipoUsuario = require(`${dbPath}/TipoUsuario`); const Usuario = require(`${dbPath}/Usuario`); const historialEquipo = async (body) => { + const pagina = validar.validarNumeroEntero(body.pagina, 'página', false); const idEquipo = validar.validarNumeroEntero( body.idEquipo, 'id equipo', true ); - const pagina = validar.validarNumero( - body.pagina, - 'página', - false, - null, - true, - true - ); return Prestamo.findAndCountAll({ include: [ diff --git a/server/controller/Prestamo/historialUsuario.js b/server/controller/Prestamo/historialUsuario.js index 4a14701..b5bf49c 100644 --- a/server/controller/Prestamo/historialUsuario.js +++ b/server/controller/Prestamo/historialUsuario.js @@ -10,19 +10,12 @@ const TipoCarrito = require(`${dbPath}/TipoCarrito`); const Usuario = require(`${dbPath}/Usuario`); const historialUsuario = async (body) => { + const pagina = validar.validarNumeroEntero(body.pagina, 'página', false); const idUsuario = validar.validarNumeroEntero( body.idUsuario, 'id usuario', true ); - const pagina = validar.validarNumero( - body.pagina, - 'página', - false, - null, - true, - true - ); return Prestamo.findAndCountAll({ include: [ diff --git a/server/controller/Prestamo/pedir.js b/server/controller/Prestamo/pedir.js index 28c8e43..938bab2 100644 --- a/server/controller/Prestamo/pedir.js +++ b/server/controller/Prestamo/pedir.js @@ -57,13 +57,13 @@ const pedir = async (body) => { where: { idUsuario, [Op.or]: [ + { activo: true }, { [Op.and]: [ { regresoInmediato: true }, { idOperadorRegreso: null }, ], }, - { activo: true }, ], }, }); @@ -100,7 +100,7 @@ const pedir = async (body) => { idEquipo = res.idEquipo; return Equipo.update({ idStatus: 1 }, { where: { idEquipo } }); }) - .then((res) => + .then(() => Prestamo.create({ horaMaxRecoger: ahora.add(10, 'm'), idUsuario, diff --git a/server/controller/Prestamo/prestamo.js b/server/controller/Prestamo/prestamo.js index ed1d1bd..6b93b1f 100644 --- a/server/controller/Prestamo/prestamo.js +++ b/server/controller/Prestamo/prestamo.js @@ -12,7 +12,7 @@ const TipoUsuario = require(`${dbPath}/TipoUsuario`); const Usuario = require(`${dbPath}/Usuario`); const prestamo = async (body) => { - const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id prestamo', true); + const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id préstamo', true); return Prestamo.findOne({ where: { idPrestamo }, @@ -60,7 +60,7 @@ const prestamo = async (body) => { 'createdAt', ], }).then((res) => { - if (!res) throw new Error('No existe este prestamo.'); + if (!res) throw new Error('No existe este préstamo.'); return res; }); }; diff --git a/server/controller/Prestamo/prestamoUsuario.js b/server/controller/Prestamo/prestamoUsuario.js index 30d26f0..44d2d83 100644 --- a/server/controller/Prestamo/prestamoUsuario.js +++ b/server/controller/Prestamo/prestamoUsuario.js @@ -14,7 +14,6 @@ const prestamo = async (body) => { return Usuario.findOne({ where: { idUsuario } }).then((res) => { if (!res) throw new Error('No existe este usuario.'); - return Prestamo.findOne({ where: { idUsuario, activo: true }, include: [ diff --git a/server/controller/Prestamo/regresar.js b/server/controller/Prestamo/regresar.js index f85492d..a0fe81d 100644 --- a/server/controller/Prestamo/regresar.js +++ b/server/controller/Prestamo/regresar.js @@ -1,6 +1,7 @@ const moment = require('moment'); const io = require('../../socket'); const { validarNumeroEntero } = require('../../helper/validar'); +const { parse } = require('dotenv'); const dbPath = '../../db/tablas'; const Equipo = require(`${dbPath}/Equipo`); const Multa = require(`${dbPath}/Multa`); @@ -17,7 +18,6 @@ const regresar = async (body) => { 'id operador regreso', true ); - let horaFin = moment(); let diferencia = null; return Operador.findOne({ where: { idOperador: idOperadorRegreso } }) @@ -34,9 +34,8 @@ const regresar = async (body) => { throw new Error('Este préstamo ya no esta activo.'); if (!res.Equipo.idStatus === 1) throw new Error('Aun no se ha entregado el equipo al usuario.'); - horaFin = moment(res.horaFin); - diferencia = (ahora - horaFin) / 60000; - if (diferencia >= 15) { + diferencia = parseInt((ahora - moment(res.horaFin)) / 60000); + if (diferencia >= 15) await Multa.create({ idPrestamo, idInfraccion: 1, @@ -49,19 +48,18 @@ const regresar = async (body) => { { where: { idUsuario: res.idUsuario } } ) ); - } return Equipo.update( { idStatus: 4 }, - { where: { idEquipo: res.Equipo.idEquipo } } + { where: { idEquipo: res.idEquipo } } ); }) - .then((res) => + .then(() => Prestamo.update( { idOperadorRegreso, horaEntrega: ahora.format(), activo: false }, { where: { idPrestamo } } ) ) - .then((res) => { + .then(() => { io.getIO().emit('actualizar'); return { message: 'Se ha regresado correctamente el equipo de cómputo.', diff --git a/server/controller/Prestamo/regresarNumeroInventario.js b/server/controller/Prestamo/regresarNumeroInventario.js index e0960d4..dd071ed 100644 --- a/server/controller/Prestamo/regresarNumeroInventario.js +++ b/server/controller/Prestamo/regresarNumeroInventario.js @@ -22,7 +22,6 @@ const regresarNumeroInventario = async (body) => { 'id operador regreso', true ); - let horaFin = moment(); let diferencia = null; let idPrestamo = null; @@ -50,8 +49,7 @@ const regresarNumeroInventario = async (body) => { if (!res.Equipo.idStatus === 1) throw new Error('Aun no se ha entregado el equipo al usuario.'); idPrestamo = res.idPrestamo; - horaFin = moment(res.horaFin); - diferencia = (ahora - horaFin) / 60000; + diferencia = parseInt((ahora - moment(res.horaFin)) / 60000); if (diferencia >= 15) await Multa.create({ idPrestamo, @@ -62,16 +60,16 @@ const regresarNumeroInventario = async (body) => { }); return Equipo.update( { idStatus: 4 }, - { where: { idEquipo: res.Equipo.idEquipo } } + { where: { idEquipo: res.idEquipo } } ); }) - .then((res) => { + .then(() => { Prestamo.update( { idOperadorRegreso, horaEntrega: ahora.format(), activo: false }, { where: { idPrestamo } } ); }) - .then((res) => { + .then(() => { io.getIO().emit('actualizar'); return { message: 'Se ha regresado correctamente el equipo de cómputo.', diff --git a/server/controller/Prestamo/regresoInmediato.js b/server/controller/Prestamo/regresoInmediato.js index 5055650..a040232 100644 --- a/server/controller/Prestamo/regresoInmediato.js +++ b/server/controller/Prestamo/regresoInmediato.js @@ -11,7 +11,6 @@ const regresoInmediato = async (body) => { const ahora = moment(); const ahoraAux = moment(); const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id prestamo', true); - let horaFin = moment(); let diferencia = null; return Prestamo.findOne({ @@ -23,33 +22,31 @@ const regresoInmediato = async (body) => { if (!res.activo) throw new Error('Este préstamo ya no esta activo.'); if (!res.Equipo.idStatus === 1) throw new Error('Aun no se ha entregado el equipo al usuario.'); - horaFin = moment(res.horaFin); - diferencia = parseInt((ahora - horaFin) / 60000); - if (diferencia >= 15) { + diferencia = parseInt((ahora - moment(res.horaFin)) / 60000); + if (diferencia >= 15) await Multa.create({ idPrestamo, idInfraccion: 1, descripcion: `Se paso por ${diferencia} minutos para entregar el equipo.`, - fechaFin: ahoraAux.add((diferencia / 15) * 7, 'd').format(), + fechaFin: ahoraAux.add(parseInt(diferencia / 15) * 7, 'd').format(), }).then((res) => Usuario.update( { multa: true }, { where: { idUsuario: res.idUsuario } } ) ); - } return Equipo.update( { idStatus: 3 }, - { where: { idEquipo: res.Equipo.idEquipo } } + { where: { idEquipo: res.idEquipo } } ); }) - .then((res) => + .then(() => Prestamo.update( { horaEntrega: ahora.format(), activo: false, regresoInmediato: true }, { where: { idPrestamo } } ) ) - .then((res) => { + .then(() => { io.getIO().emit('actualizar'); return { message: 'Se ha regresado correctamente el equipo de cómputo.', diff --git a/server/controller/Prestamo/regresoInmediatoNumeroInventario.js b/server/controller/Prestamo/regresoInmediatoNumeroInventario.js index 6f6790c..1434901 100644 --- a/server/controller/Prestamo/regresoInmediatoNumeroInventario.js +++ b/server/controller/Prestamo/regresoInmediatoNumeroInventario.js @@ -16,7 +16,6 @@ const regresoInmediatoNumeroInventario = async (body) => { true, 20 ); - let horaFin = moment(); let diferencia = null; let idPrestamo = null; @@ -29,33 +28,31 @@ const regresoInmediatoNumeroInventario = async (body) => { if (!res.activo) throw new Error('Este préstamo ya no esta activo.'); if (!res.Equipo.idStatus === 1) throw new Error('Aun no se ha entregado el equipo al usuario.'); - horaFin = moment(res.horaFin); - diferencia = parseInt((ahora - horaFin) / 60000); - if (diferencia >= 15) { + diferencia = parseInt((ahora - moment(res.horaFin)) / 60000); + if (diferencia >= 15) await Multa.create({ idPrestamo, idInfraccion: 1, descripcion: `Se paso por ${diferencia} minutos para entregar el equipo.`, - fechaFin: ahoraAux.add((diferencia / 15) * 7, 'd').format(), + fechaFin: ahoraAux.add(parseInt(diferencia / 15) * 7, 'd').format(), }).then((res) => Usuario.update( { multa: true }, { where: { idUsuario: res.idUsuario } } ) ); - } return Equipo.update( { idStatus: 3 }, - { where: { idEquipo: res.Equipo.idEquipo } } + { where: { idEquipo: res.idEquipo } } ); }) - .then((res) => + .then(() => Prestamo.update( { horaEntrega: ahora.format(), activo: false, regresoInmediato: true }, { where: { idPrestamo } } ) ) - .then((res) => { + .then(() => { io.getIO().emit('actualizar'); return { message: 'Se ha regresado correctamente el equipo de cómputo.', diff --git a/server/controller/Prestamo/status.js b/server/controller/Prestamo/status.js index 9c2204f..8c7cada 100644 --- a/server/controller/Prestamo/status.js +++ b/server/controller/Prestamo/status.js @@ -35,7 +35,7 @@ const status = async (body) => { await Equipo.update( { idStatus: 4 }, { where: { idEquipo: res.Equipo.idEquipo } } - ).then((res) => + ).then(() => Prestamo.update({ activo: false }, { where: { idPrestamo } }) ); message =