Files

82 lines
2.7 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-29 02:54:25 -05:00
const dbPath = '../../db/tablas';
2021-04-29 22:17:40 -05:00
const Carrito = require(`${dbPath}/Carrito`);
2021-04-29 02:54:25 -05:00
const Equipo = require(`${dbPath}/Equipo`);
2021-04-29 22:17:40 -05:00
const Modulo = require(`${dbPath}/Modulo`);
2021-04-29 02:54:25 -05:00
const Operador = require(`${dbPath}/Operador`);
2021-04-29 22:17:40 -05:00
const Prestamo = require(`${dbPath}/Prestamo`);
const TipoCarrito = require(`${dbPath}/TipoCarrito`);
2021-04-29 02:54:25 -05:00
2021-05-31 16:01:12 -05:00
const entregar = 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-03 16:47:25 -06:00
const idOperadorEntrega = validarNumeroEntero(
body.idOperadorEntrega,
'id operador entrega',
true
);
2022-02-14 19:19:51 -06:00
let update = {
idOperadorEntrega,
horaInicio: ahora.format(),
};
let idEquipo = null;
let idUsuario = null;
2021-04-29 02:54:25 -05:00
return Operador.findOne({ where: { idOperador: idOperadorEntrega } })
.then((res) => {
if (!res) throw new Error('No existe este operador.');
return Prestamo.findOne({
where: { idPrestamo },
include: [{ model: Equipo }],
});
})
2021-04-30 16:04:27 -05:00
.then(async (res) => {
2022-02-13 17:29:26 -06:00
if (!res) throw new Error('No existe este préstamo.');
idEquipo = res.idEquipo;
idUsuario = res.idUsuario;
2022-02-13 17:29:26 -06:00
if (!res.activo) throw new Error('Este préstamo ya no esta activo.');
if (res.Equipo.idStatus === 2)
2021-04-29 21:22:57 -05:00
throw new Error('Ya se entrego el equipo al usuario.');
2022-02-13 17:29:26 -06:00
if (moment(res.horaMaxRecoger) < ahora)
2022-01-27 13:12:38 -06:00
await Equipo.update({ idStatus: 4 }, { where: { idEquipo } })
2022-02-01 09:48:07 -06:00
.then(() =>
2022-01-20 23:41:44 -06:00
Prestamo.update({ activo: false }, { where: { idPrestamo } })
)
2022-02-01 09:48:07 -06:00
.then(() => {
io.getIO().emit('actualizar', { idUsuario });
2022-01-20 23:41:44 -06:00
throw new Error(
2022-02-13 17:29:26 -06:00
'Ya pasó el tiempo límite para recoger el equipo de cómputo.'
2022-01-20 23:41:44 -06:00
);
});
2022-03-07 09:13:25 -06:00
if (ahora > moment(`${ahora.format('YYYY-MM-DD')} 17:45`))
update.horaFin = moment(`${ahora.format('YYYY-MM-DD')} 19:45`);
else update.horaFin = ahora.add(2, 'h').format();
2022-02-22 15:16:46 -06:00
return Equipo.update(
{ idStatus: 2, prestado: true },
{ where: { idEquipo } }
);
2021-04-29 02:54:25 -05:00
})
2022-02-14 19:19:51 -06:00
.then(() => Prestamo.update(update, { where: { idPrestamo } }))
2022-02-01 09:48:07 -06:00
.then(() =>
2022-01-20 22:01:14 -06:00
Equipo.findOne({
2021-04-29 22:17:40 -05:00
where: { idEquipo },
include: [
{
model: Carrito,
include: [{ model: TipoCarrito }, { model: Modulo }],
2022-02-13 12:33:38 -06:00
attributes: ['idCarrito', 'carrito'],
2021-04-29 22:17:40 -05:00
},
],
2022-02-13 12:33:38 -06:00
attributes: ['idEquipo', 'numeroSerie', 'numeroInventario', 'equipo'],
2022-01-20 22:01:14 -06:00
})
2022-01-25 22:08:13 -06:00
)
.then((res) => {
io.getIO().emit('actualizar', { idUsuario });
2022-01-25 22:08:13 -06:00
return res;
});
2021-04-29 02:54:25 -05:00
};
2021-05-31 16:01:12 -05:00
module.exports = entregar;