Files

89 lines
2.9 KiB
JavaScript
Raw Permalink Normal View History

2021-04-30 16:04:27 -05:00
const moment = require('moment');
2022-01-25 22:08:13 -06:00
const io = require('../../socket');
2022-01-03 16:47:25 -06:00
const { validarNumeroEntero } = require('../../helper/validar');
2021-04-30 16:04:27 -05:00
const dbPath = '../../db/tablas';
2021-06-17 08:09:52 -05:00
const Carrito = require(`${dbPath}/Carrito`);
2021-04-30 16:04:27 -05:00
const Equipo = require(`${dbPath}/Equipo`);
2021-06-17 08:09:52 -05:00
const Modulo = require(`${dbPath}/Modulo`);
2021-04-30 16:04:27 -05:00
const Prestamo = require(`${dbPath}/Prestamo`);
2021-06-17 08:09:52 -05:00
const Programa = require(`${dbPath}/Programa`);
2022-01-20 23:41:44 -06:00
const Status = require(`${dbPath}/Status`);
2021-06-17 08:09:52 -05:00
const TipoCarrito = require(`${dbPath}/TipoCarrito`);
2021-04-30 16:04:27 -05:00
const status = async (body) => {
2022-01-25 22:08:13 -06:00
const ahora = moment();
2022-02-13 17:29:26 -06:00
const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id préstamo', true);
2022-01-20 23:41:44 -06:00
let message = '';
let atraso = false;
let idUsuario = null;
2021-04-30 16:04:27 -05:00
return Prestamo.findOne({
where: { idPrestamo },
2022-01-20 23:41:44 -06:00
include: [{ model: Equipo }],
})
.then(async (res) => {
2022-02-13 17:29:26 -06:00
if (!res) throw new Error('No existe este préstamo.');
idUsuario = res.idUsuario;
2022-01-20 23:41:44 -06:00
if (!res.activo) {
2022-02-13 17:29:26 -06:00
if (!res.horaEntrega) message = 'Este préstamo fue cancelado.';
else message = 'Este préstamo no es encuentra activo.';
2022-01-20 23:41:44 -06:00
} 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(
2022-01-27 13:12:38 -06:00
{ idStatus: 4 },
2022-01-20 23:41:44 -06:00
{ where: { idEquipo: res.Equipo.idEquipo } }
2022-02-01 09:48:07 -06:00
).then(() =>
2022-02-05 21:40:39 -06:00
Prestamo.update(
2022-02-15 13:09:25 -06:00
{ activo: false, canceladoOperador: true, idOperadorRegreso: 1 },
2022-02-05 21:40:39 -06:00
{ where: { idPrestamo } }
)
2022-01-20 23:41:44 -06:00
);
message =
'Ya paso el tiempo limite para recoger el dispositivo. Se ha cancelado tu prestamo.';
io.getIO().emit('actualizar', { idUsuario });
2022-02-13 17:29:26 -06:00
} else message = 'Aún no se entrega el equipo al usuario.';
2022-01-20 23:41:44 -06:00
return Prestamo.findOne({
where: { idPrestamo },
2021-06-17 08:09:52 -05:00
include: [
{
2022-01-20 23:41:44 -06:00
model: Equipo,
include: [
{
model: Carrito,
include: [{ model: TipoCarrito }, { model: Modulo }],
2022-02-13 12:33:38 -06:00
attributes: ['idCarrito', 'carrito'],
2022-01-20 23:41:44 -06:00
},
{ model: Status },
{ model: Programa },
],
attributes: [
'idEquipo',
'numeroSerie',
'numeroInventario',
2022-02-13 12:33:38 -06:00
'equipo',
2022-01-20 23:41:44 -06:00
],
2021-06-17 08:09:52 -05:00
},
],
2022-01-20 22:01:14 -06:00
attributes: [
2022-01-20 23:41:44 -06:00
'idPrestamo',
'activo',
'horaMaxRecoger',
'horaInicio',
'horaFin',
'horaEntrega',
'canceladoUsuario',
2022-01-28 16:41:46 -06:00
'regresoInmediato',
2022-01-20 23:41:44 -06:00
'createdAt',
2022-01-20 22:01:14 -06:00
],
2022-01-20 23:41:44 -06:00
});
})
.then((res) => ({ message, prestamo: res, atraso }));
2021-04-30 16:04:27 -05:00
};
module.exports = status;