From ded01c50d33ab92220350de0dcb3b01c92bb2e04 Mon Sep 17 00:00:00 2001 From: lemuel Date: Thu, 2 Sep 2021 16:13:16 -0500 Subject: [PATCH] correiciones --- .../controller/Reporte/historialReportes.js | 27 +++++++++++++------ server/controller/Reporte/reportar.js | 2 +- server/controller/Reporte/reportarEquipo.js | 13 ++++++--- server/db/tablas/Reporte.js | 10 +++++++ 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/server/controller/Reporte/historialReportes.js b/server/controller/Reporte/historialReportes.js index 97fdd34..2ad2a10 100644 --- a/server/controller/Reporte/historialReportes.js +++ b/server/controller/Reporte/historialReportes.js @@ -1,6 +1,7 @@ const validar = require('../../helper/validar'); const dbPath = '../../db/tablas'; const Reporte = require(`${dbPath}/Reporte`); +const Operador = require(`${dbPath}/Operador`); const Prestamo = require(`${dbPath}/Prestamo`); const historialReportes = async (body) => { @@ -8,19 +9,29 @@ const historialReportes = async (body) => { const pagina = validar.validarNumero(body.pagina, true); return Reporte.findAndCountAll({ - // where: { idEquipo }, - include: [{ model: Prestamo }], + where: { idEquipo }, + include: [{ model: Prestamo }, { model: Operador }], limit: 25, offset: 25 * (pagina - 1), order: [['createdAt', 'DESC']], }).then((res) => { for (let i = 0; i < res.rows.length; i++) { - // delete res.rows[i].dataValues.idInfraccion; - // delete res.rows[i].dataValues.idPrestamo; - // delete res.rows[i].dataValues.Prestamo.dataValues.idOperadorEntrega; - // delete res.rows[i].dataValues.Prestamo.dataValues.idOperadorRegreso; - // delete res.rows[i].dataValues.Prestamo.dataValues.idUsuario; - // delete res.rows[i].dataValues.Prestamo.dataValues.idEquipo; + delete res.rows[i].dataValues.idPrestamo; + delete res.rows[i].dataValues.idEquipo; + delete res.rows[i].dataValues.idOperador; + if (res.rows[i].dataValues.Prestamo) { + delete res.rows[i].dataValues.Prestamo.dataValues.idOperadorEntrega; + delete res.rows[i].dataValues.Prestamo.dataValues.idOperadorRegreso; + delete res.rows[i].dataValues.Prestamo.dataValues.idUsuario; + delete res.rows[i].dataValues.Prestamo.dataValues.idEquipo; + delete res.rows[i].dataValues.Operador; + } + if (res.rows[i].dataValues.Operador) { + delete res.rows[i].dataValues.Operador.dataValues.password; + delete res.rows[i].dataValues.Operador.dataValues.idTipoUsuario; + delete res.rows[i].dataValues.Operador.dataValues.activo; + delete res.rows[i].dataValues.Prestamo; + } } return { count: res.count, historialReportes: res.rows }; }); diff --git a/server/controller/Reporte/reportar.js b/server/controller/Reporte/reportar.js index 47d2d4d..4312fa3 100644 --- a/server/controller/Reporte/reportar.js +++ b/server/controller/Reporte/reportar.js @@ -2,11 +2,11 @@ const { validarId, validarTexto } = require('../../helper/validar'); const dbPath = '../../db/tablas'; const Reporte = require(`${dbPath}/Reporte`); const Prestamo = require(`${dbPath}/Prestamo`); -let idEquipo = null; const reportar = async (body) => { const idPrestamo = validarId(body.idPrestamo); const descripcion = validarTexto(body.descripcion, 'La descripción', 500); + let idEquipo = null; return Prestamo.findOne({ where: { idPrestamo } }) .then((res) => { diff --git a/server/controller/Reporte/reportarEquipo.js b/server/controller/Reporte/reportarEquipo.js index ddeab68..f6187dc 100644 --- a/server/controller/Reporte/reportarEquipo.js +++ b/server/controller/Reporte/reportarEquipo.js @@ -1,17 +1,22 @@ const { validarId, validarTexto } = require('../../helper/validar'); const dbPath = '../../db/tablas'; -const Reporte = require(`${dbPath}/Reporte`); const Equipo = require(`${dbPath}/Equipo`); +const Operador = require(`${dbPath}/Operador`); +const Reporte = require(`${dbPath}/Reporte`); const reportar = async (body) => { - console.log(body); + const idOperador = validarId(body.idOperador); const idEquipo = validarId(body.idEquipo); const descripcion = validarTexto(body.descripcion, 'La descripción', 500); - return Equipo.findOne({ where: { idEquipo } }) + return Operador.findOne({ where: { idOperador } }) + .then((res) => { + if (!res) throw new Error('No existe este operador.'); + return Equipo.findOne({ where: { idEquipo } }); + }) .then((res) => { if (!res) throw new Error('No existe este equipo.'); - return Reporte.create({ descripcion, idEquipo }); + return Reporte.create({ descripcion, idEquipo, idOperador }); }) .then((res) => ({ message: 'Se levanto correctamente el reporte.' })); }; diff --git a/server/db/tablas/Reporte.js b/server/db/tablas/Reporte.js index 90b0bed..19d537b 100644 --- a/server/db/tablas/Reporte.js +++ b/server/db/tablas/Reporte.js @@ -1,6 +1,7 @@ const { DataTypes, Model } = require('sequelize'); const sequelize = require('../../config/sequelize.conf'); const Equipo = require('./Equipo'); +const Operador = require('./Operador'); const Prestamo = require('./Prestamo'); class Reporte extends Model {} @@ -44,4 +45,13 @@ Reporte.belongsTo(Prestamo, { }, }); +Reporte.belongsTo(Operador, { + foreignKey: { + name: 'idOperador', + type: DataTypes.INTEGER, + allowNull: true, + defaultValue: null, + }, +}); + module.exports = Reporte;