Some checks failed
Move program CRUD from store to merchant/admin interfaces. Store becomes view-only for program config while merchant gets full CRUD and admin gets override capabilities. Merchant portal: - New API endpoints (GET/POST/PATCH/DELETE /program) - New settings page with create/edit/delete form - Overview page now has Create/Edit Program buttons - Settings menu item added to sidebar Admin portal: - New CRUD endpoints (create for merchant, update, delete) - New activate/deactivate program endpoints - Programs list has edit and toggle buttons per row - Merchant detail has create/delete/toggle program actions Store portal: - Removed POST/PATCH /program endpoints (now read-only) - Removed settings page route and template - Terminal, cards, stats, enroll unchanged Tests: 112 passed (58 new) covering merchant API, admin CRUD, store endpoint removal, and program service unit tests. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
130 lines
3.6 KiB
Python
130 lines
3.6 KiB
Python
# app/modules/loyalty/routes/pages/merchant.py
|
|
"""
|
|
Loyalty Merchant Page Routes (HTML rendering).
|
|
|
|
Merchant portal pages for:
|
|
- Loyalty overview (aggregate stats across all stores)
|
|
|
|
Authentication: merchant_token cookie or Authorization header.
|
|
|
|
Auto-discovered by the route system (merchant.py in routes/pages/ triggers
|
|
registration under /merchants/loyalty/*).
|
|
"""
|
|
|
|
import logging
|
|
|
|
from fastapi import APIRouter, Depends, Request
|
|
from fastapi.responses import HTMLResponse
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.api.deps import (
|
|
get_current_merchant_from_cookie_or_header,
|
|
get_merchant_for_current_user_page,
|
|
)
|
|
from app.core.database import get_db
|
|
from app.modules.core.utils.page_context import get_context_for_frontend
|
|
from app.modules.enums import FrontendType
|
|
from app.modules.loyalty.services import program_service
|
|
from app.modules.tenancy.models import Merchant
|
|
from app.templates_config import templates
|
|
from models.schema.auth import UserContext
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
ROUTE_CONFIG = {
|
|
"prefix": "/loyalty",
|
|
}
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
# ============================================================================
|
|
# Helper
|
|
# ============================================================================
|
|
|
|
|
|
def _get_merchant_context(
|
|
request: Request,
|
|
db: Session,
|
|
current_user: UserContext,
|
|
**extra_context,
|
|
) -> dict:
|
|
"""Build template context for merchant loyalty pages."""
|
|
return get_context_for_frontend(
|
|
FrontendType.MERCHANT,
|
|
request,
|
|
db,
|
|
user=current_user,
|
|
**extra_context,
|
|
)
|
|
|
|
|
|
# ============================================================================
|
|
# LOYALTY OVERVIEW
|
|
# ============================================================================
|
|
|
|
|
|
@router.get("/overview", response_class=HTMLResponse, include_in_schema=False)
|
|
async def merchant_loyalty_overview(
|
|
request: Request,
|
|
current_user: UserContext = Depends(get_current_merchant_from_cookie_or_header),
|
|
merchant: Merchant = Depends(get_merchant_for_current_user_page),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""
|
|
Render merchant loyalty overview page.
|
|
|
|
Shows aggregate loyalty program stats across all merchant stores.
|
|
"""
|
|
# Get merchant stats server-side
|
|
merchant_id = merchant.id
|
|
stats = {}
|
|
try:
|
|
stats = program_service.get_merchant_stats(db, merchant_id)
|
|
except Exception:
|
|
logger.warning(
|
|
f"Failed to load loyalty stats for merchant {merchant_id}",
|
|
exc_info=True,
|
|
)
|
|
|
|
context = _get_merchant_context(
|
|
request,
|
|
db,
|
|
current_user,
|
|
loyalty_stats=stats,
|
|
merchant_id=merchant_id,
|
|
)
|
|
return templates.TemplateResponse(
|
|
"loyalty/merchant/overview.html",
|
|
context,
|
|
)
|
|
|
|
|
|
# ============================================================================
|
|
# LOYALTY SETTINGS
|
|
# ============================================================================
|
|
|
|
|
|
@router.get("/settings", response_class=HTMLResponse, include_in_schema=False)
|
|
async def merchant_loyalty_settings(
|
|
request: Request,
|
|
current_user: UserContext = Depends(get_current_merchant_from_cookie_or_header),
|
|
merchant: Merchant = Depends(get_merchant_for_current_user_page),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""
|
|
Render merchant loyalty settings page.
|
|
|
|
Allows merchant to create, configure, and manage their loyalty program.
|
|
"""
|
|
context = _get_merchant_context(
|
|
request,
|
|
db,
|
|
current_user,
|
|
merchant_id=merchant.id,
|
|
)
|
|
return templates.TemplateResponse(
|
|
"loyalty/merchant/settings.html",
|
|
context,
|
|
)
|