Files
pcpuma_acatlan_api/server/controller/Prestamo/pedir.js
T

119 lines
4.0 KiB
JavaScript
Raw Normal View History

2021-04-28 01:21:22 -05:00
const moment = require('moment');
2021-05-18 22:40:30 -05:00
const { validarId } = require('../../helper/validar');
2021-04-29 01:44:51 -05:00
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`);
2021-04-28 01:21:22 -05:00
const pedir = async (body) => {
const ahora = moment();
2021-05-18 22:40:30 -05: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);
2021-04-29 01:44:51 -05:00
let idEquipo;
2021-04-28 01:21:22 -05:00
2021-05-23 11:32:22 -05:00
// Checar que sea en el horario establecido
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) => {
if (!res && idPrograma) {
2021-04-29 22:17:40 -05:00
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(
'Este usuario tiene una multa activa, no puede solicitar un equipo de computo.'
);
return Prestamo.findOne({ where: { idUsuario, activo: true } });
2021-04-28 01:21:22 -05:00
})
.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({
2021-04-29 01:44:51 -05:00
where: {
apartado: false,
2021-04-29 01:44:51 -05:00
idStatus: 1,
idPrograma: idPrograma ? idPrograma : null,
},
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-29 01:44:51 -05:00
include: [{ model: TipoCarrito }, { model: Modulo }],
2021-04-28 01:21:22 -05:00
},
2021-04-29 01:44:51 -05:00
{ model: Status },
{ model: Programa },
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;
return Equipo.update({ apartado: true }, { where: { idEquipo } });
2021-04-28 01:21:22 -05:00
})
.then((res) =>
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
})
)
2021-04-29 01:44:51 -05:00
.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;
2021-09-17 15:14:30 -05:00
delete res.dataValues.idOperadorCancelacion;
2021-04-29 01:44:51 -05:00
delete res.dataValues.idEquipo;
delete res.dataValues.Equipo.dataValues.idCarrito;
delete res.dataValues.Equipo.dataValues.idStatus;
delete res.dataValues.Equipo.dataValues.idPrograma;
2021-06-17 08:25:36 -05:00
delete res.dataValues.Equipo.dataValues.updatedAt;
2021-04-29 01:44:51 -05:00
delete res.dataValues.Equipo.dataValues.Carrito.dataValues.idTipoCarrito;
delete res.dataValues.Equipo.dataValues.Carrito.dataValues.idModulo;
2021-06-17 08:25:36 -05:00
delete res.dataValues.Equipo.dataValues.Carrito.dataValues.activo;
2021-04-29 01:44:51 -05:00
return res;
});
2021-04-28 01:21:22 -05:00
};
module.exports = pedir;