commit antes de cambios de tomy multa

This commit is contained in:
xXpuma99Xx
2021-09-24 12:30:24 -05:00
parent 793a250ee3
commit f8766e199e
2 changed files with 77 additions and 1 deletions
@@ -0,0 +1,61 @@
const moment = require('moment');
const validar = require('../../helper/validar');
const dbPath = '../../db/tablas';
const Infraccion = require(`${dbPath}/Infraccion`);
const Multa = require(`${dbPath}/Multa`);
const Operador = require(`${dbPath}/Operador`);
const Prestamo = require(`${dbPath}/Prestamo`);
const Usuario = require(`${dbPath}/Usuario`);
const multar = async (body) => {
const idOperadorMulta = validar.validarId(body.idOperadorMulta);
const idInfraccion = validar.validarId(body.idInfraccion);
const idPrestamo = validar.validarId(body.idPrestamo);
const descripcion = validar.validarAlfanumerico(
body.descripcion,
'La descripción',
500
);
let prestamo = {};
let fechaFin = moment();
return Operador.findOne({ where: { idOperador: idOperadorMulta } })
.then((res) => {
if (!res) throw new Error('No existe esta 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');
else fechaFin.add(100, 'y');
return Prestamo.findOne({
where: { idPrestamo },
include: [{ model: Usuario }],
});
})
.then((res) => {
if (!res) throw new Error('No existe este prestamo.');
prestamo = res;
return Multa.findOne({ where: { idPrestamo } });
})
.then((res) => {
if (res)
throw new Error('A este prestamo ya se le ha asignado una multa.');
return Usuario.update(
{ multa: true },
{ where: { idUsuario: prestamo.Usuario.idUsuario } }
);
})
.then((res) =>
Multa.create({
descripcion,
fechaFin: fechaFin.format(),
idOperadorMulta,
idInfraccion,
idPrestamo,
})
)
.then((res) => ({ message: `Se levantó correctamente el reporte de multa.` }));
};
module.exports = multar;