listo socket
This commit is contained in:
+1
-14
@@ -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)
|
||||
);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 },
|
||||
|
||||
@@ -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;
|
||||
})
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user