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

82 lines
2.3 KiB
JavaScript
Raw Normal View History

2021-09-02 17:27:28 -05:00
const moment = require('moment');
const validar = require('../../helper/validar');
const dbPath = '../../db/tablas';
const Infraccion = require(`${dbPath}/Infraccion`);
const Equipo = require(`${dbPath}/Equipo`);
const Multa = require(`${dbPath}/Multa`);
2021-09-25 23:13:37 -05:00
const Operador = require(`${dbPath}/Operador`);
2021-09-02 17:27:28 -05:00
const Prestamo = require(`${dbPath}/Prestamo`);
const Usuario = require(`${dbPath}/Usuario`);
2021-09-20 00:21:36 -05:00
const multarNumeroInventario = 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
);
2021-09-02 17:27:28 -05:00
const numeroInventario = validar.validarAlfanumerico(
body.numeroInventario,
'El numero de inventario',
20
);
2021-09-06 11:50:12 -05:00
const descripcion = validar.validarAlfanumerico(
2021-09-02 17:27:28 -05:00
body.descripcion,
2022-01-03 16:56:00 -06:00
'descripción',
false,
2021-09-02 17:27:28 -05:00
500
);
2021-09-25 22:45:07 -05:00
const fechaFin = moment();
2021-09-02 17:27:28 -05:00
let prestamo = {};
2021-09-17 15:14:30 -05:00
return Operador.findOne({ where: { idOperador: idOperadorMulta } })
.then((res) => {
if (!res) throw new Error('No existe esta este operador.');
return Infraccion.findOne({ where: { idInfraccion } });
})
2021-09-02 17:27:28 -05:00
.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: { activo: true },
include: [
{ model: Equipo, where: { numeroInventario } },
{ model: Usuario },
],
});
})
.then((res) => {
if (!res)
throw new Error('No existe un prestamo activo con este equipo.');
prestamo = res;
return Multa.findOne({ where: { idPrestamo: prestamo.idPrestamo } });
})
.then((res) => {
if (res)
throw new Error('A este prestamo ya se le ha asignado una multa.');
return Usuario.update(
{ multa: true },
{ where: { idUsuario: prestamo.Usuario.idUsuario } }
);
})
.then((res) =>
Multa.create({
descripcion,
2021-09-17 15:14:30 -05:00
idOperadorMulta,
2021-09-02 17:27:28 -05:00
idInfraccion,
idPrestamo: prestamo.idPrestamo,
fechaFin: fechaFin.format(),
})
)
2021-09-17 16:57:38 -05:00
.then((res) => ({
message: `Se levantó correctamente el reporte de multa.`,
}));
2021-09-02 17:27:28 -05:00
};
2021-09-20 00:21:36 -05:00
module.exports = multarNumeroInventario;