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

94 lines
2.6 KiB
JavaScript
Raw Normal View History

2021-09-24 12:30:24 -05:00
const moment = require('moment');
2022-02-01 06:44:34 -06:00
const { Op } = require('sequelize');
2021-09-24 12:30:24 -05:00
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
let prestamo = {};
2022-02-01 06:44:34 -06:00
let gravedad = '';
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-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-24 12:30:24 -05:00
else fechaFin.add(100, 'y');
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-24 12:30:24 -05:00
prestamo = res;
2022-02-01 06:44:34 -06:00
return Prestamo.findOne({
where: { idUsuario: prestamo.idUsuario, activo: true },
});
})
.then((res) => {
if (res)
throw new Error(
'No se puede multar a este usuario ya que tiene un préstamo activo.'
);
return Multa.findOne({
where: { idPrestamo, idInfraccion: { [Op.ne]: 1 } },
});
2021-09-24 12:30: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-24 12:30:24 -05:00
return Usuario.update(
{ multa: true },
2022-02-01 06:44:34 -06:00
{ where: { idUsuario: prestamo.idUsuario } }
2021-09-24 12:30:24 -05:00
);
})
.then((res) =>
Multa.create({
descripcion,
2022-02-01 06:44:34 -06:00
fechaFin: fechaFin.format('YYYY-MM-DD'),
2021-09-24 12:30:24 -05:00
idOperadorMulta,
idInfraccion,
idPrestamo,
})
)
2021-09-24 14:09:25 -05:00
.then((res) => ({
message: `Se levantó correctamente el reporte de multa.`,
}));
2021-09-24 12:30:24 -05:00
};
module.exports = multar;