fix: additional code quality and performance improvements

Code quality:
- Remove empty except blocks with proper logging
- Create shared pagination utility function
- Remove duplicate UUID validation code
- Fix dead code in translation.py

Performance:
- Fix N+1 query in followup engine (use join instead of loop)
- Add eager loading for customer health scores
- Create database indexes for common query patterns:
  - customers: (user_id, status), (user_id, last_contact_at)
  - payment_transactions: (user_id, created_at)
  - followup_logs: (user_id, customer_id)
  - notifications: (user_id, is_read)

Configuration:
- Centralize magic numbers in config.py:
  - Payment prices
  - File upload limits
  - Rate limiting settings
  - Pagination defaults
- Update auth.py to use centralized rate limiting config
- Update customer/product imports to use centralized upload limits
- Update import_service.py to use centralized MAX_ROWS
This commit is contained in:
TradeMate Dev
2026-06-11 18:25:08 +08:00
parent 13e3992d4c
commit 9e9c7ac270
11 changed files with 138 additions and 16 deletions
+4 -3
View File
@@ -155,7 +155,6 @@ async def login(
async def guest_login(request: Request, db: AsyncSession = Depends(get_db)):
# Rate limiting: max 5 guest logins per IP per 15 minutes
from app.core.redis import get_redis
import time
client_ip = request.client.host if request.client else "unknown"
cache_key = f"guest_login:{client_ip}"
@@ -163,8 +162,8 @@ async def guest_login(request: Request, db: AsyncSession = Depends(get_db)):
try:
redis_client = await get_redis()
now = int(time.time())
window = 900 # 15 minutes
limit = 5
window = settings.GUEST_LOGIN_WINDOW # 15 minutes
limit = settings.GUEST_LOGIN_LIMIT
# Get count of logins in current window
count = await redis_client.get(cache_key)
@@ -180,6 +179,8 @@ async def guest_login(request: Request, db: AsyncSession = Depends(get_db)):
pipe.expire(cache_key, window)
await pipe.execute()
except HTTPException:
raise
except Exception:
# If Redis is down, proceed without rate limiting
pass
+4 -1
View File
@@ -136,7 +136,10 @@ async def delete_customer(
return {"message": "Customer deleted"}
MAX_UPLOAD_SIZE = 10 * 1024 * 1024 # 10MB
from app.config import settings
MAX_UPLOAD_SIZE = settings.MAX_UPLOAD_SIZE
@router.post("/import")
async def import_customers(
+4 -1
View File
@@ -102,7 +102,10 @@ async def import_products(
):
from app.services.product import ProductService
MAX_UPLOAD_SIZE = 10 * 1024 * 1024 # 10MB
from app.config import settings
MAX_UPLOAD_SIZE = settings.MAX_UPLOAD_SIZE
filename = file.filename or "unknown"
file_size = 0