Files
pcpuma_acatlan_api/server/controller/Multa/multarIdPrestamo.js
T

67 lines
1.8 KiB
JavaScript
Raw Normal View History

2021-09-24 12:30:24 -05:00
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) => {
2022-01-03 16:47:25 -06:00
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,
2022-02-01 06:44:34 -06:00
'id préstamo',
2022-01-03 16:47:25 -06:00
true
);
2021-09-24 12:30:24 -05:00
const descripcion = validar.validarAlfanumerico(
body.descripcion,
2022-01-03 16:56:00 -06:00
'descripción',
false,
2021-09-24 12:30:24 -05:00
500
);
2022-02-01 06:44:34 -06:00
let fechaFin = moment();
2021-09-24 12:30:24 -05:00
return Operador.findOne({ where: { idOperador: idOperadorMulta } })
.then((res) => {
2021-09-24 14:09:25 -05:00
if (!res) throw new Error('No existe este operador.');
2021-09-24 12:30:24 -05:00
return Infraccion.findOne({ where: { idInfraccion } });
})
.then((res) => {
if (!res) throw new Error('No existe esta infracción.');
2022-02-13 20:38:54 -06:00
if (res.gravedad === '1') fechaFin.add(7, 'd');
2021-09-24 12:30:24 -05:00
else fechaFin.add(100, 'y');
2022-02-13 20:38:54 -06:00
return Prestamo.findOne({ where: { idPrestamo } });
2021-09-24 12:30:24 -05:00
})
.then((res) => {
2022-02-01 06:44:34 -06:00
if (!res) throw new Error('No existe este préstamo.');
2021-09-24 12:30:24 -05:00
return Usuario.update(
{ multa: true },
2022-02-13 20:38:54 -06:00
{ where: { idUsuario: res.idUsuario } }
2021-09-24 12:30:24 -05:00
);
})
2022-02-13 20:38:54 -06:00
.then(() =>
2021-09-24 12:30:24 -05:00
Multa.create({
2022-02-13 20:38:54 -06:00
idPrestamo,
2021-09-24 12:30:24 -05:00
idOperadorMulta,
idInfraccion,
2022-02-13 20:38:54 -06:00
descripcion,
fechaFin: fechaFin.format('YYYY-MM-DD'),
2021-09-24 12:30:24 -05:00
})
)
2022-02-13 20:38:54 -06:00
.then(() => ({
2021-09-24 14:09:25 -05:00
message: `Se levantó correctamente el reporte de multa.`,
}));
2021-09-24 12:30:24 -05:00
};
module.exports = multar;