86 lines
2.3 KiB
JavaScript
86 lines
2.3 KiB
JavaScript
const { Op } = require('sequelize');
|
|
const { validarAlfanumerico } = 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 Operador = require(`${dbPath}/Operador`);
|
|
const Prestamo = require(`${dbPath}/Prestamo`);
|
|
const Programa = require(`${dbPath}/Programa`);
|
|
const Status = require(`${dbPath}/Status`);
|
|
const TipoCarrito = require(`${dbPath}/TipoCarrito`);
|
|
const TipoUsuario = require(`${dbPath}/TipoUsuario`);
|
|
const Usuario = require(`${dbPath}/Usuario`);
|
|
|
|
const prestamoNumeroInventario = async (body) => {
|
|
const numeroInventario = validarAlfanumerico(
|
|
body.numeroInventario,
|
|
'numero de inventario',
|
|
true,
|
|
20
|
|
);
|
|
|
|
return Prestamo.findOne({
|
|
where: {
|
|
[Op.or]: [
|
|
{ activo: true },
|
|
{
|
|
[Op.and]: [{ regresoInmediato: true }, { idOperadorRegreso: null }],
|
|
},
|
|
],
|
|
},
|
|
include: [
|
|
{
|
|
model: Equipo,
|
|
where: { numeroInventario },
|
|
include: [
|
|
{
|
|
model: Carrito,
|
|
include: [{ model: TipoCarrito }, { model: Modulo }],
|
|
attributes: ['idCarrito', 'carrito'],
|
|
},
|
|
{ model: Programa },
|
|
{ model: Status },
|
|
],
|
|
attributes: ['idEquipo', 'numeroSerie', 'numeroInventario', 'equipo'],
|
|
},
|
|
{
|
|
model: Usuario,
|
|
include: [{ model: TipoUsuario }, { model: Carrera }],
|
|
attributes: ['idUsuario', 'usuario', 'nombre'],
|
|
},
|
|
{
|
|
model: Operador,
|
|
as: 'OperadorEntrega',
|
|
attributes: ['idOperador', 'operador'],
|
|
},
|
|
{
|
|
model: Operador,
|
|
as: 'OperadorRegreso',
|
|
attributes: ['idOperador', 'operador'],
|
|
},
|
|
],
|
|
attributes: [
|
|
'idPrestamo',
|
|
'activo',
|
|
'horaMaxRecoger',
|
|
'horaInicio',
|
|
'horaFin',
|
|
'horaEntrega',
|
|
'canceladoUsuario',
|
|
'canceladoOperador',
|
|
'regresoInmediato',
|
|
'createdAt',
|
|
],
|
|
}).then((res) => {
|
|
if (!res)
|
|
throw new Error(
|
|
'No existe un préstamo activo con este número de inventario.'
|
|
);
|
|
return res;
|
|
});
|
|
};
|
|
|
|
module.exports = prestamoNumeroInventario;
|