diff --git a/.gitignore b/.gitignore index 567b4d9..5687f96 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules/ .env +server/uploads/* \ No newline at end of file diff --git a/server/controller/Carrera/get.js b/server/controller/Carrera/get.js new file mode 100644 index 0000000..36db61c --- /dev/null +++ b/server/controller/Carrera/get.js @@ -0,0 +1,5 @@ +const Carrera = require('../../db/tablas/Carrera'); + +const get = async () => Carrera.findAll(); + +module.exports = get; diff --git a/server/controller/Prestamo/reporte.js b/server/controller/Prestamo/reporte.js new file mode 100644 index 0000000..0aeda1f --- /dev/null +++ b/server/controller/Prestamo/reporte.js @@ -0,0 +1,136 @@ +const { convertArrayToCSV } = require('convert-array-to-csv'); +const { Op } = require('sequelize'); +const helperPath = '../../helper'; +const helper = require(`${helperPath}/helper`); +const validar = require(`${helperPath}/validar`); +const dbPath = '../../db/tablas'; +const Carrera = require(`${dbPath}/Carrera`); +const Carrito = require(`${dbPath}/Carrito`); +const Equipo = require(`${dbPath}/Equipo`); +const Modulo = require(`${dbPath}/Modulo`); +const Operador = require(`${dbPath}/Operador`); +const Prestamo = require(`${dbPath}/Prestamo`); +const Programa = require(`${dbPath}/Programa`); +const TipoCarrito = require(`${dbPath}/TipoCarrito`); +const TipoUsuario = require(`${dbPath}/TipoUsuario`); +const Usuario = require(`${dbPath}/Usuario`); + +const reporte = async (body) => { + const inicio = validar.validarFecha(body.inicio, 'fecha de inicio', false); + const fin = validar.validarFecha(body.fin, 'fecha fin', false); + const idCarrera = body.idCarrera + ? validar.validarNumeroEntero(body.idCarrera, 'id carrera', true) + : ''; + const idTipoUsuario = body.idTipoUsuario + ? validar.validarNumeroEntero(body.idTipoUsuario, 'id tipo usuario', true) + : ''; + const idTipoCarrito = body.idTipoCarrito + ? validar.validarNumeroEntero(body.idTipoCarrito, 'id tipo carrito', true) + : ''; + const path = `server/uploads/reporte.csv`; + const data = []; + + return Prestamo.findAll({ + where: { + [Op.and]: [ + { createdAt: { [Op.gte]: inicio.format() } }, + { createdAt: { [Op.lte]: fin.format() } }, + ], + }, + include: [ + { + model: Equipo, + include: [ + { + model: Carrito, + include: [{ model: TipoCarrito }, { model: Modulo }], + }, + { model: Programa }, + ], + }, + { + model: Usuario, + include: [{ model: Carrera }, { model: TipoUsuario }], + }, + { + model: Operador, + as: 'OperadorEntrega', + }, + { + model: Operador, + as: 'OperadorRegreso', + }, + { + model: Operador, + as: 'OperadorCancelacion', + }, + ], + }) + .then(async (res) => { + for (let i = 0; i < res.length; i++) { + let agregar = false; + + if (idTipoCarrito) { + if (idTipoCarrito === res[i].Equipo.Carrito.idTipoCarrito) { + if (idTipoUsuario) { + if (idTipoUsuario === res[i].Usuario.idTipoUsuario) { + if (idCarrera) { + if (idCarrera === res[i].Usuario.idCarrera) agregar = true; + } else agregar = true; + } + } else if (idCarrera) { + if (idCarrera === res[i].Usuario.idCarrera) agregar = true; + } else agregar = true; + } + } else if (idTipoUsuario) { + if (idTipoUsuario === res[i].Usuario.idTipoUsuario) { + if (idCarrera) { + if (idCarrera === res[i].Usuario.idCarrera) agregar = true; + } else agregar = true; + } + } else if (idCarrera) { + if (idCarrera === res[i].Usuario.idCarrera) agregar = true; + } else agregar = true; + if (agregar) + data.push({ + idPrestamo: res[i].idPrestamo, + activo: res[i].activo, + horaMaxRecoger: helper.crearDDMMAAAAHHMM(res[i].horaMaxRecoger), + horaInicio: helper.crearDDMMAAAAHHMM(res[i].horaInicio), + horaFin: helper.crearDDMMAAAAHHMM(res[i].horaFin), + horaEntrega: helper.crearDDMMAAAAHHMM(res[i].horaEntrega), + canceladoUsuario: res[i].canceladoUsuario, + createdAt: helper.crearDDMMAAAAHHMM(res[i].createdAt), + numeroInventario: res[i].Equipo.numeroInventario, + numeroSerie: res[i].Equipo.numeroSerie, + sobrenombre: res[i].Equipo.sobrenombre, + programa: res[i].Equipo.Programa + ? res[i].Equipo.Programa.programa + : null, + carrito: res[i].Equipo.Carrito.carrito, + modulo: res[i].Equipo.Carrito.Modulo.modulo, + tipoCarrito: res[i].Equipo.Carrito.TipoCarrito.tipoCarrito, + nombre: res[i].Usuario.nombre, + usuario: res[i].Usuario.usuario, + tipoUsuario: res[i].Usuario.TipoUsuario.tipoUsuario, + carrera: res[i].Usuario.Carrera.carrera, + operadorEntrega: res[i].OperadorEntrega + ? res[i].OperadorEntrega.operador + : null, + operadorRegreso: res[i].OperadorRegreso + ? res[i].OperadorRegreso.operador + : null, + operadorCancelacion: res[i].OperadorCancelacion + ? res[i].OperadorCancelacion.operador + : null, + }); + } + await helper.eliminarArchivo(path).catch((err) => { + console.log(err); + }); + return helper.crearArchivo(path, convertArrayToCSV(data)); + }) + .then((res) => path); +}; + +module.exports = reporte; diff --git a/server/controller/TipoUsuario/get.js b/server/controller/TipoUsuario/get.js new file mode 100644 index 0000000..f9a83e3 --- /dev/null +++ b/server/controller/TipoUsuario/get.js @@ -0,0 +1,7 @@ +const { Op } = require('sequelize'); +const TipoUsuario = require('../../db/tablas/TipoUsuario'); + +const get = async () => + TipoUsuario.findAll({ where: { idTipoUsuario: { [Op.gt]: 2 } } }); + +module.exports = get; diff --git a/server/helper/helper.js b/server/helper/helper.js index f269aa5..c30de11 100644 --- a/server/helper/helper.js +++ b/server/helper/helper.js @@ -6,9 +6,28 @@ const crearDDMMAAAA = (date) => { let ddmmaaaa = ''; if (fechaMoment.isValid()) - ddmmaaaa = `${fechaMoment.date()}/${ - fechaMoment.month() + 1 - }/${fechaMoment.year()}`; + ddmmaaaa = `${fechaMoment.year()}/${ + fechaMoment.month() < 9 + ? '0' + (fechaMoment.month() + 1) + : fechaMoment.month() + 1 + }/${ + fechaMoment.date() < 10 ? '0' + fechaMoment.date() : fechaMoment.date() + }`; + return ddmmaaaa; +}; + +const crearDDMMAAAAHHMM = (date) => { + const fechaMoment = moment(date); + let ddmmaaaa = crearDDMMAAAA(date); + + if (fechaMoment.isValid()) + ddmmaaaa += ` ${ + fechaMoment.hour() < 10 ? '0' + fechaMoment.hour() : fechaMoment.hour() + }:${ + fechaMoment.minute() < 10 + ? '0' + fechaMoment.minute() + : fechaMoment.minute() + }`; return ddmmaaaa; }; @@ -32,6 +51,7 @@ const crearArchivo = (path, texto) => { module.exports = { crearDDMMAAAA, + crearDDMMAAAAHHMM, eliminarArchivo, crearArchivo, }; diff --git a/server/routes/Carrera.js b/server/routes/Carrera.js new file mode 100644 index 0000000..11bedb2 --- /dev/null +++ b/server/routes/Carrera.js @@ -0,0 +1,21 @@ +const express = require('express'); +const app = express(); +const { verificaToken } = require('../middleware/autentificacion'); +const route = '/carrera'; +const get = require('../controller/Carrera/get'); + +app.get( + `${route}`, + //verificaToken, + (req, res) => { + return get() + .then((data) => { + res.status(200).json(data); + }) + .catch((err) => { + res.status(400).json({ message: err.message }); + }); + } +); + +module.exports = app; diff --git a/server/routes/Prestamo.js b/server/routes/Prestamo.js index de12175..9575bf1 100644 --- a/server/routes/Prestamo.js +++ b/server/routes/Prestamo.js @@ -15,8 +15,10 @@ const regresar = require(`${controllerPath}/regresar`); const regresarNumeroInventario = require(`${controllerPath}/regresarNumeroInventario`); const prestamo = require(`${controllerPath}/prestamo`); const prestamoUsuario = require(`${controllerPath}/prestamoUsuario`); +const reporte = require(`${controllerPath}/reporte`); const status = require(`${controllerPath}/status`); +// POST app.post( `${route}/pedir`, // verificaToken, @@ -31,6 +33,7 @@ app.post( } ); +// PUT app.put( `${route}/entregar`, // verificaToken, @@ -101,6 +104,7 @@ app.put( } ); +// GET app.get( `${route}/activos`, // verificaToken, @@ -185,6 +189,20 @@ app.get( } ); +app.get( + `${route}/reporte`, + // verificaToken, + (req, res) => { + return reporte(req.query) + .then((data) => { + res.download(data); + }) + .catch((err) => { + res.status(400).json({ message: err.message }); + }); + } +); + app.get( `${route}/status`, // verificaToken, diff --git a/server/routes/TipoUsuario.js b/server/routes/TipoUsuario.js new file mode 100644 index 0000000..755c1bd --- /dev/null +++ b/server/routes/TipoUsuario.js @@ -0,0 +1,21 @@ +const express = require('express'); +const app = express(); +const { verificaToken } = require('../middleware/autentificacion'); +const route = '/tipo_usuario'; +const get = require('../controller/TipoUsuario/get'); + +app.get( + `${route}`, + //verificaToken, + (req, res) => { + return get() + .then((data) => { + res.status(200).json(data); + }) + .catch((err) => { + res.status(400).json({ message: err.message }); + }); + } +); + +module.exports = app; diff --git a/server/routes/index.js b/server/routes/index.js index 8dab1c9..00c287e 100644 --- a/server/routes/index.js +++ b/server/routes/index.js @@ -1,6 +1,7 @@ const express = require('express'); const app = express(); +app.use(require('./Carrera')); app.use(require('./Carrito')); app.use(require('./Equipo')); app.use(require('./Infraccion')); @@ -11,6 +12,7 @@ app.use(require('./Prestamo')); app.use(require('./Programa')); app.use(require('./Status')); app.use(require('./TipoCarrito')); +app.use(require('./TipoUsuario')); app.use(require('./Usuario')); module.exports = app;