Files
orion/app/modules/loyalty/routes/pages/storefront.py
Samir Boulahtit a28d5d1de5
Some checks failed
CI / pytest (push) Waiting to run
CI / ruff (push) Successful in 13s
CI / dependency-scanning (push) Has been cancelled
CI / docs (push) Has been cancelled
CI / deploy (push) Has been cancelled
CI / validate (push) Has been cancelled
fix(i18n): convert remaining $t() to server-side _() and fix store dashboard language
- Convert storefront enrollment $t() calls to server-side _() to silence
  dev-toolbar warnings (welcome bonus + join button)
- Fix store base template I18n.init() to use current_language (from middleware)
  instead of dashboard_language (hardcoded store config) so language changes
  take effect immediately
- Switch admin loyalty routes to use get_admin_context() for proper i18n support
- Switch store loyalty routes to use core get_store_context() from page_context
- Pass program object to storefront enrollment context for server-side rendering
- Add LANG-011 architecture rule: enforce $t()/_() over I18n.t() in templates
- Fix duplicate file_pattern key in LANG-004 rule (YAML validation error)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-14 17:00:42 +01:00

156 lines
4.2 KiB
Python

# app/modules/loyalty/routes/pages/storefront.py
"""
Loyalty Storefront Page Routes (HTML rendering).
Customer-facing pages for:
- Loyalty dashboard (view points, rewards, history)
- Self-service enrollment
- Digital card display
"""
import logging
from fastapi import APIRouter, Depends, Query, Request
from fastapi.responses import HTMLResponse
from sqlalchemy.orm import Session
from app.api.deps import get_current_customer_from_cookie_or_header, get_db
from app.modules.core.utils.page_context import get_storefront_context
from app.modules.customers.models import Customer
from app.modules.loyalty.services import program_service
from app.templates_config import templates
logger = logging.getLogger(__name__)
router = APIRouter()
# No custom prefix - routes are mounted directly at /storefront/
# Following same pattern as customers module
# ============================================================================
# CUSTOMER LOYALTY DASHBOARD (Authenticated)
# ============================================================================
@router.get(
"/account/loyalty",
response_class=HTMLResponse,
include_in_schema=False,
)
async def customer_loyalty_dashboard(
request: Request,
current_customer: Customer = Depends(get_current_customer_from_cookie_or_header),
db: Session = Depends(get_db),
):
"""
Render customer loyalty dashboard.
Shows points balance, available rewards, and transaction history.
"""
logger.debug(
"[STOREFRONT] customer_loyalty_dashboard REACHED",
extra={
"path": request.url.path,
"customer_id": current_customer.id if current_customer else None,
},
)
context = get_storefront_context(request, db=db)
return templates.TemplateResponse(
"loyalty/storefront/dashboard.html",
context,
)
@router.get(
"/account/loyalty/history",
response_class=HTMLResponse,
include_in_schema=False,
)
async def customer_loyalty_history(
request: Request,
current_customer: Customer = Depends(get_current_customer_from_cookie_or_header),
db: Session = Depends(get_db),
):
"""
Render full transaction history page.
"""
logger.debug(
"[STOREFRONT] customer_loyalty_history REACHED",
extra={
"path": request.url.path,
"customer_id": current_customer.id if current_customer else None,
},
)
context = get_storefront_context(request, db=db)
return templates.TemplateResponse(
"loyalty/storefront/history.html",
context,
)
# ============================================================================
# SELF-SERVICE ENROLLMENT (Public - No Authentication)
# ============================================================================
@router.get(
"/loyalty/join",
response_class=HTMLResponse,
include_in_schema=False,
)
async def loyalty_self_enrollment(
request: Request,
db: Session = Depends(get_db),
):
"""
Render self-service enrollment page.
Public page - customers can sign up via QR code at store counter.
"""
logger.debug(
"[STOREFRONT] loyalty_self_enrollment REACHED",
extra={
"path": request.url.path,
},
)
store = request.state.store
program = program_service.get_active_program_by_store(db, store.id) if store else None
context = get_storefront_context(request, db=db, program=program)
return templates.TemplateResponse(
"loyalty/storefront/enroll.html",
context,
)
@router.get(
"/loyalty/join/success",
response_class=HTMLResponse,
include_in_schema=False,
)
async def loyalty_enrollment_success(
request: Request,
card: str = Query(None, description="Card number"),
db: Session = Depends(get_db),
):
"""
Render enrollment success page.
Shows card number and next steps.
"""
logger.debug(
"[STOREFRONT] loyalty_enrollment_success REACHED",
extra={
"path": request.url.path,
"card": card,
},
)
context = get_storefront_context(request, db=db)
context["enrolled_card_number"] = card
return templates.TemplateResponse(
"loyalty/storefront/enroll-success.html",
context,
)