96 lines
2.9 KiB
JavaScript
96 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 Prestamo = require(`${dbPath}/Prestamo`);
|
|
const Status = require(`${dbPath}/Status`);
|
|
const TipoCarrito = require(`${dbPath}/TipoCarrito`);
|
|
const TipoUsuario = require(`${dbPath}/TipoUsuario`);
|
|
const Usuario = require(`${dbPath}/Usuario`);
|
|
|
|
const activos = async (body) => {
|
|
const pagina = validar.validarNumeroEntero(body.pagina, 'página', false);
|
|
const idModulo = validar.validarNumeroEntero(
|
|
body.idModulo,
|
|
'id modulo',
|
|
true
|
|
);
|
|
const idPrestamo = body.idPrestamo
|
|
? validar.validarNumeroEntero(body.idPrestamo, 'id prestamo', true)
|
|
: null;
|
|
const idTipoCarrito = body.idTipoCarrito
|
|
? validar.validarNumeroEntero(body.idTipoCarrito, 'id tipo carrito', true)
|
|
: null;
|
|
const usuario = body.usuario ? validar.validarNumeroCuenta(body.usuario) : '';
|
|
const numeroInventario = body.numeroInventario
|
|
? validar.validarAlfanumerico(
|
|
body.numeroInventario,
|
|
'numero de inventario',
|
|
true,
|
|
20
|
|
)
|
|
: '';
|
|
let whereCarrito = [{ idModulo }];
|
|
|
|
if (idTipoCarrito) whereCarrito.push({ idTipoCarrito });
|
|
return Modulo.findOne({ where: { idModulo } }).then((res) => {
|
|
if (!res) throw new Error('No existe este modulo.');
|
|
return Prestamo.findAndCountAll({
|
|
where: {
|
|
[Op.or]: [
|
|
{ activo: true },
|
|
{
|
|
[Op.and]: [{ regresoInmediato: true }, { idOperadorRegreso: null }],
|
|
},
|
|
{ idPrestamo: { [Op.like]: `%${idPrestamo}%` } },
|
|
],
|
|
},
|
|
include: [
|
|
{
|
|
model: Equipo,
|
|
where: { numeroInventario: { [Op.like]: `%${numeroInventario}%` } },
|
|
include: [
|
|
{
|
|
model: Carrito,
|
|
where: { [Op.and]: whereCarrito },
|
|
include: [{ model: TipoCarrito }, { model: Modulo }],
|
|
attributes: ['idCarrito', 'sobrenombre'],
|
|
},
|
|
{ model: Status },
|
|
],
|
|
attributes: [
|
|
'idEquipo',
|
|
'numeroSerie',
|
|
'numeroInventario',
|
|
'sobrenombre',
|
|
],
|
|
},
|
|
{
|
|
model: Usuario,
|
|
where: { usuario: { [Op.like]: `%${usuario}%` } },
|
|
include: [{ model: TipoUsuario }, { model: Carrera }],
|
|
attributes: ['idUsuario', 'usuario', 'nombre'],
|
|
},
|
|
],
|
|
attributes: [
|
|
'idPrestamo',
|
|
'activo',
|
|
'horaMaxRecoger',
|
|
'horaInicio',
|
|
'horaFin',
|
|
'horaEntrega',
|
|
'regresoInmediato',
|
|
'createdAt',
|
|
],
|
|
limit: 25,
|
|
offset: 25 * (pagina - 1),
|
|
order: [['createdAt', 'DESC']],
|
|
});
|
|
});
|
|
};
|
|
|
|
module.exports = activos;
|