101 lines
2.9 KiB
JavaScript
101 lines
2.9 KiB
JavaScript
const { Op } = require('sequelize');
|
|
const validar = require('../../helper/validar');
|
|
const dbPath = '../../db/tablas';
|
|
const Carrera = require(`${dbPath}/Carrera`);
|
|
const Carrito = require(`${dbPath}/Carrito`);
|
|
const Equipo = require(`${dbPath}/Equipo`);
|
|
const Modulo = require(`${dbPath}/Modulo`);
|
|
const Operador = require(`${dbPath}/Operador`);
|
|
const Prestamo = require(`${dbPath}/Prestamo`);
|
|
const TipoCarrito = require(`${dbPath}/TipoCarrito`);
|
|
const TipoUsuario = require(`${dbPath}/TipoUsuario`);
|
|
const Usuario = require(`${dbPath}/Usuario`);
|
|
|
|
const historial = async (body) => {
|
|
const pagina = validar.validarNumeroEntero(body.pagina, 'página', false);
|
|
const usuario = body.usuario ? validar.validarNumeroCuenta(body.usuario) : '';
|
|
const idPrestamo = body.idPrestamo
|
|
? validar.validarNumeroEntero(body.idPrestamo, 'id préstamo', true)
|
|
: '';
|
|
const numeroInventario = body.numeroInventario
|
|
? validar.validarAlfanumerico(
|
|
body.numeroInventario,
|
|
'numero de inventario',
|
|
true,
|
|
20
|
|
)
|
|
: '';
|
|
const idModulo = body.idModulo
|
|
? validar.validarNumeroEntero(body.idModulo, 'id módulo', true)
|
|
: null;
|
|
const idTipoCarrito = body.idTipoCarrito
|
|
? validar.validarNumeroEntero(body.idTipoCarrito, 'id tipo carrito', true)
|
|
: null;
|
|
let whereCarrito = [];
|
|
|
|
if (idTipoCarrito) whereCarrito.push({ idTipoCarrito });
|
|
if (idModulo) whereCarrito.push({ idModulo });
|
|
return Prestamo.findAndCountAll({
|
|
where: {
|
|
idPrestamo: { [Op.like]: `%${idPrestamo}%` },
|
|
[Op.or]: [
|
|
{ activo: true },
|
|
{ [Op.and]: [{ activo: false }, { horaEntrega: { [Op.not]: null } }] },
|
|
],
|
|
},
|
|
include: [
|
|
{
|
|
model: Equipo,
|
|
where: { numeroInventario: { [Op.like]: `%${numeroInventario}%` } },
|
|
include: [
|
|
{
|
|
model: Carrito,
|
|
where: { [Op.and]: whereCarrito },
|
|
include: [{ model: TipoCarrito }, { model: Modulo }],
|
|
attributes: ['idCarrito', 'sobrenombre'],
|
|
},
|
|
],
|
|
attributes: [
|
|
'idEquipo',
|
|
'numeroSerie',
|
|
'numeroInventario',
|
|
'sobrenombre',
|
|
'updatedAt',
|
|
],
|
|
},
|
|
{
|
|
model: Usuario,
|
|
where: { usuario: { [Op.like]: `%${usuario}%` } },
|
|
include: [{ model: TipoUsuario }, { model: Carrera }],
|
|
attributes: ['idUsuario', 'usuario', 'nombre'],
|
|
},
|
|
{
|
|
model: Operador,
|
|
as: 'OperadorEntrega',
|
|
attributes: ['idOperador', 'operador'],
|
|
},
|
|
{
|
|
model: Operador,
|
|
as: 'OperadorRegreso',
|
|
attributes: ['idOperador', 'operador'],
|
|
},
|
|
],
|
|
attributes: [
|
|
'idPrestamo',
|
|
'activo',
|
|
'horaMaxRecoger',
|
|
'horaInicio',
|
|
'horaFin',
|
|
'horaEntrega',
|
|
'canceladoUsuario',
|
|
'regresoInmediato',
|
|
'createdAt',
|
|
],
|
|
limit: 25,
|
|
offset: 25 * (pagina - 1),
|
|
order: [['createdAt', 'DESC']],
|
|
});
|
|
};
|
|
|
|
module.exports = historial;
|