2021-04-28 01:21:22 -05:00
|
|
|
const moment = require('moment');
|
2022-01-28 16:41:46 -06:00
|
|
|
const { Op } = require('sequelize');
|
2022-01-25 22:08:13 -06:00
|
|
|
const io = require('../../socket');
|
2022-01-03 16:47:25 -06:00
|
|
|
const { validarNumeroEntero } = require('../../helper/validar');
|
2021-04-29 01:44:51 -05:00
|
|
|
const dbPath = '../../db/tablas';
|
|
|
|
|
const Carrito = require(`${dbPath}/Carrito`);
|
|
|
|
|
const Equipo = require(`${dbPath}/Equipo`);
|
|
|
|
|
const Modulo = require(`${dbPath}/Modulo`);
|
2021-09-20 00:21:36 -05:00
|
|
|
const Prestamo = require(`${dbPath}/Prestamo`);
|
|
|
|
|
const Programa = require(`${dbPath}/Programa`);
|
|
|
|
|
const TipoCarrito = require(`${dbPath}/TipoCarrito`);
|
|
|
|
|
const Usuario = require(`${dbPath}/Usuario`);
|
2021-04-28 01:21:22 -05:00
|
|
|
|
|
|
|
|
const pedir = async (body) => {
|
2022-01-25 22:08:13 -06:00
|
|
|
const ahora = moment();
|
2022-02-14 19:19:51 -06:00
|
|
|
const horaMax = moment(`${ahora.format('YYYY-MM-DD')} 18:45`);
|
2022-01-25 22:08:13 -06:00
|
|
|
const horaMin = moment(`${ahora.format('YYYY-MM-DD')} 09:00`);
|
2022-01-03 16:47:25 -06:00
|
|
|
const idUsuario = validarNumeroEntero(body.idUsuario, 'id usuario', true);
|
|
|
|
|
const idTipoCarrito = validarNumeroEntero(
|
|
|
|
|
body.idTipoCarrito,
|
|
|
|
|
'id tipo carrito',
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
const idPrograma = body.idPrograma
|
|
|
|
|
? validarNumeroEntero(body.idPrograma, 'id programa', true)
|
2022-02-13 19:10:21 -06:00
|
|
|
: '';
|
2022-01-03 16:47:25 -06:00
|
|
|
const idModulo = idPrograma
|
|
|
|
|
? 1
|
|
|
|
|
: validarNumeroEntero(body.idModulo, 'id modulo', true);
|
2022-02-13 19:10:21 -06:00
|
|
|
let whereEquipo = { idStatus: 4 };
|
2021-04-29 01:44:51 -05:00
|
|
|
let idEquipo;
|
2021-04-28 01:21:22 -05:00
|
|
|
|
2022-02-20 23:53:10 -06:00
|
|
|
if (ahora.day() === 6 || ahora.day() === 0)
|
|
|
|
|
throw new Error(
|
|
|
|
|
'No se puede pedir un equipo de cómputo los días sabados y domingos.'
|
|
|
|
|
);
|
2022-03-07 09:13:25 -06:00
|
|
|
if (ahora > horaMax)
|
|
|
|
|
throw new Error('Ya no se puede realizar un préstamo el día de hoy.');
|
|
|
|
|
if (ahora < horaMin)
|
|
|
|
|
throw new Error('Aún es muy temprano para realizar un préstamo.');
|
2021-04-29 01:44:51 -05:00
|
|
|
return TipoCarrito.findOne({ where: { idTipoCarrito } })
|
|
|
|
|
.then((res) => {
|
|
|
|
|
if (!res) throw new Error('No existe este tipo de equipo.');
|
|
|
|
|
return Modulo.findOne({ where: { idModulo } });
|
|
|
|
|
})
|
|
|
|
|
.then((res) => {
|
2022-02-13 17:29:26 -06:00
|
|
|
if (!res) throw new Error('No existe este módulo.');
|
2021-04-29 01:44:51 -05:00
|
|
|
return Programa.findOne({ where: { idPrograma } });
|
|
|
|
|
})
|
|
|
|
|
.then((res) => {
|
2021-09-20 00:21:36 -05:00
|
|
|
if (!res && idPrograma) throw new Error('No existe este programa.');
|
2022-02-13 19:10:21 -06:00
|
|
|
if (idPrograma) whereEquipo.idPrograma = idPrograma;
|
2021-04-29 01:44:51 -05:00
|
|
|
return Usuario.findOne({ where: { idUsuario } });
|
|
|
|
|
})
|
2021-04-28 01:21:22 -05:00
|
|
|
.then((res) => {
|
2021-05-27 11:24:18 -05:00
|
|
|
if (!res || !res.password) throw new Error('No existe este usuario.');
|
|
|
|
|
if (!res.activo) throw new Error('Este usuario no esta activo.');
|
2021-04-28 01:21:22 -05:00
|
|
|
if (res.multa)
|
|
|
|
|
throw new Error(
|
2022-02-13 17:29:26 -06:00
|
|
|
'Tienes una multa vigente, no puedes solicitar un equipo de cómputo.'
|
2021-04-28 01:21:22 -05:00
|
|
|
);
|
2022-01-28 16:41:46 -06:00
|
|
|
return Prestamo.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
idUsuario,
|
|
|
|
|
[Op.or]: [
|
2022-02-01 09:48:07 -06:00
|
|
|
{ activo: true },
|
2022-01-28 16:41:46 -06:00
|
|
|
{
|
|
|
|
|
[Op.and]: [
|
|
|
|
|
{ regresoInmediato: true },
|
|
|
|
|
{ idOperadorRegreso: null },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
});
|
2021-04-28 01:21:22 -05:00
|
|
|
})
|
|
|
|
|
.then((res) => {
|
2022-01-28 16:41:46 -06:00
|
|
|
if (res) {
|
|
|
|
|
if (res.regresoInmediato)
|
|
|
|
|
throw new Error(
|
|
|
|
|
'No puedes pedir otro equipo de cómputo hasta que un operador confirme la entrega del equipo de cómputo del anterior préstamo.'
|
|
|
|
|
);
|
2021-04-28 01:21:22 -05:00
|
|
|
throw new Error(
|
2022-02-13 17:29:26 -06:00
|
|
|
'Tienes un préstamo activo. No puedes solicitar otro equipo de cómputo.'
|
2021-04-28 01:21:22 -05:00
|
|
|
);
|
2022-01-28 16:41:46 -06:00
|
|
|
}
|
2022-02-22 15:16:46 -06:00
|
|
|
return Equipo.count({
|
2022-02-13 19:10:21 -06:00
|
|
|
where: whereEquipo,
|
2021-04-28 01:21:22 -05:00
|
|
|
include: [
|
|
|
|
|
{
|
|
|
|
|
model: Carrito,
|
2021-04-29 22:52:17 -05:00
|
|
|
where: { idTipoCarrito, idModulo, activo: true },
|
2021-04-28 01:21:22 -05:00
|
|
|
},
|
|
|
|
|
],
|
2022-02-22 15:16:46 -06:00
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.then((res) => {
|
|
|
|
|
if (res === 0)
|
|
|
|
|
throw new Error(
|
|
|
|
|
'No existe un equipo de cómputo con las características solicitadas. Intenta elegir otro tipo de equipo.'
|
|
|
|
|
);
|
|
|
|
|
return Equipo.count({
|
|
|
|
|
where: { ...whereEquipo, prestado: false },
|
|
|
|
|
include: [
|
|
|
|
|
{
|
|
|
|
|
model: Carrito,
|
|
|
|
|
where: { idTipoCarrito, idModulo, activo: true },
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.then(async (res) => {
|
|
|
|
|
if (res === 0)
|
|
|
|
|
await Equipo.update(
|
|
|
|
|
{ prestado: false },
|
|
|
|
|
{
|
|
|
|
|
where: { ...whereEquipo, prestado: true },
|
|
|
|
|
include: [
|
|
|
|
|
{
|
|
|
|
|
model: Carrito,
|
|
|
|
|
where: { idTipoCarrito, idModulo, activo: true },
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
return Equipo.findOne({
|
|
|
|
|
where: { ...whereEquipo, prestado: false },
|
|
|
|
|
include: [
|
|
|
|
|
{
|
|
|
|
|
model: Carrito,
|
|
|
|
|
where: { idTipoCarrito, idModulo, activo: true },
|
|
|
|
|
},
|
|
|
|
|
],
|
2021-04-28 01:21:22 -05:00
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.then((res) => {
|
|
|
|
|
if (!res)
|
|
|
|
|
throw new Error(
|
2021-09-17 16:57:38 -05:00
|
|
|
'No hay un equipo de cómputo disponible con las características solicitadas. Intenta más tarde o elige otro tipo de equipo.'
|
2021-04-28 01:21:22 -05:00
|
|
|
);
|
2021-04-29 01:44:51 -05:00
|
|
|
idEquipo = res.idEquipo;
|
2022-01-20 23:41:44 -06:00
|
|
|
return Equipo.update({ idStatus: 1 }, { where: { idEquipo } });
|
2021-04-28 01:21:22 -05:00
|
|
|
})
|
2022-02-01 09:48:07 -06:00
|
|
|
.then(() =>
|
2021-04-28 01:21:22 -05:00
|
|
|
Prestamo.create({
|
2021-06-29 09:55:18 -05:00
|
|
|
horaMaxRecoger: ahora.add(10, 'm'),
|
2021-04-28 01:21:22 -05:00
|
|
|
idUsuario,
|
2021-04-29 01:44:51 -05:00
|
|
|
idEquipo,
|
2021-04-28 01:21:22 -05:00
|
|
|
})
|
|
|
|
|
)
|
2022-02-22 15:16:46 -06:00
|
|
|
.then(() => {
|
2022-02-20 23:44:57 -06:00
|
|
|
io.getIO().emit('actualizar', { idUsuario });
|
2022-01-25 22:08:13 -06:00
|
|
|
return {
|
|
|
|
|
message:
|
2022-02-10 15:11:05 -06:00
|
|
|
'Haz solicitado un equipo de cómputo correctamente. Presentate al módulo señalado y muestra tu QR.',
|
2022-01-25 22:08:13 -06:00
|
|
|
};
|
|
|
|
|
});
|
2021-04-28 01:21:22 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = pedir;
|