Complete implementation of loyalty module Phase 2 features: Database & Models: - Add company_id to LoyaltyProgram for chain-wide loyalty - Add company_id to LoyaltyCard for multi-location support - Add CompanyLoyaltySettings model for admin-controlled settings - Add points expiration, welcome bonus, and minimum redemption fields - Add POINTS_EXPIRED, WELCOME_BONUS transaction types Services: - Update program_service for company-based queries - Update card_service with enrollment and welcome bonus - Update points_service with void_points for returns - Update stamp_service for company context - Update pin_service for company-wide operations API Endpoints: - Admin: Program listing with stats, company detail views - Vendor: Terminal operations, card management, settings - Storefront: Customer card/transactions, self-enrollment UI Templates: - Admin: Programs dashboard, company detail, settings - Vendor: Terminal, cards list, card detail, settings, stats, enrollment - Storefront: Dashboard, history, enrollment, success pages Background Tasks: - Point expiration task (daily, based on inactivity) - Wallet sync task (hourly) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
152 lines
4.0 KiB
Python
152 lines
4.0 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.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,
|
|
},
|
|
)
|
|
|
|
context = get_storefront_context(request, db=db)
|
|
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,
|
|
)
|