From 2bdeb2973ad74e33e1ca45671adc3f5885b1d28c Mon Sep 17 00:00:00 2001 From: consultoria-as Date: Mon, 29 Jun 2026 23:54:58 +0000 Subject: [PATCH] fix(audit): corrige errores criticos y mayores, mejora UX/accesibilidad y optimiza rendimiento - Arregla @require_auth, permisos, race conditions, locks de caja/stock - Elimina N+1 en layaway, flotilla, dashboard y global_invoice - Asegura folios atomicos para CFDI, ordenes de servicio y polizas - Protege client_secret de MercadoLibre en backend - Conecta botones/filtros de config, customers, accounting e invoicing - Mejora accesibilidad (labels/aria-label) y estados de carga/vacio - Limpia accounting.js obsoleto y consolida accounting.v9.js - Actualiza cache busting a v32 y Service Worker a v32 - Documenta todo en docs/AUDIT_Y_MEJORAS_2026-06-15.md Tests: 35 passed --- docs/AUDIT_Y_MEJORAS_2026-06-15.md | 140 ++++++ pos/blueprints/accounting_bp.py | 186 ++++++- pos/blueprints/config_bp.py | 17 +- pos/blueprints/customers_bp.py | 61 ++- pos/blueprints/dashboard_stats_bp.py | 67 +++ pos/blueprints/fleet_bp.py | 129 ++++- pos/blueprints/invoicing_bp.py | 2 +- pos/blueprints/marketplace_external_bp.py | 62 ++- pos/blueprints/pos_bp.py | 142 +++++- pos/blueprints/service_order_bp.py | 86 +++- pos/blueprints/tasks_bp.py | 2 +- pos/migrations/runner.py | 4 + pos/migrations/v4.10_fleet_permissions.sql | 20 + pos/migrations/v4.7_workshop_business.sql | 11 + pos/migrations/v4.8_workshop_permissions.sql | 15 + .../v4.9_workshop_customers_view.sql | 7 + pos/services/accounting_engine.py | 19 +- pos/services/cfdi_queue.py | 22 +- pos/services/global_invoice.py | 73 +-- pos/services/pos_engine.py | 16 +- pos/services/service_order_engine.py | 145 ++++-- pos/services/tenant_manager.py | 3 +- pos/static/css/accounting.css | 42 ++ pos/static/css/pos-ui.css | 10 + pos/static/css/pos.css | 15 +- pos/static/css/workshop.css | 230 +++++++++ pos/static/js/accounting.min.js | 1 - .../js/{accounting.js => accounting.v9.js} | 126 ++++- pos/static/js/api.js | 27 + pos/static/js/config.js | 71 ++- pos/static/js/customers.js | 44 +- pos/static/js/dashboard.js | 104 ++-- pos/static/js/fleet.js | 114 +++-- pos/static/js/inventory.js | 37 +- pos/static/js/invoicing.js | 190 ++++--- pos/static/js/marketplace_external.js | 37 +- pos/static/js/pos-utils.js | 10 +- pos/static/js/pos.js | 194 ++++++- pos/static/js/sidebar.js | 58 ++- pos/static/js/workshop.js | 474 +++++++++++++----- pos/static/pwa/sw.js | 7 +- pos/templates/accounting.html | 49 +- pos/templates/catalog.html | 16 +- pos/templates/config.html | 12 +- pos/templates/customers.html | 34 +- pos/templates/dashboard.html | 46 +- pos/templates/diagrams.html | 6 +- pos/templates/fleet.html | 6 +- pos/templates/inventory.html | 12 +- pos/templates/invoicing.html | 55 +- pos/templates/marketplace.html | 2 +- pos/templates/marketplace_external.html | 8 +- pos/templates/pos.html | 85 +++- pos/templates/quotations.html | 6 +- pos/templates/reports.html | 8 +- pos/templates/supplier_catalog.html | 4 +- pos/templates/whatsapp.html | 8 +- pos/templates/workshop.html | 116 ++++- pos/tests/test_service_order_integration.py | 21 +- requirements.txt | 1 + .../allocate_existing_customer_payments.py | 70 +++ 61 files changed, 2879 insertions(+), 706 deletions(-) create mode 100644 docs/AUDIT_Y_MEJORAS_2026-06-15.md create mode 100644 pos/migrations/v4.10_fleet_permissions.sql create mode 100644 pos/migrations/v4.7_workshop_business.sql create mode 100644 pos/migrations/v4.8_workshop_permissions.sql create mode 100644 pos/migrations/v4.9_workshop_customers_view.sql delete mode 100644 pos/static/js/accounting.min.js rename pos/static/js/{accounting.js => accounting.v9.js} (85%) create mode 100644 pos/static/js/api.js create mode 100644 scripts/allocate_existing_customer_payments.py diff --git a/docs/AUDIT_Y_MEJORAS_2026-06-15.md b/docs/AUDIT_Y_MEJORAS_2026-06-15.md new file mode 100644 index 0000000..630f769 --- /dev/null +++ b/docs/AUDIT_Y_MEJORAS_2026-06-15.md @@ -0,0 +1,140 @@ +# Auditoría y Mejoras del Sistema Nexus POS — 15 de junio de 2026 + +## Resumen ejecutivo + +Se realizó una revisión completa del sistema Nexus POS enfocada en corregir errores críticos y mayores, eliminar código muerto, cerrar brechas de seguridad menores y estandarizar la experiencia de usuario. También se aplicaron optimizaciones de rendimiento en consultas frecuentes y se mejoró la accesibilidad de los formularios. + +- **Rama/base:** `main` +- **Tenant de pruebas:** `tenant_refaccionaria_la_casita` (`tenant_id = 33`) +- **Servicio:** `nexus-pos.service` (puerto 5001) +- **Estado final:** operativo, tests pasando + +--- + +## 1. Problemas críticos corregidos + +| Problema | Solución | Archivos principales | +|----------|----------|----------------------| +| `@require_auth` sin paréntesis en `tasks_bp` | Se cambió al decorador correcto | `pos/blueprints/tasks_bp.py` | +| Permiso inexistente `accounting.read` | Se reemplazó por `accounting.view` | `pos/blueprints/accounting_bp.py` | +| `historical_sales.html` no cargaba `api.js` | Se incluyó el helper compartido | `pos/templates/historical_sales.html`, `pos/static/js/api.js` | +| `featureProximamente` no estaba disponible en `customers.js` | Se expuso la función en el módulo | `pos/static/js/customers.js` | +| `pos_engine` usaba `threading` sin importar y la caja sin `FOR UPDATE` | Se agregó `import threading` y lock pesimista de caja | `pos/services/pos_engine.py` | +| Race condition en stock de ventas | Se agregó `SELECT ... FOR UPDATE` sobre `inventory_stock` | `pos/services/pos_engine.py` | + +--- + +## 2. Problemas mayores corregidos + +| Problema | Solución | Archivos principales | +|----------|----------|----------------------| +| N+1 en `complete_layaway` | Se cargan todos los items en una sola consulta | `pos/services/pos_engine.py` | +| Race conditions en folios CFDI y números de póliza | Se usó `pg_advisory_xact_lock(hashtext(prefix))` | `pos/services/cfdi_queue.py`, `pos/services/accounting_engine.py` | +| N+1 en flotilla (schedules/history) | Se crearon endpoints bulk `/vehicles/schedules` y `/vehicles/history` | `pos/blueprints/fleet_bp.py`, `pos/static/js/fleet.js` | +| N+1 en dashboard (ventas recientes) y `global_invoice` | Se consolidaron consultas | `pos/blueprints/dashboard_stats_bp.py`, `pos/services/global_invoice.py` | +| `client_secret` de MercadoLibre expuesto en frontend | Se movió el flujo OAuth a backend con endpoint `/connect/init` | `pos/blueprints/marketplace_external_bp.py`, `pos/static/js/marketplace_external.js` | +| Botones/filtros sin handler en config, customers y accounting | Se conectaron eventos y handlers faltantes | `pos/static/js/config.js`, `pos/static/js/customers.js`, `pos/static/js/accounting.v9.js`, `pos/templates/*.html` | +| Botones/filtros sin handler en invoicing | Se implementaron filtros, export CSV y acciones | `pos/static/js/invoicing.js`, `pos/templates/invoicing.html` | + +--- + +## 3. Mejoras de rendimiento + +- **Locks atómicos:** los folios de órdenes de servicio, CFDI y pólizas contables ahora se generan bajo `pg_advisory_xact_lock` para evitar duplicados bajo concurrencia. +- **Consultas bulk:** flotilla y dashboard redujeron drásticamente el número de queries al backend. +- **Virtual scroll:** se mantiene en tablas grandes (clientes, programas de mantenimiento, historial) para renderizado eficiente. +- **Cache busting:** se actualizaron los query strings estáticos a `?v=32` y el Service Worker a `v32` para forzar la actualización de assets en navegadores y PWA. + +--- + +## 4. Mejoras de UX y accesibilidad + +- **Formularios accesibles:** se agregaron `aria-label`, `id` y `