45 lines
1.4 KiB
JavaScript
45 lines
1.4 KiB
JavaScript
const io = require('../../socket');
|
|
const { validarNumeroEntero } = require('../../helper/validar');
|
|
const dbPath = '../../db/tablas';
|
|
const Equipo = require(`${dbPath}/Equipo`);
|
|
const Operador = require(`${dbPath}/Operador`);
|
|
const Prestamo = require(`${dbPath}/Prestamo`);
|
|
|
|
const cancelarOperador = async (body) => {
|
|
const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id préstamo', true);
|
|
const idOperadorCancelacion = validarNumeroEntero(
|
|
body.idOperadorCancelacion,
|
|
'id operador cancelación',
|
|
true
|
|
);
|
|
|
|
return Operador.findOne({ where: { idOperador: idOperadorCancelacion } })
|
|
.then((res) => {
|
|
if (!res) throw new Error('No existe este operador.');
|
|
return Prestamo.findOne({ where: { idPrestamo } });
|
|
})
|
|
.then((res) => {
|
|
if (!res) throw new Error('Este prestamo no existe.');
|
|
if (!res.activo)
|
|
throw new Error(
|
|
'Este préstamo ya no esta activo, no se puede cancelar.'
|
|
);
|
|
return Equipo.update(
|
|
{ idStatus: 4 },
|
|
{ where: { idEquipo: res.idEquipo } }
|
|
);
|
|
})
|
|
.then(() =>
|
|
Prestamo.update(
|
|
{ activo: false, idOperadorCancelacion },
|
|
{ where: { idPrestamo } }
|
|
)
|
|
)
|
|
.then(() => {
|
|
io.getIO().emit('actualizar', { idPrestamo });
|
|
return { message: 'Se canceló el préstamo correctamente.' };
|
|
});
|
|
};
|
|
|
|
module.exports = cancelarOperador;
|