feat: platform-aware storefront routing and billing improvements
Overhaul storefront URL routing to be platform-aware:
- Dev: /platforms/{code}/storefront/{store_code}/
- Prod: subdomain.platform.lu/ (internally rewritten to /storefront/)
- Add subdomain detection in PlatformContextMiddleware
- Add /storefront/ path rewrite for prod mode (subdomain/custom domain)
- Remove all silent platform fallbacks (platform_id=1)
- Add require_platform dependency for clean endpoint validation
- Update route registration, templates, module definitions, base_url calc
- Update StoreContextMiddleware for /storefront/ path detection
- Remove /stores/ from FrontendDetector STOREFRONT_PATH_PREFIXES
Billing service improvements:
- Add store_platform_sync_service to keep store_platforms in sync
- Make tier lookups platform-aware across billing services
- Add tiers for all platforms in seed data
- Add demo subscriptions to seed
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -82,17 +82,20 @@ class SubscriptionService:
|
||||
# Tier Information
|
||||
# =========================================================================
|
||||
|
||||
def get_tier_by_code(self, db: Session, tier_code: str) -> SubscriptionTier | None:
|
||||
"""Get subscription tier by code."""
|
||||
return (
|
||||
db.query(SubscriptionTier)
|
||||
.filter(SubscriptionTier.code == tier_code)
|
||||
.first()
|
||||
)
|
||||
def get_tier_by_code(
|
||||
self, db: Session, tier_code: str, platform_id: int | None = None
|
||||
) -> SubscriptionTier | None:
|
||||
"""Get subscription tier by code, optionally scoped to a platform."""
|
||||
query = db.query(SubscriptionTier).filter(SubscriptionTier.code == tier_code)
|
||||
if platform_id is not None:
|
||||
query = query.filter(SubscriptionTier.platform_id == platform_id)
|
||||
return query.first()
|
||||
|
||||
def get_tier_id(self, db: Session, tier_code: str) -> int | None:
|
||||
def get_tier_id(
|
||||
self, db: Session, tier_code: str, platform_id: int | None = None
|
||||
) -> int | None:
|
||||
"""Get tier ID from tier code. Returns None if tier not found."""
|
||||
tier = self.get_tier_by_code(db, tier_code)
|
||||
tier = self.get_tier_by_code(db, tier_code, platform_id=platform_id)
|
||||
return tier.id if tier else None
|
||||
|
||||
def get_all_tiers(
|
||||
@@ -254,7 +257,7 @@ class SubscriptionService:
|
||||
trial_ends_at = None
|
||||
status = SubscriptionStatus.ACTIVE.value
|
||||
|
||||
tier_id = self.get_tier_id(db, tier_code)
|
||||
tier_id = self.get_tier_id(db, tier_code, platform_id=platform_id)
|
||||
|
||||
subscription = MerchantSubscription(
|
||||
merchant_id=merchant_id,
|
||||
@@ -271,6 +274,15 @@ class SubscriptionService:
|
||||
db.flush()
|
||||
db.refresh(subscription)
|
||||
|
||||
# Sync store_platforms for all merchant stores
|
||||
from app.modules.billing.services.store_platform_sync_service import (
|
||||
store_platform_sync,
|
||||
)
|
||||
|
||||
store_platform_sync.sync_store_platforms_for_merchant(
|
||||
db, merchant_id, platform_id, is_active=True, tier_id=subscription.tier_id
|
||||
)
|
||||
|
||||
logger.info(
|
||||
f"Created subscription for merchant {merchant_id} on platform {platform_id} "
|
||||
f"(tier={tier_code}, status={status})"
|
||||
@@ -305,7 +317,7 @@ class SubscriptionService:
|
||||
subscription = self.get_subscription_or_raise(db, merchant_id, platform_id)
|
||||
|
||||
old_tier_id = subscription.tier_id
|
||||
new_tier = self.get_tier_by_code(db, new_tier_code)
|
||||
new_tier = self.get_tier_by_code(db, new_tier_code, platform_id=platform_id)
|
||||
if not new_tier:
|
||||
raise ValueError(f"Tier '{new_tier_code}' not found")
|
||||
|
||||
@@ -366,6 +378,15 @@ class SubscriptionService:
|
||||
db.flush()
|
||||
db.refresh(subscription)
|
||||
|
||||
# Sync store_platforms for all merchant stores
|
||||
from app.modules.billing.services.store_platform_sync_service import (
|
||||
store_platform_sync,
|
||||
)
|
||||
|
||||
store_platform_sync.sync_store_platforms_for_merchant(
|
||||
db, merchant_id, platform_id, is_active=True
|
||||
)
|
||||
|
||||
logger.info(
|
||||
f"Reactivated subscription for merchant {merchant_id} "
|
||||
f"on platform {platform_id}"
|
||||
|
||||
Reference in New Issue
Block a user