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

101 lines
2.9 KiB
JavaScript
Raw Normal View History

2021-05-26 17:01:23 -05:00
const { Op } = require('sequelize');
2021-04-28 23:58:55 -05:00
const validar = require('../../helper/validar');
const dbPath = '../../db/tablas';
2021-04-29 19:53:27 -05:00
const Carrera = require(`${dbPath}/Carrera`);
2021-04-28 23:58:55 -05:00
const Carrito = require(`${dbPath}/Carrito`);
2021-04-29 19:53:27 -05:00
const Equipo = require(`${dbPath}/Equipo`);
2021-04-28 23:58:55 -05:00
const Modulo = require(`${dbPath}/Modulo`);
2021-04-29 21:11:32 -05:00
const Operador = require(`${dbPath}/Operador`);
2021-04-29 19:53:27 -05:00
const Prestamo = require(`${dbPath}/Prestamo`);
2021-04-28 23:58:55 -05:00
const TipoCarrito = require(`${dbPath}/TipoCarrito`);
2021-04-29 19:53:27 -05:00
const TipoUsuario = require(`${dbPath}/TipoUsuario`);
const Usuario = require(`${dbPath}/Usuario`);
2021-04-28 23:58:55 -05:00
const historial = async (body) => {
2022-02-01 09:48:07 -06:00
const pagina = validar.validarNumeroEntero(body.pagina, 'página', false);
2021-05-26 17:01:23 -05:00
const usuario = body.usuario ? validar.validarNumeroCuenta(body.usuario) : '';
2022-01-03 16:47:25 -06:00
const idPrestamo = body.idPrestamo
2022-02-01 09:48:07 -06:00
? validar.validarNumeroEntero(body.idPrestamo, 'id préstamo', true)
2022-01-03 16:47:25 -06:00
: '';
2021-05-26 17:01:23 -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 17:01:23 -05:00
20
)
: '';
2022-01-03 16:47:25 -06:00
const idModulo = body.idModulo
? validar.validarNumeroEntero(body.idModulo, 'id módulo', true)
: null;
2021-05-26 17:01:23 -05:00
const idTipoCarrito = body.idTipoCarrito
2022-01-03 16:47:25 -06:00
? validar.validarNumeroEntero(body.idTipoCarrito, 'id tipo carrito', true)
2021-05-26 17:01:23 -05:00
: null;
let whereCarrito = [];
2021-04-29 21:11:32 -05:00
2021-05-26 17:01:23 -05:00
if (idTipoCarrito) whereCarrito.push({ idTipoCarrito });
if (idModulo) whereCarrito.push({ idModulo });
2021-04-29 21:11:32 -05:00
return Prestamo.findAndCountAll({
2021-06-23 13:54:22 -05:00
where: {
2021-09-17 00:32:14 -05:00
idPrestamo: { [Op.like]: `%${idPrestamo}%` },
2021-06-23 13:54:22 -05:00
[Op.or]: [
{ activo: true },
{ [Op.and]: [{ activo: false }, { horaEntrega: { [Op.not]: null } }] },
],
},
2021-04-28 23:58:55 -05:00
include: [
{
model: Equipo,
2021-05-26 17:01:23 -05:00
where: { numeroInventario: { [Op.like]: `%${numeroInventario}%` } },
2021-04-28 23:58:55 -05:00
include: [
{
model: Carrito,
2021-05-26 17:01:23 -05:00
where: { [Op.and]: whereCarrito },
2021-04-28 23:58:55 -05:00
include: [{ model: TipoCarrito }, { model: Modulo }],
2022-01-20 22:01:14 -06:00
attributes: ['idCarrito', 'sobrenombre'],
2021-04-28 23:58:55 -05:00
},
],
2022-01-20 22:01:14 -06:00
attributes: [
'idEquipo',
'numeroSerie',
'numeroInventario',
'sobrenombre',
'updatedAt',
],
2021-04-28 23:58:55 -05:00
},
2021-05-26 17:01:23 -05:00
{
model: Usuario,
where: { usuario: { [Op.like]: `%${usuario}%` } },
include: [{ model: TipoUsuario }, { model: Carrera }],
2022-01-20 22:01:14 -06:00
attributes: ['idUsuario', 'usuario', 'nombre'],
},
{
model: Operador,
as: 'OperadorEntrega',
attributes: ['idOperador', 'operador'],
2021-05-26 17:01:23 -05:00
},
2022-01-20 22:01:14 -06:00
{
model: Operador,
as: 'OperadorRegreso',
attributes: ['idOperador', 'operador'],
},
],
attributes: [
'idPrestamo',
'activo',
'horaMaxRecoger',
'horaInicio',
'horaFin',
'horaEntrega',
'canceladoUsuario',
2022-01-28 16:41:46 -06:00
'regresoInmediato',
2022-01-20 22:01:14 -06:00
'createdAt',
2021-04-28 23:58:55 -05:00
],
2021-04-29 21:11:32 -05:00
limit: 25,
offset: 25 * (pagina - 1),
2021-06-23 14:20:29 -05:00
order: [['createdAt', 'DESC']],
2022-01-24 15:58:34 -06:00
});
2021-04-28 23:58:55 -05:00
};
module.exports = historial;