130 lines
4.4 KiB
JavaScript
130 lines
4.4 KiB
JavaScript
const moment = require('moment');
|
|
const { validarId } = require('../../helper/validar');
|
|
const dbPath = '../../db/tablas';
|
|
const Carrito = require(`${dbPath}/Carrito`);
|
|
const Equipo = require(`${dbPath}/Equipo`);
|
|
const Modulo = require(`${dbPath}/Modulo`);
|
|
const Prestamo = require(`${dbPath}/Prestamo`);
|
|
const Programa = require(`${dbPath}/Programa`);
|
|
const Status = require(`${dbPath}/Status`);
|
|
const TipoCarrito = require(`${dbPath}/TipoCarrito`);
|
|
const Usuario = require(`${dbPath}/Usuario`);
|
|
|
|
const pedir = async (body) => {
|
|
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 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;
|
|
|
|
// 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.');
|
|
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(
|
|
'Tienes una multa vigente, no puedes solicitar un equipo de computo.'
|
|
);
|
|
return Prestamo.findOne({ where: { idUsuario, activo: true } });
|
|
})
|
|
.then((res) => {
|
|
if (res)
|
|
throw new Error(
|
|
'Tienes un prestamo activo. No puedes solicitar otro equipo de cómputo.'
|
|
);
|
|
return Equipo.findOne({
|
|
where: {
|
|
apartado: false,
|
|
idStatus: 1,
|
|
idPrograma,
|
|
},
|
|
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;
|