2022-01-25 22:08:13 -06:00
|
|
|
const io = require('../../socket');
|
2022-01-03 16:47:25 -06:00
|
|
|
const { validarNumeroEntero } = require('../../helper/validar');
|
2021-04-29 22:52:17 -05:00
|
|
|
const dbPath = '../../db/tablas';
|
|
|
|
|
const Equipo = require(`${dbPath}/Equipo`);
|
2021-09-17 15:14:30 -05:00
|
|
|
const Operador = require(`${dbPath}/Operador`);
|
|
|
|
|
const Prestamo = require(`${dbPath}/Prestamo`);
|
2021-04-29 22:52:17 -05:00
|
|
|
|
2021-09-17 00:43:16 -05:00
|
|
|
const cancelarOperador = async (body) => {
|
2022-02-01 09:48:07 -06:00
|
|
|
const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id préstamo', true);
|
2022-01-03 16:47:25 -06:00
|
|
|
const idOperadorCancelacion = validarNumeroEntero(
|
|
|
|
|
body.idOperadorCancelacion,
|
|
|
|
|
'id operador cancelación',
|
|
|
|
|
true
|
|
|
|
|
);
|
2021-09-17 15:14:30 -05:00
|
|
|
|
|
|
|
|
return Operador.findOne({ where: { idOperador: idOperadorCancelacion } })
|
|
|
|
|
.then((res) => {
|
2022-02-01 06:44:34 -06:00
|
|
|
if (!res) throw new Error('No existe este operador.');
|
2022-02-01 09:48:07 -06:00
|
|
|
return Prestamo.findOne({ where: { idPrestamo } });
|
2021-09-17 15:14:30 -05:00
|
|
|
})
|
2021-04-29 22:52:17 -05:00
|
|
|
.then((res) => {
|
|
|
|
|
if (!res) throw new Error('Este prestamo no existe.');
|
|
|
|
|
if (!res.activo)
|
|
|
|
|
throw new Error(
|
2022-02-01 09:48:07 -06:00
|
|
|
'Este préstamo ya no esta activo, no se puede cancelar.'
|
2021-04-29 22:52:17 -05:00
|
|
|
);
|
|
|
|
|
return Equipo.update(
|
2022-01-27 13:12:38 -06:00
|
|
|
{ idStatus: 4 },
|
2022-02-01 09:48:07 -06:00
|
|
|
{ where: { idEquipo: res.idEquipo } }
|
2021-04-29 22:52:17 -05:00
|
|
|
);
|
|
|
|
|
})
|
2022-02-01 09:48:07 -06:00
|
|
|
.then(() =>
|
2021-06-29 09:55:18 -05:00
|
|
|
Prestamo.update(
|
2021-09-17 15:14:30 -05:00
|
|
|
{ activo: false, idOperadorCancelacion },
|
2021-06-29 09:55:18 -05:00
|
|
|
{ where: { idPrestamo } }
|
|
|
|
|
)
|
2021-04-29 22:52:17 -05:00
|
|
|
)
|
2022-02-01 09:48:07 -06:00
|
|
|
.then(() => {
|
2022-01-25 22:08:13 -06:00
|
|
|
io.getIO().emit('actualizar', { idPrestamo });
|
|
|
|
|
return { message: 'Se canceló el préstamo correctamente.' };
|
|
|
|
|
});
|
2021-04-29 22:52:17 -05:00
|
|
|
};
|
|
|
|
|
|
2021-09-17 00:43:16 -05:00
|
|
|
module.exports = cancelarOperador;
|