67 lines
1.8 KiB
JavaScript
67 lines
1.8 KiB
JavaScript
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.validarNumeroEntero(
|
|
body.idOperadorMulta,
|
|
'id operador multa',
|
|
true
|
|
);
|
|
const idInfraccion = validar.validarNumeroEntero(
|
|
body.idInfraccion,
|
|
'id infracción',
|
|
true
|
|
);
|
|
const idPrestamo = validar.validarNumeroEntero(
|
|
body.idPrestamo,
|
|
'id préstamo',
|
|
true
|
|
);
|
|
const descripcion = validar.validarAlfanumerico(
|
|
body.descripcion,
|
|
'descripción',
|
|
false,
|
|
500
|
|
);
|
|
let fechaFin = moment();
|
|
|
|
return Operador.findOne({ where: { idOperador: idOperadorMulta } })
|
|
.then((res) => {
|
|
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');
|
|
else fechaFin.add(100, 'y');
|
|
return Prestamo.findOne({ where: { idPrestamo } });
|
|
})
|
|
.then((res) => {
|
|
if (!res) throw new Error('No existe este préstamo.');
|
|
return Usuario.update(
|
|
{ multa: true },
|
|
{ where: { idUsuario: res.idUsuario } }
|
|
);
|
|
})
|
|
.then(() =>
|
|
Multa.create({
|
|
idPrestamo,
|
|
idOperadorMulta,
|
|
idInfraccion,
|
|
descripcion,
|
|
fechaFin: fechaFin.format('YYYY-MM-DD'),
|
|
})
|
|
)
|
|
.then(() => ({
|
|
message: `Se levantó correctamente el reporte de multa.`,
|
|
}));
|
|
};
|
|
|
|
module.exports = multar;
|