93 lines
2.6 KiB
JavaScript
93 lines
2.6 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 prestamo = {};
|
|
let gravedad = '';
|
|
|
|
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.');
|
|
// Checar si tiene una multa activa, agragar días a multa
|
|
gravedad = res.gravedad;
|
|
if (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,
|
|
idInfraccion: { [Op.ne]: 1 },
|
|
},
|
|
});
|
|
})
|
|
.then((res) => {
|
|
if (res)
|
|
throw new Error(
|
|
'Este préstamo ya ha sido usado para multar a un usuario.'
|
|
);
|
|
return Usuario.update(
|
|
{ multa: true },
|
|
{ where: { idUsuario: prestamo.idUsuario } }
|
|
);
|
|
})
|
|
.then(() =>
|
|
Multa.create({
|
|
descripcion,
|
|
fechaFin: fechaFin.format('YYYY-MM-DD'),
|
|
idOperadorMulta,
|
|
idInfraccion,
|
|
idPrestamo: prestamo.idPrestamo,
|
|
})
|
|
)
|
|
.then(() => ({
|
|
message: `Se levantó correctamente el reporte de multa.`,
|
|
}));
|
|
};
|
|
|
|
module.exports = multarNumeroInventario;
|