feat: add SQL query tool, platform debug, loyalty settings, and multi-module improvements
Some checks failed
Some checks failed
- Add admin SQL query tool with saved queries, schema explorer presets, and collapsible category sections (dev_tools module) - Add platform debug tool for admin diagnostics - Add loyalty settings page with owner-only access control - Fix loyalty settings owner check (use currentUser instead of window.__userData) - Replace HTTPException with AuthorizationException in loyalty routes - Expand loyalty module with PIN service, Apple Wallet, program management - Improve store login with platform detection and multi-platform support - Update billing feature gates and subscription services - Add store platform sync improvements and remove is_primary column - Add unit tests for loyalty (PIN, points, stamps, program services) - Update i18n translations across dev_tools locales Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -108,10 +108,17 @@ class FeatureService:
|
||||
# Store -> Merchant Resolution
|
||||
# =========================================================================
|
||||
|
||||
def _get_merchant_for_store(self, db: Session, store_id: int) -> tuple[int | None, int | None]:
|
||||
def _get_merchant_for_store(
|
||||
self, db: Session, store_id: int, platform_id: int | None = None
|
||||
) -> tuple[int | None, int | None]:
|
||||
"""
|
||||
Resolve store_id to (merchant_id, platform_id).
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
store_id: Store ID
|
||||
platform_id: Platform ID from JWT. When provided, skips DB lookup.
|
||||
|
||||
Returns:
|
||||
Tuple of (merchant_id, platform_id), either may be None
|
||||
"""
|
||||
@@ -123,7 +130,8 @@ class FeatureService:
|
||||
return None, None
|
||||
|
||||
merchant_id = store.merchant_id
|
||||
platform_id = platform_service.get_primary_platform_id_for_store(db, store_id)
|
||||
if platform_id is None:
|
||||
platform_id = platform_service.get_first_active_platform_id_for_store(db, store_id)
|
||||
|
||||
return merchant_id, platform_id
|
||||
|
||||
@@ -204,28 +212,29 @@ class FeatureService:
|
||||
return subscription.tier.has_feature(feature_code)
|
||||
|
||||
def has_feature_for_store(
|
||||
self, db: Session, store_id: int, feature_code: str
|
||||
self, db: Session, store_id: int, feature_code: str,
|
||||
platform_id: int | None = None,
|
||||
) -> bool:
|
||||
"""
|
||||
Convenience method that resolves the store -> merchant -> platform
|
||||
hierarchy and checks whether the merchant has access to a feature.
|
||||
|
||||
Looks up the store's merchant_id and platform_id, then delegates
|
||||
to has_feature().
|
||||
|
||||
Args:
|
||||
db: Database session.
|
||||
store_id: The store ID to resolve.
|
||||
feature_code: The feature code to check.
|
||||
platform_id: Platform ID from JWT. When provided, skips DB lookup.
|
||||
|
||||
Returns:
|
||||
True if the resolved merchant has access to the feature,
|
||||
False if the store/merchant cannot be resolved or lacks access.
|
||||
"""
|
||||
merchant_id, platform_id = self._get_merchant_for_store(db, store_id)
|
||||
if merchant_id is None or platform_id is None:
|
||||
merchant_id, resolved_platform_id = self._get_merchant_for_store(
|
||||
db, store_id, platform_id=platform_id
|
||||
)
|
||||
if merchant_id is None or resolved_platform_id is None:
|
||||
return False
|
||||
return self.has_feature(db, merchant_id, platform_id, feature_code)
|
||||
return self.has_feature(db, merchant_id, resolved_platform_id, feature_code)
|
||||
|
||||
def get_merchant_feature_codes(
|
||||
self, db: Session, merchant_id: int, platform_id: int
|
||||
@@ -317,7 +326,7 @@ class FeatureService:
|
||||
feature_code: Feature code (e.g., "products_limit")
|
||||
store_id: Store ID (if checking per-store)
|
||||
merchant_id: Merchant ID (if already known)
|
||||
platform_id: Platform ID (if already known)
|
||||
platform_id: Platform ID (if already known, e.g. from JWT)
|
||||
|
||||
Returns:
|
||||
(allowed, error_message) tuple
|
||||
@@ -326,7 +335,9 @@ class FeatureService:
|
||||
|
||||
# Resolve store -> merchant if needed
|
||||
if merchant_id is None and store_id is not None:
|
||||
merchant_id, platform_id = self._get_merchant_for_store(db, store_id)
|
||||
merchant_id, platform_id = self._get_merchant_for_store(
|
||||
db, store_id, platform_id=platform_id
|
||||
)
|
||||
|
||||
if merchant_id is None or platform_id is None:
|
||||
return False, "No subscription found"
|
||||
|
||||
@@ -32,7 +32,7 @@ class StorePlatformSync:
|
||||
Upsert StorePlatform for every store belonging to a merchant.
|
||||
|
||||
- Existing entry → update is_active (and tier_id if provided)
|
||||
- Missing + is_active=True → create (set is_primary if store has none)
|
||||
- Missing + is_active=True → create
|
||||
- Missing + is_active=False → no-op
|
||||
"""
|
||||
stores = store_service.get_stores_by_merchant_id(db, merchant_id)
|
||||
|
||||
@@ -311,6 +311,7 @@ class StripeService:
|
||||
trial_days: int | None = None,
|
||||
quantity: int = 1,
|
||||
metadata: dict | None = None,
|
||||
platform_id: int | None = None,
|
||||
) -> stripe.checkout.Session:
|
||||
"""
|
||||
Create a Stripe Checkout session for subscription signup.
|
||||
@@ -324,6 +325,7 @@ class StripeService:
|
||||
trial_days: Optional trial period
|
||||
quantity: Number of items (default 1)
|
||||
metadata: Additional metadata to store
|
||||
platform_id: Platform ID (from JWT or caller). Falls back to DB lookup.
|
||||
|
||||
Returns:
|
||||
Stripe Checkout Session object
|
||||
@@ -334,7 +336,8 @@ class StripeService:
|
||||
from app.modules.tenancy.services.platform_service import platform_service
|
||||
from app.modules.tenancy.services.team_service import team_service
|
||||
|
||||
platform_id = platform_service.get_primary_platform_id_for_store(db, store.id)
|
||||
if platform_id is None:
|
||||
platform_id = platform_service.get_first_active_platform_id_for_store(db, store.id)
|
||||
subscription = None
|
||||
if store.merchant_id and platform_id:
|
||||
subscription = (
|
||||
|
||||
@@ -47,9 +47,16 @@ class SubscriptionService:
|
||||
# Store Resolution
|
||||
# =========================================================================
|
||||
|
||||
def resolve_store_to_merchant(self, db: Session, store_id: int) -> tuple[int, int]:
|
||||
def resolve_store_to_merchant(
|
||||
self, db: Session, store_id: int, platform_id: int | None = None
|
||||
) -> tuple[int, int]:
|
||||
"""Resolve store_id to (merchant_id, platform_id).
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
store_id: Store ID
|
||||
platform_id: Platform ID from JWT token. When provided, skips DB lookup.
|
||||
|
||||
Raises:
|
||||
ResourceNotFoundException: If store not found or has no platform
|
||||
"""
|
||||
@@ -59,7 +66,8 @@ class SubscriptionService:
|
||||
store = store_service.get_store_by_id_optional(db, store_id)
|
||||
if not store or not store.merchant_id:
|
||||
raise ResourceNotFoundException("Store", str(store_id))
|
||||
platform_id = platform_service.get_primary_platform_id_for_store(db, store_id)
|
||||
if platform_id is None:
|
||||
platform_id = platform_service.get_first_active_platform_id_for_store(db, store_id)
|
||||
if not platform_id:
|
||||
raise ResourceNotFoundException("StorePlatform", f"store_id={store_id}")
|
||||
return store.merchant_id, platform_id
|
||||
@@ -185,7 +193,7 @@ class SubscriptionService:
|
||||
if merchant_id is None:
|
||||
return None
|
||||
|
||||
platform_id = platform_service.get_primary_platform_id_for_store(db, store_id)
|
||||
platform_id = platform_service.get_first_active_platform_id_for_store(db, store_id)
|
||||
if platform_id is None:
|
||||
return None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user