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

113 lines
4.1 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-01-03 17:05:29 -06:00
const pagina = validar.validarNumero(
body.pagina,
'página',
false,
null,
true,
true
);
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
? validar.validarNumeroEntero(body.idPrestamo, 'id prestamo', true)
: '';
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 }],
},
],
},
2021-05-26 17:01:23 -05:00
{
model: Usuario,
where: { usuario: { [Op.like]: `%${usuario}%` } },
include: [{ model: TipoUsuario }, { model: Carrera }],
},
2021-04-29 21:11:32 -05:00
{ model: Operador, as: 'OperadorRegreso' },
{ model: Operador, as: 'OperadorEntrega' },
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']],
2021-04-28 23:58:55 -05:00
}).then((res) => {
2021-04-29 21:11:32 -05:00
for (let i = 0; i < res.rows.length; i++) {
delete res.rows[i].dataValues.idUsuario;
delete res.rows[i].dataValues.idOperadorEntrega;
delete res.rows[i].dataValues.idOperadorRegreso;
delete res.rows[i].dataValues.idEquipo;
2021-09-17 15:14:30 -05:00
delete res.rows[i].dataValues.idOperadorCancelacion;
2021-04-29 21:11:32 -05:00
delete res.rows[i].dataValues.Equipo.dataValues.idPrograma;
delete res.rows[i].dataValues.Equipo.dataValues.idStatus;
delete res.rows[i].dataValues.Equipo.dataValues.idCarrito;
2021-06-17 08:25:36 -05:00
delete res.rows[i].dataValues.Equipo.dataValues.updatedAt;
2021-04-30 16:04:27 -05:00
delete res.rows[i].dataValues.Equipo.dataValues.Carrito.dataValues
.idTipoCarrito;
delete res.rows[i].dataValues.Equipo.dataValues.Carrito.dataValues
.idModulo;
2021-05-26 17:01:23 -05:00
delete res.rows[i].dataValues.Equipo.dataValues.Carrito.dataValues.activo;
2021-04-29 21:11:32 -05:00
delete res.rows[i].dataValues.Usuario.dataValues.idTipoUsuario;
delete res.rows[i].dataValues.Usuario.dataValues.idCarrera;
delete res.rows[i].dataValues.Usuario.dataValues.password;
2021-05-26 17:01:23 -05:00
delete res.rows[i].dataValues.Usuario.dataValues.activo;
delete res.rows[i].dataValues.Usuario.dataValues.multa;
2021-05-23 21:14:20 -05:00
if (res.rows[i].OperadorEntrega) {
2021-05-23 21:10:05 -05:00
delete res.rows[i].dataValues.OperadorEntrega.dataValues.password;
2021-05-26 17:01:23 -05:00
delete res.rows[i].dataValues.OperadorEntrega.dataValues.activo;
delete res.rows[i].dataValues.OperadorEntrega.dataValues.idTipoUsuario;
2021-05-23 21:10:05 -05:00
}
2021-05-23 21:14:20 -05:00
if (res.rows[i].OperadorRegreso) {
2021-05-23 21:10:05 -05:00
delete res.rows[i].dataValues.OperadorRegreso.dataValues.password;
2021-05-26 17:01:23 -05:00
delete res.rows[i].dataValues.OperadorRegreso.dataValues.activo;
delete res.rows[i].dataValues.OperadorRegreso.dataValues.idTipoUsuario;
2021-05-23 21:10:05 -05:00
}
2021-04-28 23:58:55 -05:00
}
2021-05-04 00:32:04 -05:00
return { count: res.count, historial: res.rows };
2021-04-28 23:58:55 -05:00
});
};
module.exports = historial;