refactor: move platform API database queries to service layer
- Create platform_signup_service.py for signup flow operations - Create platform_pricing_service.py for pricing data operations - Refactor signup.py, pricing.py, letzshop_vendors.py to use services - Add # public markers to all platform endpoints - Update tests for correct mock paths and status codes Fixes architecture validation errors (API-002, API-003, SVC-006). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -4,6 +4,8 @@ Public pricing API endpoints.
|
||||
|
||||
Provides subscription tier and add-on product information
|
||||
for the marketing homepage and signup flow.
|
||||
|
||||
All endpoints are public (no authentication required).
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
@@ -11,13 +13,9 @@ from pydantic import BaseModel
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.database import get_db
|
||||
from models.database.subscription import (
|
||||
AddOnProduct,
|
||||
BillingPeriod,
|
||||
SubscriptionTier,
|
||||
TIER_LIMITS,
|
||||
TierCode,
|
||||
)
|
||||
from app.exceptions import ResourceNotFoundException
|
||||
from app.services.platform_pricing_service import platform_pricing_service
|
||||
from models.database.subscription import TierCode
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -111,88 +109,13 @@ FEATURE_DESCRIPTIONS = {
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Endpoints
|
||||
# Helper Functions
|
||||
# =============================================================================
|
||||
|
||||
|
||||
@router.get("/tiers", response_model=list[TierResponse])
|
||||
def get_tiers(db: Session = Depends(get_db)) -> list[TierResponse]:
|
||||
"""
|
||||
Get all public subscription tiers.
|
||||
|
||||
Returns tiers from database if available, falls back to hardcoded TIER_LIMITS.
|
||||
"""
|
||||
# Try to get from database first
|
||||
db_tiers = (
|
||||
db.query(SubscriptionTier)
|
||||
.filter(
|
||||
SubscriptionTier.is_active == True,
|
||||
SubscriptionTier.is_public == True,
|
||||
)
|
||||
.order_by(SubscriptionTier.display_order)
|
||||
.all()
|
||||
)
|
||||
|
||||
if db_tiers:
|
||||
return [
|
||||
TierResponse(
|
||||
code=tier.code,
|
||||
name=tier.name,
|
||||
description=tier.description,
|
||||
price_monthly=tier.price_monthly_cents / 100,
|
||||
price_annual=(tier.price_annual_cents / 100) if tier.price_annual_cents else None,
|
||||
price_monthly_cents=tier.price_monthly_cents,
|
||||
price_annual_cents=tier.price_annual_cents,
|
||||
orders_per_month=tier.orders_per_month,
|
||||
products_limit=tier.products_limit,
|
||||
team_members=tier.team_members,
|
||||
order_history_months=tier.order_history_months,
|
||||
features=tier.features or [],
|
||||
is_popular=tier.code == TierCode.PROFESSIONAL.value,
|
||||
is_enterprise=tier.code == TierCode.ENTERPRISE.value,
|
||||
)
|
||||
for tier in db_tiers
|
||||
]
|
||||
|
||||
# Fallback to hardcoded tiers
|
||||
tiers = []
|
||||
for tier_code, limits in TIER_LIMITS.items():
|
||||
tiers.append(
|
||||
TierResponse(
|
||||
code=tier_code.value,
|
||||
name=limits["name"],
|
||||
description=None,
|
||||
price_monthly=limits["price_monthly_cents"] / 100,
|
||||
price_annual=(limits["price_annual_cents"] / 100) if limits.get("price_annual_cents") else None,
|
||||
price_monthly_cents=limits["price_monthly_cents"],
|
||||
price_annual_cents=limits.get("price_annual_cents"),
|
||||
orders_per_month=limits.get("orders_per_month"),
|
||||
products_limit=limits.get("products_limit"),
|
||||
team_members=limits.get("team_members"),
|
||||
order_history_months=limits.get("order_history_months"),
|
||||
features=limits.get("features", []),
|
||||
is_popular=tier_code == TierCode.PROFESSIONAL,
|
||||
is_enterprise=tier_code == TierCode.ENTERPRISE,
|
||||
)
|
||||
)
|
||||
|
||||
return tiers
|
||||
|
||||
|
||||
@router.get("/tiers/{tier_code}", response_model=TierResponse)
|
||||
def get_tier(tier_code: str, db: Session = Depends(get_db)) -> TierResponse:
|
||||
"""Get a specific tier by code."""
|
||||
# Try database first
|
||||
tier = (
|
||||
db.query(SubscriptionTier)
|
||||
.filter(
|
||||
SubscriptionTier.code == tier_code,
|
||||
SubscriptionTier.is_active == True,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
|
||||
if tier:
|
||||
def _tier_to_response(tier, is_from_db: bool = True) -> TierResponse:
|
||||
"""Convert a tier (from DB or hardcoded) to TierResponse."""
|
||||
if is_from_db:
|
||||
return TierResponse(
|
||||
code=tier.code,
|
||||
name=tier.name,
|
||||
@@ -209,11 +132,10 @@ def get_tier(tier_code: str, db: Session = Depends(get_db)) -> TierResponse:
|
||||
is_popular=tier.code == TierCode.PROFESSIONAL.value,
|
||||
is_enterprise=tier.code == TierCode.ENTERPRISE.value,
|
||||
)
|
||||
|
||||
# Fallback to hardcoded
|
||||
try:
|
||||
tier_enum = TierCode(tier_code)
|
||||
limits = TIER_LIMITS[tier_enum]
|
||||
else:
|
||||
# Hardcoded tier format
|
||||
tier_enum = tier["tier_enum"]
|
||||
limits = tier["limits"]
|
||||
return TierResponse(
|
||||
code=tier_enum.value,
|
||||
name=limits["name"],
|
||||
@@ -230,42 +152,85 @@ def get_tier(tier_code: str, db: Session = Depends(get_db)) -> TierResponse:
|
||||
is_popular=tier_enum == TierCode.PROFESSIONAL,
|
||||
is_enterprise=tier_enum == TierCode.ENTERPRISE,
|
||||
)
|
||||
except ValueError:
|
||||
from fastapi import HTTPException
|
||||
raise HTTPException(status_code=404, detail=f"Tier '{tier_code}' not found")
|
||||
|
||||
|
||||
@router.get("/addons", response_model=list[AddOnResponse])
|
||||
def _addon_to_response(addon) -> AddOnResponse:
|
||||
"""Convert an AddOnProduct to AddOnResponse."""
|
||||
return AddOnResponse(
|
||||
code=addon.code,
|
||||
name=addon.name,
|
||||
description=addon.description,
|
||||
category=addon.category,
|
||||
price=addon.price_cents / 100,
|
||||
price_cents=addon.price_cents,
|
||||
billing_period=addon.billing_period,
|
||||
quantity_unit=addon.quantity_unit,
|
||||
quantity_value=addon.quantity_value,
|
||||
)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Endpoints
|
||||
# =============================================================================
|
||||
|
||||
|
||||
@router.get("/tiers", response_model=list[TierResponse]) # public
|
||||
def get_tiers(db: Session = Depends(get_db)) -> list[TierResponse]:
|
||||
"""
|
||||
Get all public subscription tiers.
|
||||
|
||||
Returns tiers from database if available, falls back to hardcoded TIER_LIMITS.
|
||||
"""
|
||||
# Try to get from database first
|
||||
db_tiers = platform_pricing_service.get_public_tiers(db)
|
||||
|
||||
if db_tiers:
|
||||
return [_tier_to_response(tier, is_from_db=True) for tier in db_tiers]
|
||||
|
||||
# Fallback to hardcoded tiers
|
||||
from models.database.subscription import TIER_LIMITS
|
||||
|
||||
tiers = []
|
||||
for tier_code in TIER_LIMITS:
|
||||
tier_data = platform_pricing_service.get_tier_from_hardcoded(tier_code.value)
|
||||
if tier_data:
|
||||
tiers.append(_tier_to_response(tier_data, is_from_db=False))
|
||||
|
||||
return tiers
|
||||
|
||||
|
||||
@router.get("/tiers/{tier_code}", response_model=TierResponse) # public
|
||||
def get_tier(tier_code: str, db: Session = Depends(get_db)) -> TierResponse:
|
||||
"""Get a specific tier by code."""
|
||||
# Try database first
|
||||
tier = platform_pricing_service.get_tier_by_code(db, tier_code)
|
||||
|
||||
if tier:
|
||||
return _tier_to_response(tier, is_from_db=True)
|
||||
|
||||
# Fallback to hardcoded
|
||||
tier_data = platform_pricing_service.get_tier_from_hardcoded(tier_code)
|
||||
if tier_data:
|
||||
return _tier_to_response(tier_data, is_from_db=False)
|
||||
|
||||
raise ResourceNotFoundException(
|
||||
resource_type="SubscriptionTier",
|
||||
identifier=tier_code,
|
||||
)
|
||||
|
||||
|
||||
@router.get("/addons", response_model=list[AddOnResponse]) # public
|
||||
def get_addons(db: Session = Depends(get_db)) -> list[AddOnResponse]:
|
||||
"""
|
||||
Get all available add-on products.
|
||||
|
||||
Returns add-ons from database, or empty list if none configured.
|
||||
"""
|
||||
addons = (
|
||||
db.query(AddOnProduct)
|
||||
.filter(AddOnProduct.is_active == True)
|
||||
.order_by(AddOnProduct.category, AddOnProduct.display_order)
|
||||
.all()
|
||||
)
|
||||
|
||||
return [
|
||||
AddOnResponse(
|
||||
code=addon.code,
|
||||
name=addon.name,
|
||||
description=addon.description,
|
||||
category=addon.category,
|
||||
price=addon.price_cents / 100,
|
||||
price_cents=addon.price_cents,
|
||||
billing_period=addon.billing_period,
|
||||
quantity_unit=addon.quantity_unit,
|
||||
quantity_value=addon.quantity_value,
|
||||
)
|
||||
for addon in addons
|
||||
]
|
||||
addons = platform_pricing_service.get_active_addons(db)
|
||||
return [_addon_to_response(addon) for addon in addons]
|
||||
|
||||
|
||||
@router.get("/pricing", response_model=PricingResponse)
|
||||
@router.get("/pricing", response_model=PricingResponse) # public
|
||||
def get_pricing(db: Session = Depends(get_db)) -> PricingResponse:
|
||||
"""
|
||||
Get complete pricing information (tiers + add-ons).
|
||||
|
||||
Reference in New Issue
Block a user