diff --git a/server/app.js b/server/app.js index 1c9a483..01e910f 100644 --- a/server/app.js +++ b/server/app.js @@ -5,17 +5,8 @@ const cors = require('cors'); const express = require('express'); const app = express(); const { createServer } = require('http'); -const { Server } = require('socket.io'); const httpServer = createServer(app); -const io = new Server(httpServer, { - cors: { origin: 'http://localhost:3046' }, - // cors: { - // origin: [´ - // 'https://pcpuma.acatlan.unam.mx', - // 'https://pcpuma.acatlan.unam.mx:8080', - // ], - // }, -}); +const io = require('./socket').init(httpServer); app.use(cors()); @@ -31,10 +22,6 @@ app.get('/', (req, res) => res.send('API Pcpuma')); app.use(require('./routes/index')); -io.on('connection', (socket) => { - console.log('Conectado'); -}); - httpServer.listen(Number(process.env.PORT), () => console.log(`API Pcpuma corriendo en el puerto: ${process.env.PORT}`.rainbow) ); diff --git a/server/controller/Prestamo/cancelarOperador.js b/server/controller/Prestamo/cancelarOperador.js index 4a7258a..fdcb28d 100644 --- a/server/controller/Prestamo/cancelarOperador.js +++ b/server/controller/Prestamo/cancelarOperador.js @@ -1,3 +1,4 @@ +const io = require('../../socket'); const { validarNumeroEntero } = require('../../helper/validar'); const dbPath = '../../db/tablas'; const Equipo = require(`${dbPath}/Equipo`); @@ -41,7 +42,10 @@ const cancelarOperador = async (body) => { { where: { idPrestamo } } ) ) - .then((res) => ({ message: 'Se canceló el préstamo correctamente.' })); + .then((res) => { + io.getIO().emit('actualizar', { idPrestamo }); + return { message: 'Se canceló el préstamo correctamente.' }; + }); }; module.exports = cancelarOperador; diff --git a/server/controller/Prestamo/cancelarUsuario.js b/server/controller/Prestamo/cancelarUsuario.js index a37a666..1d6ba39 100644 --- a/server/controller/Prestamo/cancelarUsuario.js +++ b/server/controller/Prestamo/cancelarUsuario.js @@ -1,3 +1,4 @@ +const io = require('../../socket'); const { validarNumeroEntero } = require('../../helper/validar'); const dbPath = '../../db/tablas'; const Prestamo = require(`${dbPath}/Prestamo`); @@ -31,7 +32,10 @@ const cancelarUsuario = async (body) => { { where: { idPrestamo } } ) ) - .then((res) => ({ message: 'Se canceló el préstamo correctamente.' })); + .then((res) => { + io.getIO().emit('actualizar'); + return { message: 'Se canceló el préstamo correctamente.' }; + }); }; module.exports = cancelarUsuario; diff --git a/server/controller/Prestamo/entregar.js b/server/controller/Prestamo/entregar.js index d5461cb..cb01943 100644 --- a/server/controller/Prestamo/entregar.js +++ b/server/controller/Prestamo/entregar.js @@ -1,4 +1,5 @@ const moment = require('moment'); +const io = require('../../socket'); const { validarNumeroEntero } = require('../../helper/validar'); const dbPath = '../../db/tablas'; const Carrito = require(`${dbPath}/Carrito`); @@ -7,9 +8,9 @@ const Modulo = require(`${dbPath}/Modulo`); const Operador = require(`${dbPath}/Operador`); const Prestamo = require(`${dbPath}/Prestamo`); const TipoCarrito = require(`${dbPath}/TipoCarrito`); -const ahora = moment(); const entregar = async (body) => { + const ahora = moment(); const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id prestamo', true); const idOperadorEntrega = validarNumeroEntero( body.idOperadorEntrega, @@ -71,7 +72,11 @@ const entregar = async (body) => { 'sobrenombre', ], }) - ); + ) + .then((res) => { + io.getIO().emit('actualizar'); + return res; + }); }; module.exports = entregar; diff --git a/server/controller/Prestamo/pedir.js b/server/controller/Prestamo/pedir.js index 03a47f0..1e8ac8c 100644 --- a/server/controller/Prestamo/pedir.js +++ b/server/controller/Prestamo/pedir.js @@ -1,4 +1,5 @@ const moment = require('moment'); +const io = require('../../socket'); const { validarNumeroEntero } = require('../../helper/validar'); const dbPath = '../../db/tablas'; const Carrito = require(`${dbPath}/Carrito`); @@ -9,19 +10,11 @@ const Programa = require(`${dbPath}/Programa`); const Status = require(`${dbPath}/Status`); const TipoCarrito = require(`${dbPath}/TipoCarrito`); const Usuario = require(`${dbPath}/Usuario`); -const ahora = moment(); -const horaMax = moment( - `${ahora.year()}-${ - ahora.month() < 9 ? '0' + (ahora.month() + 1) : ahora.month() + 1 - }-${ahora.date() < 9 ? '0' + ahora.date() : ahora.date()} 19:00` -); -const horaMin = moment( - `${ahora.year()}-${ - ahora.month() < 9 ? '0' + (ahora.month() + 1) : ahora.month() + 1 - }-${ahora.date() < 9 ? '0' + ahora.date() : ahora.date()} 09:00` -); const pedir = async (body) => { + const ahora = moment(); + const horaMax = moment(`${ahora.format('YYYY-MM-DD')} 19:00`); + const horaMin = moment(`${ahora.format('YYYY-MM-DD')} 09:00`); const idUsuario = validarNumeroEntero(body.idUsuario, 'id usuario', true); const idTipoCarrito = validarNumeroEntero( body.idTipoCarrito, @@ -96,40 +89,13 @@ const pedir = async (body) => { idEquipo, }) ) - .then((res) => - Prestamo.findOne({ - where: { idPrestamo: res.idPrestamo }, - include: [ - { - model: Equipo, - include: [ - { - model: Carrito, - include: [{ model: Modulo }, { model: TipoCarrito }], - attributes: ['idCarrito', 'sobrenombre'], - }, - { model: Status }, - { model: Programa }, - ], - attributes: [ - 'idEquipo', - 'numeroSerie', - 'numeroInventario', - 'sobrenombre', - ], - }, - ], - attributes: [ - 'idPrestamo', - 'activo', - 'horaMaxRecoger', - 'horaInicio', - 'horaFin', - 'horaEntrega', - 'createdAt', - ], - }) - ); + .then((res) => { + io.getIO().emit('actualizar', { idPrestamo: res.idPrestamo }); + return { + message: + 'Haz solicitado un equipo de cómputo correctamente. Presentate al módulo señalado y prestenta tu QR.', + }; + }); }; module.exports = pedir; diff --git a/server/controller/Prestamo/regresar.js b/server/controller/Prestamo/regresar.js index 2fefd22..c28beb6 100644 --- a/server/controller/Prestamo/regresar.js +++ b/server/controller/Prestamo/regresar.js @@ -1,14 +1,15 @@ const moment = require('moment'); +const io = require('../../socket'); const { validarNumeroEntero } = require('../../helper/validar'); const dbPath = '../../db/tablas'; const Equipo = require(`${dbPath}/Equipo`); const Multa = require(`${dbPath}/Multa`); const Operador = require(`${dbPath}/Operador`); const Prestamo = require(`${dbPath}/Prestamo`); -const ahora = moment(); -const ahoraAux = moment(); const regresar = async (body) => { + const ahora = moment(); + const ahoraAux = moment(); const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id prestamo', true); const idOperadorRegreso = validarNumeroEntero( body.idOperadorRegreso, @@ -52,9 +53,12 @@ const regresar = async (body) => { { where: { idPrestamo } } ) ) - .then((res) => ({ - message: 'Se ha regresado correctamente el equipo de cómputo.', - })); + .then((res) => { + io.getIO().emit('actualizar'); + return { + message: 'Se ha regresado correctamente el equipo de cómputo.', + }; + }); }; module.exports = regresar; diff --git a/server/controller/Prestamo/regresarNumeroInventario.js b/server/controller/Prestamo/regresarNumeroInventario.js index 5fd4beb..e8f07f8 100644 --- a/server/controller/Prestamo/regresarNumeroInventario.js +++ b/server/controller/Prestamo/regresarNumeroInventario.js @@ -1,14 +1,15 @@ const moment = require('moment'); +const io = require('../../socket'); const validar = require('../../helper/validar'); const dbPath = '../../db/tablas'; const Equipo = require(`${dbPath}/Equipo`); const Multa = require(`${dbPath}/Multa`); const Operador = require(`${dbPath}/Operador`); const Prestamo = require(`${dbPath}/Prestamo`); -const ahora = moment(); -const ahoraAux = moment(); const regresarNumeroInventario = async (body) => { + const ahora = moment(); + const ahoraAux = moment(); const numeroInventario = validar.validarAlfanumerico( body.numeroInventario, 'numero de inventario', @@ -59,9 +60,12 @@ const regresarNumeroInventario = async (body) => { { where: { idPrestamo } } ); }) - .then((res) => ({ - message: 'Se ha regresado correctamente el equipo de cómputo.', - })); + .then((res) => { + io.getIO().emit('actualizar'); + return { + message: 'Se ha regresado correctamente el equipo de cómputo.', + }; + }); }; module.exports = regresarNumeroInventario; diff --git a/server/controller/Prestamo/reporte.js b/server/controller/Prestamo/reporte.js index 0aeda1f..5f71087 100644 --- a/server/controller/Prestamo/reporte.js +++ b/server/controller/Prestamo/reporte.js @@ -1,3 +1,4 @@ +const moment = require('moment'); const { convertArrayToCSV } = require('convert-array-to-csv'); const { Op } = require('sequelize'); const helperPath = '../../helper'; @@ -95,12 +96,14 @@ const reporte = async (body) => { 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), + horaMaxRecoger: moment(res[i].horaMaxRecoger).format( + 'YYYY-MM-DD HH:MM' + ), + horaInicio: moment(res[i].horaInicio).format('YYYY-MM-DD HH:MM'), + horaFin: moment(res[i].horaFin).format('YYYY-MM-DD HH:MM'), + horaEntrega: moment(res[i].horaEntrega).format('YYYY-MM-DD HH:MM'), canceladoUsuario: res[i].canceladoUsuario, - createdAt: helper.crearDDMMAAAAHHMM(res[i].createdAt), + createdAt: moment(res[i].createdAt).format('YYYY-MM-DD HH:MM'), numeroInventario: res[i].Equipo.numeroInventario, numeroSerie: res[i].Equipo.numeroSerie, sobrenombre: res[i].Equipo.sobrenombre, @@ -125,9 +128,7 @@ const reporte = async (body) => { : null, }); } - await helper.eliminarArchivo(path).catch((err) => { - console.log(err); - }); + await helper.eliminarArchivo(path).catch((err) => {}); return helper.crearArchivo(path, convertArrayToCSV(data)); }) .then((res) => path); diff --git a/server/controller/Prestamo/status.js b/server/controller/Prestamo/status.js index 07ecfe3..2818df8 100644 --- a/server/controller/Prestamo/status.js +++ b/server/controller/Prestamo/status.js @@ -1,4 +1,5 @@ const moment = require('moment'); +const io = require('../../socket'); const { validarNumeroEntero } = require('../../helper/validar'); const dbPath = '../../db/tablas'; const Carrito = require(`${dbPath}/Carrito`); @@ -8,9 +9,9 @@ const Prestamo = require(`${dbPath}/Prestamo`); const Programa = require(`${dbPath}/Programa`); const Status = require(`${dbPath}/Status`); const TipoCarrito = require(`${dbPath}/TipoCarrito`); -const ahora = moment(); const status = async (body) => { + const ahora = moment(); const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id prestamo', true); let message = ''; let atraso = false; @@ -39,6 +40,7 @@ const status = async (body) => { ); message = 'Ya paso el tiempo limite para recoger el dispositivo. Se ha cancelado tu prestamo.'; + io.getIO().emit('actualizar'); } else message = 'Aun no se entrega el equipo al usuario.'; return Prestamo.findOne({ where: { idPrestamo }, diff --git a/server/controller/Usuario/escolares.js b/server/controller/Usuario/escolares.js index b8f697f..eed2ed6 100644 --- a/server/controller/Usuario/escolares.js +++ b/server/controller/Usuario/escolares.js @@ -19,7 +19,6 @@ const escolares = async (body) => { return obtenerAlumno(numeroCuenta) .then(async (res) => { if (!res) return obtenerProfesor(numeroCuenta); - console.log(res); esAlumno = true; return res; }) diff --git a/server/helper/helper.js b/server/helper/helper.js index c30de11..8f511f2 100644 --- a/server/helper/helper.js +++ b/server/helper/helper.js @@ -1,36 +1,6 @@ const fs = require('fs'); const moment = require('moment'); -const crearDDMMAAAA = (date) => { - const fechaMoment = moment(date); - let ddmmaaaa = ''; - - if (fechaMoment.isValid()) - 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; -}; - const eliminarArchivo = (path) => { return new Promise((resolve, reject) => { fs.unlink(path, (err) => { @@ -50,8 +20,6 @@ const crearArchivo = (path, texto) => { }; module.exports = { - crearDDMMAAAA, - crearDDMMAAAAHHMM, eliminarArchivo, crearArchivo, }; diff --git a/server/socket.js b/server/socket.js new file mode 100644 index 0000000..971f0d6 --- /dev/null +++ b/server/socket.js @@ -0,0 +1,22 @@ +const { Server } = require('socket.io'); +let io; + +module.exports = { + init(httpServer) { + io = new Server(httpServer, { + cors: { + origin: [ + 'http://localhost:3046', + 'http://localhost:3056', + // 'https://pcpuma.acatlan.unam.mx', + // 'https://pcpuma.acatlan.unam.mx:8080', + ], + }, + }); + return io; + }, + getIO() { + if (!io) throw new Error('Socket.io no se ha inicializado '); + return io; + }, +};