mejora código

This commit is contained in:
xXpuma99Xx
2022-02-01 06:44:34 -06:00
parent b3851fd6a1
commit 4437ade10c
10 changed files with 90 additions and 75 deletions
@@ -1,4 +1,5 @@
const moment = require('moment');
const { Op } = require('sequelize');
const validar = require('../../helper/validar');
const dbPath = '../../db/tablas';
const Infraccion = require(`${dbPath}/Infraccion`);
@@ -21,7 +22,7 @@ const multarNumeroInventario = async (body) => {
);
const numeroInventario = validar.validarAlfanumerico(
body.numeroInventario,
'El numero de inventario',
'mero de inventario',
20
);
const descripcion = validar.validarAlfanumerico(
@@ -30,17 +31,20 @@ const multarNumeroInventario = async (body) => {
false,
500
);
const fechaFin = moment();
let fechaFin = moment();
let prestamo = {};
let gravedad = '';
return Operador.findOne({ where: { idOperador: idOperadorMulta } })
.then((res) => {
if (!res) throw new Error('No existe esta este operador.');
if (!res) throw new Error('No existe este operador.');
return Infraccion.findOne({ where: { idInfraccion } });
})
.then((res) => {
if (!res) throw new Error('No existe esta infracción.');
if (res.gravedad == '1') fechaFin.add(7, 'd');
// Checar si tiene una multa activa, agragar días a multa
gravedad = res.gravedad;
if (gravedad === '1') fechaFin.add(7, 'd');
else fechaFin.add(100, 'y');
return Prestamo.findOne({
where: { activo: true },
@@ -54,26 +58,33 @@ const multarNumeroInventario = async (body) => {
if (!res)
throw new Error('No existe un prestamo activo con este equipo.');
prestamo = res;
return Multa.findOne({ where: { idPrestamo: prestamo.idPrestamo } });
return Multa.findOne({
where: {
idPrestamo: prestamo.idPrestamo,
idInfraccion: { [Op.ne]: 1 },
},
});
})
.then((res) => {
if (res)
throw new Error('A este prestamo ya se le ha asignado una multa.');
throw new Error(
'Este préstamo ya ha sido usado para multar a un usuario.'
);
return Usuario.update(
{ multa: true },
{ where: { idUsuario: prestamo.Usuario.idUsuario } }
{ where: { idUsuario: prestamo.idUsuario } }
);
})
.then((res) =>
.then(() =>
Multa.create({
descripcion,
fechaFin: fechaFin.format('YYYY-MM-DD'),
idOperadorMulta,
idInfraccion,
idPrestamo: prestamo.idPrestamo,
fechaFin: fechaFin.format(),
})
)
.then((res) => ({
.then(() => ({
message: `Se levantó correctamente el reporte de multa.`,
}));
};