Eliminate all 103 errors and 96 warnings from the architecture validator: Phase 1 - Validator rules & YAML: - Add NAM-001/NAM-002 exceptions for module-scoped router/service files - Fix API-004 to detect # public comments on decorator lines - Add module-specific exception bases to EXC-004 valid_bases - Exclude storefront files from AUTH-004 store context check - Add SVC-006 exceptions for loyalty service atomic commits - Fix _get_rule() to search naming_rules and auth_rules categories - Use plain # CODE comments instead of # noqa: CODE for custom rules Phase 2 - Billing module (5 route files): - Move _resolve_store_to_merchant to subscription_service - Move tier/feature queries to feature_service, admin_subscription_service - Extract 22 inline Pydantic schemas to billing/schemas/billing.py - Replace all HTTPException with domain exceptions Phase 3 - Loyalty module (4 routes + points_service): - Add 7 domain exceptions (Apple auth, enrollment, device registration) - Add service methods to card_service, program_service, apple_wallet_service - Move all db.query() from routes to service layer - Fix SVC-001: replace HTTPException in points_service with domain exception Phase 4 - Remaining modules: - tenancy: move store stats queries to admin_service - cms: move platform resolution to content_page_service, add NoPlatformSubscriptionException - messaging: move user/customer lookups to messaging_service - Add ConfigDict(from_attributes=True) to ContentPageResponse Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
185 lines
5.5 KiB
Python
185 lines
5.5 KiB
Python
# app/modules/loyalty/routes/api/storefront.py
|
|
"""
|
|
Loyalty Module - Storefront API Routes
|
|
|
|
Customer-facing endpoints for:
|
|
- View loyalty card and balance
|
|
- View transaction history
|
|
- Self-service enrollment
|
|
- Get program information
|
|
|
|
Uses store from middleware context (StoreContextMiddleware).
|
|
"""
|
|
|
|
import logging
|
|
|
|
from fastapi import APIRouter, Depends, Query, Request
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.api.deps import get_current_customer_api
|
|
from app.core.database import get_db
|
|
from app.modules.customers.schemas import CustomerContext
|
|
from app.modules.loyalty.schemas import (
|
|
CardEnrollRequest,
|
|
CardResponse,
|
|
ProgramResponse,
|
|
)
|
|
from app.modules.loyalty.services import card_service, program_service
|
|
from app.modules.tenancy.exceptions import StoreNotFoundException
|
|
|
|
storefront_router = APIRouter()
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
# =============================================================================
|
|
# Public Endpoints (No Authentication Required)
|
|
# =============================================================================
|
|
|
|
|
|
@storefront_router.get("/loyalty/program")
|
|
def get_program_info(
|
|
request: Request,
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""
|
|
Get loyalty program information for current store.
|
|
Public endpoint - no authentication required.
|
|
"""
|
|
store = getattr(request.state, "store", None)
|
|
if not store:
|
|
raise StoreNotFoundException("context", identifier_type="subdomain")
|
|
|
|
program = program_service.get_program_by_store(db, store.id)
|
|
if not program:
|
|
return None
|
|
|
|
response = ProgramResponse.model_validate(program)
|
|
response.is_stamps_enabled = program.is_stamps_enabled
|
|
response.is_points_enabled = program.is_points_enabled
|
|
response.display_name = program.display_name
|
|
|
|
return response
|
|
|
|
|
|
@storefront_router.post("/loyalty/enroll")
|
|
def self_enroll(
|
|
request: Request,
|
|
data: CardEnrollRequest,
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""
|
|
Self-service enrollment.
|
|
Public endpoint - customers can enroll via QR code without authentication.
|
|
"""
|
|
store = getattr(request.state, "store", None)
|
|
if not store:
|
|
raise StoreNotFoundException("context", identifier_type="subdomain")
|
|
|
|
# Check if self-enrollment is allowed
|
|
program_service.check_self_enrollment_allowed(db, store.merchant_id)
|
|
|
|
# Resolve customer_id
|
|
customer_id = card_service.resolve_customer_id(
|
|
db,
|
|
customer_id=data.customer_id,
|
|
email=data.email,
|
|
store_id=store.id,
|
|
)
|
|
|
|
logger.info(f"Self-enrollment for customer {customer_id} at store {store.subdomain}")
|
|
|
|
card = card_service.enroll_customer_for_store(db, customer_id, store.id)
|
|
|
|
return CardResponse.model_validate(card)
|
|
|
|
|
|
# =============================================================================
|
|
# Authenticated Endpoints
|
|
# =============================================================================
|
|
|
|
|
|
@storefront_router.get("/loyalty/card")
|
|
def get_my_card(
|
|
request: Request,
|
|
customer: CustomerContext = Depends(get_current_customer_api),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""
|
|
Get customer's loyalty card and program info.
|
|
Returns card details, program info, and available rewards.
|
|
"""
|
|
store = getattr(request.state, "store", None)
|
|
if not store:
|
|
raise StoreNotFoundException("context", identifier_type="subdomain")
|
|
|
|
logger.debug(f"Getting loyalty card for customer {customer.id}")
|
|
|
|
# Get program
|
|
program = program_service.get_program_by_store(db, store.id)
|
|
if not program:
|
|
return {"card": None, "program": None, "locations": []}
|
|
|
|
# Look up card by customer ID and merchant
|
|
card = card_service.get_card_by_customer_and_merchant(
|
|
db,
|
|
customer_id=customer.id,
|
|
merchant_id=program.merchant_id,
|
|
)
|
|
|
|
if not card:
|
|
return {"card": None, "program": None, "locations": []}
|
|
|
|
# Get merchant locations
|
|
locations = program_service.get_merchant_locations(db, program.merchant_id)
|
|
|
|
program_response = ProgramResponse.model_validate(program)
|
|
program_response.is_stamps_enabled = program.is_stamps_enabled
|
|
program_response.is_points_enabled = program.is_points_enabled
|
|
program_response.display_name = program.display_name
|
|
|
|
return {
|
|
"card": CardResponse.model_validate(card),
|
|
"program": program_response,
|
|
"locations": [{"id": v.id, "name": v.name} for v in locations],
|
|
}
|
|
|
|
|
|
@storefront_router.get("/loyalty/transactions")
|
|
def get_my_transactions(
|
|
request: Request,
|
|
skip: int = Query(0, ge=0),
|
|
limit: int = Query(20, ge=1, le=100),
|
|
customer: CustomerContext = Depends(get_current_customer_api),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""
|
|
Get customer's loyalty transaction history.
|
|
"""
|
|
store = getattr(request.state, "store", None)
|
|
if not store:
|
|
raise StoreNotFoundException("context", identifier_type="subdomain")
|
|
|
|
logger.debug(f"Getting transactions for customer {customer.id}")
|
|
|
|
# Get program
|
|
program = program_service.get_program_by_store(db, store.id)
|
|
if not program:
|
|
return {"transactions": [], "total": 0}
|
|
|
|
# Get card
|
|
card = card_service.get_card_by_customer_and_merchant(
|
|
db,
|
|
customer_id=customer.id,
|
|
merchant_id=program.merchant_id,
|
|
)
|
|
|
|
if not card:
|
|
return {"transactions": [], "total": 0}
|
|
|
|
# Get transactions with store names
|
|
tx_responses, total = card_service.get_customer_transactions_with_store_names(
|
|
db, card.id, skip=skip, limit=limit
|
|
)
|
|
|
|
return {"transactions": tx_responses, "total": total}
|