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

96 lines
2.9 KiB
JavaScript
Raw Normal View History

2021-05-26 16:39:07 -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: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
? validar.validarNumeroEntero(body.idPrestamo, 'id prestamo', true)
2022-02-01 09:48:07 -06:00
: null;
const idTipoCarrito = body.idTipoCarrito
? validar.validarNumeroEntero(body.idTipoCarrito, 'id tipo carrito', true)
: null;
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
)
: '';
let whereCarrito = [{ idModulo }];
2021-04-29 19:18:38 -05:00
2021-05-26 16:39:07 -05:00
if (idTipoCarrito) whereCarrito.push({ idTipoCarrito });
2022-01-24 15:58:34 -06:00
return Modulo.findOne({ where: { idModulo } }).then((res) => {
if (!res) throw new Error('No existe este modulo.');
return Prestamo.findAndCountAll({
2022-01-28 16:41:46 -06:00
where: {
2022-02-01 06:44:34 -06:00
[Op.or]: [
2022-02-01 09:48:07 -06:00
{ activo: true },
2022-02-01 06:44:34 -06:00
{
[Op.and]: [{ regresoInmediato: true }, { idOperadorRegreso: null }],
},
2022-02-01 10:20:22 -06:00
{ idPrestamo: { [Op.like]: `%${idPrestamo}%` } },
2022-02-01 06:44:34 -06:00
],
2022-01-28 16:41:46 -06:00
},
2022-01-24 15:58:34 -06:00
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',
2022-01-28 16:41:46 -06:00
'regresoInmediato',
2022-01-24 15:58:34 -06:00
'createdAt',
],
limit: 25,
offset: 25 * (pagina - 1),
order: [['createdAt', 'DESC']],
});
});
2021-04-28 23:58:55 -05:00
};
module.exports = activos;