49 lines
1.6 KiB
JavaScript
49 lines
1.6 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 idOperador = 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: 7 });
|
|
return Equipo.update(
|
|
{ idStatus: 7 },
|
|
{ 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;
|