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.validarAlfanumerico( 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;