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();
|
|
|
|
|
const horaMax = moment(`${ahora.format('YYYY-MM-DD')} 19: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)
|
|
|
|
|
: null;
|
|
|
|
|
const idModulo = idPrograma
|
|
|
|
|
? 1
|
|
|
|
|
: validarNumeroEntero(body.idModulo, 'id modulo', true);
|
2021-04-29 01:44:51 -05:00
|
|
|
let idEquipo;
|
2021-04-28 01:21:22 -05:00
|
|
|
|
2021-09-25 23:13:37 -05:00
|
|
|
// if (ahora > horaMax)
|
|
|
|
|
// throw new Error('Ya no se puede realizar un prestamo el día de hoy.');
|
|
|
|
|
// if (ahora < horaMin)
|
|
|
|
|
// throw new Error('Aun es muy temprano para realizar un prestamo.');
|
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) => {
|
|
|
|
|
if (!res) throw new Error('No existe este modulo.');
|
|
|
|
|
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.');
|
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(
|
2021-09-24 14:45:53 -05:00
|
|
|
'Tienes una multa vigente, no puedes solicitar un equipo de computo.'
|
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(
|
2021-09-24 14:45:53 -05:00
|
|
|
'Tienes un prestamo 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
|
|
|
}
|
2021-04-28 01:21:22 -05:00
|
|
|
return Equipo.findOne({
|
2021-04-29 01:44:51 -05:00
|
|
|
where: {
|
2022-01-27 13:12:38 -06:00
|
|
|
idStatus: 4,
|
2021-09-20 00:21:36 -05:00
|
|
|
idPrograma,
|
2021-04-29 01:44:51 -05:00
|
|
|
},
|
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
|
|
|
},
|
|
|
|
|
],
|
2021-04-29 01:44:51 -05:00
|
|
|
order: [['updatedAt']],
|
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-01-25 22:08:13 -06:00
|
|
|
.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.',
|
|
|
|
|
};
|
|
|
|
|
});
|
2021-04-28 01:21:22 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = pedir;
|