mejora código

This commit is contained in:
2022-02-01 09:48:07 -06:00
parent 4437ade10c
commit 1bd65011c4
19 changed files with 65 additions and 113 deletions
+1 -1
View File
@@ -21,7 +21,7 @@ const crear = async (body) => {
idTipoUsuario: 2,
});
})
.then((res) => ({ message: 'Se creo un nuevo operador correctamente.' }));
.then(() => ({ message: 'Se creo un nuevo operador correctamente.' }));
};
module.exports = crear;
+1 -4
View File
@@ -15,10 +15,7 @@ const login = async (body) => {
20
);
return Operador.findOne({
where: { operador },
attributes: ['idOperador', 'password', 'activo'],
})
return Operador.findOne({ where: { operador } })
.then((res) => {
if (!res) throw new Error('No existe este operador.');
if (!res.activo) throw new Error('Este operador no esta activo.');
+1 -8
View File
@@ -5,14 +5,7 @@ const Operador = require(`${dbPath}/Operador`);
const TipoUsuario = require(`${dbPath}/TipoUsuario`);
const operadores = async (body) => {
const pagina = validar.validarNumero(
body.pagina,
'página',
false,
null,
true,
true
);
const pagina = validar.validarNumeroEntero(body.pagina, 'página', false);
const operador = body.operador
? validar.validarTexto(body.operador, 'operador', true, 20)
: '';
+6 -2
View File
@@ -14,9 +14,13 @@ const update = async (body) => {
: '';
let update = {};
return Operador.findOne({ where: { idOperador, idTipoUsuario: 2 } })
return Operador.findOne({ where: { idOperador } })
.then((res) => {
if (!res) throw new Error('No existe este operador.');
if (res.idTipoUsuario != 2)
throw new Error(
'Este usuario no es de tipo operador, no se pueda actualizar su información.'
);
if (body.activo === 'desactivar') update.activo = false;
else if (body.activo === 'activar') update.activo = true;
if (password) update.password = encriptar(password);
@@ -24,7 +28,7 @@ const update = async (body) => {
throw new Error('No se mando nada para actualizar.');
return Operador.update(update, { where: { idOperador } });
})
.then((res) => ({ message: 'Se actualizó correctamente al operador.' }));
.then(() => ({ message: 'Se actualizó correctamente al operador.' }));
};
module.exports = update;
+7 -14
View File
@@ -12,23 +12,19 @@ const TipoUsuario = require(`${dbPath}/TipoUsuario`);
const Usuario = require(`${dbPath}/Usuario`);
const activos = async (body) => {
const pagina = validar.validarNumeroEntero(body.pagina, 'página', false);
const idModulo = validar.validarNumeroEntero(
body.idModulo,
'id modulo',
true
);
const pagina = validar.validarNumero(
body.pagina,
'página',
false,
null,
true,
true
);
const usuario = body.usuario ? validar.validarNumeroCuenta(body.usuario) : '';
const idPrestamo = body.idPrestamo
? validar.validarNumeroEntero(body.idPrestamo, 'id prestamo', true)
: '';
: null;
const idTipoCarrito = body.idTipoCarrito
? validar.validarNumeroEntero(body.idTipoCarrito, 'id tipo carrito', true)
: null;
const usuario = body.usuario ? validar.validarNumeroCuenta(body.usuario) : '';
const numeroInventario = body.numeroInventario
? validar.validarAlfanumerico(
body.numeroInventario,
@@ -37,9 +33,6 @@ const activos = async (body) => {
20
)
: '';
const idTipoCarrito = body.idTipoCarrito
? validar.validarNumeroEntero(body.idTipoCarrito, 'id tipo carrito', true)
: null;
let whereCarrito = [{ idModulo }];
if (idTipoCarrito) whereCarrito.push({ idTipoCarrito });
@@ -48,10 +41,10 @@ const activos = async (body) => {
return Prestamo.findAndCountAll({
where: {
[Op.or]: [
{ activo: true },
{
[Op.and]: [{ regresoInmediato: true }, { idOperadorRegreso: null }],
},
{ activo: true },
],
idPrestamo: { [Op.like]: `%${idPrestamo}%` },
},
@@ -6,7 +6,7 @@ const Operador = require(`${dbPath}/Operador`);
const Prestamo = require(`${dbPath}/Prestamo`);
const cancelarOperador = async (body) => {
const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id prestamo', true);
const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id préstamo', true);
const idOperadorCancelacion = validarNumeroEntero(
body.idOperadorCancelacion,
'id operador cancelación',
@@ -16,29 +16,26 @@ const cancelarOperador = async (body) => {
return Operador.findOne({ where: { idOperador: idOperadorCancelacion } })
.then((res) => {
if (!res) throw new Error('No existe este operador.');
return Prestamo.findOne({
where: { idPrestamo },
include: [{ model: Equipo }],
});
return Prestamo.findOne({ where: { idPrestamo } });
})
.then((res) => {
if (!res) throw new Error('Este prestamo no existe.');
if (!res.activo)
throw new Error(
'Este prestamo ya no esta activo, ya no se puede cancelar.'
'Este préstamo ya no esta activo, no se puede cancelar.'
);
return Equipo.update(
{ idStatus: 4 },
{ where: { idEquipo: res.Equipo.idEquipo } }
{ where: { idEquipo: res.idEquipo } }
);
})
.then((res) =>
.then(() =>
Prestamo.update(
{ activo: false, idOperadorCancelacion },
{ where: { idPrestamo } }
)
)
.then((res) => {
.then(() => {
io.getIO().emit('actualizar', { idPrestamo });
return { message: 'Se canceló el préstamo correctamente.' };
});
@@ -5,34 +5,34 @@ const Prestamo = require(`${dbPath}/Prestamo`);
const Equipo = require(`${dbPath}/Equipo`);
const cancelarUsuario = async (body) => {
const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id prestamo', true);
const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id préstamo', true);
return Prestamo.findOne({
where: { idPrestamo },
include: [{ model: Equipo }],
})
.then((res) => {
if (!res) throw new Error('Este prestamo no existe.');
if (!res) throw new Error('Este préstamo no existe.');
if (!res.activo)
throw new Error(
'Este prestamo ya no esta activo, ya no se puede cancelar.'
'Este préstamo ya no esta activo, no se puede cancelar.'
);
if (res.Equipo.idStatus === 2)
throw new Error(
'El equipo ya fue entregado al usuario, ya no se puede cancelar.'
'El equipo ya fue entregado al usuario, no se puede cancelar.'
);
return Equipo.update(
{ idStatus: 4 },
{ where: { idEquipo: res.Equipo.idEquipo } }
{ where: { idEquipo: res.idEquipo } }
);
})
.then((res) =>
.then(() =>
Prestamo.update(
{ activo: false, canceladoUsuario: true },
{ where: { idPrestamo } }
)
)
.then((res) => {
.then(() => {
io.getIO().emit('actualizar');
return { message: 'Se canceló el préstamo correctamente.' };
});
+5 -5
View File
@@ -35,17 +35,17 @@ const entregar = async (body) => {
throw new Error('Ya se entrego el equipo al usuario.');
else if (moment(res.horaMaxRecoger) < ahora)
await Equipo.update({ idStatus: 4 }, { where: { idEquipo } })
.then((res) =>
.then(() =>
Prestamo.update({ activo: false }, { where: { idPrestamo } })
)
.then((res) => {
.then(() => {
throw new Error(
'Ya paso el tiempo limite para recoger el dispositivo.'
'Ya paso el tiempo límite para recoger el equipo de cómputo.'
);
});
return Equipo.update({ idStatus: 2 }, { where: { idEquipo } });
})
.then((res) => {
.then(() => {
Prestamo.update(
{
idOperadorEntrega,
@@ -55,7 +55,7 @@ const entregar = async (body) => {
{ where: { idPrestamo } }
);
})
.then((res) =>
.then(() =>
Equipo.findOne({
where: { idEquipo },
include: [
+2 -9
View File
@@ -12,17 +12,10 @@ const TipoUsuario = require(`${dbPath}/TipoUsuario`);
const Usuario = require(`${dbPath}/Usuario`);
const historial = async (body) => {
const pagina = validar.validarNumero(
body.pagina,
'página',
false,
null,
true,
true
);
const pagina = validar.validarNumeroEntero(body.pagina, 'página', false);
const usuario = body.usuario ? validar.validarNumeroCuenta(body.usuario) : '';
const idPrestamo = body.idPrestamo
? validar.validarNumeroEntero(body.idPrestamo, 'id prestamo', true)
? validar.validarNumeroEntero(body.idPrestamo, 'id préstamo', true)
: '';
const numeroInventario = body.numeroInventario
? validar.validarAlfanumerico(
@@ -8,19 +8,12 @@ const TipoUsuario = require(`${dbPath}/TipoUsuario`);
const Usuario = require(`${dbPath}/Usuario`);
const historialEquipo = async (body) => {
const pagina = validar.validarNumeroEntero(body.pagina, 'página', false);
const idEquipo = validar.validarNumeroEntero(
body.idEquipo,
'id equipo',
true
);
const pagina = validar.validarNumero(
body.pagina,
'página',
false,
null,
true,
true
);
return Prestamo.findAndCountAll({
include: [
@@ -10,19 +10,12 @@ const TipoCarrito = require(`${dbPath}/TipoCarrito`);
const Usuario = require(`${dbPath}/Usuario`);
const historialUsuario = async (body) => {
const pagina = validar.validarNumeroEntero(body.pagina, 'página', false);
const idUsuario = validar.validarNumeroEntero(
body.idUsuario,
'id usuario',
true
);
const pagina = validar.validarNumero(
body.pagina,
'página',
false,
null,
true,
true
);
return Prestamo.findAndCountAll({
include: [
+2 -2
View File
@@ -57,13 +57,13 @@ const pedir = async (body) => {
where: {
idUsuario,
[Op.or]: [
{ activo: true },
{
[Op.and]: [
{ regresoInmediato: true },
{ idOperadorRegreso: null },
],
},
{ activo: true },
],
},
});
@@ -100,7 +100,7 @@ const pedir = async (body) => {
idEquipo = res.idEquipo;
return Equipo.update({ idStatus: 1 }, { where: { idEquipo } });
})
.then((res) =>
.then(() =>
Prestamo.create({
horaMaxRecoger: ahora.add(10, 'm'),
idUsuario,
+2 -2
View File
@@ -12,7 +12,7 @@ const TipoUsuario = require(`${dbPath}/TipoUsuario`);
const Usuario = require(`${dbPath}/Usuario`);
const prestamo = async (body) => {
const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id prestamo', true);
const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id préstamo', true);
return Prestamo.findOne({
where: { idPrestamo },
@@ -60,7 +60,7 @@ const prestamo = async (body) => {
'createdAt',
],
}).then((res) => {
if (!res) throw new Error('No existe este prestamo.');
if (!res) throw new Error('No existe este préstamo.');
return res;
});
};
@@ -14,7 +14,6 @@ const prestamo = async (body) => {
return Usuario.findOne({ where: { idUsuario } }).then((res) => {
if (!res) throw new Error('No existe este usuario.');
return Prestamo.findOne({
where: { idUsuario, activo: true },
include: [
+6 -8
View File
@@ -1,6 +1,7 @@
const moment = require('moment');
const io = require('../../socket');
const { validarNumeroEntero } = require('../../helper/validar');
const { parse } = require('dotenv');
const dbPath = '../../db/tablas';
const Equipo = require(`${dbPath}/Equipo`);
const Multa = require(`${dbPath}/Multa`);
@@ -17,7 +18,6 @@ const regresar = async (body) => {
'id operador regreso',
true
);
let horaFin = moment();
let diferencia = null;
return Operador.findOne({ where: { idOperador: idOperadorRegreso } })
@@ -34,9 +34,8 @@ const regresar = async (body) => {
throw new Error('Este préstamo ya no esta activo.');
if (!res.Equipo.idStatus === 1)
throw new Error('Aun no se ha entregado el equipo al usuario.');
horaFin = moment(res.horaFin);
diferencia = (ahora - horaFin) / 60000;
if (diferencia >= 15) {
diferencia = parseInt((ahora - moment(res.horaFin)) / 60000);
if (diferencia >= 15)
await Multa.create({
idPrestamo,
idInfraccion: 1,
@@ -49,19 +48,18 @@ const regresar = async (body) => {
{ where: { idUsuario: res.idUsuario } }
)
);
}
return Equipo.update(
{ idStatus: 4 },
{ where: { idEquipo: res.Equipo.idEquipo } }
{ where: { idEquipo: res.idEquipo } }
);
})
.then((res) =>
.then(() =>
Prestamo.update(
{ idOperadorRegreso, horaEntrega: ahora.format(), activo: false },
{ where: { idPrestamo } }
)
)
.then((res) => {
.then(() => {
io.getIO().emit('actualizar');
return {
message: 'Se ha regresado correctamente el equipo de cómputo.',
@@ -22,7 +22,6 @@ const regresarNumeroInventario = async (body) => {
'id operador regreso',
true
);
let horaFin = moment();
let diferencia = null;
let idPrestamo = null;
@@ -50,8 +49,7 @@ const regresarNumeroInventario = async (body) => {
if (!res.Equipo.idStatus === 1)
throw new Error('Aun no se ha entregado el equipo al usuario.');
idPrestamo = res.idPrestamo;
horaFin = moment(res.horaFin);
diferencia = (ahora - horaFin) / 60000;
diferencia = parseInt((ahora - moment(res.horaFin)) / 60000);
if (diferencia >= 15)
await Multa.create({
idPrestamo,
@@ -62,16 +60,16 @@ const regresarNumeroInventario = async (body) => {
});
return Equipo.update(
{ idStatus: 4 },
{ where: { idEquipo: res.Equipo.idEquipo } }
{ where: { idEquipo: res.idEquipo } }
);
})
.then((res) => {
.then(() => {
Prestamo.update(
{ idOperadorRegreso, horaEntrega: ahora.format(), activo: false },
{ where: { idPrestamo } }
);
})
.then((res) => {
.then(() => {
io.getIO().emit('actualizar');
return {
message: 'Se ha regresado correctamente el equipo de cómputo.',
@@ -11,7 +11,6 @@ const regresoInmediato = async (body) => {
const ahora = moment();
const ahoraAux = moment();
const idPrestamo = validarNumeroEntero(body.idPrestamo, 'id prestamo', true);
let horaFin = moment();
let diferencia = null;
return Prestamo.findOne({
@@ -23,33 +22,31 @@ const regresoInmediato = async (body) => {
if (!res.activo) throw new Error('Este préstamo ya no esta activo.');
if (!res.Equipo.idStatus === 1)
throw new Error('Aun no se ha entregado el equipo al usuario.');
horaFin = moment(res.horaFin);
diferencia = parseInt((ahora - horaFin) / 60000);
if (diferencia >= 15) {
diferencia = parseInt((ahora - moment(res.horaFin)) / 60000);
if (diferencia >= 15)
await Multa.create({
idPrestamo,
idInfraccion: 1,
descripcion: `Se paso por ${diferencia} minutos para entregar el equipo.`,
fechaFin: ahoraAux.add((diferencia / 15) * 7, 'd').format(),
fechaFin: ahoraAux.add(parseInt(diferencia / 15) * 7, 'd').format(),
}).then((res) =>
Usuario.update(
{ multa: true },
{ where: { idUsuario: res.idUsuario } }
)
);
}
return Equipo.update(
{ idStatus: 3 },
{ where: { idEquipo: res.Equipo.idEquipo } }
{ where: { idEquipo: res.idEquipo } }
);
})
.then((res) =>
.then(() =>
Prestamo.update(
{ horaEntrega: ahora.format(), activo: false, regresoInmediato: true },
{ where: { idPrestamo } }
)
)
.then((res) => {
.then(() => {
io.getIO().emit('actualizar');
return {
message: 'Se ha regresado correctamente el equipo de cómputo.',
@@ -16,7 +16,6 @@ const regresoInmediatoNumeroInventario = async (body) => {
true,
20
);
let horaFin = moment();
let diferencia = null;
let idPrestamo = null;
@@ -29,33 +28,31 @@ const regresoInmediatoNumeroInventario = async (body) => {
if (!res.activo) throw new Error('Este préstamo ya no esta activo.');
if (!res.Equipo.idStatus === 1)
throw new Error('Aun no se ha entregado el equipo al usuario.');
horaFin = moment(res.horaFin);
diferencia = parseInt((ahora - horaFin) / 60000);
if (diferencia >= 15) {
diferencia = parseInt((ahora - moment(res.horaFin)) / 60000);
if (diferencia >= 15)
await Multa.create({
idPrestamo,
idInfraccion: 1,
descripcion: `Se paso por ${diferencia} minutos para entregar el equipo.`,
fechaFin: ahoraAux.add((diferencia / 15) * 7, 'd').format(),
fechaFin: ahoraAux.add(parseInt(diferencia / 15) * 7, 'd').format(),
}).then((res) =>
Usuario.update(
{ multa: true },
{ where: { idUsuario: res.idUsuario } }
)
);
}
return Equipo.update(
{ idStatus: 3 },
{ where: { idEquipo: res.Equipo.idEquipo } }
{ where: { idEquipo: res.idEquipo } }
);
})
.then((res) =>
.then(() =>
Prestamo.update(
{ horaEntrega: ahora.format(), activo: false, regresoInmediato: true },
{ where: { idPrestamo } }
)
)
.then((res) => {
.then(() => {
io.getIO().emit('actualizar');
return {
message: 'Se ha regresado correctamente el equipo de cómputo.',
+1 -1
View File
@@ -35,7 +35,7 @@ const status = async (body) => {
await Equipo.update(
{ idStatus: 4 },
{ where: { idEquipo: res.Equipo.idEquipo } }
).then((res) =>
).then(() =>
Prestamo.update({ activo: false }, { where: { idPrestamo } })
);
message =