const { validarId } = require('../../helper/validar'); const dbPath = '../../db/tablas'; const Prestamo = require(`${dbPath}/Prestamo`); const Equipo = require(`${dbPath}/Equipo`); const cancelar = async (body) => { const idPrestamo = validarId(body.idPrestamo); return Prestamo.findOne({ where: { idPrestamo }, include: [{ model: Equipo }], }) .then((res) => { if (!res) throw new Error('Este prestamo no existe.'); if (!res.activo) throw new Error( 'Este prestamo ya no esta activo, ya no se puede cancelar.' ); if (res.Equipo.enUso) throw new Error( 'El equipo ya fue entregado al usuario, ya no se puede cancelar.' ); return Equipo.update( { apartado: false }, { where: { idEquipo: res.Equipo.idEquipo } } ); }) .then((res) => Prestamo.update({ activo: false }, { where: { idPrestamo } }) ) .then((res) => ({ message: 'Se cancelo el prestamo correctamente.' })); }; module.exports = cancelar;