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

70 lines
2.3 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`);
2022-01-28 16:41:46 -06:00
const Usuario = require(`${dbPath}/Usuario`);
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
);
2022-02-05 21:22:51 -06:00
const update = { idOperadorRegreso };
2021-09-24 14:09:25 -05:00
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.');
2022-01-28 16:41:46 -06:00
if (!res.activo && res.idOperadorRegreso)
throw new Error('Este préstamo ya no esta activo.');
2022-02-03 12:04:14 -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.');
2022-02-05 21:22:51 -06:00
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 } }
)
);
}
2021-04-29 02:54:25 -05:00
return Equipo.update(
2022-01-27 13:12:38 -06:00
{ idStatus: 4 },
2022-02-01 09:48:07 -06:00
{ where: { idEquipo: res.idEquipo } }
2021-04-29 02:54:25 -05:00
);
})
2022-02-05 21:22:51 -06:00
.then(() => Prestamo.update(update, { where: { idPrestamo } }))
2022-02-01 09:48:07 -06:00
.then(() => {
2022-01-25 22:08:13 -06:00
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;