diff --git a/server/controller/Carrito/update.js b/server/controller/Carrito/update.js index 583e17f..71d79e9 100644 --- a/server/controller/Carrito/update.js +++ b/server/controller/Carrito/update.js @@ -6,15 +6,16 @@ const update = async (body) => { const idCarrito = validarNumeroEntero(body.idCarrito, 'id carrito', true); let update = {}; - if (body.activo === 'desactivar') update.activo = false; - else if (body.activo === 'activar') update.activo = true; - else throw new Error('No se envio nada para actualizar.'); return Carrito.findOne({ where: { idCarrito } }) .then((res) => { if (!res) throw new Error('No existe este carrito.'); + // habilitar cambio de modulo u tipo de equipo + if (body.activo === 'desactivar') update.activo = false; + else if (body.activo === 'activar') update.activo = true; + else throw new Error('No se envio nada para actualizar.'); return Carrito.update(update, { where: { idCarrito } }); }) - .then((res) => ({ message: 'Se actualizó correctamente el carrito.' })); + .then(() => ({ message: 'Se actualizó correctamente el carrito.' })); }; module.exports = update; diff --git a/server/controller/Equipo/update.js b/server/controller/Equipo/update.js index 30560a3..e9ef8fa 100644 --- a/server/controller/Equipo/update.js +++ b/server/controller/Equipo/update.js @@ -50,7 +50,7 @@ const update = async (body) => { if (!res) throw new Error('No existe este equipo.'); return Equipo.update(update, { where: { idEquipo } }); }) - .then((res) => ({ message: 'Se actualizó correctamente el equipo.' })); + .then(() => ({ message: 'Se actualizó correctamente el equipo.' })); }; module.exports = update; diff --git a/server/controller/Infraccion/get.js b/server/controller/Infraccion/get.js index fa12ecc..a93847d 100644 --- a/server/controller/Infraccion/get.js +++ b/server/controller/Infraccion/get.js @@ -1,5 +1,5 @@ -const Infraccion = require('../../db/tablas/Infraccion'); const { Op } = require('sequelize'); +const Infraccion = require('../../db/tablas/Infraccion'); const get = async () => Infraccion.findAll({ where: { idInfraccion: { [Op.ne]: 1 } } }); diff --git a/server/controller/Multa/historialMultasEquipo.js b/server/controller/Multa/historialMultasEquipo.js index 55a02cb..a7d8eaf 100644 --- a/server/controller/Multa/historialMultasEquipo.js +++ b/server/controller/Multa/historialMultasEquipo.js @@ -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', ], }, diff --git a/server/controller/Multa/historialMultasUsuario.js b/server/controller/Multa/historialMultasUsuario.js index b012798..1125cec 100644 --- a/server/controller/Multa/historialMultasUsuario.js +++ b/server/controller/Multa/historialMultasUsuario.js @@ -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', ], }, diff --git a/server/controller/Multa/multar.js b/server/controller/Multa/multar.js index 004ec2e..80ba34f 100644 --- a/server/controller/Multa/multar.js +++ b/server/controller/Multa/multar.js @@ -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.`, })); }; diff --git a/server/controller/Multa/multarIdPrestamo.js b/server/controller/Multa/multarIdPrestamo.js index 5e17b68..0970863 100644 --- a/server/controller/Multa/multarIdPrestamo.js +++ b/server/controller/Multa/multarIdPrestamo.js @@ -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, diff --git a/server/controller/Multa/multarNumeroInventario.js b/server/controller/Multa/multarNumeroInventario.js index dd0eb0f..59affec 100644 --- a/server/controller/Multa/multarNumeroInventario.js +++ b/server/controller/Multa/multarNumeroInventario.js @@ -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', + 'nú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.`, })); }; diff --git a/server/controller/Prestamo/activos.js b/server/controller/Prestamo/activos.js index 4e8422c..d5441d0 100644 --- a/server/controller/Prestamo/activos.js +++ b/server/controller/Prestamo/activos.js @@ -47,7 +47,12 @@ const activos = async (body) => { if (!res) throw new Error('No existe este modulo.'); return Prestamo.findAndCountAll({ where: { - [Op.or]: [{ regresoInmediato: true }, { activo: true }], + [Op.or]: [ + { + [Op.and]: [{ regresoInmediato: true }, { idOperadorRegreso: null }], + }, + { activo: true }, + ], idPrestamo: { [Op.like]: `%${idPrestamo}%` }, }, include: [ diff --git a/server/controller/Prestamo/cancelarOperador.js b/server/controller/Prestamo/cancelarOperador.js index 604e818..77d155e 100644 --- a/server/controller/Prestamo/cancelarOperador.js +++ b/server/controller/Prestamo/cancelarOperador.js @@ -15,7 +15,7 @@ const cancelarOperador = async (body) => { return Operador.findOne({ where: { idOperador: idOperadorCancelacion } }) .then((res) => { - if (!res) throw new Error('No existe esta este operador.'); + if (!res) throw new Error('No existe este operador.'); return Prestamo.findOne({ where: { idPrestamo }, include: [{ model: Equipo }],