119 lines
4.0 KiB
JavaScript
119 lines
4.0 KiB
JavaScript
const moment = require('moment');
|
|
const { validarId } = require('../../helper/validar');
|
|
const dbPath = '../../db/tablas';
|
|
const Prestamo = require(`${dbPath}/Prestamo`);
|
|
const Usuario = require(`${dbPath}/Usuario`);
|
|
const TipoCarrito = require(`${dbPath}/TipoCarrito`);
|
|
const Carrito = require(`${dbPath}/Carrito`);
|
|
const Equipo = require(`${dbPath}/Equipo`);
|
|
const Status = require(`${dbPath}/Status`);
|
|
const Programa = require(`${dbPath}/Programa`);
|
|
const Modulo = require(`${dbPath}/Modulo`);
|
|
|
|
const pedir = async (body) => {
|
|
const ahora = moment();
|
|
const idUsuario = validarId(body.idUsuario);
|
|
const idTipoCarrito = validarId(body.idTipoCarrito);
|
|
const idPrograma = body.idPrograma ? validarId(body.idPrograma) : null;
|
|
const idModulo = idPrograma ? 1 : validarId(body.idModulo);
|
|
let idEquipo;
|
|
|
|
// Checar que sea en el horario establecido
|
|
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) => {
|
|
if (!res && idPrograma) {
|
|
throw new Error('No existe este programa.');
|
|
}
|
|
return Usuario.findOne({ where: { idUsuario } });
|
|
})
|
|
.then((res) => {
|
|
if (!res || !res.password) throw new Error('No existe este usuario.');
|
|
if (!res.activo) throw new Error('Este usuario no esta activo.');
|
|
if (res.multa)
|
|
throw new Error(
|
|
'Este usuario tiene una multa activa, no puede solicitar un equipo de computo.'
|
|
);
|
|
return Prestamo.findOne({ where: { idUsuario, activo: true } });
|
|
})
|
|
.then((res) => {
|
|
if (res)
|
|
throw new Error(
|
|
'Este usuario ya cuenta con un prestamo activo. No sepuede solicitar otro equipo de computo.'
|
|
);
|
|
return Equipo.findOne({
|
|
where: {
|
|
apartado: false,
|
|
idStatus: 1,
|
|
idPrograma: idPrograma ? idPrograma : null,
|
|
},
|
|
include: [
|
|
{
|
|
model: Carrito,
|
|
where: { idTipoCarrito, idModulo, activo: true },
|
|
include: [{ model: TipoCarrito }, { model: Modulo }],
|
|
},
|
|
{ model: Status },
|
|
{ model: Programa },
|
|
],
|
|
order: [['updatedAt']],
|
|
});
|
|
})
|
|
.then((res) => {
|
|
if (!res)
|
|
throw new Error(
|
|
'No hay un equipo de cómputo disponible con las características solicitadas. Intenta más tarde o elige otro tipo de equipo.'
|
|
);
|
|
idEquipo = res.idEquipo;
|
|
return Equipo.update({ apartado: true }, { where: { idEquipo } });
|
|
})
|
|
.then((res) =>
|
|
Prestamo.create({
|
|
horaMaxRecoger: ahora.add(10, 'm'),
|
|
idUsuario,
|
|
idEquipo,
|
|
})
|
|
)
|
|
.then((res) => {
|
|
return Prestamo.findOne({
|
|
where: { idPrestamo: res.idPrestamo },
|
|
include: [
|
|
{
|
|
model: Equipo,
|
|
include: [
|
|
{
|
|
model: Carrito,
|
|
include: [{ model: Modulo }, { model: TipoCarrito }],
|
|
},
|
|
{ model: Programa },
|
|
],
|
|
},
|
|
],
|
|
});
|
|
})
|
|
.then((res) => {
|
|
delete res.dataValues.idUsuario;
|
|
delete res.dataValues.idOperadorEntrega;
|
|
delete res.dataValues.idOperadorRegreso;
|
|
delete res.dataValues.idOperadorCancelacion;
|
|
delete res.dataValues.idEquipo;
|
|
delete res.dataValues.Equipo.dataValues.idCarrito;
|
|
delete res.dataValues.Equipo.dataValues.idStatus;
|
|
delete res.dataValues.Equipo.dataValues.idPrograma;
|
|
delete res.dataValues.Equipo.dataValues.updatedAt;
|
|
delete res.dataValues.Equipo.dataValues.Carrito.dataValues.idTipoCarrito;
|
|
delete res.dataValues.Equipo.dataValues.Carrito.dataValues.idModulo;
|
|
delete res.dataValues.Equipo.dataValues.Carrito.dataValues.activo;
|
|
return res;
|
|
});
|
|
};
|
|
|
|
module.exports = pedir;
|