72 lines
2.2 KiB
JavaScript
72 lines
2.2 KiB
JavaScript
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`);
|
|
const Prestamo = require(`${dbPath}/Prestamo`);
|
|
const Usuario = require(`${dbPath}/Usuario`);
|
|
|
|
const multarNumeroInventario = async (body) => {
|
|
const idOperadorMulta = validar.validarId(body.idOperadorMulta);
|
|
const idInfraccion = validar.validarId(body.idInfraccion);
|
|
const numeroInventario = validar.validarAlfanumerico(
|
|
body.numeroInventario,
|
|
'El numero de inventario',
|
|
20
|
|
);
|
|
const descripcion = validar.validarAlfanumerico(
|
|
body.descripcion,
|
|
'La descripción',
|
|
500
|
|
);
|
|
let prestamo = {};
|
|
let fechaFin = moment();
|
|
|
|
return Operador.findOne({ where: { idOperador: idOperadorMulta } })
|
|
.then((res) => {
|
|
if (!res) throw new Error('No existe esta 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 } },
|
|
{ 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,
|
|
idOperadorMulta,
|
|
idInfraccion,
|
|
idPrestamo: prestamo.idPrestamo,
|
|
fechaFin: fechaFin.format(),
|
|
})
|
|
)
|
|
.then((res) => ({
|
|
message: `Se levantó correctamente el reporte de multa.`,
|
|
}));
|
|
};
|
|
|
|
module.exports = multarNumeroInventario;
|