Files
pcpuma_acatlan_api/server/controller/Prestamo/pedir.js
T
2022-01-20 23:41:44 -06:00

136 lines
4.1 KiB
JavaScript

const moment = require('moment');
const { validarNumeroEntero } = 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 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 pedir = async (body) => {
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);
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: {
idStatus: 3,
idPrograma,
},
include: [
{
model: Carrito,
where: { idTipoCarrito, idModulo, activo: true },
},
],
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({ idStatus: 1 }, { where: { idEquipo } });
})
.then((res) =>
Prestamo.create({
horaMaxRecoger: ahora.add(10, 'm'),
idUsuario,
idEquipo,
})
)
.then((res) =>
Prestamo.findOne({
where: { idPrestamo: res.idPrestamo },
include: [
{
model: Equipo,
include: [
{
model: Carrito,
include: [{ model: Modulo }, { model: TipoCarrito }],
attributes: ['idCarrito', 'sobrenombre'],
},
{ model: Status },
{ model: Programa },
],
attributes: [
'idEquipo',
'numeroSerie',
'numeroInventario',
'sobrenombre',
],
},
],
attributes: [
'idPrestamo',
'activo',
'horaMaxRecoger',
'horaInicio',
'horaFin',
'horaEntrega',
'createdAt',
],
})
);
};
module.exports = pedir;