89 lines
2.9 KiB
JavaScript
89 lines
2.9 KiB
JavaScript
const moment = require('moment');
|
|
const io = require('../../socket');
|
|
const { validarNumeroEntero } = require('../../helper/validar');
|
|
const dbPath = '../../db/tablas';
|
|
const Carrito = require(`${dbPath}/Carrito`);
|
|
const Equipo = require(`${dbPath}/Equipo`);
|
|
const Modulo = require(`${dbPath}/Modulo`);
|
|
const Prestamo = require(`${dbPath}/Prestamo`);
|
|
const Programa = require(`${dbPath}/Programa`);
|
|
const Status = require(`${dbPath}/Status`);
|
|
const TipoCarrito = require(`${dbPath}/TipoCarrito`);
|
|
|
|
const status = async (body) => {
|
|
const ahora = moment();
|
|
const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id préstamo', true);
|
|
let message = '';
|
|
let atraso = false;
|
|
let idUsuario = null;
|
|
|
|
return Prestamo.findOne({
|
|
where: { idPrestamo },
|
|
include: [{ model: Equipo }],
|
|
})
|
|
.then(async (res) => {
|
|
if (!res) throw new Error('No existe este préstamo.');
|
|
idUsuario = res.idUsuario;
|
|
if (!res.activo) {
|
|
if (!res.horaEntrega) message = 'Este préstamo fue cancelado.';
|
|
else message = 'Este préstamo no es encuentra activo.';
|
|
} else if (res.Equipo.idStatus === 2) {
|
|
if (moment(res.horaFin) < ahora) {
|
|
message =
|
|
'Ya paso la hora de entrega. El usuario puede ser acreedor a una multa por el retraso.';
|
|
atraso = true;
|
|
} else message = 'El equipo ya fue entregado al usuario.';
|
|
} else if (moment(res.horaMaxRecoger) < ahora) {
|
|
await Equipo.update(
|
|
{ idStatus: 4 },
|
|
{ where: { idEquipo: res.Equipo.idEquipo } }
|
|
).then(() =>
|
|
Prestamo.update(
|
|
{ activo: false, canceladoOperador: true, idOperadorRegreso: 1 },
|
|
{ where: { idPrestamo } }
|
|
)
|
|
);
|
|
message =
|
|
'Ya paso el tiempo limite para recoger el dispositivo. Se ha cancelado tu prestamo.';
|
|
io.getIO().emit('actualizar', { idUsuario });
|
|
} else message = 'Aún no se entrega el equipo al usuario.';
|
|
return Prestamo.findOne({
|
|
where: { idPrestamo },
|
|
include: [
|
|
{
|
|
model: Equipo,
|
|
include: [
|
|
{
|
|
model: Carrito,
|
|
include: [{ model: TipoCarrito }, { model: Modulo }],
|
|
attributes: ['idCarrito', 'carrito'],
|
|
},
|
|
{ model: Status },
|
|
{ model: Programa },
|
|
],
|
|
attributes: [
|
|
'idEquipo',
|
|
'numeroSerie',
|
|
'numeroInventario',
|
|
'equipo',
|
|
],
|
|
},
|
|
],
|
|
attributes: [
|
|
'idPrestamo',
|
|
'activo',
|
|
'horaMaxRecoger',
|
|
'horaInicio',
|
|
'horaFin',
|
|
'horaEntrega',
|
|
'canceladoUsuario',
|
|
'regresoInmediato',
|
|
'createdAt',
|
|
],
|
|
});
|
|
})
|
|
.then((res) => ({ message, prestamo: res, atraso }));
|
|
};
|
|
|
|
module.exports = status;
|