const io = require('../../socket'); const { validarNumeroEntero } = require('../../helper/validar'); const dbPath = '../../db/tablas'; const Prestamo = require(`${dbPath}/Prestamo`); const Equipo = require(`${dbPath}/Equipo`); const cancelarUsuario = async (body) => { const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id préstamo', true); return Prestamo.findOne({ where: { idPrestamo }, include: [{ model: Equipo }], }) .then((res) => { if (!res) throw new Error('Este préstamo no existe.'); if (!res.activo) throw new Error( 'Este préstamo ya no esta activo, no se puede cancelar.' ); if (res.Equipo.idStatus === 2) throw new Error( 'El equipo ya fue entregado al usuario, no se puede cancelar.' ); return Equipo.update( { idStatus: 4 }, { where: { idEquipo: res.idEquipo } } ); }) .then(() => Prestamo.update( { activo: false, canceladoUsuario: true }, { where: { idPrestamo } } ) ) .then(() => { io.getIO().emit('actualizar'); return { message: 'Se canceló el préstamo correctamente.' }; }); }; module.exports = cancelarUsuario;