Files

103 lines
3.2 KiB
JavaScript
Raw Permalink Normal View History

2022-02-15 13:09:25 -06:00
const { Op, where } = require('sequelize');
2021-04-28 23:58:55 -05:00
const validar = require('../../helper/validar');
const dbPath = '../../db/tablas';
2021-04-29 19:18:38 -05:00
const Carrera = require(`${dbPath}/Carrera`);
2021-04-28 23:58:55 -05:00
const Carrito = require(`${dbPath}/Carrito`);
2021-04-29 19:18:38 -05:00
const Equipo = require(`${dbPath}/Equipo`);
2021-04-28 23:58:55 -05:00
const Modulo = require(`${dbPath}/Modulo`);
2021-04-29 19:18:38 -05:00
const Prestamo = require(`${dbPath}/Prestamo`);
2022-01-21 00:32:51 -06:00
const Status = require(`${dbPath}/Status`);
2021-04-28 23:58:55 -05:00
const TipoCarrito = require(`${dbPath}/TipoCarrito`);
2021-04-29 19:18:38 -05:00
const TipoUsuario = require(`${dbPath}/TipoUsuario`);
const Usuario = require(`${dbPath}/Usuario`);
2021-04-28 23:58:55 -05:00
const activos = async (body) => {
2022-02-01 09:48:07 -06:00
const pagina = validar.validarNumeroEntero(body.pagina, 'página', false);
2022-01-03 16:47:25 -06:00
const idModulo = validar.validarNumeroEntero(
body.idModulo,
'id modulo',
true
);
const idPrestamo = body.idPrestamo
2022-02-13 17:29:26 -06:00
? validar.validarNumeroEntero(body.idPrestamo, 'id préstamo', true)
2022-02-13 19:10:21 -06:00
: '';
2022-02-01 09:48:07 -06:00
const idTipoCarrito = body.idTipoCarrito
? validar.validarNumeroEntero(body.idTipoCarrito, 'id tipo carrito', true)
2022-02-13 19:10:21 -06:00
: '';
2022-02-01 09:48:07 -06:00
const usuario = body.usuario ? validar.validarNumeroCuenta(body.usuario) : '';
2021-05-26 16:39:07 -05:00
const numeroInventario = body.numeroInventario
? validar.validarAlfanumerico(
body.numeroInventario,
2022-01-03 16:56:00 -06:00
'numero de inventario',
true,
2021-05-26 16:39:07 -05:00
20
)
: '';
2022-02-15 13:09:25 -06:00
const carrito = body.carrito
? validar.validarAlfanumerico(body.carrito, 'carrito', true)
: '';
const equipo = body.equipo
? validar.validarAlfanumerico(body.equipo, 'equipo', true)
: '';
2022-02-13 19:10:21 -06:00
let wherePrestamo = {
[Op.or]: [
{ activo: true },
{
[Op.and]: [{ regresoInmediato: true }, { idOperadorRegreso: null }],
},
],
};
2021-05-26 16:39:07 -05:00
let whereCarrito = [{ idModulo }];
2022-02-15 13:09:25 -06:00
let whereEquipo = {};
2021-04-29 19:18:38 -05:00
2022-02-15 13:09:25 -06:00
if (equipo) whereEquipo.equipo = equipo;
if (numeroInventario)
whereEquipo.numeroInventario = { [Op.like]: `%${numeroInventario}%` };
if (carrito) whereCarrito.push({ carrito });
2021-05-26 16:39:07 -05:00
if (idTipoCarrito) whereCarrito.push({ idTipoCarrito });
2022-02-13 19:10:21 -06:00
if (idPrestamo) wherePrestamo.idPrestamo = { [Op.like]: `%${idPrestamo}%` };
2022-01-24 15:58:34 -06:00
return Modulo.findOne({ where: { idModulo } }).then((res) => {
2022-02-13 17:29:26 -06:00
if (!res) throw new Error('No existe este módulo.');
2022-01-24 15:58:34 -06:00
return Prestamo.findAndCountAll({
2022-02-13 19:10:21 -06:00
where: wherePrestamo,
2022-01-24 15:58:34 -06:00
include: [
{
model: Equipo,
2022-02-15 13:09:25 -06:00
where: whereEquipo,
2022-01-24 15:58:34 -06:00
include: [
{
model: Carrito,
where: { [Op.and]: whereCarrito },
include: [{ model: TipoCarrito }, { model: Modulo }],
2022-02-13 12:33:38 -06:00
attributes: ['idCarrito', 'carrito'],
2022-01-24 15:58:34 -06:00
},
{ model: Status },
],
2022-02-13 12:33:38 -06:00
attributes: ['idEquipo', 'numeroSerie', 'numeroInventario', 'equipo'],
2022-01-24 15:58:34 -06:00
},
{
model: Usuario,
where: { usuario: { [Op.like]: `%${usuario}%` } },
include: [{ model: TipoUsuario }, { model: Carrera }],
attributes: ['idUsuario', 'usuario', 'nombre'],
},
],
attributes: [
'idPrestamo',
'activo',
'horaMaxRecoger',
'horaInicio',
'horaFin',
'horaEntrega',
2022-01-28 16:41:46 -06:00
'regresoInmediato',
2022-01-24 15:58:34 -06:00
'createdAt',
],
limit: 25,
offset: 25 * (pagina - 1),
2022-02-14 15:42:53 -06:00
order: [['idPrestamo', 'ASC']],
2022-01-24 15:58:34 -06:00
});
});
2021-04-28 23:58:55 -05:00
};
module.exports = activos;