2021-09-02 09:33:24 -05:00
|
|
|
const { validarId, validarTexto } = require('../../helper/validar');
|
|
|
|
|
const dbPath = '../../db/tablas';
|
|
|
|
|
const Reporte = require(`${dbPath}/Reporte`);
|
|
|
|
|
const Prestamo = require(`${dbPath}/Prestamo`);
|
|
|
|
|
|
|
|
|
|
const reportar = async (body) => {
|
|
|
|
|
const idPrestamo = validarId(body.idPrestamo);
|
2021-09-06 11:50:12 -05:00
|
|
|
const descripcion = validarAlfanumerico(
|
|
|
|
|
body.descripcion,
|
|
|
|
|
'La descripción',
|
|
|
|
|
500
|
|
|
|
|
);
|
2021-09-02 16:13:16 -05:00
|
|
|
let idEquipo = null;
|
2021-09-02 09:33:24 -05:00
|
|
|
|
|
|
|
|
return Prestamo.findOne({ where: { idPrestamo } })
|
|
|
|
|
.then((res) => {
|
|
|
|
|
if (!res) throw new Error('No existe este prestamo.');
|
2021-09-02 14:55:56 -05:00
|
|
|
console.log(res.dataValues);
|
|
|
|
|
idEquipo = res.idEquipo;
|
2021-09-02 09:33:24 -05:00
|
|
|
return Reporte.findOne({ where: { idPrestamo } });
|
|
|
|
|
})
|
|
|
|
|
.then((res) => {
|
|
|
|
|
if (res) throw new Error('Ya existe un reporte con este prestamo.');
|
2021-09-02 14:55:56 -05:00
|
|
|
return Reporte.create({ descripcion, idPrestamo, idEquipo });
|
2021-09-02 09:33:24 -05:00
|
|
|
})
|
|
|
|
|
.then((res) => ({ message: 'Se levanto correctamente el reporte.' }));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = reportar;
|