Files
pcpuma_acatlan_api/server/controller/Prestamo/regresoInmediato.js
T
2022-02-05 21:22:51 -06:00

59 lines
1.9 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 Prestamo = require(`${dbPath}/Prestamo`);
const Usuario = require(`${dbPath}/Usuario`);
const regresoInmediato = async (body) => {
const ahora = moment();
const ahoraAux = moment();
const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id prestamo', true);
let diferencia = null;
return Prestamo.findOne({
where: { idPrestamo },
include: [{ model: Equipo }],
})
.then(async (res) => {
if (!res) throw new Error('No existe este préstamo.');
if (!res.activo) 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.');
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: 3 },
{ where: { idEquipo: res.idEquipo } }
);
})
.then(() =>
Prestamo.update(
{ horaEntrega: ahora.format(), activo: false, regresoInmediato: true },
{ where: { idPrestamo } }
)
)
.then(() => {
io.getIO().emit('actualizar');
return {
message: 'Se ha regresado correctamente el equipo de cómputo.',
};
});
};
module.exports = regresoInmediato;