Files

75 lines
2.1 KiB
JavaScript
Raw Permalink Normal View History

2021-09-02 17:27:28 -05:00
const moment = require('moment');
2022-02-01 06:44:34 -06:00
const { Op } = require('sequelize');
2021-09-02 17:27:28 -05:00
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,
2022-02-01 06:44:34 -06:00
'número de inventario',
2021-09-02 17:27:28 -05:00
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
);
2022-02-01 06:44:34 -06:00
let fechaFin = moment();
2022-02-13 20:38:54 -06:00
let idPrestamo = null;
2021-09-02 17:27:28 -05:00
2021-09-17 15:14:30 -05:00
return Operador.findOne({ where: { idOperador: idOperadorMulta } })
.then((res) => {
2022-02-01 06:44:34 -06:00
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 17:27:28 -05:00
.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-02 17:27:28 -05:00
else fechaFin.add(100, 'y');
return Prestamo.findOne({
where: { activo: true },
2022-02-13 20:38:54 -06:00
include: [{ model: Equipo, where: { numeroInventario } }],
2021-09-02 17:27:28 -05:00
});
})
.then((res) => {
if (!res)
throw new Error('No existe un prestamo activo con este equipo.');
2022-02-13 20:38:54 -06:00
idPrestamo = res.idPrestamo;
2021-09-02 17:27:28 -05:00
return Usuario.update(
{ multa: true },
2022-02-13 20:38:54 -06:00
{ where: { idUsuario: res.idUsuario } }
2021-09-02 17:27:28 -05:00
);
})
2022-02-01 06:44:34 -06:00
.then(() =>
2021-09-02 17:27:28 -05:00
Multa.create({
2022-02-13 20:38:54 -06:00
idPrestamo,
2021-09-17 15:14:30 -05:00
idOperadorMulta,
2021-09-02 17:27:28 -05:00
idInfraccion,
2022-02-13 20:38:54 -06:00
descripcion,
fechaFin: fechaFin.format('YYYY-MM-DD'),
2021-09-02 17:27:28 -05:00
})
)
2022-02-01 06:44:34 -06:00
.then(() => ({
2021-09-17 16:57:38 -05:00
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;