reporte y multa por numero de inventari

This commit is contained in:
2021-09-02 17:27:28 -05:00
parent ded01c50d3
commit bf7dd8f394
4 changed files with 135 additions and 1 deletions
@@ -0,0 +1,41 @@
const validar = require('../../helper/validar');
const dbPath = '../../db/tablas';
const Reporte = require(`${dbPath}/Reporte`);
const Prestamo = require(`${dbPath}/Prestamo`);
const Equipo = require(`${dbPath}/Equipo`);
const reportar = async (body) => {
const numeroInventario = validar.validarAlfanumerico(
body.numeroInventario,
'El numero de inventario',
20
);
const descripcion = validar.validarTexto(
body.descripcion,
'La descripción',
500
);
let prestamo = {};
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.');
prestamo = res;
return Reporte.findOne({ where: { idPrestamo: prestamo.idPrestamo } });
})
.then((res) => {
if (res) throw new Error('Ya existe un reporte con este prestamo.');
return Reporte.create({
descripcion,
idPrestamo: prestamo.idPrestamo,
idEquipo: prestamo.idEquipo,
});
})
.then((res) => ({ message: 'Se levanto correctamente el reporte.' }));
};
module.exports = reportar;