mejora código

This commit is contained in:
xXpuma99Xx
2022-02-01 06:44:34 -06:00
parent b3851fd6a1
commit 4437ade10c
10 changed files with 90 additions and 75 deletions
@@ -1,23 +1,12 @@
const validar = require('../../helper/validar');
const { validarNumeroEntero } = require('../../helper/validar');
const dbPath = '../../db/tablas';
const Multa = require(`${dbPath}/Multa`);
const Operador = require(`${dbPath}/Operador`);
const Prestamo = require(`${dbPath}/Prestamo`);
const historialMultasEquipo = async (body) => {
const idEquipo = validar.validarNumeroEntero(
body.idEquipo,
'id equipo',
true
);
const pagina = validar.validarNumero(
body.pagina,
'página',
false,
null,
true,
true
);
const pagina = validarNumeroEntero(body.pagina, 'página', false);
const idEquipo = validarNumeroEntero(body.idEquipo, 'id equipo', true);
return Multa.findAndCountAll({
include: [
@@ -32,6 +21,7 @@ const historialMultasEquipo = async (body) => {
'horaFin',
'horaEntrega',
'canceladoUsuario',
'regresoInmediato',
'createdAt',
],
},
@@ -1,4 +1,4 @@
const validar = require('../../helper/validar');
const { validarNumeroEntero } = require('../../helper/validar');
const dbPath = '../../db/tablas';
const Carrito = require(`${dbPath}/Carrito`);
const Equipo = require(`${dbPath}/Equipo`);
@@ -10,19 +10,8 @@ const Prestamo = require(`${dbPath}/Prestamo`);
const TipoCarrito = require(`${dbPath}/TipoCarrito`);
const historialMultasUsuario = async (body) => {
const idUsuario = validar.validarNumeroEntero(
body.idUsuario,
'id usuario',
true
);
const pagina = validar.validarNumero(
body.pagina,
'página',
false,
null,
true,
true
);
const pagina = validarNumeroEntero(body.pagina, 'página', false);
const idUsuario = validarNumeroEntero(body.idUsuario, 'id usuario', true);
return Multa.findAndCountAll({
include: [
@@ -55,6 +44,7 @@ const historialMultasUsuario = async (body) => {
'horaFin',
'horaEntrega',
'canceladoUsuario',
'regresoInmediato',
'createdAt',
],
},
+22 -21
View File
@@ -1,4 +1,5 @@
const moment = require('moment');
const { Op } = require('sequelize');
const validar = require('../../helper/validar');
const dbPath = '../../db/tablas';
const Infraccion = require(`${dbPath}/Infraccion`);
@@ -13,11 +14,6 @@ const multar = async (body) => {
'id operador multa',
true
);
const idUsuario = validar.validarNumeroEntero(
body.idUsuario,
'id usuario',
true
);
const idInfraccion = validar.validarNumeroEntero(
body.idInfraccion,
'id infracción',
@@ -25,7 +21,7 @@ const multar = async (body) => {
);
const idPrestamo = validar.validarNumeroEntero(
body.idPrestamo,
'id prestamo',
'id préstamo',
true
);
const descripcion = validar.validarAlfanumerico(
@@ -34,8 +30,9 @@ const multar = async (body) => {
false,
500
);
const fechaFin = moment();
let fechaFin = moment();
let prestamo = {};
let gravedad = '';
return Operador.findOne({ where: { idOperador: idOperadorMulta } })
.then((res) => {
@@ -44,7 +41,9 @@ const multar = async (body) => {
})
.then((res) => {
if (!res) throw new Error('No existe esta infracción.');
if (res.gravedad == '1') fechaFin.add(7, 'd');
// Checar si tiene una multa activa, agragar días a multa
gravedad = res.gravedad;
if (gravedad === '1') fechaFin.add(7, 'd');
else fechaFin.add(100, 'y');
return Prestamo.findOne({
where: { idPrestamo },
@@ -52,39 +51,41 @@ const multar = async (body) => {
});
})
.then((res) => {
if (!res) throw new Error('No existe este prestamo.');
if (!res) throw new Error('No existe este préstamo.');
prestamo = res;
return Usuario.findOne({ where: { idUsuario } });
})
.then((res) => {
if (!res) throw new Error('No existe este usuario.');
return Prestamo.findOne({ where: { idUsuario, activo: true } });
return Prestamo.findOne({
where: { idUsuario: prestamo.idUsuario, activo: true },
});
})
.then((res) => {
if (res)
throw new Error(
'No se puede multar a este usuario ya que tiene un prestamo activo.'
'No se puede multar a este usuario ya que tiene un préstamo activo.'
);
return Multa.findOne({ where: { idPrestamo } });
return Multa.findOne({
where: { idPrestamo, idInfraccion: { [Op.ne]: 1 } },
});
})
.then((res) => {
if (res)
throw new Error('A este prestamo ya se le ha asignado una multa.');
throw new Error(
'Este préstamo ya ha sido usado para multar a un usuario.'
);
return Usuario.update(
{ multa: true },
{ where: { idUsuario: prestamo.Usuario.idUsuario } }
{ where: { idUsuario: prestamo.idUsuario } }
);
})
.then((res) =>
.then(() =>
Multa.create({
descripcion,
fechaFin: fechaFin.format(),
fechaFin: fechaFin.format('YYYY-MM-DD'),
idOperadorMulta,
idInfraccion,
idPrestamo,
})
)
.then((res) => ({
.then(() => ({
message: `Se levantó correctamente el reporte de multa.`,
}));
};
+25 -8
View File
@@ -1,4 +1,5 @@
const moment = require('moment');
const { Op } = require('sequelize');
const validar = require('../../helper/validar');
const dbPath = '../../db/tablas';
const Infraccion = require(`${dbPath}/Infraccion`);
@@ -20,7 +21,7 @@ const multar = async (body) => {
);
const idPrestamo = validar.validarNumeroEntero(
body.idPrestamo,
'id prestamo',
'id préstamo',
true
);
const descripcion = validar.validarAlfanumerico(
@@ -29,8 +30,9 @@ const multar = async (body) => {
false,
500
);
const fechaFin = moment();
let fechaFin = moment();
let prestamo = {};
let gravedad = '';
return Operador.findOne({ where: { idOperador: idOperadorMulta } })
.then((res) => {
@@ -39,7 +41,9 @@ const multar = async (body) => {
})
.then((res) => {
if (!res) throw new Error('No existe esta infracción.');
if (res.gravedad == '1') fechaFin.add(7, 'd');
// Checar si tiene una multa activa, agragar días a multa
gravedad = res.gravedad;
if (gravedad === '1') fechaFin.add(7, 'd');
else fechaFin.add(100, 'y');
return Prestamo.findOne({
where: { idPrestamo },
@@ -47,22 +51,35 @@ const multar = async (body) => {
});
})
.then((res) => {
if (!res) throw new Error('No existe este prestamo.');
if (!res) throw new Error('No existe este préstamo.');
prestamo = res;
return Multa.findOne({ where: { idPrestamo } });
return Prestamo.findOne({
where: { idUsuario: prestamo.idUsuario, activo: true },
});
})
.then((res) => {
if (res)
throw new Error('A este prestamo ya se le ha asignado una multa.');
throw new Error(
'No se puede multar a este usuario ya que tiene un préstamo activo.'
);
return Multa.findOne({
where: { idPrestamo, idInfraccion: { [Op.ne]: 1 } },
});
})
.then((res) => {
if (res)
throw new Error(
'Este préstamo ya ha sido usado para multar a un usuario.'
);
return Usuario.update(
{ multa: true },
{ where: { idUsuario: prestamo.Usuario.idUsuario } }
{ where: { idUsuario: prestamo.idUsuario } }
);
})
.then((res) =>
Multa.create({
descripcion,
fechaFin: fechaFin.format(),
fechaFin: fechaFin.format('YYYY-MM-DD'),
idOperadorMulta,
idInfraccion,
idPrestamo,
@@ -1,4 +1,5 @@
const moment = require('moment');
const { Op } = require('sequelize');
const validar = require('../../helper/validar');
const dbPath = '../../db/tablas';
const Infraccion = require(`${dbPath}/Infraccion`);
@@ -21,7 +22,7 @@ const multarNumeroInventario = async (body) => {
);
const numeroInventario = validar.validarAlfanumerico(
body.numeroInventario,
'El numero de inventario',
'mero de inventario',
20
);
const descripcion = validar.validarAlfanumerico(
@@ -30,17 +31,20 @@ const multarNumeroInventario = async (body) => {
false,
500
);
const fechaFin = moment();
let fechaFin = moment();
let prestamo = {};
let gravedad = '';
return Operador.findOne({ where: { idOperador: idOperadorMulta } })
.then((res) => {
if (!res) throw new Error('No existe esta este operador.');
if (!res) throw new Error('No existe este operador.');
return Infraccion.findOne({ where: { idInfraccion } });
})
.then((res) => {
if (!res) throw new Error('No existe esta infracción.');
if (res.gravedad == '1') fechaFin.add(7, 'd');
// Checar si tiene una multa activa, agragar días a multa
gravedad = res.gravedad;
if (gravedad === '1') fechaFin.add(7, 'd');
else fechaFin.add(100, 'y');
return Prestamo.findOne({
where: { activo: true },
@@ -54,26 +58,33 @@ const multarNumeroInventario = async (body) => {
if (!res)
throw new Error('No existe un prestamo activo con este equipo.');
prestamo = res;
return Multa.findOne({ where: { idPrestamo: prestamo.idPrestamo } });
return Multa.findOne({
where: {
idPrestamo: prestamo.idPrestamo,
idInfraccion: { [Op.ne]: 1 },
},
});
})
.then((res) => {
if (res)
throw new Error('A este prestamo ya se le ha asignado una multa.');
throw new Error(
'Este préstamo ya ha sido usado para multar a un usuario.'
);
return Usuario.update(
{ multa: true },
{ where: { idUsuario: prestamo.Usuario.idUsuario } }
{ where: { idUsuario: prestamo.idUsuario } }
);
})
.then((res) =>
.then(() =>
Multa.create({
descripcion,
fechaFin: fechaFin.format('YYYY-MM-DD'),
idOperadorMulta,
idInfraccion,
idPrestamo: prestamo.idPrestamo,
fechaFin: fechaFin.format(),
})
)
.then((res) => ({
.then(() => ({
message: `Se levantó correctamente el reporte de multa.`,
}));
};