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

76 lines
2.3 KiB
JavaScript
Raw Normal View History

2021-09-02 08:21:35 -05:00
const moment = require('moment');
2021-09-06 11:56:03 -05:00
const validar = require('../../helper/validar');
2021-09-02 08:21:35 -05:00
const dbPath = '../../db/tablas';
2021-09-02 09:33:24 -05:00
const Infraccion = require(`${dbPath}/Infraccion`);
2021-09-17 15:14:30 -05:00
const Multa = require(`${dbPath}/Multa`);
const Operador = require(`${dbPath}/Operador`);
2021-09-02 08:21:35 -05:00
const Prestamo = require(`${dbPath}/Prestamo`);
const Usuario = require(`${dbPath}/Usuario`);
const multar = async (body) => {
2021-09-17 15:14:30 -05:00
const idOperadorMulta = validar.validarId(body.idOperadorMulta);
2021-09-24 14:09:25 -05:00
const idUsuario = validar.validarId(body.idUsuario);
2021-09-06 11:56:03 -05:00
const idInfraccion = validar.validarId(body.idInfraccion);
const idPrestamo = validar.validarId(body.idPrestamo);
const descripcion = validar.validarAlfanumerico(
2021-09-06 11:50:12 -05:00
body.descripcion,
'La descripción',
500
);
2021-09-25 22:45:07 -05:00
const fechaFin = moment();
2021-09-02 09:33:24 -05:00
let prestamo = {};
2021-09-25 22:45:07 -05:00
2021-09-17 15:14:30 -05:00
return Operador.findOne({ where: { idOperador: idOperadorMulta } })
.then((res) => {
if (!res) throw new Error('No existe este operador.');
2021-09-17 15:14:30 -05:00
return Infraccion.findOne({ where: { idInfraccion } });
})
2021-09-02 08:21:35 -05:00
.then((res) => {
if (!res) throw new Error('No existe esta infracción.');
2021-09-02 09:33:24 -05:00
if (res.gravedad == '1') fechaFin.add(7, 'd');
else fechaFin.add(100, 'y');
2021-09-02 08:21:35 -05:00
return Prestamo.findOne({
where: { idPrestamo },
include: [{ model: Usuario }],
});
})
.then((res) => {
if (!res) throw new Error('No existe este prestamo.');
2021-09-02 09:33:24 -05:00
prestamo = res;
2021-09-25 22:45:07 -05:00
return Usuario.findOne({ where: { idUsuario } });
})
.then((res) => {
if (!res) throw new Error('No existe este usuario.');
return Prestamo.findOne({ where: { idUsuario, activo: true } });
})
.then((res) => {
if (res)
throw new Error(
'No se puede multar a este usuario ya que tiene un prestamo activo.'
);
2021-09-02 09:33:24 -05:00
return Multa.findOne({ where: { idPrestamo } });
})
.then((res) => {
if (res)
throw new Error('A este prestamo ya se le ha asignado una multa.');
2021-09-02 08:21:35 -05:00
return Usuario.update(
{ multa: true },
2021-09-02 09:33:24 -05:00
{ where: { idUsuario: prestamo.Usuario.idUsuario } }
2021-09-02 08:21:35 -05:00
);
})
2021-09-02 09:33:24 -05:00
.then((res) =>
Multa.create({
2021-09-02 08:21:35 -05:00
descripcion,
2021-09-14 18:17:17 -05:00
fechaFin: fechaFin.format(),
2021-09-17 15:14:30 -05:00
idOperadorMulta,
2021-09-02 08:21:35 -05:00
idInfraccion,
idPrestamo,
2021-09-02 09:33:24 -05:00
})
)
.then((res) => ({
message: `Se levantó correctamente el reporte de multa.`,
}));
2021-09-02 08:21:35 -05:00
};
module.exports = multar;