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

94 lines
2.7 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-01-03 16:47:25 -06:00
const idModulo = validar.validarNumeroEntero(
body.idModulo,
'id modulo',
true
);
2022-01-03 17:05:29 -06:00
const pagina = validar.validarNumero(
body.pagina,
'página',
false,
null,
true,
true
);
2021-05-26 16:39:07 -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 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
)
: '';
const idTipoCarrito = body.idTipoCarrito
2022-01-03 16:47:25 -06:00
? validar.validarNumeroEntero(body.idTipoCarrito, 'id tipo carrito', true)
2021-05-26 16:39:07 -05:00
: null;
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({
where: { activo: true, 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',
'createdAt',
],
limit: 25,
offset: 25 * (pagina - 1),
order: [['createdAt', 'DESC']],
});
});
2021-04-28 23:58:55 -05:00
};
module.exports = activos;