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

58 lines
1.7 KiB
JavaScript
Raw Normal View History

2022-01-25 22:08:13 -06:00
const io = require('../../socket');
2022-03-22 11:42:41 -06:00
const validar = require('../../helper/validar');
2021-04-29 22:52:17 -05:00
const dbPath = '../../db/tablas';
const Equipo = require(`${dbPath}/Equipo`);
2022-03-22 11:42:41 -06:00
const Motivo = require(`${dbPath}/Motivo`);
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-03-22 11:42:41 -06:00
const idPrestamo = validar.validarNumeroEntero(
body.idPrestamo,
'id préstamo',
true
);
const idOperador = validar.validarNumeroEntero(
body.idOperador,
'id operador',
true
);
2022-03-21 20:48:29 -06:00
const motivo = validar.validarAlfanumerico(body.motivo, 'motivo', true, 500);
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
})
2022-03-21 20:48:29 -06:00
.then(async (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
);
2022-03-21 22:38:51 -06:00
await Motivo.create({ motivo, idEquipo: res.idEquipo, idStatus: 9 });
2021-04-29 22:52:17 -05:00
return Equipo.update(
2022-03-21 22:38:51 -06:00
{ idStatus: 9 },
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;