82 lines
2.7 KiB
JavaScript
82 lines
2.7 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 Operador = require(`${dbPath}/Operador`);
|
|
const Prestamo = require(`${dbPath}/Prestamo`);
|
|
const TipoCarrito = require(`${dbPath}/TipoCarrito`);
|
|
|
|
const entregar = async (body) => {
|
|
const ahora = moment();
|
|
const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id préstamo', true);
|
|
const idOperadorEntrega = validarNumeroEntero(
|
|
body.idOperadorEntrega,
|
|
'id operador entrega',
|
|
true
|
|
);
|
|
let update = {
|
|
idOperadorEntrega,
|
|
horaInicio: ahora.format(),
|
|
};
|
|
let idEquipo = null;
|
|
let idUsuario = null;
|
|
|
|
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 }],
|
|
});
|
|
})
|
|
.then(async (res) => {
|
|
if (!res) throw new Error('No existe este préstamo.');
|
|
idEquipo = res.idEquipo;
|
|
idUsuario = res.idUsuario;
|
|
if (!res.activo) throw new Error('Este préstamo ya no esta activo.');
|
|
if (res.Equipo.idStatus === 2)
|
|
throw new Error('Ya se entrego el equipo al usuario.');
|
|
if (moment(res.horaMaxRecoger) < ahora)
|
|
await Equipo.update({ idStatus: 4 }, { where: { idEquipo } })
|
|
.then(() =>
|
|
Prestamo.update({ activo: false }, { where: { idPrestamo } })
|
|
)
|
|
.then(() => {
|
|
io.getIO().emit('actualizar', { idUsuario });
|
|
throw new Error(
|
|
'Ya pasó el tiempo límite para recoger el equipo de cómputo.'
|
|
);
|
|
});
|
|
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();
|
|
return Equipo.update(
|
|
{ idStatus: 2, prestado: true },
|
|
{ where: { idEquipo } }
|
|
);
|
|
})
|
|
.then(() => Prestamo.update(update, { where: { idPrestamo } }))
|
|
.then(() =>
|
|
Equipo.findOne({
|
|
where: { idEquipo },
|
|
include: [
|
|
{
|
|
model: Carrito,
|
|
include: [{ model: TipoCarrito }, { model: Modulo }],
|
|
attributes: ['idCarrito', 'carrito'],
|
|
},
|
|
],
|
|
attributes: ['idEquipo', 'numeroSerie', 'numeroInventario', 'equipo'],
|
|
})
|
|
)
|
|
.then((res) => {
|
|
io.getIO().emit('actualizar', { idUsuario });
|
|
return res;
|
|
});
|
|
};
|
|
|
|
module.exports = entregar;
|