75 lines
2.1 KiB
JavaScript
75 lines
2.1 KiB
JavaScript
const moment = require('moment');
|
|
const { Op } = require('sequelize');
|
|
const validar = require('../../helper/validar');
|
|
const dbPath = '../../db/tablas';
|
|
const Infraccion = require(`${dbPath}/Infraccion`);
|
|
const Equipo = require(`${dbPath}/Equipo`);
|
|
const Multa = require(`${dbPath}/Multa`);
|
|
const Operador = require(`${dbPath}/Operador`);
|
|
const Prestamo = require(`${dbPath}/Prestamo`);
|
|
const Usuario = require(`${dbPath}/Usuario`);
|
|
|
|
const multarNumeroInventario = async (body) => {
|
|
const idOperadorMulta = validar.validarNumeroEntero(
|
|
body.idOperadorMulta,
|
|
'id operador multa',
|
|
true
|
|
);
|
|
const idInfraccion = validar.validarNumeroEntero(
|
|
body.idInfraccion,
|
|
'id infracción',
|
|
true
|
|
);
|
|
const numeroInventario = validar.validarAlfanumerico(
|
|
body.numeroInventario,
|
|
'número de inventario',
|
|
20
|
|
);
|
|
const descripcion = validar.validarAlfanumerico(
|
|
body.descripcion,
|
|
'descripción',
|
|
false,
|
|
500
|
|
);
|
|
let fechaFin = moment();
|
|
let idPrestamo = null;
|
|
|
|
return Operador.findOne({ where: { idOperador: idOperadorMulta } })
|
|
.then((res) => {
|
|
if (!res) throw new Error('No existe este operador.');
|
|
return Infraccion.findOne({ where: { idInfraccion } });
|
|
})
|
|
.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 } }],
|
|
});
|
|
})
|
|
.then((res) => {
|
|
if (!res)
|
|
throw new Error('No existe un prestamo activo con este equipo.');
|
|
idPrestamo = res.idPrestamo;
|
|
return Usuario.update(
|
|
{ multa: true },
|
|
{ where: { idUsuario: res.idUsuario } }
|
|
);
|
|
})
|
|
.then(() =>
|
|
Multa.create({
|
|
idPrestamo,
|
|
idOperadorMulta,
|
|
idInfraccion,
|
|
descripcion,
|
|
fechaFin: fechaFin.format('YYYY-MM-DD'),
|
|
})
|
|
)
|
|
.then(() => ({
|
|
message: `Se levantó correctamente el reporte de multa.`,
|
|
}));
|
|
};
|
|
|
|
module.exports = multarNumeroInventario;
|