70 lines
2.3 KiB
JavaScript
70 lines
2.3 KiB
JavaScript
const moment = require('moment');
|
|
const io = require('../../socket');
|
|
const { validarNumeroEntero } = require('../../helper/validar');
|
|
const dbPath = '../../db/tablas';
|
|
const Equipo = require(`${dbPath}/Equipo`);
|
|
const Multa = require(`${dbPath}/Multa`);
|
|
const Operador = require(`${dbPath}/Operador`);
|
|
const Prestamo = require(`${dbPath}/Prestamo`);
|
|
const Usuario = require(`${dbPath}/Usuario`);
|
|
|
|
const regresar = async (body) => {
|
|
const ahora = moment();
|
|
const ahoraAux = moment();
|
|
const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id prestamo', true);
|
|
const idOperadorRegreso = validarNumeroEntero(
|
|
body.idOperadorRegreso,
|
|
'id operador regreso',
|
|
true
|
|
);
|
|
const update = { idOperadorRegreso };
|
|
let diferencia = null;
|
|
|
|
return Operador.findOne({ where: { idOperador: idOperadorRegreso } })
|
|
.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.');
|
|
if (!res.activo && res.idOperadorRegreso)
|
|
throw new Error('Este préstamo ya no esta activo.');
|
|
if (res.Equipo.idStatus === 1)
|
|
throw new Error('Aun no se ha entregado el equipo al usuario.');
|
|
if (!res.regresoInmediato) {
|
|
update.horaEntrega = ahora.format();
|
|
update.activo = false;
|
|
diferencia = parseInt((ahora - moment(res.horaFin)) / 60000);
|
|
if (diferencia >= 15)
|
|
await Multa.create({
|
|
idPrestamo,
|
|
idInfraccion: 1,
|
|
idOperadorMulta: 1,
|
|
descripcion: `Se paso por ${diferencia} minutos para entregar el equipo.`,
|
|
fechaFin: ahoraAux.add(parseInt(diferencia / 15) * 7, 'd').format(),
|
|
}).then((res) =>
|
|
Usuario.update(
|
|
{ multa: true },
|
|
{ where: { idUsuario: res.idUsuario } }
|
|
)
|
|
);
|
|
}
|
|
return Equipo.update(
|
|
{ idStatus: 4 },
|
|
{ where: { idEquipo: res.idEquipo } }
|
|
);
|
|
})
|
|
.then(() => Prestamo.update(update, { where: { idPrestamo } }))
|
|
.then(() => {
|
|
io.getIO().emit('actualizar');
|
|
return {
|
|
message: 'Se ha regresado correctamente el equipo de cómputo.',
|
|
};
|
|
});
|
|
};
|
|
|
|
module.exports = regresar;
|