Files
pcpuma_acatlan_api/server/controller/Prestamo/regresar.js
T

65 lines
2.1 KiB
JavaScript
Raw Normal View History

2021-04-29 02:54:25 -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';
const Equipo = require(`${dbPath}/Equipo`);
2021-09-24 14:09:25 -05:00
const Multa = require(`${dbPath}/Multa`);
2021-04-29 02:54:25 -05:00
const Operador = require(`${dbPath}/Operador`);
2021-09-20 00:21:36 -05:00
const Prestamo = require(`${dbPath}/Prestamo`);
2021-04-29 02:54:25 -05:00
2021-05-04 00:32:04 -05:00
const regresar = async (body) => {
2022-01-25 22:08:13 -06:00
const ahora = moment();
const ahoraAux = moment();
2022-01-03 16:47:25 -06:00
const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id prestamo', true);
const idOperadorRegreso = validarNumeroEntero(
body.idOperadorRegreso,
'id operador regreso',
true
);
2021-09-24 14:09:25 -05:00
let horaFin = moment();
let diferencia = null;
2021-04-29 02:54:25 -05:00
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) throw new Error('Este préstamo ya no esta activo.');
2022-01-20 23:41:44 -06:00
if (!res.Equipo.idStatus === 1)
2021-04-29 21:22:57 -05:00
throw new Error('Aun no se ha entregado el equipo al usuario.');
2021-09-24 14:09:25 -05:00
horaFin = moment(res.horaFin);
diferencia = (ahora - horaFin) / 60000;
2022-01-20 23:41:44 -06:00
if (diferencia >= 15)
2021-09-24 14:09:25 -05:00
await Multa.create({
idPrestamo,
idInfraccion: 1,
2021-09-24 14:09:25 -05:00
idOperadorMulta: idOperadorRegreso,
descripcion: `Se paso por ${diferencia} minutos para entregar el equipo.`,
2022-01-20 23:41:44 -06:00
fechaFin: ahoraAux.add(parseInt(diferencia / 15) * 7, 'd').format(),
});
2021-04-29 02:54:25 -05:00
return Equipo.update(
2022-01-27 13:12:38 -06:00
{ idStatus: 4 },
2021-04-29 02:54:25 -05:00
{ where: { idEquipo: res.Equipo.idEquipo } }
);
})
2021-09-24 14:09:25 -05:00
.then((res) =>
Prestamo.update(
2021-04-29 02:54:25 -05:00
{ idOperadorRegreso, horaEntrega: ahora.format(), activo: false },
{ where: { idPrestamo } }
2021-09-24 14:09:25 -05:00
)
)
2022-01-25 22:08:13 -06:00
.then((res) => {
io.getIO().emit('actualizar');
return {
message: 'Se ha regresado correctamente el equipo de cómputo.',
};
});
2021-04-29 02:54:25 -05:00
};
2021-05-04 00:32:04 -05:00
module.exports = regresar;