2021-09-02 17:27:28 -05:00
|
|
|
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
|
|
|
|
|
);
|
2021-09-06 11:50:12 -05:00
|
|
|
const descripcion = validar.validarAlfanumerico(
|
2021-09-02 17:27:28 -05:00
|
|
|
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;
|