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

94 lines
2.5 KiB
JavaScript
Raw Normal View History

2021-09-02 08:21:35 -05:00
const moment = require('moment');
2022-02-01 06:44:34 -06:00
const { Op } = require('sequelize');
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) => {
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-06 11:56:03 -05:00
const descripcion = validar.validarAlfanumerico(
2021-09-06 11:50:12 -05:00
body.descripcion,
2022-01-03 16:56:00 -06:00
'descripción',
false,
2021-09-06 11:50:12 -05:00
500
);
2022-02-01 06:44:34 -06:00
let fechaFin = moment();
2021-09-02 09:33:24 -05:00
let prestamo = {};
2022-02-01 06:44:34 -06:00
let gravedad = '';
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.');
2022-02-01 06:44:34 -06:00
// Checar si tiene una multa activa, agragar días a multa
gravedad = res.gravedad;
if (gravedad === '1') fechaFin.add(7, 'd');
2021-09-02 09:33:24 -05:00
else fechaFin.add(100, 'y');
2021-09-02 08:21:35 -05:00
return Prestamo.findOne({
where: { idPrestamo },
include: [{ model: Usuario }],
});
})
.then((res) => {
2022-02-01 06:44:34 -06:00
if (!res) throw new Error('No existe este préstamo.');
2021-09-02 09:33:24 -05:00
prestamo = res;
2022-02-01 06:44:34 -06:00
return Prestamo.findOne({
where: { idUsuario: prestamo.idUsuario, activo: true },
});
2021-09-25 22:45:07 -05:00
})
.then((res) => {
if (res)
throw new Error(
2022-02-01 06:44:34 -06:00
'No se puede multar a este usuario ya que tiene un préstamo activo.'
2021-09-25 22:45:07 -05:00
);
2022-02-01 06:44:34 -06:00
return Multa.findOne({
where: { idPrestamo, idInfraccion: { [Op.ne]: 1 } },
});
2021-09-02 09:33:24 -05:00
})
.then((res) => {
if (res)
2022-02-01 06:44:34 -06:00
throw new Error(
'Este préstamo ya ha sido usado para multar a un usuario.'
);
2021-09-02 08:21:35 -05:00
return Usuario.update(
{ multa: true },
2022-02-01 06:44:34 -06:00
{ where: { idUsuario: prestamo.idUsuario } }
2021-09-02 08:21:35 -05:00
);
})
2022-02-01 06:44:34 -06:00
.then(() =>
2021-09-02 09:33:24 -05:00
Multa.create({
2021-09-02 08:21:35 -05:00
descripcion,
2022-02-01 06:44:34 -06:00
fechaFin: fechaFin.format('YYYY-MM-DD'),
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
})
)
2022-02-01 06:44:34 -06:00
.then(() => ({
message: `Se levantó correctamente el reporte de multa.`,
}));
2021-09-02 08:21:35 -05:00
};
module.exports = multar;