Files
2022-03-22 11:42:41 -06:00

58 lines
1.7 KiB
JavaScript

const io = require('../../socket');
const validar = require('../../helper/validar');
const dbPath = '../../db/tablas';
const Equipo = require(`${dbPath}/Equipo`);
const Motivo = require(`${dbPath}/Motivo`);
const Operador = require(`${dbPath}/Operador`);
const Prestamo = require(`${dbPath}/Prestamo`);
const cancelarOperador = async (body) => {
const idPrestamo = validar.validarNumeroEntero(
body.idPrestamo,
'id préstamo',
true
);
const idOperador = validar.validarNumeroEntero(
body.idOperador,
'id operador',
true
);
const motivo = validar.validarAlfanumerico(body.motivo, 'motivo', true, 500);
let idUsuario = null;
return Operador.findOne({ where: { idOperador } })
.then((res) => {
if (!res) throw new Error('No existe este operador.');
return Prestamo.findOne({ where: { idPrestamo } });
})
.then(async (res) => {
if (!res) throw new Error('Este préstamo no existe.');
idUsuario = res.idUsuario;
if (!res.activo)
throw new Error(
'Este préstamo ya no esta activo, no se puede cancelar.'
);
await Motivo.create({ motivo, idEquipo: res.idEquipo, idStatus: 9 });
return Equipo.update(
{ idStatus: 9 },
{ where: { idEquipo: res.idEquipo } }
);
})
.then(() =>
Prestamo.update(
{
activo: false,
idOperadorRegreso: idOperador,
canceladoOperador: true,
},
{ where: { idPrestamo } }
)
)
.then(() => {
io.getIO().emit('actualizar', { idUsuario });
return { message: 'Se canceló el préstamo correctamente.' };
});
};
module.exports = cancelarOperador;