Files
pcpuma_acatlan_api/server/controller/Prestamo/cancelarOperador.js
T

47 lines
1.4 KiB
JavaScript
Raw Normal View History

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-02-05 21:40:39 -06:00
const idOperador = validarNumeroEntero(body.idOperador, 'id operador', true);
let idUsuario = null;
2021-09-17 15:14:30 -05:00
2022-02-05 22:28:11 -06:00
return Operador.findOne({ where: { idOperador } })
2021-09-17 15:14:30 -05:00
.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) => {
2022-02-13 17:29:26 -06:00
if (!res) throw new Error('Este préstamo no existe.');
idUsuario = res.idUsuario;
2021-04-29 22:52:17 -05:00
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(
2022-02-05 22:28:11 -06:00
{
activo: false,
idOperadorRegreso: idOperador,
canceladoOperador: true,
},
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(() => {
io.getIO().emit('actualizar', { idUsuario });
2022-01-25 22:08:13 -06:00
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;