reporte y multa por numero de inventari
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
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 multar = async (body) => {
|
||||
const idInfraccion = validar.validarId(body.idInfraccion);
|
||||
const numeroInventario = validar.validarAlfanumerico(
|
||||
body.numeroInventario,
|
||||
'El numero de inventario',
|
||||
20
|
||||
);
|
||||
const descripcion = validar.validarTexto(
|
||||
body.descripcion,
|
||||
'La descripción',
|
||||
500
|
||||
);
|
||||
let prestamo = {};
|
||||
let fechaFin = moment();
|
||||
|
||||
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,
|
||||
idInfraccion,
|
||||
idPrestamo: prestamo.idPrestamo,
|
||||
fechaFin: fechaFin.format(),
|
||||
})
|
||||
)
|
||||
.then((res) => ({ message: `Se multo correctamente al alumno.` }));
|
||||
};
|
||||
|
||||
module.exports = multar;
|
||||
Reference in New Issue
Block a user