diff --git a/server/controller/Multa/historialMultas.js b/server/controller/Multa/historialMultas.js new file mode 100644 index 0000000..800dcd4 --- /dev/null +++ b/server/controller/Multa/historialMultas.js @@ -0,0 +1,35 @@ +const validar = require('../../helper/validar'); +const dbPath = '../../db/tablas'; +const Multa = require(`${dbPath}/Multa`); +const Infraccion = require(`${dbPath}/Infraccion`); +const Prestamo = require(`${dbPath}/Prestamo`); + +const historialMultas = async (body) => { + const idUsuario = validar.validarId(body.idUsuario); + const pagina = validar.validarNumero(body.pagina, true); + + return Multa.findAndCountAll({ + include: [ + { + model: Prestamo, + where: { idUsuario }, + }, + { model: Infraccion }, + ], + 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; + } + return { count: res.count, historialMultas: res.rows }; + }); +}; + +module.exports = historialMultas; diff --git a/server/controller/Reporte/historialReportes.js b/server/controller/Reporte/historialReportes.js new file mode 100644 index 0000000..97fdd34 --- /dev/null +++ b/server/controller/Reporte/historialReportes.js @@ -0,0 +1,29 @@ +const validar = require('../../helper/validar'); +const dbPath = '../../db/tablas'; +const Reporte = require(`${dbPath}/Reporte`); +const Prestamo = require(`${dbPath}/Prestamo`); + +const historialReportes = async (body) => { + const idEquipo = validar.validarId(body.idEquipo); + const pagina = validar.validarNumero(body.pagina, true); + + return Reporte.findAndCountAll({ + // where: { idEquipo }, + include: [{ model: Prestamo }], + 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; + } + return { count: res.count, historialReportes: res.rows }; + }); +}; + +module.exports = historialReportes; diff --git a/server/controller/Reporte/reportar.js b/server/controller/Reporte/reportar.js index a524b20..47d2d4d 100644 --- a/server/controller/Reporte/reportar.js +++ b/server/controller/Reporte/reportar.js @@ -2,6 +2,7 @@ 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); @@ -10,11 +11,13 @@ const reportar = async (body) => { return Prestamo.findOne({ where: { idPrestamo } }) .then((res) => { if (!res) throw new Error('No existe este prestamo.'); + console.log(res.dataValues); + idEquipo = res.idEquipo; return Reporte.findOne({ where: { idPrestamo } }); }) .then((res) => { if (res) throw new Error('Ya existe un reporte con este prestamo.'); - return Reporte.create({ descripcion, idPrestamo }); + return Reporte.create({ descripcion, idPrestamo, idEquipo }); }) .then((res) => ({ message: 'Se levanto correctamente el reporte.' })); }; diff --git a/server/routes/Multa.js b/server/routes/Multa.js index 2ea6133..f9cba66 100644 --- a/server/routes/Multa.js +++ b/server/routes/Multa.js @@ -3,6 +3,7 @@ const app = express(); const { verificaToken } = require('../middleware/autentificacion'); const route = '/multa'; const multar = require('../controller/Multa/multar'); +const historialMultas = require('../controller/Multa/historialMultas'); app.post( `${route}`, @@ -18,4 +19,18 @@ app.post( } ); +app.get( + `${route}/historial_multas`, + //verificaToken, + (req, res) => { + return historialMultas(req.query) + .then((data) => { + res.status(200).json(data); + }) + .catch((err) => { + res.status(400).json({ message: err.message }); + }); + } +); + module.exports = app; diff --git a/server/routes/Reporte.js b/server/routes/Reporte.js index aa4a2ef..c00a1a3 100644 --- a/server/routes/Reporte.js +++ b/server/routes/Reporte.js @@ -4,6 +4,7 @@ const { verificaToken } = require('../middleware/autentificacion'); const route = '/reporte'; const reportar = require('../controller/Reporte/reportar'); const reportarEquipo = require('../controller/Reporte/reportarEquipo'); +const historialReportes = require('../controller/Reporte/historialReportes'); app.post( `${route}/reportar`, @@ -33,4 +34,18 @@ app.post( } ); +app.get( + `${route}/historial_reportes`, + //verificaToken, + (req, res) => { + return historialReportes(req.query) + .then((data) => { + res.status(200).json(data); + }) + .catch((err) => { + res.status(400).json({ message: err.message }); + }); + } +); + module.exports = app;