Compare commits
3 Commits
874e254c11
...
9c27fa02b0
| Author | SHA1 | Date | |
|---|---|---|---|
| 9c27fa02b0 | |||
| 7c43d6f4a2 | |||
| 9173448645 |
@@ -192,7 +192,9 @@ api_endpoint_rules:
|
||||
def stripe_webhook(request: Request):
|
||||
...
|
||||
pattern:
|
||||
file_pattern: "app/api/v1/**/*.py"
|
||||
file_pattern:
|
||||
- "app/api/v1/**/*.py"
|
||||
- "app/modules/*/routes/api/**/*.py"
|
||||
required_if_not_public:
|
||||
- "Depends(get_current_"
|
||||
auto_exclude_files:
|
||||
@@ -205,11 +207,15 @@ api_endpoint_rules:
|
||||
name: "Multi-tenant endpoints must scope queries to vendor_id"
|
||||
severity: "error"
|
||||
description: |
|
||||
All queries in vendor/shop contexts must filter by vendor_id.
|
||||
All queries in vendor/storefront contexts must filter by vendor_id.
|
||||
Use request.state.vendor_id from middleware.
|
||||
pattern:
|
||||
file_pattern: "app/api/v1/vendor/**/*.py"
|
||||
file_pattern: "app/api/v1/storefront/**/*.py"
|
||||
file_pattern:
|
||||
- "app/api/v1/vendor/**/*.py"
|
||||
- "app/modules/*/routes/api/store*.py"
|
||||
file_pattern:
|
||||
- "app/api/v1/storefront/**/*.py"
|
||||
- "app/modules/*/routes/api/storefront*.py"
|
||||
discouraged_patterns:
|
||||
- "db.query(.*).all()"
|
||||
|
||||
|
||||
@@ -9,7 +9,9 @@ auth_rules:
|
||||
description: |
|
||||
Authentication must use JWT tokens in Authorization: Bearer header
|
||||
pattern:
|
||||
file_pattern: "app/api/**/*.py"
|
||||
file_pattern:
|
||||
- "app/api/**/*.py"
|
||||
- "app/modules/*/routes/api/**/*.py"
|
||||
enforcement: "middleware"
|
||||
|
||||
- id: "AUTH-002"
|
||||
@@ -18,7 +20,9 @@ auth_rules:
|
||||
description: |
|
||||
Use Depends(get_current_admin/vendor/customer) for role checks
|
||||
pattern:
|
||||
file_pattern: "app/api/v1/**/*.py"
|
||||
file_pattern:
|
||||
- "app/api/v1/**/*.py"
|
||||
- "app/modules/*/routes/api/**/*.py"
|
||||
required: "Depends\\(get_current_"
|
||||
|
||||
- id: "AUTH-003"
|
||||
@@ -36,10 +40,10 @@ auth_rules:
|
||||
description: |
|
||||
Two vendor context patterns exist - use the appropriate one:
|
||||
|
||||
1. SHOP ENDPOINTS (public, no authentication required):
|
||||
1. STOREFRONT ENDPOINTS (public, no authentication required):
|
||||
- Use: vendor: Vendor = Depends(require_vendor_context())
|
||||
- Vendor is detected from URL/subdomain/domain
|
||||
- File pattern: app/api/v1/storefront/**/*.py
|
||||
- File pattern: app/api/v1/storefront/**/*.py, app/modules/*/routes/api/storefront*.py
|
||||
- Mark as public with: # public
|
||||
|
||||
2. VENDOR API ENDPOINTS (authenticated):
|
||||
@@ -49,15 +53,19 @@ auth_rules:
|
||||
- File pattern: app/api/v1/vendor/**/*.py
|
||||
|
||||
DEPRECATED for vendor APIs:
|
||||
- require_vendor_context() - only for shop endpoints
|
||||
- require_vendor_context() - only for storefront endpoints
|
||||
- getattr(request.state, "vendor", None) without permission dependency
|
||||
|
||||
See: docs/backend/vendor-in-token-architecture.md
|
||||
pattern:
|
||||
file_pattern: "app/api/v1/vendor/**/*.py"
|
||||
file_pattern:
|
||||
- "app/api/v1/vendor/**/*.py"
|
||||
- "app/modules/*/routes/api/store*.py"
|
||||
anti_patterns:
|
||||
- "require_vendor_context\\(\\)"
|
||||
file_pattern: "app/api/v1/storefront/**/*.py"
|
||||
file_pattern:
|
||||
- "app/api/v1/storefront/**/*.py"
|
||||
- "app/modules/*/routes/api/storefront*.py"
|
||||
required_patterns:
|
||||
- "require_vendor_context\\(\\)|# public"
|
||||
|
||||
@@ -149,7 +157,9 @@ multi_tenancy_rules:
|
||||
description: |
|
||||
In vendor/shop contexts, all database queries must filter by vendor_id
|
||||
pattern:
|
||||
file_pattern: "app/services/**/*.py"
|
||||
file_pattern:
|
||||
- "app/services/**/*.py"
|
||||
- "app/modules/*/services/**/*.py"
|
||||
context: "vendor_shop"
|
||||
required_pattern: ".filter\\(.*vendor_id.*\\)"
|
||||
|
||||
@@ -159,5 +169,7 @@ multi_tenancy_rules:
|
||||
description: |
|
||||
Queries must never access data from other vendors
|
||||
pattern:
|
||||
file_pattern: "app/services/**/*.py"
|
||||
file_pattern:
|
||||
- "app/services/**/*.py"
|
||||
- "app/modules/*/services/**/*.py"
|
||||
enforcement: "database_query_level"
|
||||
|
||||
@@ -10,7 +10,9 @@ exception_rules:
|
||||
Create domain-specific exceptions in app/exceptions/ for better
|
||||
error handling and clarity.
|
||||
pattern:
|
||||
file_pattern: "app/exceptions/**/*.py"
|
||||
file_pattern:
|
||||
- "app/exceptions/**/*.py"
|
||||
- "app/modules/*/exceptions.py"
|
||||
encouraged_structure: |
|
||||
class VendorError(Exception):
|
||||
"""Base exception for vendor-related errors"""
|
||||
@@ -34,7 +36,9 @@ exception_rules:
|
||||
description: |
|
||||
When catching exceptions, log them with context and stack trace.
|
||||
pattern:
|
||||
file_pattern: "app/services/**/*.py"
|
||||
file_pattern:
|
||||
- "app/services/**/*.py"
|
||||
- "app/modules/*/services/**/*.py"
|
||||
encouraged_patterns:
|
||||
- "logger.error"
|
||||
- "exc_info=True"
|
||||
@@ -47,7 +51,9 @@ exception_rules:
|
||||
subclasses like ResourceNotFoundException, ValidationException, etc.).
|
||||
This ensures the global exception handler catches and converts them properly.
|
||||
pattern:
|
||||
file_pattern: "app/exceptions/**/*.py"
|
||||
file_pattern:
|
||||
- "app/exceptions/**/*.py"
|
||||
- "app/modules/*/exceptions.py"
|
||||
required_base_class: "WizamartException"
|
||||
example_good: |
|
||||
class VendorNotFoundException(ResourceNotFoundException):
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# Architecture Rules - Model Rules
|
||||
# Rules for models/database/*.py and models/schema/*.py files
|
||||
# Rules for models/database/*.py, models/schema/*.py, app/modules/*/models/**/*.py, and app/modules/*/schemas/**/*.py files
|
||||
|
||||
model_rules:
|
||||
|
||||
@@ -10,7 +10,9 @@ model_rules:
|
||||
All database models must inherit from SQLAlchemy Base and use proper
|
||||
column definitions with types and constraints.
|
||||
pattern:
|
||||
file_pattern: "models/database/**/*.py"
|
||||
file_pattern:
|
||||
- "models/database/**/*.py"
|
||||
- "app/modules/*/models/**/*.py"
|
||||
required_patterns:
|
||||
- "class.*\\(Base\\):"
|
||||
|
||||
@@ -21,7 +23,10 @@ model_rules:
|
||||
Never mix SQLAlchemy and Pydantic in the same model.
|
||||
SQLAlchemy = database schema, Pydantic = API validation/serialization.
|
||||
pattern:
|
||||
file_pattern: "models/**/*.py"
|
||||
file_pattern:
|
||||
- "models/**/*.py"
|
||||
- "app/modules/*/models/**/*.py"
|
||||
- "app/modules/*/schemas/**/*.py"
|
||||
anti_patterns:
|
||||
- "class.*\\(Base, BaseModel\\):"
|
||||
|
||||
@@ -31,7 +36,9 @@ model_rules:
|
||||
description: |
|
||||
Pydantic response models must enable from_attributes to work with SQLAlchemy models.
|
||||
pattern:
|
||||
file_pattern: "models/schema/**/*.py"
|
||||
file_pattern:
|
||||
- "models/schema/**/*.py"
|
||||
- "app/modules/*/schemas/**/*.py"
|
||||
required_in_response_models:
|
||||
- "from_attributes = True"
|
||||
|
||||
@@ -51,5 +58,7 @@ model_rules:
|
||||
Junction/join tables use both entity names in plural:
|
||||
- Good: vendor_users, order_items, product_translations
|
||||
pattern:
|
||||
file_pattern: "models/database/**/*.py"
|
||||
file_pattern:
|
||||
- "models/database/**/*.py"
|
||||
- "app/modules/*/models/**/*.py"
|
||||
check: "table_naming_plural"
|
||||
|
||||
@@ -23,7 +23,9 @@ money_handling_rules:
|
||||
|
||||
Column naming convention: Use `_cents` suffix for all monetary columns.
|
||||
pattern:
|
||||
file_pattern: "models/database/**/*.py"
|
||||
file_pattern:
|
||||
- "models/database/**/*.py"
|
||||
- "app/modules/*/models/**/*.py"
|
||||
required_patterns:
|
||||
- "_cents = Column(Integer"
|
||||
anti_patterns:
|
||||
@@ -79,7 +81,9 @@ money_handling_rules:
|
||||
|
||||
Or use model validators to convert before response serialization.
|
||||
pattern:
|
||||
file_pattern: "models/schema/**/*.py"
|
||||
file_pattern:
|
||||
- "models/schema/**/*.py"
|
||||
- "app/modules/*/schemas/**/*.py"
|
||||
check: "money_response_format"
|
||||
|
||||
- id: "MON-004"
|
||||
@@ -124,7 +128,9 @@ money_handling_rules:
|
||||
tax = subtotal * 0.17 # Floating point!
|
||||
total = subtotal + tax
|
||||
pattern:
|
||||
file_pattern: "app/services/**/*.py"
|
||||
file_pattern:
|
||||
- "app/services/**/*.py"
|
||||
- "app/modules/*/services/**/*.py"
|
||||
check: "money_arithmetic"
|
||||
|
||||
- id: "MON-006"
|
||||
|
||||
@@ -15,6 +15,10 @@ naming_rules:
|
||||
- "__init__.py"
|
||||
- "auth.py"
|
||||
- "health.py"
|
||||
- "store.py"
|
||||
- "admin.py"
|
||||
- "platform.py"
|
||||
- "storefront.py"
|
||||
|
||||
- id: "NAM-002"
|
||||
name: "Service files use SINGULAR + 'service' suffix"
|
||||
@@ -22,8 +26,17 @@ naming_rules:
|
||||
description: |
|
||||
Service files should use singular name + _service (vendor_service.py)
|
||||
pattern:
|
||||
file_pattern: "app/services/**/*.py"
|
||||
file_pattern:
|
||||
- "app/services/**/*.py"
|
||||
- "app/modules/*/services/**/*.py"
|
||||
check: "service_naming"
|
||||
exceptions:
|
||||
- "*_features.py"
|
||||
- "*_metrics.py"
|
||||
- "*_widgets.py"
|
||||
- "*_aggregator.py"
|
||||
- "*_provider.py"
|
||||
- "*_presets.py"
|
||||
|
||||
- id: "NAM-003"
|
||||
name: "Model files use SINGULAR names"
|
||||
@@ -31,14 +44,16 @@ naming_rules:
|
||||
description: |
|
||||
Both database and schema model files use singular names (product.py)
|
||||
pattern:
|
||||
file_pattern: "models/**/*.py"
|
||||
file_pattern:
|
||||
- "models/**/*.py"
|
||||
- "app/modules/*/models/**/*.py"
|
||||
check: "singular_naming"
|
||||
|
||||
- id: "NAM-004"
|
||||
name: "Use consistent terminology: vendor not shop"
|
||||
severity: "warning"
|
||||
description: |
|
||||
Use 'vendor' consistently, not 'shop' (except for shop frontend)
|
||||
Use 'vendor' consistently, not 'shop' (except for storefront)
|
||||
pattern:
|
||||
file_pattern: "app/**/*.py"
|
||||
discouraged_terms:
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# Architecture Rules - Service Layer Rules
|
||||
# Rules for app/services/**/*.py files
|
||||
# Rules for app/services/**/*.py and app/modules/*/services/**/*.py files
|
||||
|
||||
service_layer_rules:
|
||||
|
||||
@@ -10,7 +10,9 @@ service_layer_rules:
|
||||
Services are business logic layer - they should NOT know about HTTP.
|
||||
Raise domain-specific exceptions instead (ValueError, custom exceptions).
|
||||
pattern:
|
||||
file_pattern: "app/services/**/*.py"
|
||||
file_pattern:
|
||||
- "app/services/**/*.py"
|
||||
- "app/modules/*/services/**/*.py"
|
||||
anti_patterns:
|
||||
- "raise HTTPException"
|
||||
- "from fastapi import HTTPException"
|
||||
@@ -22,7 +24,9 @@ service_layer_rules:
|
||||
Services should raise meaningful domain exceptions, not generic Exception.
|
||||
Create custom exception classes for business rule violations.
|
||||
pattern:
|
||||
file_pattern: "app/services/**/*.py"
|
||||
file_pattern:
|
||||
- "app/services/**/*.py"
|
||||
- "app/modules/*/services/**/*.py"
|
||||
discouraged_patterns:
|
||||
- "raise Exception\\("
|
||||
|
||||
@@ -33,7 +37,9 @@ service_layer_rules:
|
||||
Service methods should receive database session as a parameter for testability
|
||||
and transaction control. Never create session inside service.
|
||||
pattern:
|
||||
file_pattern: "app/services/**/*.py"
|
||||
file_pattern:
|
||||
- "app/services/**/*.py"
|
||||
- "app/modules/*/services/**/*.py"
|
||||
required_in_method_signature:
|
||||
- "db: Session"
|
||||
anti_patterns:
|
||||
@@ -47,7 +53,9 @@ service_layer_rules:
|
||||
Service methods should accept Pydantic models for complex inputs
|
||||
to ensure type safety and validation.
|
||||
pattern:
|
||||
file_pattern: "app/services/**/*.py"
|
||||
file_pattern:
|
||||
- "app/services/**/*.py"
|
||||
- "app/modules/*/services/**/*.py"
|
||||
encouraged_patterns:
|
||||
- "BaseModel"
|
||||
|
||||
@@ -57,7 +65,9 @@ service_layer_rules:
|
||||
description: |
|
||||
All database queries must be scoped to vendor_id to prevent cross-tenant data access.
|
||||
pattern:
|
||||
file_pattern: "app/services/**/*.py"
|
||||
file_pattern:
|
||||
- "app/services/**/*.py"
|
||||
- "app/modules/*/services/**/*.py"
|
||||
check: "vendor_scoping"
|
||||
|
||||
- id: "SVC-006"
|
||||
@@ -74,11 +84,22 @@ service_layer_rules:
|
||||
|
||||
The endpoint should call db.commit() after all service operations succeed.
|
||||
pattern:
|
||||
file_pattern: "app/services/**/*.py"
|
||||
file_pattern:
|
||||
- "app/services/**/*.py"
|
||||
- "app/modules/*/services/**/*.py"
|
||||
anti_patterns:
|
||||
- "db.commit()"
|
||||
exceptions:
|
||||
- "log_service.py"
|
||||
- "card_service.py"
|
||||
- "wallet_service.py"
|
||||
- "program_service.py"
|
||||
- "points_service.py"
|
||||
- "apple_wallet_service.py"
|
||||
- "pin_service.py"
|
||||
- "stamp_service.py"
|
||||
- "google_wallet_service.py"
|
||||
- "theme_presets.py"
|
||||
|
||||
- id: "SVC-007"
|
||||
name: "Service return types must match API response schemas"
|
||||
@@ -113,5 +134,7 @@ service_layer_rules:
|
||||
result = service.get_stats(db)
|
||||
StatsResponse(**result) # Raises if keys don't match
|
||||
pattern:
|
||||
file_pattern: "app/services/**/*.py"
|
||||
file_pattern:
|
||||
- "app/services/**/*.py"
|
||||
- "app/modules/*/services/**/*.py"
|
||||
check: "schema_compatibility"
|
||||
|
||||
@@ -20,19 +20,19 @@ MERCHANT ROUTES (/merchants/*):
|
||||
- Role: store (merchant owners are store-role users who own merchants)
|
||||
- Validates: User owns the merchant via Merchant.owner_user_id
|
||||
|
||||
CUSTOMER/SHOP ROUTES (/shop/account/*):
|
||||
- Cookie: customer_token (path=/shop) OR Authorization header
|
||||
CUSTOMER/STOREFRONT ROUTES (/storefront/account/*):
|
||||
- Cookie: customer_token (path=/storefront) OR Authorization header
|
||||
- Role: customer only
|
||||
- Blocks: admins, stores
|
||||
- Note: Public shop pages (/shop/products, etc.) don't require auth
|
||||
- Note: Public storefront pages (/storefront/products, etc.) don't require auth
|
||||
|
||||
This dual authentication approach supports:
|
||||
- HTML pages: Use cookies (automatic browser behavior)
|
||||
- API calls: Use Authorization headers (explicit JavaScript control)
|
||||
|
||||
The cookie path restrictions prevent cross-context cookie leakage:
|
||||
- admin_token is NEVER sent to /store/* or /shop/*
|
||||
- store_token is NEVER sent to /admin/* or /shop/*
|
||||
- admin_token is NEVER sent to /store/* or /storefront/*
|
||||
- store_token is NEVER sent to /admin/* or /storefront/*
|
||||
- customer_token is NEVER sent to /admin/* or /store/*
|
||||
"""
|
||||
|
||||
@@ -1019,7 +1019,7 @@ def get_merchant_for_current_user_page(
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# CUSTOMER AUTHENTICATION (SHOP)
|
||||
# CUSTOMER AUTHENTICATION (STOREFRONT)
|
||||
# ============================================================================
|
||||
|
||||
|
||||
@@ -1095,7 +1095,7 @@ def _validate_customer_token(token: str, request: Request, db: Session):
|
||||
raise InvalidTokenException("Customer account is inactive")
|
||||
|
||||
# Validate store context matches token
|
||||
# This prevents using a customer token from store A on store B's shop
|
||||
# This prevents using a customer token from store A on store B's storefront
|
||||
request_store = getattr(request.state, "store", None)
|
||||
if request_store and token_store_id:
|
||||
if request_store.id != token_store_id:
|
||||
@@ -1123,8 +1123,8 @@ def get_current_customer_from_cookie_or_header(
|
||||
"""
|
||||
Get current customer from customer_token cookie or Authorization header.
|
||||
|
||||
Used for shop account HTML pages (/shop/account/*) that need cookie-based auth.
|
||||
Note: Public shop pages (/shop/products, etc.) don't use this dependency.
|
||||
Used for storefront account HTML pages (/storefront/account/*) that need cookie-based auth.
|
||||
Note: Public storefront pages (/storefront/products, etc.) don't use this dependency.
|
||||
|
||||
Validates that token store_id matches request store (URL-based detection).
|
||||
|
||||
@@ -1164,7 +1164,7 @@ def get_current_customer_api(
|
||||
"""
|
||||
Get current customer from Authorization header ONLY.
|
||||
|
||||
Used for shop API endpoints that should not accept cookies.
|
||||
Used for storefront API endpoints that should not accept cookies.
|
||||
Validates that token store_id matches request store (URL-based detection).
|
||||
|
||||
Args:
|
||||
|
||||
@@ -46,8 +46,6 @@ class FrontendDetector:
|
||||
STOREFRONT_PATH_PREFIXES = (
|
||||
"/storefront",
|
||||
"/api/v1/storefront",
|
||||
"/shop", # Legacy support
|
||||
"/api/v1/shop", # Legacy support
|
||||
"/stores/", # Path-based store access
|
||||
)
|
||||
MERCHANT_PATH_PREFIXES = ("/merchants", "/api/v1/merchants")
|
||||
@@ -113,7 +111,7 @@ class FrontendDetector:
|
||||
return FrontendType.PLATFORM
|
||||
|
||||
# 3. Store subdomain detection (wizamart.oms.lu)
|
||||
# If subdomain exists and is not reserved -> it's a store shop
|
||||
# If subdomain exists and is not reserved -> it's a store storefront
|
||||
if subdomain and subdomain not in cls.RESERVED_SUBDOMAINS:
|
||||
logger.debug(
|
||||
f"[FRONTEND_DETECTOR] Detected STOREFRONT from subdomain: {subdomain}"
|
||||
|
||||
@@ -18,11 +18,11 @@ from typing import Any
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.modules.catalog.models import Product
|
||||
from app.modules.customers.models.customer import Customer
|
||||
from app.modules.inventory.models import Inventory
|
||||
from app.modules.marketplace.models import MarketplaceImportJob, MarketplaceProduct
|
||||
from app.modules.orders.models import Order
|
||||
from app.modules.catalog.models import Product # IMPORT-002
|
||||
from app.modules.customers.models.customer import Customer # IMPORT-002
|
||||
from app.modules.inventory.models import Inventory # IMPORT-002
|
||||
from app.modules.marketplace.models import MarketplaceImportJob, MarketplaceProduct # IMPORT-002
|
||||
from app.modules.orders.models import Order # IMPORT-002
|
||||
from app.modules.tenancy.exceptions import (
|
||||
AdminOperationException,
|
||||
StoreNotFoundException,
|
||||
|
||||
@@ -89,6 +89,13 @@ def _get_store_router():
|
||||
return store_router
|
||||
|
||||
|
||||
def _get_metrics_provider():
|
||||
"""Lazy import of metrics provider to avoid circular imports."""
|
||||
from app.modules.billing.services.billing_metrics import billing_metrics_provider
|
||||
|
||||
return billing_metrics_provider
|
||||
|
||||
|
||||
def _get_feature_provider():
|
||||
"""Lazy import of feature provider to avoid circular imports."""
|
||||
from app.modules.billing.services.billing_features import billing_feature_provider
|
||||
@@ -271,6 +278,8 @@ billing_module = ModuleDefinition(
|
||||
],
|
||||
# Feature provider for feature flags
|
||||
feature_provider=_get_feature_provider,
|
||||
# Metrics provider for subscription metrics
|
||||
metrics_provider=_get_metrics_provider,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -22,7 +22,6 @@ from app.modules.billing.models.subscription import (
|
||||
AddOnProduct,
|
||||
BillingHistory,
|
||||
BillingPeriod,
|
||||
CapacitySnapshot,
|
||||
StoreAddOn,
|
||||
StripeWebhookEvent,
|
||||
SubscriptionStatus,
|
||||
@@ -46,7 +45,6 @@ __all__ = [
|
||||
"StoreAddOn",
|
||||
"StripeWebhookEvent",
|
||||
"BillingHistory",
|
||||
"CapacitySnapshot",
|
||||
# Merchant Subscription
|
||||
"MerchantSubscription",
|
||||
# Feature Limits
|
||||
|
||||
@@ -345,61 +345,3 @@ class BillingHistory(Base, TimestampMixin):
|
||||
|
||||
def __repr__(self):
|
||||
return f"<BillingHistory(store_id={self.store_id}, invoice='{self.invoice_number}', status='{self.status}')>"
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Capacity Planning
|
||||
# ============================================================================
|
||||
|
||||
|
||||
class CapacitySnapshot(Base, TimestampMixin):
|
||||
"""
|
||||
Daily snapshot of platform capacity metrics.
|
||||
|
||||
Used for growth trending and capacity forecasting.
|
||||
Captured daily by background job.
|
||||
"""
|
||||
|
||||
__tablename__ = "capacity_snapshots"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
snapshot_date = Column(DateTime(timezone=True), nullable=False, unique=True, index=True)
|
||||
|
||||
# Store metrics
|
||||
total_stores = Column(Integer, default=0, nullable=False)
|
||||
active_stores = Column(Integer, default=0, nullable=False)
|
||||
trial_stores = Column(Integer, default=0, nullable=False)
|
||||
|
||||
# Subscription metrics
|
||||
total_subscriptions = Column(Integer, default=0, nullable=False)
|
||||
active_subscriptions = Column(Integer, default=0, nullable=False)
|
||||
|
||||
# Resource metrics
|
||||
total_products = Column(Integer, default=0, nullable=False)
|
||||
total_orders_month = Column(Integer, default=0, nullable=False)
|
||||
total_team_members = Column(Integer, default=0, nullable=False)
|
||||
|
||||
# Storage metrics
|
||||
storage_used_gb = Column(Numeric(10, 2), default=0, nullable=False)
|
||||
db_size_mb = Column(Numeric(10, 2), default=0, nullable=False)
|
||||
|
||||
# Capacity metrics (theoretical limits from subscriptions)
|
||||
theoretical_products_limit = Column(Integer, nullable=True)
|
||||
theoretical_orders_limit = Column(Integer, nullable=True)
|
||||
theoretical_team_limit = Column(Integer, nullable=True)
|
||||
|
||||
# Tier distribution (JSON: {"essential": 10, "professional": 5, ...})
|
||||
tier_distribution = Column(JSON, nullable=True)
|
||||
|
||||
# Performance metrics
|
||||
avg_response_ms = Column(Integer, nullable=True)
|
||||
peak_cpu_percent = Column(Numeric(5, 2), nullable=True)
|
||||
peak_memory_percent = Column(Numeric(5, 2), nullable=True)
|
||||
|
||||
# Indexes
|
||||
__table_args__ = (
|
||||
Index("ix_capacity_snapshots_date", "snapshot_date"),
|
||||
)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<CapacitySnapshot(date={self.snapshot_date}, stores={self.total_stores})>"
|
||||
|
||||
@@ -11,7 +11,7 @@ Provides admin API endpoints for subscription and billing management:
|
||||
|
||||
import logging
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Path, Query
|
||||
from fastapi import APIRouter, Depends, Path, Query
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_current_admin_api, require_module_access
|
||||
@@ -62,9 +62,7 @@ def list_subscription_tiers(
|
||||
"""List all subscription tiers."""
|
||||
tiers = admin_subscription_service.get_tiers(db, include_inactive=include_inactive, platform_id=platform_id)
|
||||
|
||||
from app.modules.tenancy.models import Platform
|
||||
|
||||
platforms_map = {p.id: p.name for p in db.query(Platform).all()}
|
||||
platforms_map = admin_subscription_service.get_platform_names_map(db)
|
||||
tiers_response = []
|
||||
for t in tiers:
|
||||
resp = SubscriptionTierResponse.model_validate(t)
|
||||
@@ -147,18 +145,17 @@ def list_merchant_subscriptions(
|
||||
db, page=page, per_page=per_page, status=status, tier=tier, search=search
|
||||
)
|
||||
|
||||
from app.modules.tenancy.models import Platform
|
||||
platforms_map = admin_subscription_service.get_platform_names_map(db)
|
||||
|
||||
subscriptions = []
|
||||
for sub, merchant in data["results"]:
|
||||
sub_resp = MerchantSubscriptionAdminResponse.model_validate(sub)
|
||||
tier_name = sub.tier.name if sub.tier else None
|
||||
platform = db.query(Platform).filter(Platform.id == sub.platform_id).first()
|
||||
subscriptions.append(
|
||||
MerchantSubscriptionWithMerchant(
|
||||
**sub_resp.model_dump(),
|
||||
merchant_name=merchant.name,
|
||||
platform_name=platform.name if platform else "",
|
||||
platform_name=platforms_map.get(sub.platform_id, ""),
|
||||
tier_name=tier_name,
|
||||
)
|
||||
)
|
||||
@@ -268,12 +265,13 @@ def get_subscription_for_store(
|
||||
of subscription entries with feature usage metrics.
|
||||
"""
|
||||
from app.modules.billing.services.feature_service import feature_service
|
||||
from app.modules.tenancy.models import Platform
|
||||
|
||||
# Resolve store to merchant + all platform IDs
|
||||
merchant_id, platform_ids = feature_service._get_merchant_and_platforms_for_store(db, store_id)
|
||||
if merchant_id is None or not platform_ids:
|
||||
raise HTTPException(status_code=404, detail="Store not found or has no platform association")
|
||||
raise ResourceNotFoundException("Store", str(store_id))
|
||||
|
||||
platforms_map = admin_subscription_service.get_platform_names_map(db)
|
||||
|
||||
results = []
|
||||
for pid in platform_ids:
|
||||
@@ -308,14 +306,11 @@ def get_subscription_for_store(
|
||||
"is_approaching_limit": (fs.percent_used or 0) >= 80,
|
||||
})
|
||||
|
||||
# Resolve platform name
|
||||
platform = db.query(Platform).filter(Platform.id == pid).first()
|
||||
|
||||
results.append({
|
||||
"subscription": MerchantSubscriptionAdminResponse.model_validate(sub).model_dump(),
|
||||
"tier": tier_info,
|
||||
"features": usage_metrics,
|
||||
"platform_name": platform.name if platform else "",
|
||||
"platform_name": platforms_map.get(pid, ""),
|
||||
})
|
||||
|
||||
return {"subscriptions": results}
|
||||
|
||||
@@ -12,16 +12,12 @@ All routes require module access control for the 'billing' module.
|
||||
|
||||
import logging
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Path
|
||||
from fastapi import APIRouter, Depends, Path
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_current_admin_api, require_module_access
|
||||
from app.core.database import get_db
|
||||
from app.modules.billing.models import SubscriptionTier
|
||||
from app.modules.billing.models.tier_feature_limit import (
|
||||
MerchantFeatureOverride,
|
||||
TierFeatureLimit,
|
||||
)
|
||||
from app.modules.billing.exceptions import InvalidFeatureCodesError
|
||||
from app.modules.billing.schemas import (
|
||||
FeatureCatalogResponse,
|
||||
FeatureDeclarationResponse,
|
||||
@@ -30,6 +26,7 @@ from app.modules.billing.schemas import (
|
||||
TierFeatureLimitEntry,
|
||||
)
|
||||
from app.modules.billing.services.feature_aggregator import feature_aggregator
|
||||
from app.modules.billing.services.feature_service import feature_service
|
||||
from app.modules.enums import FrontendType
|
||||
from models.schema.auth import UserContext
|
||||
|
||||
@@ -40,23 +37,6 @@ admin_features_router = APIRouter(
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Helper Functions
|
||||
# ============================================================================
|
||||
|
||||
|
||||
def _get_tier_or_404(db: Session, tier_code: str) -> SubscriptionTier:
|
||||
"""Look up a SubscriptionTier by code, raising 404 if not found."""
|
||||
tier = (
|
||||
db.query(SubscriptionTier)
|
||||
.filter(SubscriptionTier.code == tier_code)
|
||||
.first()
|
||||
)
|
||||
if not tier:
|
||||
raise HTTPException(status_code=404, detail=f"Tier '{tier_code}' not found")
|
||||
return tier
|
||||
|
||||
|
||||
def _declaration_to_response(decl) -> FeatureDeclarationResponse:
|
||||
"""Convert a FeatureDeclaration dataclass to its Pydantic response schema."""
|
||||
return FeatureDeclarationResponse(
|
||||
@@ -120,14 +100,7 @@ def get_tier_feature_limits(
|
||||
Returns all TierFeatureLimit rows associated with the tier,
|
||||
each containing a feature_code and its optional limit_value.
|
||||
"""
|
||||
tier = _get_tier_or_404(db, tier_code)
|
||||
|
||||
rows = (
|
||||
db.query(TierFeatureLimit)
|
||||
.filter(TierFeatureLimit.tier_id == tier.id)
|
||||
.order_by(TierFeatureLimit.feature_code)
|
||||
.all()
|
||||
)
|
||||
rows = feature_service.get_tier_feature_limits(db, tier_code)
|
||||
|
||||
return [
|
||||
TierFeatureLimitEntry(
|
||||
@@ -156,32 +129,15 @@ def upsert_tier_feature_limits(
|
||||
inserts the provided entries. Only entries with enabled=True
|
||||
are persisted (disabled entries are simply omitted).
|
||||
"""
|
||||
tier = _get_tier_or_404(db, tier_code)
|
||||
|
||||
# Validate feature codes against the catalog
|
||||
submitted_codes = {e.feature_code for e in entries}
|
||||
invalid_codes = feature_aggregator.validate_feature_codes(submitted_codes)
|
||||
if invalid_codes:
|
||||
raise HTTPException(
|
||||
status_code=422,
|
||||
detail=f"Unknown feature codes: {sorted(invalid_codes)}",
|
||||
)
|
||||
raise InvalidFeatureCodesError(invalid_codes)
|
||||
|
||||
# Delete existing limits for this tier
|
||||
db.query(TierFeatureLimit).filter(TierFeatureLimit.tier_id == tier.id).delete()
|
||||
|
||||
# Insert new limits (only enabled entries)
|
||||
new_rows = []
|
||||
for entry in entries:
|
||||
if not entry.enabled:
|
||||
continue
|
||||
row = TierFeatureLimit(
|
||||
tier_id=tier.id,
|
||||
feature_code=entry.feature_code,
|
||||
limit_value=entry.limit_value,
|
||||
)
|
||||
db.add(row)
|
||||
new_rows.append(row)
|
||||
new_rows = feature_service.upsert_tier_feature_limits(
|
||||
db, tier_code, [e.model_dump() for e in entries]
|
||||
)
|
||||
|
||||
db.commit()
|
||||
|
||||
@@ -222,12 +178,7 @@ def get_merchant_feature_overrides(
|
||||
Returns MerchantFeatureOverride rows that allow per-merchant
|
||||
exceptions to the default tier limits (e.g. granting extra products).
|
||||
"""
|
||||
rows = (
|
||||
db.query(MerchantFeatureOverride)
|
||||
.filter(MerchantFeatureOverride.merchant_id == merchant_id)
|
||||
.order_by(MerchantFeatureOverride.feature_code)
|
||||
.all()
|
||||
)
|
||||
rows = feature_service.get_merchant_overrides(db, merchant_id)
|
||||
|
||||
return [MerchantFeatureOverrideResponse.model_validate(row) for row in rows]
|
||||
|
||||
@@ -251,50 +202,23 @@ def upsert_merchant_feature_overrides(
|
||||
|
||||
The platform_id is derived from the admin's current platform context.
|
||||
"""
|
||||
from app.exceptions import ValidationException
|
||||
|
||||
platform_id = current_user.token_platform_id
|
||||
if not platform_id:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Platform context required. Select a platform first.",
|
||||
raise ValidationException(
|
||||
message="Platform context required. Select a platform first.",
|
||||
)
|
||||
|
||||
# Validate feature codes against the catalog
|
||||
submitted_codes = {e.feature_code for e in entries}
|
||||
invalid_codes = feature_aggregator.validate_feature_codes(submitted_codes)
|
||||
if invalid_codes:
|
||||
raise HTTPException(
|
||||
status_code=422,
|
||||
detail=f"Unknown feature codes: {sorted(invalid_codes)}",
|
||||
)
|
||||
raise InvalidFeatureCodesError(invalid_codes)
|
||||
|
||||
results = []
|
||||
for entry in entries:
|
||||
existing = (
|
||||
db.query(MerchantFeatureOverride)
|
||||
.filter(
|
||||
MerchantFeatureOverride.merchant_id == merchant_id,
|
||||
MerchantFeatureOverride.platform_id == platform_id,
|
||||
MerchantFeatureOverride.feature_code == entry.feature_code,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
|
||||
if existing:
|
||||
existing.limit_value = entry.limit_value
|
||||
existing.is_enabled = entry.is_enabled
|
||||
existing.reason = entry.reason
|
||||
results.append(existing)
|
||||
else:
|
||||
row = MerchantFeatureOverride(
|
||||
merchant_id=merchant_id,
|
||||
platform_id=platform_id,
|
||||
feature_code=entry.feature_code,
|
||||
limit_value=entry.limit_value,
|
||||
is_enabled=entry.is_enabled,
|
||||
reason=entry.reason,
|
||||
)
|
||||
db.add(row)
|
||||
results.append(row)
|
||||
results = feature_service.upsert_merchant_overrides(
|
||||
db, merchant_id, platform_id, [e.model_dump() for e in entries]
|
||||
)
|
||||
|
||||
db.commit()
|
||||
|
||||
|
||||
@@ -9,15 +9,21 @@ for all billing service calls.
|
||||
|
||||
import logging
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from pydantic import BaseModel
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_current_store_api, require_module_access
|
||||
from app.core.database import get_db
|
||||
from app.modules.billing.schemas.billing import (
|
||||
InvoiceListResponse,
|
||||
InvoiceResponse,
|
||||
SubscriptionStatusResponse,
|
||||
TierListResponse,
|
||||
TierResponse,
|
||||
)
|
||||
from app.modules.billing.services import billing_service, subscription_service
|
||||
from app.modules.enums import FrontendType
|
||||
from app.modules.tenancy.models import User
|
||||
from models.schema.auth import UserContext
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -28,96 +34,6 @@ store_router = APIRouter(
|
||||
)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Helpers
|
||||
# ============================================================================
|
||||
|
||||
|
||||
def _resolve_store_to_merchant(db: Session, store_id: int) -> tuple[int, int]:
|
||||
"""Resolve store_id to (merchant_id, platform_id)."""
|
||||
from app.modules.tenancy.models import Store, StorePlatform
|
||||
|
||||
store = db.query(Store).filter(Store.id == store_id).first()
|
||||
if not store or not store.merchant_id:
|
||||
raise HTTPException(status_code=404, detail="Store not found")
|
||||
|
||||
sp = db.query(StorePlatform.platform_id).filter(
|
||||
StorePlatform.store_id == store_id
|
||||
).first()
|
||||
if not sp:
|
||||
raise HTTPException(status_code=404, detail="Store not linked to platform")
|
||||
|
||||
return store.merchant_id, sp[0]
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Schemas
|
||||
# ============================================================================
|
||||
|
||||
|
||||
class SubscriptionStatusResponse(BaseModel):
|
||||
"""Current subscription status."""
|
||||
|
||||
tier_code: str
|
||||
tier_name: str
|
||||
status: str
|
||||
is_trial: bool
|
||||
trial_ends_at: str | None = None
|
||||
period_start: str | None = None
|
||||
period_end: str | None = None
|
||||
cancelled_at: str | None = None
|
||||
cancellation_reason: str | None = None
|
||||
has_payment_method: bool
|
||||
last_payment_error: str | None = None
|
||||
feature_codes: list[str] = []
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class TierResponse(BaseModel):
|
||||
"""Subscription tier information."""
|
||||
|
||||
code: str
|
||||
name: str
|
||||
description: str | None = None
|
||||
price_monthly_cents: int
|
||||
price_annual_cents: int | None = None
|
||||
feature_codes: list[str] = []
|
||||
is_current: bool = False
|
||||
can_upgrade: bool = False
|
||||
can_downgrade: bool = False
|
||||
|
||||
|
||||
class TierListResponse(BaseModel):
|
||||
"""List of available tiers."""
|
||||
|
||||
tiers: list[TierResponse]
|
||||
current_tier: str
|
||||
|
||||
|
||||
class InvoiceResponse(BaseModel):
|
||||
"""Invoice information."""
|
||||
|
||||
id: int
|
||||
invoice_number: str | None = None
|
||||
invoice_date: str
|
||||
due_date: str | None = None
|
||||
total_cents: int
|
||||
amount_paid_cents: int
|
||||
currency: str
|
||||
status: str
|
||||
pdf_url: str | None = None
|
||||
hosted_url: str | None = None
|
||||
|
||||
|
||||
class InvoiceListResponse(BaseModel):
|
||||
"""List of invoices."""
|
||||
|
||||
invoices: list[InvoiceResponse]
|
||||
total: int
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Core Billing Endpoints
|
||||
# ============================================================================
|
||||
@@ -125,12 +41,12 @@ class InvoiceListResponse(BaseModel):
|
||||
|
||||
@store_router.get("/subscription", response_model=SubscriptionStatusResponse)
|
||||
def get_subscription_status(
|
||||
current_user: User = Depends(get_current_store_api),
|
||||
current_user: UserContext = Depends(get_current_store_api),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""Get current subscription status."""
|
||||
store_id = current_user.token_store_id
|
||||
merchant_id, platform_id = _resolve_store_to_merchant(db, store_id)
|
||||
merchant_id, platform_id = subscription_service.resolve_store_to_merchant(db, store_id)
|
||||
|
||||
subscription, tier = billing_service.get_subscription_with_tier(db, merchant_id, platform_id)
|
||||
|
||||
@@ -162,12 +78,12 @@ def get_subscription_status(
|
||||
|
||||
@store_router.get("/tiers", response_model=TierListResponse)
|
||||
def get_available_tiers(
|
||||
current_user: User = Depends(get_current_store_api),
|
||||
current_user: UserContext = Depends(get_current_store_api),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""Get available subscription tiers for upgrade/downgrade."""
|
||||
store_id = current_user.token_store_id
|
||||
merchant_id, platform_id = _resolve_store_to_merchant(db, store_id)
|
||||
merchant_id, platform_id = subscription_service.resolve_store_to_merchant(db, store_id)
|
||||
|
||||
subscription = subscription_service.get_or_create_subscription(db, merchant_id, platform_id)
|
||||
current_tier_id = subscription.tier_id
|
||||
@@ -184,12 +100,12 @@ def get_available_tiers(
|
||||
def get_invoices(
|
||||
skip: int = Query(0, ge=0),
|
||||
limit: int = Query(20, ge=1, le=100),
|
||||
current_user: User = Depends(get_current_store_api),
|
||||
current_user: UserContext = Depends(get_current_store_api),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""Get invoice history."""
|
||||
store_id = current_user.token_store_id
|
||||
merchant_id, platform_id = _resolve_store_to_merchant(db, store_id)
|
||||
merchant_id, platform_id = subscription_service.resolve_store_to_merchant(db, store_id)
|
||||
|
||||
invoices, total = billing_service.get_invoices(db, merchant_id, skip=skip, limit=limit)
|
||||
|
||||
|
||||
@@ -15,14 +15,24 @@ Resolves store_id to (merchant_id, platform_id) for all billing service calls.
|
||||
|
||||
import logging
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_current_store_api, require_module_access
|
||||
from app.core.config import settings
|
||||
from app.core.database import get_db
|
||||
from app.modules.billing.schemas.billing import (
|
||||
CancelRequest,
|
||||
CancelResponse,
|
||||
ChangeTierRequest,
|
||||
ChangeTierResponse,
|
||||
CheckoutRequest,
|
||||
CheckoutResponse,
|
||||
PortalResponse,
|
||||
UpcomingInvoiceResponse,
|
||||
)
|
||||
from app.modules.billing.services import billing_service
|
||||
from app.modules.billing.services.subscription_service import subscription_service
|
||||
from app.modules.enums import FrontendType
|
||||
from models.schema.auth import UserContext
|
||||
|
||||
@@ -32,91 +42,6 @@ store_checkout_router = APIRouter(
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Helpers
|
||||
# ============================================================================
|
||||
|
||||
|
||||
def _resolve_store_to_merchant(db: Session, store_id: int) -> tuple[int, int]:
|
||||
"""Resolve store_id to (merchant_id, platform_id)."""
|
||||
from app.modules.tenancy.models import Store, StorePlatform
|
||||
|
||||
store = db.query(Store).filter(Store.id == store_id).first()
|
||||
if not store or not store.merchant_id:
|
||||
raise HTTPException(status_code=404, detail="Store not found")
|
||||
|
||||
sp = db.query(StorePlatform.platform_id).filter(
|
||||
StorePlatform.store_id == store_id
|
||||
).first()
|
||||
if not sp:
|
||||
raise HTTPException(status_code=404, detail="Store not linked to platform")
|
||||
|
||||
return store.merchant_id, sp[0]
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Schemas
|
||||
# ============================================================================
|
||||
|
||||
|
||||
class CheckoutRequest(BaseModel):
|
||||
"""Request to create a checkout session."""
|
||||
|
||||
tier_code: str
|
||||
is_annual: bool = False
|
||||
|
||||
|
||||
class CheckoutResponse(BaseModel):
|
||||
"""Checkout session response."""
|
||||
|
||||
checkout_url: str
|
||||
session_id: str
|
||||
|
||||
|
||||
class PortalResponse(BaseModel):
|
||||
"""Customer portal session response."""
|
||||
|
||||
portal_url: str
|
||||
|
||||
|
||||
class CancelRequest(BaseModel):
|
||||
"""Request to cancel subscription."""
|
||||
|
||||
reason: str | None = None
|
||||
immediately: bool = False
|
||||
|
||||
|
||||
class CancelResponse(BaseModel):
|
||||
"""Cancellation response."""
|
||||
|
||||
message: str
|
||||
effective_date: str
|
||||
|
||||
|
||||
class UpcomingInvoiceResponse(BaseModel):
|
||||
"""Upcoming invoice preview."""
|
||||
|
||||
amount_due_cents: int
|
||||
currency: str
|
||||
next_payment_date: str | None = None
|
||||
line_items: list[dict] = []
|
||||
|
||||
|
||||
class ChangeTierRequest(BaseModel):
|
||||
"""Request to change subscription tier."""
|
||||
|
||||
tier_code: str
|
||||
is_annual: bool = False
|
||||
|
||||
|
||||
class ChangeTierResponse(BaseModel):
|
||||
"""Response for tier change."""
|
||||
|
||||
message: str
|
||||
new_tier: str
|
||||
effective_immediately: bool
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Endpoints
|
||||
# ============================================================================
|
||||
@@ -130,15 +55,13 @@ def create_checkout_session(
|
||||
):
|
||||
"""Create a Stripe checkout session for subscription."""
|
||||
store_id = current_user.token_store_id
|
||||
merchant_id, platform_id = _resolve_store_to_merchant(db, store_id)
|
||||
merchant_id, platform_id = subscription_service.resolve_store_to_merchant(db, store_id)
|
||||
|
||||
from app.modules.tenancy.models import Store
|
||||
|
||||
store = db.query(Store).filter(Store.id == store_id).first()
|
||||
store_code = subscription_service.get_store_code(db, store_id)
|
||||
|
||||
base_url = f"https://{settings.platform_domain}"
|
||||
success_url = f"{base_url}/store/{store.store_code}/billing?success=true"
|
||||
cancel_url = f"{base_url}/store/{store.store_code}/billing?cancelled=true"
|
||||
success_url = f"{base_url}/store/{store_code}/billing?success=true"
|
||||
cancel_url = f"{base_url}/store/{store_code}/billing?cancelled=true"
|
||||
|
||||
result = billing_service.create_checkout_session(
|
||||
db=db,
|
||||
@@ -161,12 +84,10 @@ def create_portal_session(
|
||||
):
|
||||
"""Create a Stripe customer portal session."""
|
||||
store_id = current_user.token_store_id
|
||||
merchant_id, platform_id = _resolve_store_to_merchant(db, store_id)
|
||||
merchant_id, platform_id = subscription_service.resolve_store_to_merchant(db, store_id)
|
||||
|
||||
from app.modules.tenancy.models import Store
|
||||
|
||||
store = db.query(Store).filter(Store.id == store_id).first()
|
||||
return_url = f"https://{settings.platform_domain}/store/{store.store_code}/billing"
|
||||
store_code = subscription_service.get_store_code(db, store_id)
|
||||
return_url = f"https://{settings.platform_domain}/store/{store_code}/billing"
|
||||
|
||||
result = billing_service.create_portal_session(db, merchant_id, platform_id, return_url)
|
||||
|
||||
@@ -181,7 +102,7 @@ def cancel_subscription(
|
||||
):
|
||||
"""Cancel subscription."""
|
||||
store_id = current_user.token_store_id
|
||||
merchant_id, platform_id = _resolve_store_to_merchant(db, store_id)
|
||||
merchant_id, platform_id = subscription_service.resolve_store_to_merchant(db, store_id)
|
||||
|
||||
result = billing_service.cancel_subscription(
|
||||
db=db,
|
||||
@@ -205,7 +126,7 @@ def reactivate_subscription(
|
||||
):
|
||||
"""Reactivate a cancelled subscription."""
|
||||
store_id = current_user.token_store_id
|
||||
merchant_id, platform_id = _resolve_store_to_merchant(db, store_id)
|
||||
merchant_id, platform_id = subscription_service.resolve_store_to_merchant(db, store_id)
|
||||
|
||||
result = billing_service.reactivate_subscription(db, merchant_id, platform_id)
|
||||
db.commit()
|
||||
@@ -220,7 +141,7 @@ def get_upcoming_invoice(
|
||||
):
|
||||
"""Preview the upcoming invoice."""
|
||||
store_id = current_user.token_store_id
|
||||
merchant_id, platform_id = _resolve_store_to_merchant(db, store_id)
|
||||
merchant_id, platform_id = subscription_service.resolve_store_to_merchant(db, store_id)
|
||||
|
||||
result = billing_service.get_upcoming_invoice(db, merchant_id, platform_id)
|
||||
|
||||
@@ -240,7 +161,7 @@ def change_tier(
|
||||
):
|
||||
"""Change subscription tier (upgrade/downgrade)."""
|
||||
store_id = current_user.token_store_id
|
||||
merchant_id, platform_id = _resolve_store_to_merchant(db, store_id)
|
||||
merchant_id, platform_id = subscription_service.resolve_store_to_merchant(db, store_id)
|
||||
|
||||
result = billing_service.change_tier(
|
||||
db=db,
|
||||
|
||||
@@ -19,13 +19,21 @@ All routes require module access control for the 'billing' module.
|
||||
|
||||
import logging
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from pydantic import BaseModel
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_current_store_api, require_module_access
|
||||
from app.core.database import get_db
|
||||
from app.modules.billing.exceptions import FeatureNotFoundError
|
||||
from app.modules.billing.schemas.billing import (
|
||||
CategoryListResponse,
|
||||
FeatureCodeListResponse,
|
||||
FeatureDetailResponse,
|
||||
FeatureGroupedResponse,
|
||||
FeatureListResponse,
|
||||
FeatureResponse,
|
||||
StoreFeatureCheckResponse,
|
||||
)
|
||||
from app.modules.billing.services.feature_aggregator import feature_aggregator
|
||||
from app.modules.billing.services.feature_service import feature_service
|
||||
from app.modules.billing.services.subscription_service import subscription_service
|
||||
@@ -39,100 +47,6 @@ store_features_router = APIRouter(
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Helpers
|
||||
# ============================================================================
|
||||
|
||||
|
||||
def _resolve_store_to_merchant(db: Session, store_id: int) -> tuple[int, int]:
|
||||
"""Resolve store_id to (merchant_id, platform_id)."""
|
||||
from app.modules.tenancy.models import Store, StorePlatform
|
||||
|
||||
store = db.query(Store).filter(Store.id == store_id).first()
|
||||
if not store or not store.merchant_id:
|
||||
raise HTTPException(status_code=404, detail="Store not found")
|
||||
|
||||
sp = db.query(StorePlatform.platform_id).filter(
|
||||
StorePlatform.store_id == store_id
|
||||
).first()
|
||||
if not sp:
|
||||
raise HTTPException(status_code=404, detail="Store not linked to platform")
|
||||
|
||||
return store.merchant_id, sp[0]
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Response Schemas
|
||||
# ============================================================================
|
||||
|
||||
|
||||
class FeatureCodeListResponse(BaseModel):
|
||||
"""Simple list of available feature codes for quick checks."""
|
||||
|
||||
features: list[str]
|
||||
tier_code: str
|
||||
tier_name: str
|
||||
|
||||
|
||||
class FeatureResponse(BaseModel):
|
||||
"""Full feature information."""
|
||||
|
||||
code: str
|
||||
name: str
|
||||
description: str | None = None
|
||||
category: str
|
||||
feature_type: str | None = None
|
||||
ui_icon: str | None = None
|
||||
is_available: bool
|
||||
|
||||
|
||||
class FeatureListResponse(BaseModel):
|
||||
"""List of features with metadata."""
|
||||
|
||||
features: list[FeatureResponse]
|
||||
available_count: int
|
||||
total_count: int
|
||||
tier_code: str
|
||||
tier_name: str
|
||||
|
||||
|
||||
class FeatureDetailResponse(BaseModel):
|
||||
"""Single feature detail with upgrade info."""
|
||||
|
||||
code: str
|
||||
name: str
|
||||
description: str | None = None
|
||||
category: str
|
||||
feature_type: str | None = None
|
||||
ui_icon: str | None = None
|
||||
is_available: bool
|
||||
# Upgrade info (only if not available)
|
||||
upgrade_tier_code: str | None = None
|
||||
upgrade_tier_name: str | None = None
|
||||
upgrade_tier_price_monthly_cents: int | None = None
|
||||
|
||||
|
||||
class CategoryListResponse(BaseModel):
|
||||
"""List of feature categories."""
|
||||
|
||||
categories: list[str]
|
||||
|
||||
|
||||
class FeatureGroupedResponse(BaseModel):
|
||||
"""Features grouped by category."""
|
||||
|
||||
categories: dict[str, list[FeatureResponse]]
|
||||
available_count: int
|
||||
total_count: int
|
||||
|
||||
|
||||
class FeatureCheckResponse(BaseModel):
|
||||
"""Quick feature availability check response."""
|
||||
|
||||
has_feature: bool
|
||||
feature_code: str
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Internal Helpers
|
||||
# ============================================================================
|
||||
@@ -181,7 +95,7 @@ def get_available_features(
|
||||
List of feature codes the store has access to
|
||||
"""
|
||||
store_id = current_user.token_store_id
|
||||
merchant_id, platform_id = _resolve_store_to_merchant(db, store_id)
|
||||
merchant_id, platform_id = subscription_service.resolve_store_to_merchant(db, store_id)
|
||||
|
||||
# Get available feature codes
|
||||
feature_codes = feature_service.get_merchant_feature_codes(db, merchant_id, platform_id)
|
||||
@@ -220,7 +134,7 @@ def get_features(
|
||||
List of features with metadata and availability
|
||||
"""
|
||||
store_id = current_user.token_store_id
|
||||
merchant_id, platform_id = _resolve_store_to_merchant(db, store_id)
|
||||
merchant_id, platform_id = subscription_service.resolve_store_to_merchant(db, store_id)
|
||||
|
||||
# Get all declarations and available codes
|
||||
all_declarations = feature_aggregator.get_all_declarations()
|
||||
@@ -283,7 +197,7 @@ def get_features_grouped(
|
||||
Useful for rendering feature comparison tables or settings pages.
|
||||
"""
|
||||
store_id = current_user.token_store_id
|
||||
merchant_id, platform_id = _resolve_store_to_merchant(db, store_id)
|
||||
merchant_id, platform_id = subscription_service.resolve_store_to_merchant(db, store_id)
|
||||
|
||||
# Get declarations grouped by category and available codes
|
||||
by_category = feature_aggregator.get_declarations_by_category()
|
||||
@@ -313,7 +227,7 @@ def get_features_grouped(
|
||||
)
|
||||
|
||||
|
||||
@store_features_router.get("/check/{feature_code}", response_model=FeatureCheckResponse)
|
||||
@store_features_router.get("/check/{feature_code}", response_model=StoreFeatureCheckResponse)
|
||||
def check_feature(
|
||||
feature_code: str,
|
||||
current_user: UserContext = Depends(get_current_store_api),
|
||||
@@ -334,7 +248,7 @@ def check_feature(
|
||||
store_id = current_user.token_store_id
|
||||
has = feature_service.has_feature_for_store(db, store_id, feature_code)
|
||||
|
||||
return FeatureCheckResponse(has_feature=has, feature_code=feature_code)
|
||||
return StoreFeatureCheckResponse(has_feature=has, feature_code=feature_code)
|
||||
|
||||
|
||||
@store_features_router.get("/{feature_code}", response_model=FeatureDetailResponse)
|
||||
@@ -356,7 +270,7 @@ def get_feature_detail(
|
||||
Feature details with upgrade info if locked
|
||||
"""
|
||||
store_id = current_user.token_store_id
|
||||
merchant_id, platform_id = _resolve_store_to_merchant(db, store_id)
|
||||
merchant_id, platform_id = subscription_service.resolve_store_to_merchant(db, store_id)
|
||||
|
||||
# Get feature declaration
|
||||
decl = feature_aggregator.get_declaration(feature_code)
|
||||
|
||||
@@ -17,12 +17,25 @@ from app.modules.billing.schemas.billing import (
|
||||
# Billing History schemas
|
||||
BillingHistoryResponse,
|
||||
BillingHistoryWithMerchant,
|
||||
# Store Checkout schemas
|
||||
CancelRequest,
|
||||
CancelResponse,
|
||||
CategoryListResponse,
|
||||
ChangeTierRequest as BillingChangeTierRequest,
|
||||
ChangeTierResponse as BillingChangeTierResponse,
|
||||
# Checkout & Portal schemas
|
||||
CheckoutRequest,
|
||||
CheckoutResponse,
|
||||
FeatureCatalogResponse,
|
||||
FeatureCodeListResponse,
|
||||
# Feature Catalog schemas
|
||||
FeatureDeclarationResponse,
|
||||
FeatureDetailResponse,
|
||||
FeatureGroupedResponse,
|
||||
FeatureListResponse,
|
||||
FeatureResponse,
|
||||
InvoiceListResponse,
|
||||
InvoiceResponse,
|
||||
# Merchant Feature Override schemas
|
||||
MerchantFeatureOverrideEntry,
|
||||
MerchantFeatureOverrideResponse,
|
||||
@@ -32,16 +45,22 @@ from app.modules.billing.schemas.billing import (
|
||||
MerchantSubscriptionAdminUpdate,
|
||||
MerchantSubscriptionListResponse,
|
||||
MerchantSubscriptionWithMerchant,
|
||||
PortalResponse,
|
||||
PortalSessionResponse,
|
||||
StoreFeatureCheckResponse,
|
||||
# Stats schemas
|
||||
SubscriptionStatsResponse,
|
||||
SubscriptionStatusResponse,
|
||||
SubscriptionTierBase,
|
||||
SubscriptionTierCreate,
|
||||
SubscriptionTierListResponse,
|
||||
SubscriptionTierResponse,
|
||||
SubscriptionTierUpdate,
|
||||
TierListResponse,
|
||||
TierResponse,
|
||||
# Subscription Tier Admin schemas
|
||||
TierFeatureLimitEntry,
|
||||
UpcomingInvoiceResponse,
|
||||
)
|
||||
from app.modules.billing.schemas.subscription import (
|
||||
ChangeTierRequest,
|
||||
@@ -113,6 +132,26 @@ __all__ = [
|
||||
"CheckoutRequest",
|
||||
"CheckoutResponse",
|
||||
"PortalSessionResponse",
|
||||
"PortalResponse",
|
||||
"CancelRequest",
|
||||
"CancelResponse",
|
||||
"UpcomingInvoiceResponse",
|
||||
"BillingChangeTierRequest",
|
||||
"BillingChangeTierResponse",
|
||||
# Store subscription schemas (billing.py)
|
||||
"SubscriptionStatusResponse",
|
||||
"TierResponse",
|
||||
"TierListResponse",
|
||||
"InvoiceResponse",
|
||||
"InvoiceListResponse",
|
||||
# Store feature schemas (billing.py)
|
||||
"FeatureCodeListResponse",
|
||||
"FeatureResponse",
|
||||
"FeatureListResponse",
|
||||
"FeatureDetailResponse",
|
||||
"CategoryListResponse",
|
||||
"FeatureGroupedResponse",
|
||||
"StoreFeatureCheckResponse",
|
||||
# Stats schemas (billing.py)
|
||||
"SubscriptionStatsResponse",
|
||||
# Feature Catalog schemas (billing.py)
|
||||
|
||||
@@ -358,3 +358,192 @@ class FeatureCatalogResponse(BaseModel):
|
||||
|
||||
features: dict[str, list[FeatureDeclarationResponse]]
|
||||
total_count: int
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Store Checkout Schemas
|
||||
# ============================================================================
|
||||
|
||||
|
||||
class PortalResponse(BaseModel):
|
||||
"""Customer portal session response."""
|
||||
|
||||
portal_url: str
|
||||
|
||||
|
||||
class CancelRequest(BaseModel):
|
||||
"""Request to cancel subscription."""
|
||||
|
||||
reason: str | None = None
|
||||
immediately: bool = False
|
||||
|
||||
|
||||
class CancelResponse(BaseModel):
|
||||
"""Cancellation response."""
|
||||
|
||||
message: str
|
||||
effective_date: str
|
||||
|
||||
|
||||
class UpcomingInvoiceResponse(BaseModel):
|
||||
"""Upcoming invoice preview."""
|
||||
|
||||
amount_due_cents: int
|
||||
currency: str
|
||||
next_payment_date: str | None = None
|
||||
line_items: list[dict] = []
|
||||
|
||||
|
||||
class ChangeTierRequest(BaseModel):
|
||||
"""Request to change subscription tier."""
|
||||
|
||||
tier_code: str
|
||||
is_annual: bool = False
|
||||
|
||||
|
||||
class ChangeTierResponse(BaseModel):
|
||||
"""Response for tier change."""
|
||||
|
||||
message: str
|
||||
new_tier: str
|
||||
effective_immediately: bool
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Store Subscription Schemas
|
||||
# ============================================================================
|
||||
|
||||
|
||||
class SubscriptionStatusResponse(BaseModel):
|
||||
"""Current subscription status."""
|
||||
|
||||
tier_code: str
|
||||
tier_name: str
|
||||
status: str
|
||||
is_trial: bool
|
||||
trial_ends_at: str | None = None
|
||||
period_start: str | None = None
|
||||
period_end: str | None = None
|
||||
cancelled_at: str | None = None
|
||||
cancellation_reason: str | None = None
|
||||
has_payment_method: bool
|
||||
last_payment_error: str | None = None
|
||||
feature_codes: list[str] = []
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class TierResponse(BaseModel):
|
||||
"""Subscription tier information."""
|
||||
|
||||
code: str
|
||||
name: str
|
||||
description: str | None = None
|
||||
price_monthly_cents: int
|
||||
price_annual_cents: int | None = None
|
||||
feature_codes: list[str] = []
|
||||
is_current: bool = False
|
||||
can_upgrade: bool = False
|
||||
can_downgrade: bool = False
|
||||
|
||||
|
||||
class TierListResponse(BaseModel):
|
||||
"""List of available tiers."""
|
||||
|
||||
tiers: list[TierResponse]
|
||||
current_tier: str
|
||||
|
||||
|
||||
class InvoiceResponse(BaseModel):
|
||||
"""Invoice information."""
|
||||
|
||||
id: int
|
||||
invoice_number: str | None = None
|
||||
invoice_date: str
|
||||
due_date: str | None = None
|
||||
total_cents: int
|
||||
amount_paid_cents: int
|
||||
currency: str
|
||||
status: str
|
||||
pdf_url: str | None = None
|
||||
hosted_url: str | None = None
|
||||
|
||||
|
||||
class InvoiceListResponse(BaseModel):
|
||||
"""List of invoices."""
|
||||
|
||||
invoices: list[InvoiceResponse]
|
||||
total: int
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Store Feature Schemas
|
||||
# ============================================================================
|
||||
|
||||
|
||||
class FeatureCodeListResponse(BaseModel):
|
||||
"""Simple list of available feature codes for quick checks."""
|
||||
|
||||
features: list[str]
|
||||
tier_code: str
|
||||
tier_name: str
|
||||
|
||||
|
||||
class FeatureResponse(BaseModel):
|
||||
"""Full feature information."""
|
||||
|
||||
code: str
|
||||
name: str
|
||||
description: str | None = None
|
||||
category: str
|
||||
feature_type: str | None = None
|
||||
ui_icon: str | None = None
|
||||
is_available: bool
|
||||
|
||||
|
||||
class FeatureListResponse(BaseModel):
|
||||
"""List of features with metadata."""
|
||||
|
||||
features: list[FeatureResponse]
|
||||
available_count: int
|
||||
total_count: int
|
||||
tier_code: str
|
||||
tier_name: str
|
||||
|
||||
|
||||
class FeatureDetailResponse(BaseModel):
|
||||
"""Single feature detail with upgrade info."""
|
||||
|
||||
code: str
|
||||
name: str
|
||||
description: str | None = None
|
||||
category: str
|
||||
feature_type: str | None = None
|
||||
ui_icon: str | None = None
|
||||
is_available: bool
|
||||
# Upgrade info (only if not available)
|
||||
upgrade_tier_code: str | None = None
|
||||
upgrade_tier_name: str | None = None
|
||||
upgrade_tier_price_monthly_cents: int | None = None
|
||||
|
||||
|
||||
class CategoryListResponse(BaseModel):
|
||||
"""List of feature categories."""
|
||||
|
||||
categories: list[str]
|
||||
|
||||
|
||||
class FeatureGroupedResponse(BaseModel):
|
||||
"""Features grouped by category."""
|
||||
|
||||
categories: dict[str, list[FeatureResponse]]
|
||||
available_count: int
|
||||
total_count: int
|
||||
|
||||
|
||||
class StoreFeatureCheckResponse(BaseModel):
|
||||
"""Quick feature availability check response."""
|
||||
|
||||
has_feature: bool
|
||||
feature_code: str
|
||||
|
||||
@@ -21,10 +21,6 @@ from app.modules.billing.services.billing_service import (
|
||||
BillingService,
|
||||
billing_service,
|
||||
)
|
||||
from app.modules.billing.services.capacity_forecast_service import (
|
||||
CapacityForecastService,
|
||||
capacity_forecast_service,
|
||||
)
|
||||
from app.modules.billing.services.feature_service import (
|
||||
FeatureService,
|
||||
feature_service,
|
||||
@@ -68,8 +64,6 @@ __all__ = [
|
||||
"SubscriptionNotCancelledError",
|
||||
"FeatureService",
|
||||
"feature_service",
|
||||
"CapacityForecastService",
|
||||
"capacity_forecast_service",
|
||||
"PlatformPricingService",
|
||||
"platform_pricing_service",
|
||||
"UsageService",
|
||||
|
||||
@@ -269,6 +269,23 @@ class AdminSubscriptionService:
|
||||
"pages": ceil(total / per_page) if total > 0 else 0,
|
||||
}
|
||||
|
||||
# =========================================================================
|
||||
# Platform Helpers
|
||||
# =========================================================================
|
||||
|
||||
def get_platform_names_map(self, db: Session) -> dict[int, str]:
|
||||
"""Get mapping of platform_id -> platform_name."""
|
||||
from app.modules.tenancy.models import Platform
|
||||
|
||||
return {p.id: p.name for p in db.query(Platform).all()}
|
||||
|
||||
def get_platform_name(self, db: Session, platform_id: int) -> str | None:
|
||||
"""Get platform name by ID."""
|
||||
from app.modules.tenancy.models import Platform
|
||||
|
||||
p = db.query(Platform).filter(Platform.id == platform_id).first()
|
||||
return p.name if p else None
|
||||
|
||||
# =========================================================================
|
||||
# Statistics
|
||||
# =========================================================================
|
||||
|
||||
115
app/modules/billing/services/billing_metrics.py
Normal file
115
app/modules/billing/services/billing_metrics.py
Normal file
@@ -0,0 +1,115 @@
|
||||
# app/modules/billing/services/billing_metrics.py
|
||||
"""
|
||||
Metrics provider for the billing module.
|
||||
|
||||
Provides metrics for:
|
||||
- Subscription counts (total, active, trial)
|
||||
"""
|
||||
|
||||
import logging
|
||||
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.modules.contracts.metrics import (
|
||||
MetricsContext,
|
||||
MetricValue,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class BillingMetricsProvider:
|
||||
"""
|
||||
Metrics provider for billing module.
|
||||
|
||||
Provides subscription metrics at the platform level.
|
||||
"""
|
||||
|
||||
@property
|
||||
def metrics_category(self) -> str:
|
||||
return "billing"
|
||||
|
||||
def get_store_metrics(
|
||||
self,
|
||||
db: Session,
|
||||
store_id: int,
|
||||
context: MetricsContext | None = None,
|
||||
) -> list[MetricValue]:
|
||||
"""
|
||||
Get metrics for a specific store.
|
||||
|
||||
Subscriptions are merchant-level, not store-level, so no store metrics.
|
||||
"""
|
||||
return []
|
||||
|
||||
def get_platform_metrics(
|
||||
self,
|
||||
db: Session,
|
||||
platform_id: int,
|
||||
context: MetricsContext | None = None,
|
||||
) -> list[MetricValue]:
|
||||
"""
|
||||
Get subscription metrics aggregated for a platform.
|
||||
|
||||
Provides:
|
||||
- Total subscriptions
|
||||
- Active subscriptions (active + trial)
|
||||
- Trial subscriptions
|
||||
"""
|
||||
from app.modules.billing.models import MerchantSubscription, SubscriptionStatus
|
||||
|
||||
try:
|
||||
total_subs = (
|
||||
db.query(func.count(MerchantSubscription.id)).scalar() or 0
|
||||
)
|
||||
|
||||
active_subs = (
|
||||
db.query(func.count(MerchantSubscription.id))
|
||||
.filter(MerchantSubscription.status.in_(["active", "trial"]))
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
|
||||
trial_subs = (
|
||||
db.query(func.count(MerchantSubscription.id))
|
||||
.filter(MerchantSubscription.status == SubscriptionStatus.TRIAL.value)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
|
||||
return [
|
||||
MetricValue(
|
||||
key="billing.total_subscriptions",
|
||||
value=total_subs,
|
||||
label="Total Subscriptions",
|
||||
category="billing",
|
||||
icon="credit-card",
|
||||
description="Total number of merchant subscriptions",
|
||||
),
|
||||
MetricValue(
|
||||
key="billing.active_subscriptions",
|
||||
value=active_subs,
|
||||
label="Active Subscriptions",
|
||||
category="billing",
|
||||
icon="check-circle",
|
||||
description="Subscriptions with active or trial status",
|
||||
),
|
||||
MetricValue(
|
||||
key="billing.trial_subscriptions",
|
||||
value=trial_subs,
|
||||
label="Trial Subscriptions",
|
||||
category="billing",
|
||||
icon="clock",
|
||||
description="Subscriptions currently in trial period",
|
||||
),
|
||||
]
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to get billing platform metrics: {e}")
|
||||
return []
|
||||
|
||||
|
||||
# Singleton instance
|
||||
billing_metrics_provider = BillingMetricsProvider()
|
||||
|
||||
__all__ = ["BillingMetricsProvider", "billing_metrics_provider"]
|
||||
@@ -34,6 +34,7 @@ from app.modules.billing.models import (
|
||||
MerchantSubscription,
|
||||
SubscriptionTier,
|
||||
)
|
||||
from app.modules.billing.models.tier_feature_limit import TierFeatureLimit
|
||||
from app.modules.contracts.features import FeatureType
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -434,6 +435,87 @@ class FeatureService:
|
||||
|
||||
return summaries
|
||||
|
||||
# =========================================================================
|
||||
# Tier Feature Limit Management
|
||||
# =========================================================================
|
||||
|
||||
def get_tier_feature_limits(self, db: Session, tier_code: str) -> list:
|
||||
"""Get feature limits for a tier."""
|
||||
from app.modules.billing.services import admin_subscription_service
|
||||
|
||||
tier = admin_subscription_service.get_tier_by_code(db, tier_code)
|
||||
return (
|
||||
db.query(TierFeatureLimit)
|
||||
.filter(TierFeatureLimit.tier_id == tier.id)
|
||||
.order_by(TierFeatureLimit.feature_code)
|
||||
.all()
|
||||
)
|
||||
|
||||
def upsert_tier_feature_limits(self, db: Session, tier_code: str, entries: list[dict]) -> list:
|
||||
"""Replace feature limits for a tier. Returns list of new TierFeatureLimit objects."""
|
||||
from app.modules.billing.services import admin_subscription_service
|
||||
|
||||
tier = admin_subscription_service.get_tier_by_code(db, tier_code)
|
||||
db.query(TierFeatureLimit).filter(TierFeatureLimit.tier_id == tier.id).delete()
|
||||
new_rows = []
|
||||
for entry in entries:
|
||||
if not entry.get("enabled", True):
|
||||
continue
|
||||
row = TierFeatureLimit(
|
||||
tier_id=tier.id,
|
||||
feature_code=entry["feature_code"],
|
||||
limit_value=entry.get("limit_value"),
|
||||
)
|
||||
db.add(row)
|
||||
new_rows.append(row)
|
||||
return new_rows
|
||||
|
||||
# =========================================================================
|
||||
# Merchant Feature Override Management
|
||||
# =========================================================================
|
||||
|
||||
def get_merchant_overrides(self, db: Session, merchant_id: int) -> list:
|
||||
"""Get feature overrides for a merchant."""
|
||||
return (
|
||||
db.query(MerchantFeatureOverride)
|
||||
.filter(MerchantFeatureOverride.merchant_id == merchant_id)
|
||||
.order_by(MerchantFeatureOverride.feature_code)
|
||||
.all()
|
||||
)
|
||||
|
||||
def upsert_merchant_overrides(
|
||||
self, db: Session, merchant_id: int, platform_id: int, entries: list[dict]
|
||||
) -> list:
|
||||
"""Upsert feature overrides for a merchant."""
|
||||
results = []
|
||||
for entry in entries:
|
||||
existing = (
|
||||
db.query(MerchantFeatureOverride)
|
||||
.filter(
|
||||
MerchantFeatureOverride.merchant_id == merchant_id,
|
||||
MerchantFeatureOverride.platform_id == platform_id,
|
||||
MerchantFeatureOverride.feature_code == entry["feature_code"],
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if existing:
|
||||
existing.limit_value = entry.get("limit_value")
|
||||
existing.is_enabled = entry.get("is_enabled", True)
|
||||
existing.reason = entry.get("reason")
|
||||
results.append(existing)
|
||||
else:
|
||||
row = MerchantFeatureOverride(
|
||||
merchant_id=merchant_id,
|
||||
platform_id=platform_id,
|
||||
feature_code=entry["feature_code"],
|
||||
limit_value=entry.get("limit_value"),
|
||||
is_enabled=entry.get("is_enabled", True),
|
||||
reason=entry.get("reason"),
|
||||
)
|
||||
db.add(row)
|
||||
results.append(row)
|
||||
return results
|
||||
|
||||
# =========================================================================
|
||||
# Cache Management
|
||||
# =========================================================================
|
||||
|
||||
@@ -28,6 +28,7 @@ from datetime import UTC, datetime, timedelta
|
||||
|
||||
from sqlalchemy.orm import Session, joinedload
|
||||
|
||||
from app.exceptions import ResourceNotFoundException
|
||||
from app.modules.billing.exceptions import (
|
||||
SubscriptionNotFoundException, # Re-exported for backward compatibility
|
||||
)
|
||||
@@ -44,6 +45,41 @@ logger = logging.getLogger(__name__)
|
||||
class SubscriptionService:
|
||||
"""Service for merchant-level subscription management."""
|
||||
|
||||
# =========================================================================
|
||||
# Store Resolution
|
||||
# =========================================================================
|
||||
|
||||
def resolve_store_to_merchant(self, db: Session, store_id: int) -> tuple[int, int]:
|
||||
"""Resolve store_id to (merchant_id, platform_id).
|
||||
|
||||
Raises:
|
||||
ResourceNotFoundException: If store not found or has no platform
|
||||
"""
|
||||
from app.modules.tenancy.models import Store, StorePlatform
|
||||
|
||||
store = db.query(Store).filter(Store.id == store_id).first()
|
||||
if not store or not store.merchant_id:
|
||||
raise ResourceNotFoundException("Store", str(store_id))
|
||||
sp = db.query(StorePlatform.platform_id).filter(
|
||||
StorePlatform.store_id == store_id
|
||||
).first()
|
||||
if not sp:
|
||||
raise ResourceNotFoundException("StorePlatform", f"store_id={store_id}")
|
||||
return store.merchant_id, sp[0]
|
||||
|
||||
def get_store_code(self, db: Session, store_id: int) -> str:
|
||||
"""Get the store_code for a given store_id.
|
||||
|
||||
Raises:
|
||||
ResourceNotFoundException: If store not found
|
||||
"""
|
||||
from app.modules.tenancy.models import Store
|
||||
|
||||
store = db.query(Store).filter(Store.id == store_id).first()
|
||||
if not store:
|
||||
raise ResourceNotFoundException("Store", str(store_id))
|
||||
return store.store_code
|
||||
|
||||
# =========================================================================
|
||||
# Tier Information
|
||||
# =========================================================================
|
||||
|
||||
@@ -11,7 +11,7 @@ from datetime import datetime
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
|
||||
from app.modules.inventory.schemas import InventoryLocationResponse
|
||||
from app.modules.marketplace.schemas import MarketplaceProductResponse
|
||||
from app.modules.marketplace.schemas import MarketplaceProductResponse # IMPORT-002
|
||||
|
||||
|
||||
class ProductResponse(BaseModel):
|
||||
|
||||
@@ -11,7 +11,7 @@ from datetime import datetime
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
from app.modules.inventory.schemas import InventoryLocationResponse
|
||||
from app.modules.marketplace.schemas import MarketplaceProductResponse
|
||||
from app.modules.marketplace.schemas import MarketplaceProductResponse # IMPORT-002
|
||||
|
||||
|
||||
class ProductCreate(BaseModel):
|
||||
|
||||
@@ -20,7 +20,7 @@ from app.modules.catalog.exceptions import (
|
||||
)
|
||||
from app.modules.catalog.models import Product
|
||||
from app.modules.catalog.schemas import ProductCreate, ProductUpdate
|
||||
from app.modules.marketplace.models import MarketplaceProduct
|
||||
from app.modules.marketplace.models import MarketplaceProduct # IMPORT-002
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ __all__ = [
|
||||
"ContentPageNotPublishedException",
|
||||
"UnauthorizedContentPageAccessException",
|
||||
"StoreNotAssociatedException",
|
||||
"NoPlatformSubscriptionException",
|
||||
"ContentPageValidationException",
|
||||
# Media exceptions
|
||||
"MediaNotFoundException",
|
||||
@@ -128,6 +129,20 @@ class StoreNotAssociatedException(AuthorizationException):
|
||||
)
|
||||
|
||||
|
||||
class NoPlatformSubscriptionException(BusinessLogicException):
|
||||
"""Raised when a store is not subscribed to any platform."""
|
||||
|
||||
def __init__(self, store_id: int | None = None):
|
||||
details = {}
|
||||
if store_id:
|
||||
details["store_id"] = store_id
|
||||
super().__init__(
|
||||
message="Store is not subscribed to any platform",
|
||||
error_code="NO_PLATFORM_SUBSCRIPTION",
|
||||
details=details if details else None,
|
||||
)
|
||||
|
||||
|
||||
class ContentPageValidationException(ValidationException):
|
||||
"""Raised when content page data validation fails."""
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ from app.modules.cms.schemas import (
|
||||
StoreContentPageUpdate,
|
||||
)
|
||||
from app.modules.cms.services import content_page_service
|
||||
from app.modules.tenancy.models import User
|
||||
from app.modules.tenancy.models import User # API-007
|
||||
from app.modules.tenancy.services.store_service import (
|
||||
StoreService, # MOD-004 - shared platform service
|
||||
)
|
||||
@@ -36,25 +36,6 @@ store_content_pages_router = APIRouter(prefix="/content-pages")
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _resolve_platform_id(db: Session, store_id: int) -> int | None:
|
||||
"""Resolve platform_id from store's primary StorePlatform. Returns None if not found."""
|
||||
from app.modules.tenancy.models import StorePlatform
|
||||
primary_sp = (
|
||||
db.query(StorePlatform)
|
||||
.filter(StorePlatform.store_id == store_id, StorePlatform.is_primary.is_(True))
|
||||
.first()
|
||||
)
|
||||
if primary_sp:
|
||||
return primary_sp.platform_id
|
||||
# Fallback: any active store_platform
|
||||
any_sp = (
|
||||
db.query(StorePlatform)
|
||||
.filter(StorePlatform.store_id == store_id, StorePlatform.is_active.is_(True))
|
||||
.first()
|
||||
)
|
||||
return any_sp.platform_id if any_sp else None
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# STORE CONTENT PAGES
|
||||
# ============================================================================
|
||||
@@ -71,7 +52,7 @@ def list_store_pages(
|
||||
|
||||
Returns store-specific overrides + platform defaults (store overrides take precedence).
|
||||
"""
|
||||
platform_id = _resolve_platform_id(db, current_user.token_store_id)
|
||||
platform_id = content_page_service.resolve_platform_id(db, current_user.token_store_id)
|
||||
pages = content_page_service.list_pages_for_store(
|
||||
db, platform_id=platform_id, store_id=current_user.token_store_id, include_unpublished=include_unpublished
|
||||
)
|
||||
@@ -169,11 +150,8 @@ def get_platform_default(
|
||||
|
||||
Useful for stores to view the original before/after overriding.
|
||||
"""
|
||||
# Get store's platform
|
||||
platform_id = _resolve_platform_id(db, current_user.token_store_id)
|
||||
if platform_id is None:
|
||||
from fastapi import HTTPException
|
||||
raise HTTPException(status_code=400, detail="Store is not subscribed to any platform")
|
||||
# Get store's platform (raises NoPlatformSubscriptionException if none)
|
||||
platform_id = content_page_service.resolve_platform_id_or_raise(db, current_user.token_store_id)
|
||||
|
||||
# Get platform default (store_id=None)
|
||||
page = content_page_service.get_store_default_page(
|
||||
@@ -198,7 +176,7 @@ def get_page(
|
||||
|
||||
Returns store override if exists, otherwise platform default.
|
||||
"""
|
||||
platform_id = _resolve_platform_id(db, current_user.token_store_id)
|
||||
platform_id = content_page_service.resolve_platform_id(db, current_user.token_store_id)
|
||||
page = content_page_service.get_page_for_store_or_raise(
|
||||
db,
|
||||
platform_id=platform_id,
|
||||
|
||||
@@ -27,6 +27,7 @@ logger = logging.getLogger(__name__)
|
||||
# ============================================================================
|
||||
|
||||
|
||||
# public - storefront content pages are publicly accessible
|
||||
@router.get("/navigation", response_model=list[ContentPageListItem])
|
||||
def get_navigation_pages(request: Request, db: Session = Depends(get_db)):
|
||||
"""
|
||||
|
||||
@@ -8,7 +8,7 @@ Schemas are organized by context:
|
||||
- Public/Shop: Read-only public access
|
||||
"""
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
# ============================================================================
|
||||
# ADMIN SCHEMAS
|
||||
@@ -68,6 +68,8 @@ class ContentPageUpdate(BaseModel):
|
||||
class ContentPageResponse(BaseModel):
|
||||
"""Schema for content page response (admin/store)."""
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
id: int
|
||||
platform_id: int | None = None
|
||||
platform_code: str | None = None
|
||||
|
||||
@@ -40,6 +40,65 @@ logger = logging.getLogger(__name__)
|
||||
class ContentPageService:
|
||||
"""Service for content page operations with multi-platform support."""
|
||||
|
||||
# =========================================================================
|
||||
# Platform Resolution
|
||||
# =========================================================================
|
||||
|
||||
@staticmethod
|
||||
def resolve_platform_id(db: Session, store_id: int) -> int | None:
|
||||
"""
|
||||
Resolve platform_id from store's primary StorePlatform.
|
||||
|
||||
Resolution order:
|
||||
1. Primary StorePlatform for the store
|
||||
2. Any active StorePlatform for the store (fallback)
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
store_id: Store ID
|
||||
|
||||
Returns:
|
||||
Platform ID or None if no platform association found
|
||||
"""
|
||||
from app.modules.tenancy.models import StorePlatform
|
||||
|
||||
primary_sp = (
|
||||
db.query(StorePlatform)
|
||||
.filter(StorePlatform.store_id == store_id, StorePlatform.is_primary.is_(True))
|
||||
.first()
|
||||
)
|
||||
if primary_sp:
|
||||
return primary_sp.platform_id
|
||||
# Fallback: any active store_platform
|
||||
any_sp = (
|
||||
db.query(StorePlatform)
|
||||
.filter(StorePlatform.store_id == store_id, StorePlatform.is_active.is_(True))
|
||||
.first()
|
||||
)
|
||||
return any_sp.platform_id if any_sp else None
|
||||
|
||||
@staticmethod
|
||||
def resolve_platform_id_or_raise(db: Session, store_id: int) -> int:
|
||||
"""
|
||||
Resolve platform_id or raise NoPlatformSubscriptionException.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
store_id: Store ID
|
||||
|
||||
Returns:
|
||||
Platform ID
|
||||
|
||||
Raises:
|
||||
NoPlatformSubscriptionException: If no platform found
|
||||
"""
|
||||
from app.modules.cms.exceptions import NoPlatformSubscriptionException
|
||||
|
||||
platform_id = ContentPageService.resolve_platform_id(db, store_id)
|
||||
if platform_id is None:
|
||||
raise NoPlatformSubscriptionException(store_id=store_id)
|
||||
return platform_id
|
||||
|
||||
# =========================================================================
|
||||
# Three-Tier Resolution Methods (for store storefronts)
|
||||
# =========================================================================
|
||||
@@ -272,6 +331,46 @@ class ContentPageService:
|
||||
.all()
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def get_store_default_page(
|
||||
db: Session,
|
||||
platform_id: int,
|
||||
slug: str,
|
||||
include_unpublished: bool = False,
|
||||
) -> ContentPage | None:
|
||||
"""
|
||||
Get a single store default page by slug (fallback for stores who haven't customized).
|
||||
|
||||
These are non-platform-marketing pages with store_id=NULL.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
platform_id: Platform ID
|
||||
slug: Page slug
|
||||
include_unpublished: Include draft pages
|
||||
|
||||
Returns:
|
||||
ContentPage or None
|
||||
"""
|
||||
filters = [
|
||||
ContentPage.platform_id == platform_id,
|
||||
ContentPage.slug == slug,
|
||||
ContentPage.store_id.is_(None),
|
||||
ContentPage.is_platform_page.is_(False),
|
||||
]
|
||||
|
||||
if not include_unpublished:
|
||||
filters.append(ContentPage.is_published.is_(True))
|
||||
|
||||
page = db.query(ContentPage).filter(and_(*filters)).first()
|
||||
|
||||
if page:
|
||||
logger.debug(f"[CMS] Found store default page: {slug} for platform_id={platform_id}")
|
||||
else:
|
||||
logger.debug(f"[CMS] No store default page found: {slug} for platform_id={platform_id}")
|
||||
|
||||
return page
|
||||
|
||||
@staticmethod
|
||||
def list_store_defaults(
|
||||
db: Session,
|
||||
|
||||
@@ -80,7 +80,7 @@ class CustomerLoginResponse(BaseModel):
|
||||
# ============================================================================
|
||||
|
||||
|
||||
@router.post("/auth/register", response_model=CustomerResponse)
|
||||
@router.post("/auth/register", response_model=CustomerResponse) # public
|
||||
def register_customer(
|
||||
request: Request, customer_data: CustomerRegister, db: Session = Depends(get_db)
|
||||
):
|
||||
@@ -129,7 +129,7 @@ def register_customer(
|
||||
return CustomerResponse.model_validate(customer)
|
||||
|
||||
|
||||
@router.post("/auth/login", response_model=CustomerLoginResponse)
|
||||
@router.post("/auth/login", response_model=CustomerLoginResponse) # public
|
||||
def customer_login(
|
||||
request: Request,
|
||||
user_credentials: UserLogin,
|
||||
@@ -218,7 +218,7 @@ def customer_login(
|
||||
)
|
||||
|
||||
|
||||
@router.post("/auth/logout", response_model=LogoutResponse)
|
||||
@router.post("/auth/logout", response_model=LogoutResponse) # public
|
||||
def customer_logout(request: Request, response: Response):
|
||||
"""
|
||||
Customer logout for current store.
|
||||
@@ -260,7 +260,7 @@ def customer_logout(request: Request, response: Response):
|
||||
return LogoutResponse(message="Logged out successfully")
|
||||
|
||||
|
||||
@router.post("/auth/forgot-password", response_model=PasswordResetRequestResponse)
|
||||
@router.post("/auth/forgot-password", response_model=PasswordResetRequestResponse) # public
|
||||
def forgot_password(request: Request, email: str, db: Session = Depends(get_db)):
|
||||
"""
|
||||
Request password reset for customer.
|
||||
@@ -328,7 +328,7 @@ def forgot_password(request: Request, email: str, db: Session = Depends(get_db))
|
||||
)
|
||||
|
||||
|
||||
@router.post("/auth/reset-password", response_model=PasswordResetResponse)
|
||||
@router.post("/auth/reset-password", response_model=PasswordResetResponse) # public
|
||||
def reset_password(
|
||||
request: Request, reset_token: str, new_password: str, db: Session = Depends(get_db)
|
||||
):
|
||||
|
||||
@@ -603,7 +603,7 @@ class InventoryService:
|
||||
query = query.filter(Inventory.quantity <= low_stock)
|
||||
|
||||
if search:
|
||||
from app.modules.marketplace.models import (
|
||||
from app.modules.marketplace.models import ( # IMPORT-002
|
||||
MarketplaceProduct,
|
||||
MarketplaceProductTranslation,
|
||||
)
|
||||
|
||||
@@ -16,8 +16,8 @@ from app.modules.catalog.exceptions import ProductNotFoundException
|
||||
from app.modules.catalog.models import Product
|
||||
from app.modules.inventory.models.inventory import Inventory
|
||||
from app.modules.inventory.models.inventory_transaction import InventoryTransaction
|
||||
from app.modules.orders.exceptions import OrderNotFoundException
|
||||
from app.modules.orders.models import Order
|
||||
from app.modules.orders.exceptions import OrderNotFoundException # IMPORT-002
|
||||
from app.modules.orders.models import Order # IMPORT-002
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -239,6 +239,98 @@ class AppleWalletNotConfiguredException(LoyaltyException):
|
||||
)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Authentication Exceptions
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class InvalidAppleAuthTokenException(LoyaltyException):
|
||||
"""Raised when Apple Wallet auth token is invalid."""
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(
|
||||
message="Invalid Apple Wallet authentication token",
|
||||
error_code="INVALID_APPLE_AUTH_TOKEN",
|
||||
)
|
||||
self.status_code = 401
|
||||
|
||||
|
||||
class ApplePassGenerationException(LoyaltyException):
|
||||
"""Raised when Apple Wallet pass generation fails."""
|
||||
|
||||
def __init__(self, card_id: int):
|
||||
super().__init__(
|
||||
message="Failed to generate Apple Wallet pass",
|
||||
error_code="APPLE_PASS_GENERATION_FAILED",
|
||||
details={"card_id": card_id},
|
||||
)
|
||||
self.status_code = 500
|
||||
|
||||
|
||||
class DeviceRegistrationException(LoyaltyException):
|
||||
"""Raised when Apple Wallet device registration/unregistration fails."""
|
||||
|
||||
def __init__(self, device_id: str, operation: str = "register"):
|
||||
super().__init__(
|
||||
message=f"Failed to {operation} device",
|
||||
error_code="DEVICE_REGISTRATION_FAILED",
|
||||
details={"device_id": device_id, "operation": operation},
|
||||
)
|
||||
self.status_code = 500
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Enrollment Exceptions
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class SelfEnrollmentDisabledException(LoyaltyException):
|
||||
"""Raised when self-enrollment is not allowed."""
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(
|
||||
message="Self-enrollment is not available",
|
||||
error_code="SELF_ENROLLMENT_DISABLED",
|
||||
)
|
||||
self.status_code = 403
|
||||
|
||||
|
||||
class CustomerNotFoundByEmailException(LoyaltyException):
|
||||
"""Raised when customer is not found by email during enrollment."""
|
||||
|
||||
def __init__(self, email: str):
|
||||
super().__init__(
|
||||
message="Customer not found with provided email",
|
||||
error_code="CUSTOMER_NOT_FOUND_BY_EMAIL",
|
||||
details={"email": email},
|
||||
)
|
||||
|
||||
|
||||
class CustomerIdentifierRequiredException(LoyaltyException):
|
||||
"""Raised when neither customer_id nor email is provided."""
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(
|
||||
message="Either customer_id or email is required",
|
||||
error_code="CUSTOMER_IDENTIFIER_REQUIRED",
|
||||
)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Order Exceptions
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class OrderReferenceRequiredException(LoyaltyException):
|
||||
"""Raised when order reference is required but not provided."""
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(
|
||||
message="Order reference required",
|
||||
error_code="ORDER_REFERENCE_REQUIRED",
|
||||
)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Validation Exceptions
|
||||
# =============================================================================
|
||||
@@ -283,6 +375,16 @@ __all__ = [
|
||||
"WalletIntegrationException",
|
||||
"GoogleWalletNotConfiguredException",
|
||||
"AppleWalletNotConfiguredException",
|
||||
# Authentication
|
||||
"InvalidAppleAuthTokenException",
|
||||
"ApplePassGenerationException",
|
||||
"DeviceRegistrationException",
|
||||
# Enrollment
|
||||
"SelfEnrollmentDisabledException",
|
||||
"CustomerNotFoundByEmailException",
|
||||
"CustomerIdentifierRequiredException",
|
||||
# Order
|
||||
"OrderReferenceRequiredException",
|
||||
# Validation
|
||||
"LoyaltyValidationException",
|
||||
]
|
||||
|
||||
@@ -10,7 +10,7 @@ Platform admin endpoints for:
|
||||
|
||||
import logging
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Path, Query
|
||||
from fastapi import APIRouter, Depends, Path, Query
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_current_admin_api, require_module_access
|
||||
@@ -25,7 +25,7 @@ from app.modules.loyalty.schemas import (
|
||||
ProgramStatsResponse,
|
||||
)
|
||||
from app.modules.loyalty.services import program_service
|
||||
from app.modules.tenancy.models import User
|
||||
from app.modules.tenancy.models import User # API-007
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -51,11 +51,6 @@ def list_programs(
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""List all loyalty programs (platform admin)."""
|
||||
from sqlalchemy import func
|
||||
|
||||
from app.modules.loyalty.models import LoyaltyCard, LoyaltyTransaction
|
||||
from app.modules.tenancy.models import Merchant
|
||||
|
||||
programs, total = program_service.list_programs(
|
||||
db,
|
||||
skip=skip,
|
||||
@@ -71,45 +66,13 @@ def list_programs(
|
||||
response.is_points_enabled = program.is_points_enabled
|
||||
response.display_name = program.display_name
|
||||
|
||||
# Get merchant name
|
||||
merchant = db.query(Merchant).filter(Merchant.id == program.merchant_id).first()
|
||||
if merchant:
|
||||
response.merchant_name = merchant.name
|
||||
|
||||
# Get basic stats for this program
|
||||
response.total_cards = (
|
||||
db.query(func.count(LoyaltyCard.id))
|
||||
.filter(LoyaltyCard.merchant_id == program.merchant_id)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
response.active_cards = (
|
||||
db.query(func.count(LoyaltyCard.id))
|
||||
.filter(
|
||||
LoyaltyCard.merchant_id == program.merchant_id,
|
||||
LoyaltyCard.is_active == True,
|
||||
)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
response.total_points_issued = (
|
||||
db.query(func.sum(LoyaltyTransaction.points_delta))
|
||||
.filter(
|
||||
LoyaltyTransaction.merchant_id == program.merchant_id,
|
||||
LoyaltyTransaction.points_delta > 0,
|
||||
)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
response.total_points_redeemed = (
|
||||
db.query(func.sum(func.abs(LoyaltyTransaction.points_delta)))
|
||||
.filter(
|
||||
LoyaltyTransaction.merchant_id == program.merchant_id,
|
||||
LoyaltyTransaction.points_delta < 0,
|
||||
)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
# Get aggregation stats from service
|
||||
list_stats = program_service.get_program_list_stats(db, program)
|
||||
response.merchant_name = list_stats["merchant_name"]
|
||||
response.total_cards = list_stats["total_cards"]
|
||||
response.active_cards = list_stats["active_cards"]
|
||||
response.total_points_issued = list_stats["total_points_issued"]
|
||||
response.total_points_redeemed = list_stats["total_points_redeemed"]
|
||||
|
||||
program_responses.append(response)
|
||||
|
||||
@@ -157,8 +120,6 @@ def get_merchant_stats(
|
||||
):
|
||||
"""Get merchant-wide loyalty statistics across all locations."""
|
||||
stats = program_service.get_merchant_stats(db, merchant_id)
|
||||
if "error" in stats:
|
||||
raise HTTPException(status_code=404, detail=stats["error"])
|
||||
|
||||
return MerchantStatsResponse(**stats)
|
||||
|
||||
@@ -208,76 +169,4 @@ def get_platform_stats(
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""Get platform-wide loyalty statistics."""
|
||||
from sqlalchemy import func
|
||||
|
||||
from app.modules.loyalty.models import (
|
||||
LoyaltyCard,
|
||||
LoyaltyProgram,
|
||||
LoyaltyTransaction,
|
||||
)
|
||||
|
||||
# Program counts
|
||||
total_programs = db.query(func.count(LoyaltyProgram.id)).scalar() or 0
|
||||
active_programs = (
|
||||
db.query(func.count(LoyaltyProgram.id))
|
||||
.filter(LoyaltyProgram.is_active == True)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
|
||||
# Card counts
|
||||
total_cards = db.query(func.count(LoyaltyCard.id)).scalar() or 0
|
||||
active_cards = (
|
||||
db.query(func.count(LoyaltyCard.id))
|
||||
.filter(LoyaltyCard.is_active == True)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
|
||||
# Transaction counts (last 30 days)
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
thirty_days_ago = datetime.now(UTC) - timedelta(days=30)
|
||||
transactions_30d = (
|
||||
db.query(func.count(LoyaltyTransaction.id))
|
||||
.filter(LoyaltyTransaction.transaction_at >= thirty_days_ago)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
|
||||
# Points issued/redeemed (last 30 days)
|
||||
points_issued_30d = (
|
||||
db.query(func.sum(LoyaltyTransaction.points_delta))
|
||||
.filter(
|
||||
LoyaltyTransaction.transaction_at >= thirty_days_ago,
|
||||
LoyaltyTransaction.points_delta > 0,
|
||||
)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
|
||||
points_redeemed_30d = (
|
||||
db.query(func.sum(func.abs(LoyaltyTransaction.points_delta)))
|
||||
.filter(
|
||||
LoyaltyTransaction.transaction_at >= thirty_days_ago,
|
||||
LoyaltyTransaction.points_delta < 0,
|
||||
)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
|
||||
# Merchant count with programs
|
||||
merchants_with_programs = (
|
||||
db.query(func.count(func.distinct(LoyaltyProgram.merchant_id))).scalar() or 0
|
||||
)
|
||||
|
||||
return {
|
||||
"total_programs": total_programs,
|
||||
"active_programs": active_programs,
|
||||
"merchants_with_programs": merchants_with_programs,
|
||||
"total_cards": total_cards,
|
||||
"active_cards": active_cards,
|
||||
"transactions_30d": transactions_30d,
|
||||
"points_issued_30d": points_issued_30d,
|
||||
"points_redeemed_30d": points_redeemed_30d,
|
||||
}
|
||||
return program_service.get_platform_stats(db)
|
||||
|
||||
@@ -9,18 +9,14 @@ Platform endpoints for:
|
||||
"""
|
||||
|
||||
import logging
|
||||
from datetime import datetime
|
||||
|
||||
from fastapi import APIRouter, Depends, Header, HTTPException, Path, Response
|
||||
from fastapi import APIRouter, Depends, Header, Path, Response
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.modules.loyalty.exceptions import (
|
||||
LoyaltyException,
|
||||
)
|
||||
from app.modules.loyalty.models import LoyaltyCard
|
||||
from app.modules.loyalty.services import (
|
||||
apple_wallet_service,
|
||||
card_service,
|
||||
program_service,
|
||||
)
|
||||
|
||||
@@ -41,23 +37,11 @@ def get_program_by_store_code(
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""Get loyalty program info by store code (for enrollment page)."""
|
||||
from app.modules.tenancy.models import Store
|
||||
|
||||
# Find store by code (store_code or subdomain)
|
||||
store = (
|
||||
db.query(Store)
|
||||
.filter(
|
||||
(Store.store_code == store_code) | (Store.subdomain == store_code)
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if not store:
|
||||
raise HTTPException(status_code=404, detail="Store not found")
|
||||
store = program_service.get_store_by_code(db, store_code)
|
||||
|
||||
# Get program
|
||||
program = program_service.get_active_program_by_store(db, store.id)
|
||||
if not program:
|
||||
raise HTTPException(status_code=404, detail="No active loyalty program")
|
||||
# Get program (raises LoyaltyProgramNotFoundException if not found)
|
||||
program = program_service.require_active_program_by_store(db, store.id)
|
||||
|
||||
return {
|
||||
"store_name": store.name,
|
||||
@@ -88,21 +72,10 @@ def download_apple_pass(
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""Download Apple Wallet pass for a card."""
|
||||
# Find card by serial number
|
||||
card = (
|
||||
db.query(LoyaltyCard)
|
||||
.filter(LoyaltyCard.apple_serial_number == serial_number)
|
||||
.first()
|
||||
)
|
||||
# Find card by serial number (raises LoyaltyCardNotFoundException if not found)
|
||||
card = card_service.require_card_by_serial_number(db, serial_number)
|
||||
|
||||
if not card:
|
||||
raise HTTPException(status_code=404, detail="Pass not found")
|
||||
|
||||
try:
|
||||
pass_data = apple_wallet_service.generate_pass(db, card)
|
||||
except LoyaltyException as e:
|
||||
logger.error(f"Failed to generate Apple pass for card {card.id}: {e}")
|
||||
raise HTTPException(status_code=500, detail="Failed to generate pass")
|
||||
pass_data = apple_wallet_service.generate_pass_safe(db, card)
|
||||
|
||||
return Response(
|
||||
content=pass_data,
|
||||
@@ -132,34 +105,17 @@ def register_device(
|
||||
|
||||
Called by Apple when user adds pass to wallet.
|
||||
"""
|
||||
# Validate authorization token
|
||||
auth_token = None
|
||||
if authorization and authorization.startswith("ApplePass "):
|
||||
auth_token = authorization.split(" ", 1)[1]
|
||||
# Find card (raises LoyaltyCardNotFoundException if not found)
|
||||
card = card_service.require_card_by_serial_number(db, serial_number)
|
||||
|
||||
# Find card
|
||||
card = (
|
||||
db.query(LoyaltyCard)
|
||||
.filter(LoyaltyCard.apple_serial_number == serial_number)
|
||||
.first()
|
||||
)
|
||||
|
||||
if not card:
|
||||
raise HTTPException(status_code=404)
|
||||
|
||||
# Verify auth token
|
||||
if not auth_token or auth_token != card.apple_auth_token:
|
||||
raise HTTPException(status_code=401)
|
||||
# Verify auth token (raises InvalidAppleAuthTokenException if invalid)
|
||||
apple_wallet_service.verify_auth_token(card, authorization)
|
||||
|
||||
# Get push token from request body
|
||||
# Note: In real implementation, parse the JSON body for pushToken
|
||||
# For now, use device_id as a placeholder
|
||||
try:
|
||||
apple_wallet_service.register_device(db, card, device_id, device_id)
|
||||
return Response(status_code=201)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to register device: {e}")
|
||||
raise HTTPException(status_code=500)
|
||||
apple_wallet_service.register_device_safe(db, card, device_id, device_id)
|
||||
return Response(status_code=201)
|
||||
|
||||
|
||||
@platform_router.delete("/apple/v1/devices/{device_id}/registrations/{pass_type_id}/{serial_number}")
|
||||
@@ -175,31 +131,14 @@ def unregister_device(
|
||||
|
||||
Called by Apple when user removes pass from wallet.
|
||||
"""
|
||||
# Validate authorization token
|
||||
auth_token = None
|
||||
if authorization and authorization.startswith("ApplePass "):
|
||||
auth_token = authorization.split(" ", 1)[1]
|
||||
# Find card (raises LoyaltyCardNotFoundException if not found)
|
||||
card = card_service.require_card_by_serial_number(db, serial_number)
|
||||
|
||||
# Find card
|
||||
card = (
|
||||
db.query(LoyaltyCard)
|
||||
.filter(LoyaltyCard.apple_serial_number == serial_number)
|
||||
.first()
|
||||
)
|
||||
# Verify auth token (raises InvalidAppleAuthTokenException if invalid)
|
||||
apple_wallet_service.verify_auth_token(card, authorization)
|
||||
|
||||
if not card:
|
||||
raise HTTPException(status_code=404)
|
||||
|
||||
# Verify auth token
|
||||
if not auth_token or auth_token != card.apple_auth_token:
|
||||
raise HTTPException(status_code=401)
|
||||
|
||||
try:
|
||||
apple_wallet_service.unregister_device(db, card, device_id)
|
||||
return Response(status_code=200)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to unregister device: {e}")
|
||||
raise HTTPException(status_code=500)
|
||||
apple_wallet_service.unregister_device_safe(db, card, device_id)
|
||||
return Response(status_code=200)
|
||||
|
||||
|
||||
@platform_router.get("/apple/v1/devices/{device_id}/registrations/{pass_type_id}")
|
||||
@@ -214,32 +153,11 @@ def get_serial_numbers(
|
||||
|
||||
Called by Apple to check for updated passes.
|
||||
"""
|
||||
from app.modules.loyalty.models import AppleDeviceRegistration
|
||||
|
||||
# Find all cards registered to this device
|
||||
registrations = (
|
||||
db.query(AppleDeviceRegistration)
|
||||
.filter(AppleDeviceRegistration.device_library_identifier == device_id)
|
||||
.all()
|
||||
# Get cards registered to this device, optionally filtered by update time
|
||||
cards = apple_wallet_service.get_updated_cards_for_device(
|
||||
db, device_id, updated_since=passesUpdatedSince
|
||||
)
|
||||
|
||||
if not registrations:
|
||||
return Response(status_code=204)
|
||||
|
||||
# Get cards that have been updated since the given timestamp
|
||||
card_ids = [r.card_id for r in registrations]
|
||||
|
||||
query = db.query(LoyaltyCard).filter(LoyaltyCard.id.in_(card_ids))
|
||||
|
||||
if passesUpdatedSince:
|
||||
try:
|
||||
since = datetime.fromisoformat(passesUpdatedSince.replace("Z", "+00:00"))
|
||||
query = query.filter(LoyaltyCard.updated_at > since)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
cards = query.all()
|
||||
|
||||
if not cards:
|
||||
return Response(status_code=204)
|
||||
|
||||
@@ -265,30 +183,13 @@ def get_latest_pass(
|
||||
|
||||
Called by Apple to fetch updated pass data.
|
||||
"""
|
||||
# Validate authorization token
|
||||
auth_token = None
|
||||
if authorization and authorization.startswith("ApplePass "):
|
||||
auth_token = authorization.split(" ", 1)[1]
|
||||
# Find card (raises LoyaltyCardNotFoundException if not found)
|
||||
card = card_service.require_card_by_serial_number(db, serial_number)
|
||||
|
||||
# Find card
|
||||
card = (
|
||||
db.query(LoyaltyCard)
|
||||
.filter(LoyaltyCard.apple_serial_number == serial_number)
|
||||
.first()
|
||||
)
|
||||
# Verify auth token (raises InvalidAppleAuthTokenException if invalid)
|
||||
apple_wallet_service.verify_auth_token(card, authorization)
|
||||
|
||||
if not card:
|
||||
raise HTTPException(status_code=404)
|
||||
|
||||
# Verify auth token
|
||||
if not auth_token or auth_token != card.apple_auth_token:
|
||||
raise HTTPException(status_code=401)
|
||||
|
||||
try:
|
||||
pass_data = apple_wallet_service.generate_pass(db, card)
|
||||
except LoyaltyException as e:
|
||||
logger.error(f"Failed to generate Apple pass for card {card.id}: {e}")
|
||||
raise HTTPException(status_code=500, detail="Failed to generate pass")
|
||||
pass_data = apple_wallet_service.generate_pass_safe(db, card)
|
||||
|
||||
return Response(
|
||||
content=pass_data,
|
||||
|
||||
@@ -15,16 +15,12 @@ Cards can be used at any store within the same merchant.
|
||||
|
||||
import logging
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Path, Query, Request
|
||||
from fastapi import APIRouter, Depends, Path, Query, Request
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_current_store_api, require_module_access
|
||||
from app.core.database import get_db
|
||||
from app.modules.enums import FrontendType
|
||||
from app.modules.loyalty.exceptions import (
|
||||
LoyaltyCardNotFoundException,
|
||||
LoyaltyException,
|
||||
)
|
||||
from app.modules.loyalty.schemas import (
|
||||
CardEnrollRequest,
|
||||
CardListResponse,
|
||||
@@ -63,7 +59,7 @@ from app.modules.loyalty.services import (
|
||||
program_service,
|
||||
stamp_service,
|
||||
)
|
||||
from app.modules.tenancy.models import Store, User
|
||||
from app.modules.tenancy.models import User # API-007
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -83,10 +79,7 @@ def get_client_info(request: Request) -> tuple[str | None, str | None]:
|
||||
|
||||
def get_store_merchant_id(db: Session, store_id: int) -> int:
|
||||
"""Get the merchant ID for a store."""
|
||||
store = db.query(Store).filter(Store.id == store_id).first()
|
||||
if not store:
|
||||
raise HTTPException(status_code=404, detail="Store not found")
|
||||
return store.merchant_id
|
||||
return program_service.get_store_merchant_id(db, store_id)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
@@ -102,9 +95,7 @@ def get_program(
|
||||
"""Get the merchant's loyalty program."""
|
||||
store_id = current_user.token_store_id
|
||||
|
||||
program = program_service.get_program_by_store(db, store_id)
|
||||
if not program:
|
||||
raise HTTPException(status_code=404, detail="No loyalty program configured")
|
||||
program = program_service.require_program_by_store(db, store_id)
|
||||
|
||||
response = ProgramResponse.model_validate(program)
|
||||
response.is_stamps_enabled = program.is_stamps_enabled
|
||||
@@ -124,10 +115,7 @@ def create_program(
|
||||
store_id = current_user.token_store_id
|
||||
merchant_id = get_store_merchant_id(db, store_id)
|
||||
|
||||
try:
|
||||
program = program_service.create_program(db, merchant_id, data)
|
||||
except LoyaltyException as e:
|
||||
raise HTTPException(status_code=e.status_code, detail=e.message)
|
||||
program = program_service.create_program(db, merchant_id, data)
|
||||
|
||||
response = ProgramResponse.model_validate(program)
|
||||
response.is_stamps_enabled = program.is_stamps_enabled
|
||||
@@ -146,10 +134,7 @@ def update_program(
|
||||
"""Update the merchant's loyalty program."""
|
||||
store_id = current_user.token_store_id
|
||||
|
||||
program = program_service.get_program_by_store(db, store_id)
|
||||
if not program:
|
||||
raise HTTPException(status_code=404, detail="No loyalty program configured")
|
||||
|
||||
program = program_service.require_program_by_store(db, store_id)
|
||||
program = program_service.update_program(db, program.id, data)
|
||||
|
||||
response = ProgramResponse.model_validate(program)
|
||||
@@ -168,9 +153,7 @@ def get_stats(
|
||||
"""Get loyalty program statistics."""
|
||||
store_id = current_user.token_store_id
|
||||
|
||||
program = program_service.get_program_by_store(db, store_id)
|
||||
if not program:
|
||||
raise HTTPException(status_code=404, detail="No loyalty program configured")
|
||||
program = program_service.require_program_by_store(db, store_id)
|
||||
|
||||
stats = program_service.get_program_stats(db, program.id)
|
||||
return ProgramStatsResponse(**stats)
|
||||
@@ -186,8 +169,6 @@ def get_merchant_stats(
|
||||
merchant_id = get_store_merchant_id(db, store_id)
|
||||
|
||||
stats = program_service.get_merchant_stats(db, merchant_id)
|
||||
if "error" in stats:
|
||||
raise HTTPException(status_code=404, detail=stats["error"])
|
||||
|
||||
return MerchantStatsResponse(**stats)
|
||||
|
||||
@@ -205,9 +186,7 @@ def list_pins(
|
||||
"""List staff PINs for this store location."""
|
||||
store_id = current_user.token_store_id
|
||||
|
||||
program = program_service.get_program_by_store(db, store_id)
|
||||
if not program:
|
||||
raise HTTPException(status_code=404, detail="No loyalty program configured")
|
||||
program = program_service.require_program_by_store(db, store_id)
|
||||
|
||||
# List PINs for this store only
|
||||
pins = pin_service.list_pins(db, program.id, store_id=store_id)
|
||||
@@ -227,9 +206,7 @@ def create_pin(
|
||||
"""Create a new staff PIN for this store location."""
|
||||
store_id = current_user.token_store_id
|
||||
|
||||
program = program_service.get_program_by_store(db, store_id)
|
||||
if not program:
|
||||
raise HTTPException(status_code=404, detail="No loyalty program configured")
|
||||
program = program_service.require_program_by_store(db, store_id)
|
||||
|
||||
pin = pin_service.create_pin(db, program.id, store_id, data)
|
||||
return PinResponse.model_validate(pin)
|
||||
@@ -292,9 +269,7 @@ def list_cards(
|
||||
store_id = current_user.token_store_id
|
||||
merchant_id = get_store_merchant_id(db, store_id)
|
||||
|
||||
program = program_service.get_program_by_store(db, store_id)
|
||||
if not program:
|
||||
raise HTTPException(status_code=404, detail="No loyalty program configured")
|
||||
program = program_service.require_program_by_store(db, store_id)
|
||||
|
||||
# Filter by enrolled_at_store_id if requested
|
||||
filter_store_id = store_id if enrolled_here else None
|
||||
@@ -352,17 +327,15 @@ def lookup_card(
|
||||
"""
|
||||
store_id = current_user.token_store_id
|
||||
|
||||
try:
|
||||
# Uses lookup_card_for_store which validates merchant membership
|
||||
card = card_service.lookup_card_for_store(
|
||||
db,
|
||||
store_id,
|
||||
card_id=card_id,
|
||||
qr_code=qr_code,
|
||||
card_number=card_number,
|
||||
)
|
||||
except LoyaltyCardNotFoundException:
|
||||
raise HTTPException(status_code=404, detail="Card not found")
|
||||
# Uses lookup_card_for_store which validates merchant membership
|
||||
# Raises LoyaltyCardNotFoundException if not found
|
||||
card = card_service.lookup_card_for_store(
|
||||
db,
|
||||
store_id,
|
||||
card_id=card_id,
|
||||
qr_code=qr_code,
|
||||
card_number=card_number,
|
||||
)
|
||||
|
||||
program = card.program
|
||||
|
||||
@@ -420,13 +393,14 @@ def enroll_customer(
|
||||
"""
|
||||
store_id = current_user.token_store_id
|
||||
|
||||
if not data.customer_id:
|
||||
raise HTTPException(status_code=400, detail="customer_id is required")
|
||||
customer_id = card_service.resolve_customer_id(
|
||||
db,
|
||||
customer_id=data.customer_id,
|
||||
email=data.email,
|
||||
store_id=store_id,
|
||||
)
|
||||
|
||||
try:
|
||||
card = card_service.enroll_customer_for_store(db, data.customer_id, store_id)
|
||||
except LoyaltyException as e:
|
||||
raise HTTPException(status_code=e.status_code, detail=e.message)
|
||||
card = card_service.enroll_customer_for_store(db, customer_id, store_id)
|
||||
|
||||
program = card.program
|
||||
|
||||
@@ -461,11 +435,8 @@ def get_card_transactions(
|
||||
"""Get transaction history for a card."""
|
||||
store_id = current_user.token_store_id
|
||||
|
||||
# Verify card belongs to this merchant
|
||||
try:
|
||||
card_service.lookup_card_for_store(db, store_id, card_id=card_id)
|
||||
except LoyaltyCardNotFoundException:
|
||||
raise HTTPException(status_code=404, detail="Card not found")
|
||||
# Verify card belongs to this merchant (raises LoyaltyCardNotFoundException if not found)
|
||||
card_service.lookup_card_for_store(db, store_id, card_id=card_id)
|
||||
|
||||
transactions, total = card_service.get_card_transactions(
|
||||
db, card_id, skip=skip, limit=limit
|
||||
@@ -493,20 +464,17 @@ def add_stamp(
|
||||
store_id = current_user.token_store_id
|
||||
ip, user_agent = get_client_info(request)
|
||||
|
||||
try:
|
||||
result = stamp_service.add_stamp(
|
||||
db,
|
||||
store_id=store_id,
|
||||
card_id=data.card_id,
|
||||
qr_code=data.qr_code,
|
||||
card_number=data.card_number,
|
||||
staff_pin=data.staff_pin,
|
||||
ip_address=ip,
|
||||
user_agent=user_agent,
|
||||
notes=data.notes,
|
||||
)
|
||||
except LoyaltyException as e:
|
||||
raise HTTPException(status_code=e.status_code, detail=e.message)
|
||||
result = stamp_service.add_stamp(
|
||||
db,
|
||||
store_id=store_id,
|
||||
card_id=data.card_id,
|
||||
qr_code=data.qr_code,
|
||||
card_number=data.card_number,
|
||||
staff_pin=data.staff_pin,
|
||||
ip_address=ip,
|
||||
user_agent=user_agent,
|
||||
notes=data.notes,
|
||||
)
|
||||
|
||||
return StampResponse(**result)
|
||||
|
||||
@@ -522,20 +490,17 @@ def redeem_stamps(
|
||||
store_id = current_user.token_store_id
|
||||
ip, user_agent = get_client_info(request)
|
||||
|
||||
try:
|
||||
result = stamp_service.redeem_stamps(
|
||||
db,
|
||||
store_id=store_id,
|
||||
card_id=data.card_id,
|
||||
qr_code=data.qr_code,
|
||||
card_number=data.card_number,
|
||||
staff_pin=data.staff_pin,
|
||||
ip_address=ip,
|
||||
user_agent=user_agent,
|
||||
notes=data.notes,
|
||||
)
|
||||
except LoyaltyException as e:
|
||||
raise HTTPException(status_code=e.status_code, detail=e.message)
|
||||
result = stamp_service.redeem_stamps(
|
||||
db,
|
||||
store_id=store_id,
|
||||
card_id=data.card_id,
|
||||
qr_code=data.qr_code,
|
||||
card_number=data.card_number,
|
||||
staff_pin=data.staff_pin,
|
||||
ip_address=ip,
|
||||
user_agent=user_agent,
|
||||
notes=data.notes,
|
||||
)
|
||||
|
||||
return StampRedeemResponse(**result)
|
||||
|
||||
@@ -551,22 +516,19 @@ def void_stamps(
|
||||
store_id = current_user.token_store_id
|
||||
ip, user_agent = get_client_info(request)
|
||||
|
||||
try:
|
||||
result = stamp_service.void_stamps(
|
||||
db,
|
||||
store_id=store_id,
|
||||
card_id=data.card_id,
|
||||
qr_code=data.qr_code,
|
||||
card_number=data.card_number,
|
||||
stamps_to_void=data.stamps_to_void,
|
||||
original_transaction_id=data.original_transaction_id,
|
||||
staff_pin=data.staff_pin,
|
||||
ip_address=ip,
|
||||
user_agent=user_agent,
|
||||
notes=data.notes,
|
||||
)
|
||||
except LoyaltyException as e:
|
||||
raise HTTPException(status_code=e.status_code, detail=e.message)
|
||||
result = stamp_service.void_stamps(
|
||||
db,
|
||||
store_id=store_id,
|
||||
card_id=data.card_id,
|
||||
qr_code=data.qr_code,
|
||||
card_number=data.card_number,
|
||||
stamps_to_void=data.stamps_to_void,
|
||||
original_transaction_id=data.original_transaction_id,
|
||||
staff_pin=data.staff_pin,
|
||||
ip_address=ip,
|
||||
user_agent=user_agent,
|
||||
notes=data.notes,
|
||||
)
|
||||
|
||||
return StampVoidResponse(**result)
|
||||
|
||||
@@ -587,22 +549,19 @@ def earn_points(
|
||||
store_id = current_user.token_store_id
|
||||
ip, user_agent = get_client_info(request)
|
||||
|
||||
try:
|
||||
result = points_service.earn_points(
|
||||
db,
|
||||
store_id=store_id,
|
||||
card_id=data.card_id,
|
||||
qr_code=data.qr_code,
|
||||
card_number=data.card_number,
|
||||
purchase_amount_cents=data.purchase_amount_cents,
|
||||
order_reference=data.order_reference,
|
||||
staff_pin=data.staff_pin,
|
||||
ip_address=ip,
|
||||
user_agent=user_agent,
|
||||
notes=data.notes,
|
||||
)
|
||||
except LoyaltyException as e:
|
||||
raise HTTPException(status_code=e.status_code, detail=e.message)
|
||||
result = points_service.earn_points(
|
||||
db,
|
||||
store_id=store_id,
|
||||
card_id=data.card_id,
|
||||
qr_code=data.qr_code,
|
||||
card_number=data.card_number,
|
||||
purchase_amount_cents=data.purchase_amount_cents,
|
||||
order_reference=data.order_reference,
|
||||
staff_pin=data.staff_pin,
|
||||
ip_address=ip,
|
||||
user_agent=user_agent,
|
||||
notes=data.notes,
|
||||
)
|
||||
|
||||
return PointsEarnResponse(**result)
|
||||
|
||||
@@ -618,21 +577,18 @@ def redeem_points(
|
||||
store_id = current_user.token_store_id
|
||||
ip, user_agent = get_client_info(request)
|
||||
|
||||
try:
|
||||
result = points_service.redeem_points(
|
||||
db,
|
||||
store_id=store_id,
|
||||
card_id=data.card_id,
|
||||
qr_code=data.qr_code,
|
||||
card_number=data.card_number,
|
||||
reward_id=data.reward_id,
|
||||
staff_pin=data.staff_pin,
|
||||
ip_address=ip,
|
||||
user_agent=user_agent,
|
||||
notes=data.notes,
|
||||
)
|
||||
except LoyaltyException as e:
|
||||
raise HTTPException(status_code=e.status_code, detail=e.message)
|
||||
result = points_service.redeem_points(
|
||||
db,
|
||||
store_id=store_id,
|
||||
card_id=data.card_id,
|
||||
qr_code=data.qr_code,
|
||||
card_number=data.card_number,
|
||||
reward_id=data.reward_id,
|
||||
staff_pin=data.staff_pin,
|
||||
ip_address=ip,
|
||||
user_agent=user_agent,
|
||||
notes=data.notes,
|
||||
)
|
||||
|
||||
return PointsRedeemResponse(**result)
|
||||
|
||||
@@ -648,23 +604,20 @@ def void_points(
|
||||
store_id = current_user.token_store_id
|
||||
ip, user_agent = get_client_info(request)
|
||||
|
||||
try:
|
||||
result = points_service.void_points(
|
||||
db,
|
||||
store_id=store_id,
|
||||
card_id=data.card_id,
|
||||
qr_code=data.qr_code,
|
||||
card_number=data.card_number,
|
||||
points_to_void=data.points_to_void,
|
||||
original_transaction_id=data.original_transaction_id,
|
||||
order_reference=data.order_reference,
|
||||
staff_pin=data.staff_pin,
|
||||
ip_address=ip,
|
||||
user_agent=user_agent,
|
||||
notes=data.notes,
|
||||
)
|
||||
except LoyaltyException as e:
|
||||
raise HTTPException(status_code=e.status_code, detail=e.message)
|
||||
result = points_service.void_points(
|
||||
db,
|
||||
store_id=store_id,
|
||||
card_id=data.card_id,
|
||||
qr_code=data.qr_code,
|
||||
card_number=data.card_number,
|
||||
points_to_void=data.points_to_void,
|
||||
original_transaction_id=data.original_transaction_id,
|
||||
order_reference=data.order_reference,
|
||||
staff_pin=data.staff_pin,
|
||||
ip_address=ip,
|
||||
user_agent=user_agent,
|
||||
notes=data.notes,
|
||||
)
|
||||
|
||||
return PointsVoidResponse(**result)
|
||||
|
||||
@@ -681,18 +634,15 @@ def adjust_points(
|
||||
store_id = current_user.token_store_id
|
||||
ip, user_agent = get_client_info(request)
|
||||
|
||||
try:
|
||||
result = points_service.adjust_points(
|
||||
db,
|
||||
card_id=card_id,
|
||||
points_delta=data.points_delta,
|
||||
store_id=store_id,
|
||||
reason=data.reason,
|
||||
staff_pin=data.staff_pin,
|
||||
ip_address=ip,
|
||||
user_agent=user_agent,
|
||||
)
|
||||
except LoyaltyException as e:
|
||||
raise HTTPException(status_code=e.status_code, detail=e.message)
|
||||
result = points_service.adjust_points(
|
||||
db,
|
||||
card_id=card_id,
|
||||
points_delta=data.points_delta,
|
||||
store_id=store_id,
|
||||
reason=data.reason,
|
||||
staff_pin=data.staff_pin,
|
||||
ip_address=ip,
|
||||
user_agent=user_agent,
|
||||
)
|
||||
|
||||
return PointsAdjustResponse(**result)
|
||||
|
||||
@@ -13,7 +13,7 @@ Uses store from middleware context (StoreContextMiddleware).
|
||||
|
||||
import logging
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Request
|
||||
from fastapi import APIRouter, Depends, Query, Request
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_current_customer_api
|
||||
@@ -76,26 +76,15 @@ def self_enroll(
|
||||
raise StoreNotFoundException("context", identifier_type="subdomain")
|
||||
|
||||
# Check if self-enrollment is allowed
|
||||
settings = program_service.get_merchant_settings(db, store.merchant_id)
|
||||
if settings and not settings.allow_self_enrollment:
|
||||
raise HTTPException(403, "Self-enrollment is not available")
|
||||
program_service.check_self_enrollment_allowed(db, store.merchant_id)
|
||||
|
||||
# Resolve customer_id
|
||||
customer_id = data.customer_id
|
||||
if not customer_id and data.email:
|
||||
from app.modules.customers.models.customer import Customer
|
||||
|
||||
customer = (
|
||||
db.query(Customer)
|
||||
.filter(Customer.email == data.email, Customer.store_id == store.id)
|
||||
.first()
|
||||
)
|
||||
if not customer:
|
||||
raise HTTPException(400, "Customer not found with provided email")
|
||||
customer_id = customer.id
|
||||
|
||||
if not customer_id:
|
||||
raise HTTPException(400, "Either customer_id or email is required")
|
||||
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}")
|
||||
|
||||
@@ -141,12 +130,7 @@ def get_my_card(
|
||||
return {"card": None, "program": None, "locations": []}
|
||||
|
||||
# Get merchant locations
|
||||
from app.modules.tenancy.models import Store as StoreModel
|
||||
locations = (
|
||||
db.query(StoreModel)
|
||||
.filter(StoreModel.merchant_id == program.merchant_id, StoreModel.is_active == True)
|
||||
.all()
|
||||
)
|
||||
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
|
||||
@@ -192,39 +176,9 @@ def get_my_transactions(
|
||||
if not card:
|
||||
return {"transactions": [], "total": 0}
|
||||
|
||||
# Get transactions
|
||||
from app.modules.loyalty.models import LoyaltyTransaction
|
||||
from app.modules.tenancy.models import Store as StoreModel
|
||||
|
||||
query = (
|
||||
db.query(LoyaltyTransaction)
|
||||
.filter(LoyaltyTransaction.card_id == card.id)
|
||||
.order_by(LoyaltyTransaction.transaction_at.desc())
|
||||
# Get transactions with store names
|
||||
tx_responses, total = card_service.get_customer_transactions_with_store_names(
|
||||
db, card.id, skip=skip, limit=limit
|
||||
)
|
||||
|
||||
total = query.count()
|
||||
transactions = query.offset(skip).limit(limit).all()
|
||||
|
||||
# Build response with store names
|
||||
tx_responses = []
|
||||
for tx in transactions:
|
||||
tx_data = {
|
||||
"id": tx.id,
|
||||
"transaction_type": tx.transaction_type.value if hasattr(tx.transaction_type, "value") else str(tx.transaction_type),
|
||||
"points_delta": tx.points_delta,
|
||||
"stamps_delta": tx.stamps_delta,
|
||||
"points_balance_after": tx.points_balance_after,
|
||||
"stamps_balance_after": tx.stamps_balance_after,
|
||||
"transaction_at": tx.transaction_at.isoformat() if tx.transaction_at else None,
|
||||
"notes": tx.notes,
|
||||
"store_name": None,
|
||||
}
|
||||
|
||||
if tx.store_id:
|
||||
store_obj = db.query(StoreModel).filter(StoreModel.id == tx.store_id).first()
|
||||
if store_obj:
|
||||
tx_data["store_name"] = store_obj.name
|
||||
|
||||
tx_responses.append(tx_data)
|
||||
|
||||
return {"transactions": tx_responses, "total": total}
|
||||
|
||||
@@ -19,7 +19,10 @@ from sqlalchemy.orm import Session
|
||||
|
||||
from app.modules.loyalty.config import config
|
||||
from app.modules.loyalty.exceptions import (
|
||||
ApplePassGenerationException,
|
||||
AppleWalletNotConfiguredException,
|
||||
DeviceRegistrationException,
|
||||
InvalidAppleAuthTokenException,
|
||||
WalletIntegrationException,
|
||||
)
|
||||
from app.modules.loyalty.models import (
|
||||
@@ -45,6 +48,152 @@ class AppleWalletService:
|
||||
and config.apple_signer_key_path
|
||||
)
|
||||
|
||||
# =========================================================================
|
||||
# Auth Verification
|
||||
# =========================================================================
|
||||
|
||||
def verify_auth_token(self, card: LoyaltyCard, authorization: str | None) -> None:
|
||||
"""
|
||||
Verify the Apple Wallet authorization token for a card.
|
||||
|
||||
Args:
|
||||
card: Loyalty card
|
||||
authorization: Authorization header value (e.g. "ApplePass <token>")
|
||||
|
||||
Raises:
|
||||
InvalidAppleAuthTokenException: If token is missing or invalid
|
||||
"""
|
||||
auth_token = None
|
||||
if authorization and authorization.startswith("ApplePass "):
|
||||
auth_token = authorization.split(" ", 1)[1]
|
||||
|
||||
if not auth_token or auth_token != card.apple_auth_token:
|
||||
raise InvalidAppleAuthTokenException()
|
||||
|
||||
def generate_pass_safe(self, db: Session, card: LoyaltyCard) -> bytes:
|
||||
"""
|
||||
Generate an Apple Wallet pass, wrapping LoyaltyException into
|
||||
ApplePassGenerationException.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
card: Loyalty card
|
||||
|
||||
Returns:
|
||||
Bytes of the .pkpass file
|
||||
|
||||
Raises:
|
||||
ApplePassGenerationException: If pass generation fails
|
||||
"""
|
||||
from app.modules.loyalty.exceptions import LoyaltyException
|
||||
|
||||
try:
|
||||
return self.generate_pass(db, card)
|
||||
except LoyaltyException as e:
|
||||
logger.error(f"Failed to generate Apple pass for card {card.id}: {e}")
|
||||
raise ApplePassGenerationException(card.id)
|
||||
|
||||
def get_device_registrations(self, db: Session, device_id: str) -> list:
|
||||
"""
|
||||
Get all device registrations for a device library identifier.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
device_id: Device library identifier
|
||||
|
||||
Returns:
|
||||
List of AppleDeviceRegistration objects
|
||||
"""
|
||||
return (
|
||||
db.query(AppleDeviceRegistration)
|
||||
.filter(AppleDeviceRegistration.device_library_identifier == device_id)
|
||||
.all()
|
||||
)
|
||||
|
||||
def get_updated_cards_for_device(
|
||||
self,
|
||||
db: Session,
|
||||
device_id: str,
|
||||
updated_since: str | None = None,
|
||||
) -> list[LoyaltyCard] | None:
|
||||
"""
|
||||
Get cards registered to a device, optionally filtered by update time.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
device_id: Device library identifier
|
||||
updated_since: ISO timestamp to filter by
|
||||
|
||||
Returns:
|
||||
List of LoyaltyCard objects, or None if no registrations found
|
||||
"""
|
||||
from datetime import datetime
|
||||
|
||||
registrations = self.get_device_registrations(db, device_id)
|
||||
if not registrations:
|
||||
return None
|
||||
|
||||
card_ids = [r.card_id for r in registrations]
|
||||
query = db.query(LoyaltyCard).filter(LoyaltyCard.id.in_(card_ids))
|
||||
|
||||
if updated_since:
|
||||
try:
|
||||
since = datetime.fromisoformat(updated_since.replace("Z", "+00:00"))
|
||||
query = query.filter(LoyaltyCard.updated_at > since)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
cards = query.all()
|
||||
return cards if cards else None
|
||||
|
||||
def register_device_safe(
|
||||
self,
|
||||
db: Session,
|
||||
card: LoyaltyCard,
|
||||
device_id: str,
|
||||
push_token: str,
|
||||
) -> None:
|
||||
"""
|
||||
Register a device, wrapping exceptions into DeviceRegistrationException.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
card: Loyalty card
|
||||
device_id: Device library identifier
|
||||
push_token: Push token
|
||||
|
||||
Raises:
|
||||
DeviceRegistrationException: If registration fails
|
||||
"""
|
||||
try:
|
||||
self.register_device(db, card, device_id, push_token)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to register device: {e}")
|
||||
raise DeviceRegistrationException(device_id, "register")
|
||||
|
||||
def unregister_device_safe(
|
||||
self,
|
||||
db: Session,
|
||||
card: LoyaltyCard,
|
||||
device_id: str,
|
||||
) -> None:
|
||||
"""
|
||||
Unregister a device, wrapping exceptions into DeviceRegistrationException.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
card: Loyalty card
|
||||
device_id: Device library identifier
|
||||
|
||||
Raises:
|
||||
DeviceRegistrationException: If unregistration fails
|
||||
"""
|
||||
try:
|
||||
self.unregister_device(db, card, device_id)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to unregister device: {e}")
|
||||
raise DeviceRegistrationException(device_id, "unregister")
|
||||
|
||||
# =========================================================================
|
||||
# Pass Generation
|
||||
# =========================================================================
|
||||
|
||||
@@ -19,6 +19,8 @@ from datetime import UTC, datetime
|
||||
from sqlalchemy.orm import Session, joinedload
|
||||
|
||||
from app.modules.loyalty.exceptions import (
|
||||
CustomerIdentifierRequiredException,
|
||||
CustomerNotFoundByEmailException,
|
||||
LoyaltyCardAlreadyExistsException,
|
||||
LoyaltyCardNotFoundException,
|
||||
LoyaltyProgramInactiveException,
|
||||
@@ -106,6 +108,15 @@ class CardService:
|
||||
.first()
|
||||
)
|
||||
|
||||
def get_card_by_serial_number(self, db: Session, serial_number: str) -> LoyaltyCard | None:
|
||||
"""Get a loyalty card by Apple serial number."""
|
||||
return (
|
||||
db.query(LoyaltyCard)
|
||||
.options(joinedload(LoyaltyCard.program))
|
||||
.filter(LoyaltyCard.apple_serial_number == serial_number)
|
||||
.first()
|
||||
)
|
||||
|
||||
def require_card(self, db: Session, card_id: int) -> LoyaltyCard:
|
||||
"""Get a card or raise exception if not found."""
|
||||
card = self.get_card(db, card_id)
|
||||
@@ -113,6 +124,54 @@ class CardService:
|
||||
raise LoyaltyCardNotFoundException(str(card_id))
|
||||
return card
|
||||
|
||||
def require_card_by_serial_number(self, db: Session, serial_number: str) -> LoyaltyCard:
|
||||
"""Get a card by Apple serial number or raise exception if not found."""
|
||||
card = self.get_card_by_serial_number(db, serial_number)
|
||||
if not card:
|
||||
raise LoyaltyCardNotFoundException(serial_number)
|
||||
return card
|
||||
|
||||
def resolve_customer_id(
|
||||
self,
|
||||
db: Session,
|
||||
*,
|
||||
customer_id: int | None,
|
||||
email: str | None,
|
||||
store_id: int,
|
||||
) -> int:
|
||||
"""
|
||||
Resolve a customer ID from either a direct ID or email lookup.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
customer_id: Direct customer ID (used if provided)
|
||||
email: Customer email to look up
|
||||
store_id: Store ID for scoping the email lookup
|
||||
|
||||
Returns:
|
||||
Resolved customer ID
|
||||
|
||||
Raises:
|
||||
CustomerIdentifierRequiredException: If neither customer_id nor email provided
|
||||
CustomerNotFoundByEmailException: If email lookup fails
|
||||
"""
|
||||
if customer_id:
|
||||
return customer_id
|
||||
|
||||
if email:
|
||||
from app.modules.customers.models.customer import Customer
|
||||
|
||||
customer = (
|
||||
db.query(Customer)
|
||||
.filter(Customer.email == email, Customer.store_id == store_id)
|
||||
.first()
|
||||
)
|
||||
if not customer:
|
||||
raise CustomerNotFoundByEmailException(email)
|
||||
return customer.id
|
||||
|
||||
raise CustomerIdentifierRequiredException()
|
||||
|
||||
def lookup_card(
|
||||
self,
|
||||
db: Session,
|
||||
@@ -478,6 +537,53 @@ class CardService:
|
||||
|
||||
return transactions, total
|
||||
|
||||
def get_customer_transactions_with_store_names(
|
||||
self,
|
||||
db: Session,
|
||||
card_id: int,
|
||||
*,
|
||||
skip: int = 0,
|
||||
limit: int = 20,
|
||||
) -> tuple[list[dict], int]:
|
||||
"""
|
||||
Get transaction history for a card with store names resolved.
|
||||
|
||||
Returns a list of dicts with transaction data including store_name.
|
||||
"""
|
||||
from app.modules.tenancy.models import Store as StoreModel
|
||||
|
||||
query = (
|
||||
db.query(LoyaltyTransaction)
|
||||
.filter(LoyaltyTransaction.card_id == card_id)
|
||||
.order_by(LoyaltyTransaction.transaction_at.desc())
|
||||
)
|
||||
|
||||
total = query.count()
|
||||
transactions = query.offset(skip).limit(limit).all()
|
||||
|
||||
tx_responses = []
|
||||
for tx in transactions:
|
||||
tx_data = {
|
||||
"id": tx.id,
|
||||
"transaction_type": tx.transaction_type.value if hasattr(tx.transaction_type, "value") else str(tx.transaction_type),
|
||||
"points_delta": tx.points_delta,
|
||||
"stamps_delta": tx.stamps_delta,
|
||||
"points_balance_after": tx.points_balance_after,
|
||||
"stamps_balance_after": tx.stamps_balance_after,
|
||||
"transaction_at": tx.transaction_at.isoformat() if tx.transaction_at else None,
|
||||
"notes": tx.notes,
|
||||
"store_name": None,
|
||||
}
|
||||
|
||||
if tx.store_id:
|
||||
store_obj = db.query(StoreModel).filter(StoreModel.id == tx.store_id).first()
|
||||
if store_obj:
|
||||
tx_data["store_name"] = store_obj.name
|
||||
|
||||
tx_responses.append(tx_data)
|
||||
|
||||
return tx_responses, total
|
||||
|
||||
|
||||
# Singleton instance
|
||||
card_service = CardService()
|
||||
|
||||
@@ -17,7 +17,6 @@ Handles points operations including:
|
||||
import logging
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from fastapi import HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.modules.loyalty.exceptions import (
|
||||
@@ -25,6 +24,7 @@ from app.modules.loyalty.exceptions import (
|
||||
InvalidRewardException,
|
||||
LoyaltyCardInactiveException,
|
||||
LoyaltyProgramInactiveException,
|
||||
OrderReferenceRequiredException,
|
||||
StaffPinRequiredException,
|
||||
)
|
||||
from app.modules.loyalty.models import LoyaltyTransaction, TransactionType
|
||||
@@ -99,7 +99,7 @@ class PointsService:
|
||||
from app.modules.loyalty.services.program_service import program_service
|
||||
settings = program_service.get_merchant_settings(db, card.merchant_id)
|
||||
if settings and settings.require_order_reference and not order_reference:
|
||||
raise HTTPException(400, "Order reference required")
|
||||
raise OrderReferenceRequiredException()
|
||||
|
||||
# Check minimum purchase amount
|
||||
if program.minimum_purchase_cents > 0 and purchase_amount_cents < program.minimum_purchase_cents:
|
||||
|
||||
@@ -111,6 +111,13 @@ class ProgramService:
|
||||
raise LoyaltyProgramNotFoundException(f"merchant:{merchant_id}")
|
||||
return program
|
||||
|
||||
def require_active_program_by_store(self, db: Session, store_id: int) -> LoyaltyProgram:
|
||||
"""Get a store's active program or raise exception if not found."""
|
||||
program = self.get_active_program_by_store(db, store_id)
|
||||
if not program:
|
||||
raise LoyaltyProgramNotFoundException(f"store:{store_id}")
|
||||
return program
|
||||
|
||||
def require_program_by_store(self, db: Session, store_id: int) -> LoyaltyProgram:
|
||||
"""Get a store's program or raise exception if not found."""
|
||||
program = self.get_program_by_store(db, store_id)
|
||||
@@ -118,6 +125,235 @@ class ProgramService:
|
||||
raise LoyaltyProgramNotFoundException(f"store:{store_id}")
|
||||
return program
|
||||
|
||||
def get_store_by_code(self, db: Session, store_code: str):
|
||||
"""
|
||||
Find a store by its store_code or subdomain.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
store_code: Store code or subdomain
|
||||
|
||||
Returns:
|
||||
Store object
|
||||
|
||||
Raises:
|
||||
StoreNotFoundException: If store not found
|
||||
"""
|
||||
from app.modules.tenancy.exceptions import StoreNotFoundException
|
||||
from app.modules.tenancy.models import Store
|
||||
|
||||
store = (
|
||||
db.query(Store)
|
||||
.filter(
|
||||
(Store.store_code == store_code) | (Store.subdomain == store_code)
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if not store:
|
||||
raise StoreNotFoundException(store_code)
|
||||
return store
|
||||
|
||||
def get_store_merchant_id(self, db: Session, store_id: int) -> int:
|
||||
"""
|
||||
Get the merchant ID for a store.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
store_id: Store ID
|
||||
|
||||
Returns:
|
||||
Merchant ID
|
||||
|
||||
Raises:
|
||||
StoreNotFoundException: If store not found
|
||||
"""
|
||||
from app.modules.tenancy.exceptions import StoreNotFoundException
|
||||
from app.modules.tenancy.models import Store
|
||||
|
||||
store = db.query(Store).filter(Store.id == store_id).first()
|
||||
if not store:
|
||||
raise StoreNotFoundException(str(store_id), identifier_type="id")
|
||||
return store.merchant_id
|
||||
|
||||
def get_merchant_locations(self, db: Session, merchant_id: int) -> list:
|
||||
"""
|
||||
Get all active store locations for a merchant.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
merchant_id: Merchant ID
|
||||
|
||||
Returns:
|
||||
List of active Store objects
|
||||
"""
|
||||
from app.modules.tenancy.models import Store
|
||||
|
||||
return (
|
||||
db.query(Store)
|
||||
.filter(Store.merchant_id == merchant_id, Store.is_active == True)
|
||||
.all()
|
||||
)
|
||||
|
||||
def get_program_list_stats(self, db: Session, program) -> dict:
|
||||
"""
|
||||
Get aggregation stats for a program used in list views.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
program: LoyaltyProgram instance
|
||||
|
||||
Returns:
|
||||
Dict with merchant_name, total_cards, active_cards,
|
||||
total_points_issued, total_points_redeemed
|
||||
"""
|
||||
from sqlalchemy import func
|
||||
|
||||
from app.modules.loyalty.models import LoyaltyCard, LoyaltyTransaction
|
||||
from app.modules.tenancy.models import Merchant
|
||||
|
||||
merchant = db.query(Merchant).filter(Merchant.id == program.merchant_id).first()
|
||||
merchant_name = merchant.name if merchant else None
|
||||
|
||||
total_cards = (
|
||||
db.query(func.count(LoyaltyCard.id))
|
||||
.filter(LoyaltyCard.merchant_id == program.merchant_id)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
active_cards = (
|
||||
db.query(func.count(LoyaltyCard.id))
|
||||
.filter(
|
||||
LoyaltyCard.merchant_id == program.merchant_id,
|
||||
LoyaltyCard.is_active == True,
|
||||
)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
total_points_issued = (
|
||||
db.query(func.sum(LoyaltyTransaction.points_delta))
|
||||
.filter(
|
||||
LoyaltyTransaction.merchant_id == program.merchant_id,
|
||||
LoyaltyTransaction.points_delta > 0,
|
||||
)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
total_points_redeemed = (
|
||||
db.query(func.sum(func.abs(LoyaltyTransaction.points_delta)))
|
||||
.filter(
|
||||
LoyaltyTransaction.merchant_id == program.merchant_id,
|
||||
LoyaltyTransaction.points_delta < 0,
|
||||
)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
|
||||
return {
|
||||
"merchant_name": merchant_name,
|
||||
"total_cards": total_cards,
|
||||
"active_cards": active_cards,
|
||||
"total_points_issued": total_points_issued,
|
||||
"total_points_redeemed": total_points_redeemed,
|
||||
}
|
||||
|
||||
def get_platform_stats(self, db: Session) -> dict:
|
||||
"""
|
||||
Get platform-wide loyalty statistics.
|
||||
|
||||
Returns dict with:
|
||||
- total_programs, active_programs
|
||||
- merchants_with_programs
|
||||
- total_cards, active_cards
|
||||
- transactions_30d
|
||||
- points_issued_30d, points_redeemed_30d
|
||||
"""
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
from sqlalchemy import func
|
||||
|
||||
from app.modules.loyalty.models import (
|
||||
LoyaltyCard,
|
||||
LoyaltyProgram,
|
||||
LoyaltyTransaction,
|
||||
)
|
||||
|
||||
# Program counts
|
||||
total_programs = db.query(func.count(LoyaltyProgram.id)).scalar() or 0
|
||||
active_programs = (
|
||||
db.query(func.count(LoyaltyProgram.id))
|
||||
.filter(LoyaltyProgram.is_active == True)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
|
||||
# Card counts
|
||||
total_cards = db.query(func.count(LoyaltyCard.id)).scalar() or 0
|
||||
active_cards = (
|
||||
db.query(func.count(LoyaltyCard.id))
|
||||
.filter(LoyaltyCard.is_active == True)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
|
||||
# Transaction counts (last 30 days)
|
||||
thirty_days_ago = datetime.now(UTC) - timedelta(days=30)
|
||||
transactions_30d = (
|
||||
db.query(func.count(LoyaltyTransaction.id))
|
||||
.filter(LoyaltyTransaction.transaction_at >= thirty_days_ago)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
|
||||
# Points issued/redeemed (last 30 days)
|
||||
points_issued_30d = (
|
||||
db.query(func.sum(LoyaltyTransaction.points_delta))
|
||||
.filter(
|
||||
LoyaltyTransaction.transaction_at >= thirty_days_ago,
|
||||
LoyaltyTransaction.points_delta > 0,
|
||||
)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
|
||||
points_redeemed_30d = (
|
||||
db.query(func.sum(func.abs(LoyaltyTransaction.points_delta)))
|
||||
.filter(
|
||||
LoyaltyTransaction.transaction_at >= thirty_days_ago,
|
||||
LoyaltyTransaction.points_delta < 0,
|
||||
)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
|
||||
# Merchant count with programs
|
||||
merchants_with_programs = (
|
||||
db.query(func.count(func.distinct(LoyaltyProgram.merchant_id))).scalar() or 0
|
||||
)
|
||||
|
||||
return {
|
||||
"total_programs": total_programs,
|
||||
"active_programs": active_programs,
|
||||
"merchants_with_programs": merchants_with_programs,
|
||||
"total_cards": total_cards,
|
||||
"active_cards": active_cards,
|
||||
"transactions_30d": transactions_30d,
|
||||
"points_issued_30d": points_issued_30d,
|
||||
"points_redeemed_30d": points_redeemed_30d,
|
||||
}
|
||||
|
||||
def check_self_enrollment_allowed(self, db: Session, merchant_id: int) -> None:
|
||||
"""
|
||||
Check if self-enrollment is allowed for a merchant.
|
||||
|
||||
Raises:
|
||||
SelfEnrollmentDisabledException: If self-enrollment is disabled
|
||||
"""
|
||||
from app.modules.loyalty.exceptions import SelfEnrollmentDisabledException
|
||||
|
||||
settings = self.get_merchant_settings(db, merchant_id)
|
||||
if settings and not settings.allow_self_enrollment:
|
||||
raise SelfEnrollmentDisabledException()
|
||||
|
||||
def list_programs(
|
||||
self,
|
||||
db: Session,
|
||||
|
||||
@@ -12,8 +12,8 @@ from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_current_admin_api, require_module_access
|
||||
from app.core.database import get_db
|
||||
from app.modules.analytics.schemas import ImportStatsResponse
|
||||
from app.modules.analytics.services.stats_service import stats_service
|
||||
from app.modules.analytics.schemas import ImportStatsResponse # IMPORT-002
|
||||
from app.modules.analytics.services.stats_service import stats_service # IMPORT-002
|
||||
from app.modules.enums import FrontendType
|
||||
from app.modules.marketplace.schemas import (
|
||||
AdminMarketplaceImportJobListResponse,
|
||||
|
||||
@@ -447,7 +447,7 @@ class MarketplaceProductService:
|
||||
"""
|
||||
try:
|
||||
# SVC-005 - Admin/internal function for inventory lookup by GTIN
|
||||
inventory_entries = db.query(Inventory).filter(Inventory.gtin == gtin).all()
|
||||
inventory_entries = db.query(Inventory).filter(Inventory.gtin == gtin).all() # SVC-005
|
||||
if not inventory_entries:
|
||||
return None
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ from app.modules.messaging.exceptions import (
|
||||
ConversationClosedException,
|
||||
ConversationNotFoundException,
|
||||
)
|
||||
from app.modules.messaging.models.message import ConversationType, ParticipantType
|
||||
from app.modules.messaging.models.message import ConversationType, ParticipantType # API-007
|
||||
from app.modules.messaging.schemas import (
|
||||
ConversationDetailResponse,
|
||||
ConversationListResponse,
|
||||
@@ -130,7 +130,7 @@ def list_conversations(
|
||||
last_message_at=conv.last_message_at,
|
||||
message_count=conv.message_count,
|
||||
unread_count=unread,
|
||||
other_participant_name=_get_other_participant_name(conv, customer.id),
|
||||
other_participant_name=_get_other_participant_name(db, conv, customer.id),
|
||||
)
|
||||
)
|
||||
|
||||
@@ -221,7 +221,7 @@ def get_conversation(
|
||||
content=msg.content,
|
||||
sender_type=msg.sender_type.value,
|
||||
sender_id=msg.sender_id,
|
||||
sender_name=_get_sender_name(msg),
|
||||
sender_name=_get_sender_name(db, msg),
|
||||
is_system_message=msg.is_system_message,
|
||||
attachments=[
|
||||
{
|
||||
@@ -250,7 +250,7 @@ def get_conversation(
|
||||
last_message_at=conversation.last_message_at,
|
||||
message_count=conversation.message_count,
|
||||
messages=messages,
|
||||
other_participant_name=_get_other_participant_name(conversation, customer.id),
|
||||
other_participant_name=_get_other_participant_name(db, conversation, customer.id),
|
||||
)
|
||||
|
||||
|
||||
@@ -333,7 +333,7 @@ async def send_message(
|
||||
content=message.content,
|
||||
sender_type=message.sender_type.value,
|
||||
sender_id=message.sender_id,
|
||||
sender_name=_get_sender_name(message),
|
||||
sender_name=_get_sender_name(db, message),
|
||||
is_system_message=message.is_system_message,
|
||||
attachments=[
|
||||
{
|
||||
@@ -482,47 +482,11 @@ async def get_attachment_thumbnail(
|
||||
# ============================================================================
|
||||
|
||||
|
||||
def _get_other_participant_name(conversation, customer_id: int) -> str:
|
||||
def _get_other_participant_name(db: Session, conversation, customer_id: int) -> str:
|
||||
"""Get the name of the other participant (the store user)."""
|
||||
for participant in conversation.participants:
|
||||
if participant.participant_type == ParticipantType.STORE:
|
||||
from app.modules.tenancy.models import User
|
||||
|
||||
user = (
|
||||
User.query.filter_by(id=participant.participant_id).first()
|
||||
if hasattr(User, "query")
|
||||
else None
|
||||
)
|
||||
if user:
|
||||
return f"{user.first_name} {user.last_name}"
|
||||
return "Shop Support"
|
||||
return "Shop Support"
|
||||
return messaging_service.get_other_participant_name(db, conversation, customer_id)
|
||||
|
||||
|
||||
def _get_sender_name(message) -> str:
|
||||
def _get_sender_name(db: Session, message) -> str:
|
||||
"""Get sender name for a message."""
|
||||
if message.sender_type == ParticipantType.CUSTOMER:
|
||||
from app.modules.customers.models import Customer
|
||||
|
||||
customer = (
|
||||
Customer.query.filter_by(id=message.sender_id).first()
|
||||
if hasattr(Customer, "query")
|
||||
else None
|
||||
)
|
||||
if customer:
|
||||
return f"{customer.first_name} {customer.last_name}"
|
||||
return "Customer"
|
||||
if message.sender_type == ParticipantType.STORE:
|
||||
from app.modules.tenancy.models import User
|
||||
|
||||
user = (
|
||||
User.query.filter_by(id=message.sender_id).first()
|
||||
if hasattr(User, "query")
|
||||
else None
|
||||
)
|
||||
if user:
|
||||
return f"{user.first_name} {user.last_name}"
|
||||
return "Shop Support"
|
||||
if message.sender_type == ParticipantType.ADMIN:
|
||||
return "Platform Support"
|
||||
return "Unknown"
|
||||
return messaging_service.get_sender_name(db, message)
|
||||
|
||||
@@ -530,6 +530,64 @@ class MessagingService:
|
||||
return p
|
||||
return None
|
||||
|
||||
# =========================================================================
|
||||
# DISPLAY NAME RESOLUTION
|
||||
# =========================================================================
|
||||
|
||||
def get_other_participant_name(
|
||||
self,
|
||||
db: Session,
|
||||
conversation: Conversation,
|
||||
customer_id: int,
|
||||
) -> str:
|
||||
"""
|
||||
Get the display name of the other participant (the store user) in a conversation.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
conversation: Conversation with participants loaded
|
||||
customer_id: ID of the current customer
|
||||
|
||||
Returns:
|
||||
Display name string, or "Shop Support" as fallback
|
||||
"""
|
||||
for participant in conversation.participants:
|
||||
if participant.participant_type == ParticipantType.STORE:
|
||||
user = db.query(User).filter(User.id == participant.participant_id).first()
|
||||
if user:
|
||||
return f"{user.first_name} {user.last_name}"
|
||||
return "Shop Support"
|
||||
return "Shop Support"
|
||||
|
||||
def get_sender_name(
|
||||
self,
|
||||
db: Session,
|
||||
message: Message,
|
||||
) -> str:
|
||||
"""
|
||||
Get the display name for a message sender.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
message: Message object with sender_type and sender_id
|
||||
|
||||
Returns:
|
||||
Display name string
|
||||
"""
|
||||
if message.sender_type == ParticipantType.CUSTOMER:
|
||||
customer = db.query(Customer).filter(Customer.id == message.sender_id).first()
|
||||
if customer:
|
||||
return f"{customer.first_name} {customer.last_name}"
|
||||
return "Customer"
|
||||
if message.sender_type == ParticipantType.STORE:
|
||||
user = db.query(User).filter(User.id == message.sender_id).first()
|
||||
if user:
|
||||
return f"{user.first_name} {user.last_name}"
|
||||
return "Shop Support"
|
||||
if message.sender_type == ParticipantType.ADMIN:
|
||||
return "Platform Support"
|
||||
return "Unknown"
|
||||
|
||||
# =========================================================================
|
||||
# NOTIFICATION PREFERENCES
|
||||
# =========================================================================
|
||||
|
||||
@@ -2,11 +2,10 @@
|
||||
"""
|
||||
Monitoring module database models.
|
||||
|
||||
Re-exports monitoring-related models from their source locations.
|
||||
Provides monitoring-related models including capacity snapshots.
|
||||
"""
|
||||
|
||||
# CapacitySnapshot is in billing module (tracks system capacity over time)
|
||||
from app.modules.billing.models import CapacitySnapshot
|
||||
from app.modules.monitoring.models.capacity_snapshot import CapacitySnapshot
|
||||
|
||||
# Admin notification and logging models
|
||||
from app.modules.messaging.models import AdminNotification
|
||||
|
||||
71
app/modules/monitoring/models/capacity_snapshot.py
Normal file
71
app/modules/monitoring/models/capacity_snapshot.py
Normal file
@@ -0,0 +1,71 @@
|
||||
# app/modules/monitoring/models/capacity_snapshot.py
|
||||
"""
|
||||
Capacity snapshot model for platform capacity monitoring.
|
||||
|
||||
Stores daily snapshots of platform metrics for growth trending and capacity forecasting.
|
||||
"""
|
||||
|
||||
from sqlalchemy import (
|
||||
Column,
|
||||
DateTime,
|
||||
Index,
|
||||
Integer,
|
||||
Numeric,
|
||||
)
|
||||
from sqlalchemy.dialects.sqlite import JSON
|
||||
|
||||
from app.core.database import Base
|
||||
from models.database.base import TimestampMixin
|
||||
|
||||
|
||||
class CapacitySnapshot(Base, TimestampMixin):
|
||||
"""
|
||||
Daily snapshot of platform capacity metrics.
|
||||
|
||||
Used for growth trending and capacity forecasting.
|
||||
Captured daily by background job.
|
||||
"""
|
||||
|
||||
__tablename__ = "capacity_snapshots"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
snapshot_date = Column(DateTime(timezone=True), nullable=False, unique=True, index=True)
|
||||
|
||||
# Store metrics
|
||||
total_stores = Column(Integer, default=0, nullable=False)
|
||||
active_stores = Column(Integer, default=0, nullable=False)
|
||||
trial_stores = Column(Integer, default=0, nullable=False)
|
||||
|
||||
# Subscription metrics
|
||||
total_subscriptions = Column(Integer, default=0, nullable=False)
|
||||
active_subscriptions = Column(Integer, default=0, nullable=False)
|
||||
|
||||
# Resource metrics
|
||||
total_products = Column(Integer, default=0, nullable=False)
|
||||
total_orders_month = Column(Integer, default=0, nullable=False)
|
||||
total_team_members = Column(Integer, default=0, nullable=False)
|
||||
|
||||
# Storage metrics
|
||||
storage_used_gb = Column(Numeric(10, 2), default=0, nullable=False)
|
||||
db_size_mb = Column(Numeric(10, 2), default=0, nullable=False)
|
||||
|
||||
# Capacity metrics (theoretical limits from subscriptions)
|
||||
theoretical_products_limit = Column(Integer, nullable=True)
|
||||
theoretical_orders_limit = Column(Integer, nullable=True)
|
||||
theoretical_team_limit = Column(Integer, nullable=True)
|
||||
|
||||
# Tier distribution (JSON: {"essential": 10, "professional": 5, ...})
|
||||
tier_distribution = Column(JSON, nullable=True)
|
||||
|
||||
# Performance metrics
|
||||
avg_response_ms = Column(Integer, nullable=True)
|
||||
peak_cpu_percent = Column(Numeric(5, 2), nullable=True)
|
||||
peak_memory_percent = Column(Numeric(5, 2), nullable=True)
|
||||
|
||||
# Indexes
|
||||
__table_args__ = (
|
||||
Index("ix_capacity_snapshots_date", "snapshot_date"),
|
||||
)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<CapacitySnapshot(date={self.snapshot_date}, stores={self.total_stores})>"
|
||||
@@ -172,7 +172,7 @@ async def get_growth_trends(
|
||||
|
||||
Returns growth rates and projections for key metrics.
|
||||
"""
|
||||
from app.modules.billing.services.capacity_forecast_service import (
|
||||
from app.modules.monitoring.services.capacity_forecast_service import (
|
||||
capacity_forecast_service,
|
||||
)
|
||||
|
||||
@@ -189,7 +189,7 @@ async def get_scaling_recommendations(
|
||||
|
||||
Returns prioritized list of recommendations.
|
||||
"""
|
||||
from app.modules.billing.services.capacity_forecast_service import (
|
||||
from app.modules.monitoring.services.capacity_forecast_service import (
|
||||
capacity_forecast_service,
|
||||
)
|
||||
|
||||
@@ -206,7 +206,7 @@ async def capture_snapshot(
|
||||
|
||||
Normally run automatically by daily background job.
|
||||
"""
|
||||
from app.modules.billing.services.capacity_forecast_service import (
|
||||
from app.modules.monitoring.services.capacity_forecast_service import (
|
||||
capacity_forecast_service,
|
||||
)
|
||||
|
||||
|
||||
@@ -9,6 +9,10 @@ from app.modules.monitoring.services.admin_audit_service import (
|
||||
AdminAuditService,
|
||||
admin_audit_service,
|
||||
)
|
||||
from app.modules.monitoring.services.capacity_forecast_service import (
|
||||
CapacityForecastService,
|
||||
capacity_forecast_service,
|
||||
)
|
||||
from app.modules.monitoring.services.background_tasks_service import (
|
||||
BackgroundTasksService,
|
||||
background_tasks_service,
|
||||
@@ -25,6 +29,8 @@ from app.modules.monitoring.services.platform_health_service import (
|
||||
__all__ = [
|
||||
"admin_audit_service",
|
||||
"AdminAuditService",
|
||||
"capacity_forecast_service",
|
||||
"CapacityForecastService",
|
||||
"background_tasks_service",
|
||||
"BackgroundTasksService",
|
||||
"log_service",
|
||||
|
||||
@@ -45,7 +45,7 @@ class BackgroundTasksService:
|
||||
def get_running_test_runs(self, db: Session) -> list[TestRun]:
|
||||
"""Get currently running test runs"""
|
||||
# SVC-005 - Platform-level, TestRuns not store-scoped
|
||||
return db.query(TestRun).filter(TestRun.status == "running").all()
|
||||
return db.query(TestRun).filter(TestRun.status == "running").all() # SVC-005
|
||||
|
||||
def get_import_stats(self, db: Session) -> dict:
|
||||
"""Get import job statistics"""
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# app/modules/billing/services/capacity_forecast_service.py
|
||||
# app/modules/monitoring/services/capacity_forecast_service.py
|
||||
"""
|
||||
Capacity forecasting service for growth trends and scaling recommendations.
|
||||
|
||||
@@ -16,13 +16,9 @@ from decimal import Decimal
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.modules.billing.models import (
|
||||
CapacitySnapshot,
|
||||
MerchantSubscription,
|
||||
SubscriptionStatus,
|
||||
)
|
||||
from app.modules.contracts.metrics import MetricsContext
|
||||
from app.modules.core.services.stats_aggregator import stats_aggregator
|
||||
from app.modules.monitoring.models.capacity_snapshot import CapacitySnapshot
|
||||
from app.modules.tenancy.models import Platform, Store, StoreUser
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -75,22 +71,7 @@ class CapacityForecastService:
|
||||
or 0
|
||||
)
|
||||
|
||||
# Subscription metrics
|
||||
total_subs = db.query(func.count(MerchantSubscription.id)).scalar() or 0
|
||||
active_subs = (
|
||||
db.query(func.count(MerchantSubscription.id))
|
||||
.filter(MerchantSubscription.status.in_(["active", "trial"]))
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
trial_stores = (
|
||||
db.query(func.count(MerchantSubscription.id))
|
||||
.filter(MerchantSubscription.status == SubscriptionStatus.TRIAL.value)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
|
||||
# Resource metrics via provider pattern (avoids direct catalog/orders imports)
|
||||
# Resource metrics via provider pattern (avoids cross-module imports)
|
||||
start_of_month = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
|
||||
platform = db.query(Platform).first()
|
||||
platform_id = platform.id if platform else 1
|
||||
@@ -100,6 +81,11 @@ class CapacityForecastService:
|
||||
context=MetricsContext(date_from=start_of_month),
|
||||
)
|
||||
|
||||
# Subscription metrics via stats aggregator (avoids billing → monitoring violation)
|
||||
total_subs = stats.get("billing.total_subscriptions", 0)
|
||||
active_subs = stats.get("billing.active_subscriptions", 0)
|
||||
trial_stores = stats.get("billing.trial_subscriptions", 0)
|
||||
|
||||
total_products = stats.get("catalog.total_products", 0)
|
||||
total_team = (
|
||||
db.query(func.count(StoreUser.id))
|
||||
@@ -27,7 +27,7 @@ def capture_capacity_snapshot(self):
|
||||
Returns:
|
||||
dict: Snapshot summary with store and product counts.
|
||||
"""
|
||||
from app.modules.billing.services.capacity_forecast_service import (
|
||||
from app.modules.monitoring.services.capacity_forecast_service import (
|
||||
capacity_forecast_service,
|
||||
)
|
||||
|
||||
|
||||
0
app/modules/monitoring/tests/__init__.py
Normal file
0
app/modules/monitoring/tests/__init__.py
Normal file
0
app/modules/monitoring/tests/unit/__init__.py
Normal file
0
app/modules/monitoring/tests/unit/__init__.py
Normal file
@@ -1,4 +1,4 @@
|
||||
# tests/unit/services/test_capacity_forecast_service.py
|
||||
# app/modules/monitoring/tests/unit/test_capacity_forecast_service.py
|
||||
"""
|
||||
Unit tests for CapacityForecastService.
|
||||
|
||||
@@ -14,8 +14,8 @@ from decimal import Decimal
|
||||
|
||||
import pytest
|
||||
|
||||
from app.modules.billing.models import CapacitySnapshot
|
||||
from app.modules.billing.services.capacity_forecast_service import (
|
||||
from app.modules.monitoring.models import CapacitySnapshot
|
||||
from app.modules.monitoring.services.capacity_forecast_service import (
|
||||
INFRASTRUCTURE_SCALING,
|
||||
CapacityForecastService,
|
||||
capacity_forecast_service,
|
||||
@@ -31,7 +31,7 @@ from app.modules.catalog.models import Product
|
||||
from app.modules.customers.exceptions import CustomerNotFoundException
|
||||
from app.modules.customers.models.customer import Customer
|
||||
from app.modules.inventory.exceptions import InsufficientInventoryException
|
||||
from app.modules.marketplace.models import (
|
||||
from app.modules.marketplace.models import ( # IMPORT-002
|
||||
MarketplaceProduct,
|
||||
MarketplaceProductTranslation,
|
||||
)
|
||||
|
||||
@@ -106,22 +106,13 @@ def get_store_statistics_endpoint(
|
||||
current_admin: UserContext = Depends(get_current_admin_api),
|
||||
):
|
||||
"""Get store statistics for admin dashboard (Admin only)."""
|
||||
from app.modules.tenancy.models import Store
|
||||
|
||||
# Query store statistics directly to avoid analytics module dependency
|
||||
total = db.query(Store).count()
|
||||
verified = db.query(Store).filter(Store.is_verified == True).count()
|
||||
active = db.query(Store).filter(Store.is_active == True).count()
|
||||
inactive = total - active
|
||||
pending = db.query(Store).filter(
|
||||
Store.is_active == True, Store.is_verified == False
|
||||
).count()
|
||||
stats = admin_service.get_store_statistics(db)
|
||||
|
||||
return StoreStatsResponse(
|
||||
total=total,
|
||||
verified=verified,
|
||||
pending=pending,
|
||||
inactive=inactive,
|
||||
total=stats["total"],
|
||||
verified=stats["verified"],
|
||||
pending=stats["pending"],
|
||||
inactive=stats["inactive"],
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -718,6 +718,34 @@ class AdminService:
|
||||
# STATISTICS
|
||||
# ============================================================================
|
||||
|
||||
def get_store_statistics(self, db: Session) -> dict:
|
||||
"""
|
||||
Get store statistics for admin dashboard.
|
||||
|
||||
Returns:
|
||||
Dict with total, verified, pending, and inactive counts.
|
||||
"""
|
||||
try:
|
||||
total = db.query(Store).count()
|
||||
verified = db.query(Store).filter(Store.is_verified == True).count() # noqa: E712
|
||||
active = db.query(Store).filter(Store.is_active == True).count() # noqa: E712
|
||||
inactive = total - active
|
||||
pending = db.query(Store).filter(
|
||||
Store.is_active == True, Store.is_verified == False # noqa: E712
|
||||
).count()
|
||||
|
||||
return {
|
||||
"total": total,
|
||||
"verified": verified,
|
||||
"pending": pending,
|
||||
"inactive": inactive,
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get store statistics: {str(e)}")
|
||||
raise AdminOperationException(
|
||||
operation="get_store_statistics", reason="Database query failed"
|
||||
)
|
||||
|
||||
def get_recent_stores(self, db: Session, limit: int = 5) -> list[dict]:
|
||||
"""Get recently created stores."""
|
||||
try:
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
{# app/templates/admin/merchant-detail.html #}
|
||||
{# noqa: fe-004 - Alpine.js x-model bindings incompatible with standard form macros #}
|
||||
{# noqa: fe-008 - Alpine.js x-model bindings incompatible with standard form macros #}
|
||||
{% extends "admin/base.html" %}
|
||||
{% from 'shared/macros/alerts.html' import loading_state, error_state %}
|
||||
{% from 'shared/macros/headers.html' import detail_page_header %}
|
||||
|
||||
@@ -14,7 +14,7 @@ Migration guide:
|
||||
- RequestContext.API -> Check with FrontendDetector.is_api_request()
|
||||
- RequestContext.ADMIN -> FrontendType.ADMIN
|
||||
- RequestContext.STORE_DASHBOARD -> FrontendType.STORE
|
||||
- RequestContext.SHOP -> FrontendType.STOREFRONT
|
||||
- RequestContext.STOREFRONT -> FrontendType.STOREFRONT
|
||||
- RequestContext.FALLBACK -> FrontendType.PLATFORM (or handle API separately)
|
||||
|
||||
- get_request_context(request) -> get_frontend_type(request)
|
||||
@@ -44,14 +44,14 @@ class RequestContext(str, Enum):
|
||||
- API -> Use FrontendDetector.is_api_request() + FrontendType
|
||||
- ADMIN -> FrontendType.ADMIN
|
||||
- STORE_DASHBOARD -> FrontendType.STORE
|
||||
- SHOP -> FrontendType.STOREFRONT
|
||||
- STOREFRONT -> FrontendType.STOREFRONT
|
||||
- FALLBACK -> FrontendType.PLATFORM
|
||||
"""
|
||||
|
||||
API = "api"
|
||||
ADMIN = "admin"
|
||||
STORE_DASHBOARD = "store"
|
||||
SHOP = "shop"
|
||||
STOREFRONT = "storefront"
|
||||
FALLBACK = "fallback"
|
||||
|
||||
|
||||
@@ -82,7 +82,7 @@ def get_request_context(request: Request) -> RequestContext:
|
||||
mapping = {
|
||||
FrontendType.ADMIN: RequestContext.ADMIN,
|
||||
FrontendType.STORE: RequestContext.STORE_DASHBOARD,
|
||||
FrontendType.STOREFRONT: RequestContext.SHOP,
|
||||
FrontendType.STOREFRONT: RequestContext.STOREFRONT,
|
||||
FrontendType.PLATFORM: RequestContext.FALLBACK,
|
||||
}
|
||||
|
||||
|
||||
@@ -239,31 +239,26 @@ class StoreContextManager:
|
||||
"""Check if request is for API endpoints."""
|
||||
return FrontendDetector.is_api_request(request.url.path)
|
||||
|
||||
@staticmethod
|
||||
def is_shop_api_request(request: Request) -> bool:
|
||||
"""Check if request is for shop API endpoints."""
|
||||
return request.url.path.startswith("/api/v1/shop/")
|
||||
|
||||
@staticmethod
|
||||
def extract_store_from_referer(request: Request) -> dict | None:
|
||||
"""
|
||||
Extract store context from Referer header.
|
||||
|
||||
Used for shop API requests where store context comes from the page
|
||||
that made the API call (e.g., JavaScript on /stores/wizamart/shop/products
|
||||
calling /api/v1/shop/products).
|
||||
Used for storefront API requests where store context comes from the page
|
||||
that made the API call (e.g., JavaScript on /stores/wizamart/storefront/products
|
||||
calling /api/v1/storefront/products).
|
||||
|
||||
Extracts store from Referer URL patterns:
|
||||
- http://localhost:8000/stores/wizamart/shop/... → wizamart
|
||||
- http://wizamart.platform.com/shop/... → wizamart (subdomain) # noqa
|
||||
- http://custom-domain.com/shop/... → custom-domain.com # noqa
|
||||
- http://localhost:8000/stores/wizamart/storefront/... → wizamart
|
||||
- http://wizamart.platform.com/storefront/... → wizamart (subdomain) # noqa
|
||||
- http://custom-domain.com/storefront/... → custom-domain.com # noqa
|
||||
|
||||
Returns store context dict or None if unable to extract.
|
||||
"""
|
||||
referer = request.headers.get("referer") or request.headers.get("origin")
|
||||
|
||||
if not referer:
|
||||
logger.debug("[STORE] No Referer/Origin header for shop API request")
|
||||
logger.debug("[STORE] No Referer/Origin header for storefront API request")
|
||||
return None
|
||||
|
||||
try:
|
||||
@@ -287,7 +282,7 @@ class StoreContextManager:
|
||||
)
|
||||
|
||||
# Method 1: Path-based detection from referer path
|
||||
# /stores/wizamart/shop/products → wizamart
|
||||
# /stores/wizamart/storefront/products → wizamart
|
||||
if referer_path.startswith(("/stores/", "/store/")):
|
||||
prefix = (
|
||||
"/stores/" if referer_path.startswith("/stores/") else "/store/"
|
||||
@@ -448,75 +443,10 @@ class StoreContextMiddleware(BaseHTTPMiddleware):
|
||||
request.state.clean_path = request.url.path
|
||||
return await call_next(request)
|
||||
|
||||
# Handle shop API routes specially - extract store from Referer header
|
||||
if StoreContextManager.is_shop_api_request(request):
|
||||
logger.debug(
|
||||
f"[STORE] Shop API request detected: {request.url.path}",
|
||||
extra={
|
||||
"path": request.url.path,
|
||||
"referer": request.headers.get("referer", ""),
|
||||
},
|
||||
)
|
||||
|
||||
store_context = StoreContextManager.extract_store_from_referer(request)
|
||||
|
||||
if store_context:
|
||||
db_gen = get_db()
|
||||
db = next(db_gen)
|
||||
try:
|
||||
store = StoreContextManager.get_store_from_context(
|
||||
db, store_context
|
||||
)
|
||||
|
||||
if store:
|
||||
request.state.store = store
|
||||
request.state.store_context = store_context
|
||||
request.state.clean_path = request.url.path
|
||||
|
||||
logger.debug(
|
||||
"[STORE_CONTEXT] Store detected from Referer for shop API",
|
||||
extra={
|
||||
"store_id": store.id,
|
||||
"store_name": store.name,
|
||||
"store_subdomain": store.subdomain,
|
||||
"detection_method": store_context.get(
|
||||
"detection_method"
|
||||
),
|
||||
"api_path": request.url.path,
|
||||
"referer": store_context.get("referer", ""),
|
||||
},
|
||||
)
|
||||
else:
|
||||
logger.warning(
|
||||
"[WARNING] Store context from Referer but store not found",
|
||||
extra={
|
||||
"context": store_context,
|
||||
"detection_method": store_context.get(
|
||||
"detection_method"
|
||||
),
|
||||
"api_path": request.url.path,
|
||||
},
|
||||
)
|
||||
request.state.store = None
|
||||
request.state.store_context = store_context
|
||||
request.state.clean_path = request.url.path
|
||||
finally:
|
||||
db.close()
|
||||
else:
|
||||
logger.warning(
|
||||
"[STORE] Shop API request without Referer header",
|
||||
extra={"path": request.url.path},
|
||||
)
|
||||
request.state.store = None
|
||||
request.state.store_context = None
|
||||
request.state.clean_path = request.url.path
|
||||
|
||||
return await call_next(request)
|
||||
|
||||
# Skip store detection for other API routes (admin API, store API have store_id in URL)
|
||||
# Skip store detection for API routes (admin API, store API have store_id in URL)
|
||||
if StoreContextManager.is_api_request(request):
|
||||
logger.debug(
|
||||
f"[STORE] Skipping store detection for non-shop API: {request.url.path}",
|
||||
f"[STORE] Skipping store detection for non-storefront API: {request.url.path}",
|
||||
extra={"path": request.url.path, "reason": "api"},
|
||||
)
|
||||
request.state.store = None
|
||||
|
||||
@@ -1949,6 +1949,9 @@ class ArchitectureValidator:
|
||||
return
|
||||
if "_auth.py" in file_path.name:
|
||||
return
|
||||
# Skip webhook files - they receive external callbacks
|
||||
if file_path.name == "webhooks.py":
|
||||
return
|
||||
|
||||
# This is a warning-level check
|
||||
# Look for endpoints without proper authentication
|
||||
@@ -1971,6 +1974,14 @@ class ArchitectureValidator:
|
||||
if "@router." in line and (
|
||||
"post" in line or "put" in line or "delete" in line
|
||||
):
|
||||
# Check if decorator line itself has a public/authenticated marker
|
||||
if (
|
||||
"# public" in line.lower()
|
||||
or "# authenticated" in line.lower()
|
||||
or "# noqa: api-004" in line.lower()
|
||||
):
|
||||
continue
|
||||
|
||||
# Check previous line and next 15 lines for auth or public marker
|
||||
# (increased from 5 to handle multi-line decorators and long function signatures)
|
||||
has_auth = False
|
||||
@@ -1989,6 +2000,7 @@ class ArchitectureValidator:
|
||||
# Check for public endpoint markers
|
||||
if (
|
||||
"# public" in ctx_line.lower()
|
||||
or "# authenticated" in ctx_line.lower()
|
||||
or "# noqa: api-004" in ctx_line.lower()
|
||||
):
|
||||
is_public = True
|
||||
@@ -2007,7 +2019,7 @@ class ArchitectureValidator:
|
||||
suggestion = (
|
||||
"Add Depends(get_current_admin_api), or mark as '# public'"
|
||||
)
|
||||
elif "/shop/" in file_path_str:
|
||||
elif "/storefront/" in file_path_str:
|
||||
suggestion = "Add Depends(get_current_customer_api), or mark as '# public'"
|
||||
else:
|
||||
suggestion = "Add authentication dependency or mark as '# public' if intentionally unauthenticated"
|
||||
@@ -2024,11 +2036,11 @@ class ArchitectureValidator:
|
||||
)
|
||||
|
||||
def _check_store_scoping(self, file_path: Path, content: str, lines: list[str]):
|
||||
"""API-005: Check that store/shop endpoints scope queries to store_id"""
|
||||
"""API-005: Check that store/storefront endpoints scope queries to store_id"""
|
||||
file_path_str = str(file_path)
|
||||
|
||||
# Only check store and shop API files
|
||||
if "/store/" not in file_path_str and "/shop/" not in file_path_str:
|
||||
# Only check store and storefront API files
|
||||
if "/store/" not in file_path_str and "/storefront/" not in file_path_str:
|
||||
return
|
||||
|
||||
# Skip auth files
|
||||
@@ -2059,7 +2071,7 @@ class ArchitectureValidator:
|
||||
severity=Severity.WARNING,
|
||||
file_path=file_path,
|
||||
line_number=i,
|
||||
message="Query in store/shop endpoint may not be scoped to store_id",
|
||||
message="Query in store/storefront endpoint may not be scoped to store_id",
|
||||
context=line.strip()[:60],
|
||||
suggestion="Add .filter(Model.store_id == store_id) to ensure tenant isolation",
|
||||
)
|
||||
@@ -2123,6 +2135,7 @@ class ArchitectureValidator:
|
||||
print("🔧 Validating service layer...")
|
||||
|
||||
service_files = list(target_path.glob("app/services/**/*.py"))
|
||||
service_files += list(target_path.glob("app/modules/*/services/**/*.py"))
|
||||
self.result.files_checked += len(service_files)
|
||||
|
||||
for file_path in service_files:
|
||||
@@ -2156,7 +2169,7 @@ class ArchitectureValidator:
|
||||
if "admin" in file_path_str.lower():
|
||||
return
|
||||
|
||||
if "noqa: svc-005" in content.lower():
|
||||
if "svc-005" in content.lower():
|
||||
return
|
||||
|
||||
# Look for patterns that suggest unscoped queries
|
||||
@@ -2286,9 +2299,11 @@ class ArchitectureValidator:
|
||||
if not rule:
|
||||
return
|
||||
|
||||
# Exception: log_service.py is allowed to commit (audit logs)
|
||||
if "log_service.py" in str(file_path):
|
||||
return
|
||||
# Check exceptions from YAML config
|
||||
exceptions = rule.get("pattern", {}).get("exceptions", [])
|
||||
for exc in exceptions:
|
||||
if exc in str(file_path):
|
||||
return
|
||||
|
||||
# Check for file-level noqa comment
|
||||
if "svc-006" in content.lower():
|
||||
@@ -2322,6 +2337,7 @@ class ArchitectureValidator:
|
||||
|
||||
# Validate database models
|
||||
db_model_files = list(target_path.glob("models/database/**/*.py"))
|
||||
db_model_files += list(target_path.glob("app/modules/*/models/**/*.py"))
|
||||
self.result.files_checked += len(db_model_files)
|
||||
|
||||
for file_path in db_model_files:
|
||||
@@ -2339,6 +2355,7 @@ class ArchitectureValidator:
|
||||
|
||||
# Validate schema models
|
||||
schema_model_files = list(target_path.glob("models/schema/**/*.py"))
|
||||
schema_model_files += list(target_path.glob("app/modules/*/schemas/**/*.py"))
|
||||
self.result.files_checked += len(schema_model_files)
|
||||
|
||||
for file_path in schema_model_files:
|
||||
@@ -2576,6 +2593,7 @@ class ArchitectureValidator:
|
||||
|
||||
# EXC-004: Check exception inheritance in exceptions module
|
||||
exception_files = list(target_path.glob("app/exceptions/**/*.py"))
|
||||
exception_files += list(target_path.glob("app/modules/*/exceptions.py"))
|
||||
for file_path in exception_files:
|
||||
if "__init__" in file_path.name or "handler" in file_path.name:
|
||||
continue
|
||||
@@ -2619,6 +2637,10 @@ class ArchitectureValidator:
|
||||
"AuthenticationException",
|
||||
"AuthorizationException",
|
||||
"ConflictException",
|
||||
"BusinessLogicException",
|
||||
"MarketplaceException",
|
||||
"LetzshopClientError",
|
||||
"LoyaltyException",
|
||||
"Exception", # Allow base Exception for some cases
|
||||
]
|
||||
|
||||
@@ -2640,24 +2662,37 @@ class ArchitectureValidator:
|
||||
print("📛 Validating naming conventions...")
|
||||
|
||||
# NAM-001: API files use PLURAL names
|
||||
nam001_rule = self._get_rule("NAM-001")
|
||||
nam001_exceptions = (
|
||||
nam001_rule.get("pattern", {}).get("exceptions", []) if nam001_rule else []
|
||||
)
|
||||
api_files = list(target_path.glob("app/api/v1/**/*.py"))
|
||||
api_files += list(target_path.glob("app/modules/*/routes/api/**/*.py"))
|
||||
for file_path in api_files:
|
||||
if file_path.name in ["__init__.py", "auth.py", "health.py"]:
|
||||
if file_path.name in nam001_exceptions:
|
||||
continue
|
||||
if "_auth.py" in file_path.name:
|
||||
continue
|
||||
self._check_api_file_naming(file_path)
|
||||
|
||||
# NAM-002: Service files use SINGULAR + 'service' suffix
|
||||
nam002_rule = self._get_rule("NAM-002")
|
||||
nam002_exceptions = (
|
||||
nam002_rule.get("pattern", {}).get("exceptions", []) if nam002_rule else []
|
||||
)
|
||||
service_files = list(target_path.glob("app/services/**/*.py"))
|
||||
service_files += list(target_path.glob("app/modules/*/services/**/*.py"))
|
||||
for file_path in service_files:
|
||||
if file_path.name == "__init__.py":
|
||||
continue
|
||||
# Check glob-style exceptions (e.g. *_features.py)
|
||||
if any(file_path.match(exc) for exc in nam002_exceptions):
|
||||
continue
|
||||
self._check_service_file_naming(file_path)
|
||||
|
||||
# NAM-003: Model files use SINGULAR names
|
||||
model_files = list(target_path.glob("models/**/*.py"))
|
||||
model_files += list(target_path.glob("app/modules/*/models/**/*.py"))
|
||||
for file_path in model_files:
|
||||
if file_path.name == "__init__.py":
|
||||
continue
|
||||
@@ -2845,18 +2880,23 @@ class ArchitectureValidator:
|
||||
|
||||
# AUTH-004: Check store context patterns
|
||||
store_api_files = list(target_path.glob("app/api/v1/store/**/*.py"))
|
||||
store_api_files += list(target_path.glob("app/modules/*/routes/api/store*.py"))
|
||||
for file_path in store_api_files:
|
||||
if file_path.name == "__init__.py" or file_path.name == "auth.py":
|
||||
continue
|
||||
# storefront*.py files are handled separately - they SHOULD use require_store_context
|
||||
if file_path.name.startswith("storefront"):
|
||||
continue
|
||||
content = file_path.read_text()
|
||||
self._check_store_context_pattern(file_path, content)
|
||||
|
||||
shop_api_files = list(target_path.glob("app/api/v1/shop/**/*.py"))
|
||||
for file_path in shop_api_files:
|
||||
storefront_api_files = list(target_path.glob("app/api/v1/storefront/**/*.py"))
|
||||
storefront_api_files += list(target_path.glob("app/modules/*/routes/api/storefront*.py"))
|
||||
for file_path in storefront_api_files:
|
||||
if file_path.name == "__init__.py" or file_path.name == "auth.py":
|
||||
continue
|
||||
content = file_path.read_text()
|
||||
self._check_shop_context_pattern(file_path, content)
|
||||
self._check_storefront_context_pattern(file_path, content)
|
||||
|
||||
def _check_store_context_pattern(self, file_path: Path, content: str):
|
||||
"""AUTH-004: Check that store API endpoints use token-based store context"""
|
||||
@@ -2880,12 +2920,12 @@ class ArchitectureValidator:
|
||||
)
|
||||
return
|
||||
|
||||
def _check_shop_context_pattern(self, file_path: Path, content: str):
|
||||
"""AUTH-004: Check that shop API endpoints use proper store context"""
|
||||
def _check_storefront_context_pattern(self, file_path: Path, content: str):
|
||||
"""AUTH-004: Check that storefront API endpoints use proper store context"""
|
||||
if "noqa: auth-004" in content.lower():
|
||||
return
|
||||
|
||||
# Shop APIs that need store context should use require_store_context,
|
||||
# Storefront APIs that need store context should use require_store_context,
|
||||
# # public, or # authenticated (customer auth includes store context)
|
||||
has_store_context = (
|
||||
"require_store_context" in content
|
||||
@@ -2908,11 +2948,11 @@ class ArchitectureValidator:
|
||||
):
|
||||
self._add_violation(
|
||||
rule_id="AUTH-004",
|
||||
rule_name="Shop endpoints need store context",
|
||||
rule_name="Storefront endpoints need store context",
|
||||
severity=Severity.INFO,
|
||||
file_path=file_path,
|
||||
line_number=i,
|
||||
message="Shop endpoint may need store context dependency",
|
||||
message="Storefront endpoint may need store context dependency",
|
||||
context=line.strip()[:60],
|
||||
suggestion="Add Depends(require_store_context()) or mark as '# public'",
|
||||
)
|
||||
@@ -3577,6 +3617,7 @@ class ArchitectureValidator:
|
||||
def _check_template_language_inline_patterns(self, target_path: Path):
|
||||
"""LANG-002, LANG-003: Check inline Alpine.js and tojson|safe usage in templates"""
|
||||
template_files = list(target_path.glob("app/templates/**/*.html"))
|
||||
template_files += list(target_path.glob("app/modules/*/templates/**/*.html"))
|
||||
|
||||
for file_path in template_files:
|
||||
if self._should_ignore_file(file_path):
|
||||
@@ -4622,8 +4663,8 @@ class ArchitectureValidator:
|
||||
if imported_module == module_name:
|
||||
continue
|
||||
|
||||
# Skip noqa comments
|
||||
if "noqa:" in line.lower() and "import" in line.lower():
|
||||
# Skip suppression comments (# IMPORT-002 or # noqa: import-002)
|
||||
if "import-002" in line.lower():
|
||||
continue
|
||||
|
||||
# contracts module cannot import from any module
|
||||
@@ -4880,6 +4921,8 @@ class ArchitectureValidator:
|
||||
"service_layer_rules",
|
||||
"model_rules",
|
||||
"exception_rules",
|
||||
"naming_rules",
|
||||
"auth_rules",
|
||||
"javascript_rules",
|
||||
"template_rules",
|
||||
"frontend_component_rules",
|
||||
|
||||
@@ -26,7 +26,7 @@ from main import app
|
||||
from tests.integration.middleware.middleware_test_routes import (
|
||||
admin_router,
|
||||
api_router,
|
||||
shop_router,
|
||||
storefront_router,
|
||||
store_router,
|
||||
)
|
||||
from tests.integration.middleware.middleware_test_routes import (
|
||||
@@ -39,7 +39,7 @@ if not any(r.path.startswith("/middleware-test") for r in app.routes if hasattr(
|
||||
app.include_router(api_router)
|
||||
app.include_router(admin_router)
|
||||
app.include_router(store_router)
|
||||
app.include_router(shop_router)
|
||||
app.include_router(storefront_router)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
||||
@@ -10,7 +10,7 @@ IMPORTANT: Routes are organized by prefix to avoid conflicts:
|
||||
- /api/middleware-test/* - API context testing
|
||||
- /admin/middleware-test/* - Admin context testing
|
||||
- /store/middleware-test/* - Store dashboard context testing
|
||||
- /shop/middleware-test/* - Shop context testing
|
||||
- /storefront/middleware-test/* - Storefront context testing
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter, Request
|
||||
@@ -531,15 +531,15 @@ async def test_store_dashboard_theme(request: Request):
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Shop Context Test Router
|
||||
# Storefront Context Test Router
|
||||
# =============================================================================
|
||||
|
||||
shop_router = APIRouter(prefix="/shop/middleware-test")
|
||||
storefront_router = APIRouter(prefix="/storefront/middleware-test")
|
||||
|
||||
|
||||
@shop_router.get("/context")
|
||||
async def test_shop_context(request: Request):
|
||||
"""Test shop context detection."""
|
||||
@storefront_router.get("/context")
|
||||
async def test_storefront_context(request: Request):
|
||||
"""Test storefront context detection."""
|
||||
context_type = getattr(request.state, "context_type", None)
|
||||
store = getattr(request.state, "store", None)
|
||||
theme = getattr(request.state, "theme", None)
|
||||
@@ -552,9 +552,9 @@ async def test_shop_context(request: Request):
|
||||
}
|
||||
|
||||
|
||||
@shop_router.get("/custom-domain-context")
|
||||
async def test_shop_custom_domain_context(request: Request):
|
||||
"""Test shop context with custom domain."""
|
||||
@storefront_router.get("/custom-domain-context")
|
||||
async def test_storefront_custom_domain_context(request: Request):
|
||||
"""Test storefront context with custom domain."""
|
||||
context_type = getattr(request.state, "context_type", None)
|
||||
store = getattr(request.state, "store", None)
|
||||
return {
|
||||
@@ -564,9 +564,9 @@ async def test_shop_custom_domain_context(request: Request):
|
||||
}
|
||||
|
||||
|
||||
@shop_router.get("/theme")
|
||||
async def test_shop_theme(request: Request):
|
||||
"""Test theme in shop context."""
|
||||
@storefront_router.get("/theme")
|
||||
async def test_storefront_theme(request: Request):
|
||||
"""Test theme in storefront context."""
|
||||
context_type = getattr(request.state, "context_type", None)
|
||||
theme = getattr(request.state, "theme", None)
|
||||
colors = theme.get("colors", {}) if theme else {}
|
||||
|
||||
@@ -7,7 +7,6 @@ Tests cover:
|
||||
- Path-based detection (dev mode)
|
||||
- Subdomain-based detection (prod mode)
|
||||
- Custom domain detection
|
||||
- Legacy /shop/ path support
|
||||
- Priority order of detection methods
|
||||
"""
|
||||
|
||||
@@ -103,15 +102,6 @@ class TestFrontendDetectorStorefront:
|
||||
)
|
||||
assert result == FrontendType.STOREFRONT
|
||||
|
||||
def test_detect_storefront_legacy_shop_path(self):
|
||||
"""Test storefront detection from legacy /shop path."""
|
||||
result = FrontendDetector.detect(host="localhost", path="/shop/products")
|
||||
assert result == FrontendType.STOREFRONT
|
||||
|
||||
def test_detect_storefront_legacy_shop_api_path(self):
|
||||
"""Test storefront detection from legacy /api/v1/shop path."""
|
||||
result = FrontendDetector.detect(host="localhost", path="/api/v1/shop/cart")
|
||||
assert result == FrontendType.STOREFRONT
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
|
||||
@@ -32,7 +32,7 @@ class TestRequestContextEnumBackwardCompatibility:
|
||||
assert RequestContext.API.value == "api"
|
||||
assert RequestContext.ADMIN.value == "admin"
|
||||
assert RequestContext.STORE_DASHBOARD.value == "store"
|
||||
assert RequestContext.SHOP.value == "shop"
|
||||
assert RequestContext.STOREFRONT.value == "storefront"
|
||||
assert RequestContext.FALLBACK.value == "fallback"
|
||||
|
||||
def test_request_context_types(self):
|
||||
@@ -101,7 +101,7 @@ class TestGetRequestContextBackwardCompatibility:
|
||||
assert context == RequestContext.STORE_DASHBOARD
|
||||
|
||||
def test_get_request_context_maps_storefront(self):
|
||||
"""Test get_request_context maps FrontendType.STOREFRONT to RequestContext.SHOP."""
|
||||
"""Test get_request_context maps FrontendType.STOREFRONT to RequestContext.STOREFRONT."""
|
||||
from app.modules.enums import FrontendType
|
||||
|
||||
request = Mock(spec=Request)
|
||||
@@ -113,7 +113,7 @@ class TestGetRequestContextBackwardCompatibility:
|
||||
warnings.simplefilter("ignore", DeprecationWarning)
|
||||
context = get_request_context(request)
|
||||
|
||||
assert context == RequestContext.SHOP
|
||||
assert context == RequestContext.STOREFRONT
|
||||
|
||||
def test_get_request_context_maps_platform_to_fallback(self):
|
||||
"""Test get_request_context maps FrontendType.PLATFORM to RequestContext.FALLBACK."""
|
||||
|
||||
@@ -312,7 +312,10 @@ class TestPlatformContextManager:
|
||||
def test_get_platform_from_domain_not_found(self):
|
||||
"""Test domain lookup when platform not found."""
|
||||
mock_db = Mock(spec=Session)
|
||||
mock_db.query.return_value.filter.return_value.filter.return_value.first.return_value = None
|
||||
# Ensure all query chain variants return None for .first()
|
||||
query_mock = mock_db.query.return_value
|
||||
query_mock.filter.return_value.first.return_value = None
|
||||
query_mock.filter.return_value.filter.return_value.first.return_value = None
|
||||
|
||||
context = {"detection_method": "domain", "domain": "unknown.lu"}
|
||||
|
||||
@@ -367,8 +370,11 @@ class TestPlatformContextManager:
|
||||
def test_get_platform_inactive_not_returned(self):
|
||||
"""Test that inactive platforms are not returned."""
|
||||
mock_db = Mock(spec=Session)
|
||||
# First call returns None (is_active filter excludes it)
|
||||
mock_db.query.return_value.filter.return_value.filter.return_value.first.return_value = None
|
||||
# Ensure all query chain variants return None for .first()
|
||||
# (primary Platform lookup and StoreDomain/MerchantDomain fallbacks)
|
||||
query_mock = mock_db.query.return_value
|
||||
query_mock.filter.return_value.first.return_value = None
|
||||
query_mock.filter.return_value.filter.return_value.first.return_value = None
|
||||
|
||||
context = {"detection_method": "domain", "domain": "inactive.lu"}
|
||||
|
||||
|
||||
@@ -102,10 +102,10 @@ class TestStoreContextManager:
|
||||
"""Test path-based detection with /store/ prefix."""
|
||||
request = Mock(spec=Request)
|
||||
request.headers = {"host": "localhost"}
|
||||
request.url = Mock(path="/store/store1/shop")
|
||||
request.url = Mock(path="/store/store1/storefront")
|
||||
# Set platform_clean_path to simulate PlatformContextMiddleware output
|
||||
request.state = Mock()
|
||||
request.state.platform_clean_path = "/store/store1/shop"
|
||||
request.state.platform_clean_path = "/store/store1/storefront"
|
||||
|
||||
context = StoreContextManager.detect_store_context(request)
|
||||
|
||||
@@ -119,10 +119,10 @@ class TestStoreContextManager:
|
||||
"""Test path-based detection with /stores/ prefix."""
|
||||
request = Mock(spec=Request)
|
||||
request.headers = {"host": "localhost"}
|
||||
request.url = Mock(path="/stores/store1/shop")
|
||||
request.url = Mock(path="/stores/store1/storefront")
|
||||
# Set platform_clean_path to simulate PlatformContextMiddleware output
|
||||
request.state = Mock()
|
||||
request.state.platform_clean_path = "/stores/store1/shop"
|
||||
request.state.platform_clean_path = "/stores/store1/storefront"
|
||||
|
||||
context = StoreContextManager.detect_store_context(request)
|
||||
|
||||
@@ -245,7 +245,13 @@ class TestStoreContextManager:
|
||||
def test_get_store_from_custom_domain_not_found(self):
|
||||
"""Test custom domain not found in database."""
|
||||
mock_db = Mock(spec=Session)
|
||||
mock_db.query.return_value.filter.return_value.filter.return_value.filter.return_value.first.return_value = None
|
||||
# Ensure all query chain variants return None for .first()
|
||||
# (primary StoreDomain lookup and MerchantDomain fallback)
|
||||
query_mock = mock_db.query.return_value
|
||||
query_mock.filter.return_value.first.return_value = None
|
||||
query_mock.filter.return_value.filter.return_value.first.return_value = None
|
||||
query_mock.filter.return_value.filter.return_value.filter.return_value.first.return_value = None
|
||||
query_mock.filter.return_value.order_by.return_value.first.return_value = None
|
||||
|
||||
context = {"detection_method": "custom_domain", "domain": "nonexistent.com"}
|
||||
|
||||
@@ -310,24 +316,24 @@ class TestStoreContextManager:
|
||||
def test_extract_clean_path_from_store_path(self):
|
||||
"""Test extracting clean path from /store/ prefix."""
|
||||
request = Mock(spec=Request)
|
||||
request.url = Mock(path="/store/store1/shop/products")
|
||||
request.url = Mock(path="/store/store1/storefront/products")
|
||||
|
||||
store_context = {"detection_method": "path", "path_prefix": "/store/store1"}
|
||||
|
||||
clean_path = StoreContextManager.extract_clean_path(request, store_context)
|
||||
|
||||
assert clean_path == "/shop/products"
|
||||
assert clean_path == "/storefront/products"
|
||||
|
||||
def test_extract_clean_path_from_stores_path(self):
|
||||
"""Test extracting clean path from /stores/ prefix."""
|
||||
request = Mock(spec=Request)
|
||||
request.url = Mock(path="/stores/store1/shop/products")
|
||||
request.url = Mock(path="/stores/store1/storefront/products")
|
||||
|
||||
store_context = {"detection_method": "path", "path_prefix": "/stores/store1"}
|
||||
|
||||
clean_path = StoreContextManager.extract_clean_path(request, store_context)
|
||||
|
||||
assert clean_path == "/shop/products"
|
||||
assert clean_path == "/storefront/products"
|
||||
|
||||
def test_extract_clean_path_root(self):
|
||||
"""Test extracting clean path when result is empty (should return /)."""
|
||||
@@ -343,22 +349,22 @@ class TestStoreContextManager:
|
||||
def test_extract_clean_path_no_path_context(self):
|
||||
"""Test extracting clean path for non-path detection methods."""
|
||||
request = Mock(spec=Request)
|
||||
request.url = Mock(path="/shop/products")
|
||||
request.url = Mock(path="/storefront/products")
|
||||
|
||||
store_context = {"detection_method": "subdomain", "subdomain": "store1"}
|
||||
|
||||
clean_path = StoreContextManager.extract_clean_path(request, store_context)
|
||||
|
||||
assert clean_path == "/shop/products"
|
||||
assert clean_path == "/storefront/products"
|
||||
|
||||
def test_extract_clean_path_no_context(self):
|
||||
"""Test extracting clean path with no store context."""
|
||||
request = Mock(spec=Request)
|
||||
request.url = Mock(path="/shop/products")
|
||||
request.url = Mock(path="/storefront/products")
|
||||
|
||||
clean_path = StoreContextManager.extract_clean_path(request, None)
|
||||
|
||||
assert clean_path == "/shop/products"
|
||||
assert clean_path == "/storefront/products"
|
||||
|
||||
# ========================================================================
|
||||
# Request Type Detection Tests
|
||||
@@ -392,7 +398,7 @@ class TestStoreContextManager:
|
||||
"""Test non-admin request."""
|
||||
request = Mock(spec=Request)
|
||||
request.headers = {"host": "store1.platform.com"}
|
||||
request.url = Mock(path="/shop")
|
||||
request.url = Mock(path="/storefront")
|
||||
|
||||
assert StoreContextManager.is_admin_request(request) is False
|
||||
|
||||
@@ -406,49 +412,10 @@ class TestStoreContextManager:
|
||||
def test_is_not_api_request(self):
|
||||
"""Test non-API request."""
|
||||
request = Mock(spec=Request)
|
||||
request.url = Mock(path="/shop/products")
|
||||
request.url = Mock(path="/storefront/products")
|
||||
|
||||
assert StoreContextManager.is_api_request(request) is False
|
||||
|
||||
# ========================================================================
|
||||
# Shop API Request Detection Tests
|
||||
# ========================================================================
|
||||
|
||||
def test_is_shop_api_request(self):
|
||||
"""Test shop API request detection."""
|
||||
request = Mock(spec=Request)
|
||||
request.url = Mock(path="/api/v1/shop/products")
|
||||
|
||||
assert StoreContextManager.is_shop_api_request(request) is True
|
||||
|
||||
def test_is_shop_api_request_cart(self):
|
||||
"""Test shop API request detection for cart endpoint."""
|
||||
request = Mock(spec=Request)
|
||||
request.url = Mock(path="/api/v1/shop/cart")
|
||||
|
||||
assert StoreContextManager.is_shop_api_request(request) is True
|
||||
|
||||
def test_is_not_shop_api_request_admin(self):
|
||||
"""Test non-shop API request (admin API)."""
|
||||
request = Mock(spec=Request)
|
||||
request.url = Mock(path="/api/v1/admin/stores")
|
||||
|
||||
assert StoreContextManager.is_shop_api_request(request) is False
|
||||
|
||||
def test_is_not_shop_api_request_store(self):
|
||||
"""Test non-shop API request (store API)."""
|
||||
request = Mock(spec=Request)
|
||||
request.url = Mock(path="/api/v1/store/products")
|
||||
|
||||
assert StoreContextManager.is_shop_api_request(request) is False
|
||||
|
||||
def test_is_not_shop_api_request_non_api(self):
|
||||
"""Test non-shop API request (non-API path)."""
|
||||
request = Mock(spec=Request)
|
||||
request.url = Mock(path="/shop/products")
|
||||
|
||||
assert StoreContextManager.is_shop_api_request(request) is False
|
||||
|
||||
# ========================================================================
|
||||
# Extract Store From Referer Tests
|
||||
# ========================================================================
|
||||
@@ -457,7 +424,7 @@ class TestStoreContextManager:
|
||||
"""Test extracting store from referer with /stores/ path."""
|
||||
request = Mock(spec=Request)
|
||||
request.headers = {
|
||||
"referer": "http://localhost:8000/stores/wizamart/shop/products"
|
||||
"referer": "http://localhost:8000/stores/wizamart/storefront/products"
|
||||
}
|
||||
|
||||
context = StoreContextManager.extract_store_from_referer(request)
|
||||
@@ -472,7 +439,7 @@ class TestStoreContextManager:
|
||||
"""Test extracting store from referer with /store/ path."""
|
||||
request = Mock(spec=Request)
|
||||
request.headers = {
|
||||
"referer": "http://localhost:8000/store/myshop/shop/products"
|
||||
"referer": "http://localhost:8000/store/myshop/storefront/products"
|
||||
}
|
||||
|
||||
context = StoreContextManager.extract_store_from_referer(request)
|
||||
@@ -486,7 +453,7 @@ class TestStoreContextManager:
|
||||
def test_extract_store_from_referer_subdomain(self):
|
||||
"""Test extracting store from referer with subdomain."""
|
||||
request = Mock(spec=Request)
|
||||
request.headers = {"referer": "http://wizamart.platform.com/shop/products"}
|
||||
request.headers = {"referer": "http://wizamart.platform.com/storefront/products"}
|
||||
|
||||
with patch("middleware.store_context.settings") as mock_settings:
|
||||
mock_settings.platform_domain = "platform.com"
|
||||
@@ -501,7 +468,7 @@ class TestStoreContextManager:
|
||||
def test_extract_store_from_referer_custom_domain(self):
|
||||
"""Test extracting store from referer with custom domain."""
|
||||
request = Mock(spec=Request)
|
||||
request.headers = {"referer": "http://my-custom-shop.com/shop/products"}
|
||||
request.headers = {"referer": "http://my-custom-shop.com/storefront/products"}
|
||||
|
||||
with patch("middleware.store_context.settings") as mock_settings:
|
||||
mock_settings.platform_domain = "platform.com"
|
||||
@@ -525,7 +492,7 @@ class TestStoreContextManager:
|
||||
def test_extract_store_from_referer_origin_header(self):
|
||||
"""Test extracting store from origin header when referer is missing."""
|
||||
request = Mock(spec=Request)
|
||||
request.headers = {"origin": "http://localhost:8000/stores/testshop/shop"}
|
||||
request.headers = {"origin": "http://localhost:8000/stores/testshop/storefront"}
|
||||
|
||||
context = StoreContextManager.extract_store_from_referer(request)
|
||||
|
||||
@@ -548,7 +515,7 @@ class TestStoreContextManager:
|
||||
def test_extract_store_from_referer_ignores_www_subdomain(self):
|
||||
"""Test that www subdomain is not extracted from referer."""
|
||||
request = Mock(spec=Request)
|
||||
request.headers = {"referer": "http://www.platform.com/shop"}
|
||||
request.headers = {"referer": "http://www.platform.com/storefront"}
|
||||
|
||||
with patch("middleware.store_context.settings") as mock_settings:
|
||||
mock_settings.platform_domain = "platform.com"
|
||||
@@ -560,7 +527,7 @@ class TestStoreContextManager:
|
||||
def test_extract_store_from_referer_localhost_not_custom_domain(self):
|
||||
"""Test that localhost is not treated as custom domain."""
|
||||
request = Mock(spec=Request)
|
||||
request.headers = {"referer": "http://localhost:8000/shop"}
|
||||
request.headers = {"referer": "http://localhost:8000/storefront"}
|
||||
|
||||
with patch("middleware.store_context.settings") as mock_settings:
|
||||
mock_settings.platform_domain = "platform.com"
|
||||
@@ -601,7 +568,7 @@ class TestStoreContextManager:
|
||||
@pytest.mark.parametrize(
|
||||
"path",
|
||||
[
|
||||
"/shop/products",
|
||||
"/storefront/products",
|
||||
"/admin/dashboard",
|
||||
"/api/stores",
|
||||
"/about",
|
||||
@@ -686,7 +653,7 @@ class TestStoreContextMiddleware:
|
||||
|
||||
request = Mock(spec=Request)
|
||||
request.headers = {"host": "store1.platform.com"}
|
||||
request.url = Mock(path="/shop/products")
|
||||
request.url = Mock(path="/storefront/products")
|
||||
request.state = Mock()
|
||||
|
||||
call_next = AsyncMock(return_value=Mock())
|
||||
@@ -714,7 +681,7 @@ class TestStoreContextMiddleware:
|
||||
patch.object(
|
||||
StoreContextManager,
|
||||
"extract_clean_path",
|
||||
return_value="/shop/products",
|
||||
return_value="/storefront/products",
|
||||
),
|
||||
patch("middleware.store_context.get_db", return_value=iter([mock_db])),
|
||||
):
|
||||
@@ -722,7 +689,7 @@ class TestStoreContextMiddleware:
|
||||
|
||||
assert request.state.store is mock_store
|
||||
assert request.state.store_context == store_context
|
||||
assert request.state.clean_path == "/shop/products"
|
||||
assert request.state.clean_path == "/storefront/products"
|
||||
call_next.assert_called_once_with(request)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -732,7 +699,7 @@ class TestStoreContextMiddleware:
|
||||
|
||||
request = Mock(spec=Request)
|
||||
request.headers = {"host": "nonexistent.platform.com"}
|
||||
request.url = Mock(path="/shop")
|
||||
request.url = Mock(path="/storefront")
|
||||
request.state = Mock()
|
||||
|
||||
call_next = AsyncMock(return_value=Mock())
|
||||
@@ -756,7 +723,7 @@ class TestStoreContextMiddleware:
|
||||
|
||||
assert request.state.store is None
|
||||
assert request.state.store_context == store_context
|
||||
assert request.state.clean_path == "/shop"
|
||||
assert request.state.clean_path == "/storefront"
|
||||
call_next.assert_called_once_with(request)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -820,146 +787,6 @@ class TestStoreContextMiddleware:
|
||||
assert request.state.clean_path == path
|
||||
call_next.assert_called_once_with(request)
|
||||
|
||||
# ========================================================================
|
||||
# Shop API Request Handling Tests
|
||||
# ========================================================================
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_middleware_shop_api_with_referer_store_found(self):
|
||||
"""Test middleware handles shop API request with store from Referer."""
|
||||
middleware = StoreContextMiddleware(app=None)
|
||||
|
||||
request = Mock(spec=Request)
|
||||
request.headers = {
|
||||
"host": "localhost",
|
||||
"referer": "http://localhost:8000/stores/wizamart/shop/products",
|
||||
}
|
||||
request.url = Mock(path="/api/v1/shop/cart")
|
||||
request.state = Mock()
|
||||
|
||||
call_next = AsyncMock(return_value=Mock())
|
||||
|
||||
mock_store = Mock()
|
||||
mock_store.id = 1
|
||||
mock_store.name = "Wizamart"
|
||||
mock_store.subdomain = "wizamart"
|
||||
|
||||
store_context = {
|
||||
"subdomain": "wizamart",
|
||||
"detection_method": "path",
|
||||
"path_prefix": "/stores/wizamart",
|
||||
"full_prefix": "/stores/",
|
||||
}
|
||||
|
||||
mock_db = MagicMock()
|
||||
|
||||
with (
|
||||
patch.object(StoreContextManager, "is_admin_request", return_value=False),
|
||||
patch.object(
|
||||
StoreContextManager, "is_static_file_request", return_value=False
|
||||
),
|
||||
patch.object(
|
||||
StoreContextManager, "is_shop_api_request", return_value=True
|
||||
),
|
||||
patch.object(
|
||||
StoreContextManager,
|
||||
"extract_store_from_referer",
|
||||
return_value=store_context,
|
||||
),
|
||||
patch.object(
|
||||
StoreContextManager,
|
||||
"get_store_from_context",
|
||||
return_value=mock_store,
|
||||
),
|
||||
patch("middleware.store_context.get_db", return_value=iter([mock_db])),
|
||||
):
|
||||
await middleware.dispatch(request, call_next)
|
||||
|
||||
assert request.state.store is mock_store
|
||||
assert request.state.store_context == store_context
|
||||
assert request.state.clean_path == "/api/v1/shop/cart"
|
||||
call_next.assert_called_once_with(request)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_middleware_shop_api_with_referer_store_not_found(self):
|
||||
"""Test middleware handles shop API when store from Referer not in database."""
|
||||
middleware = StoreContextMiddleware(app=None)
|
||||
|
||||
request = Mock(spec=Request)
|
||||
request.headers = {
|
||||
"host": "localhost",
|
||||
"referer": "http://localhost:8000/stores/nonexistent/shop/products",
|
||||
}
|
||||
request.url = Mock(path="/api/v1/shop/cart")
|
||||
request.state = Mock()
|
||||
|
||||
call_next = AsyncMock(return_value=Mock())
|
||||
|
||||
store_context = {
|
||||
"subdomain": "nonexistent",
|
||||
"detection_method": "path",
|
||||
"path_prefix": "/stores/nonexistent",
|
||||
"full_prefix": "/stores/",
|
||||
}
|
||||
|
||||
mock_db = MagicMock()
|
||||
|
||||
with (
|
||||
patch.object(StoreContextManager, "is_admin_request", return_value=False),
|
||||
patch.object(
|
||||
StoreContextManager, "is_static_file_request", return_value=False
|
||||
),
|
||||
patch.object(
|
||||
StoreContextManager, "is_shop_api_request", return_value=True
|
||||
),
|
||||
patch.object(
|
||||
StoreContextManager,
|
||||
"extract_store_from_referer",
|
||||
return_value=store_context,
|
||||
),
|
||||
patch.object(
|
||||
StoreContextManager, "get_store_from_context", return_value=None
|
||||
),
|
||||
patch("middleware.store_context.get_db", return_value=iter([mock_db])),
|
||||
):
|
||||
await middleware.dispatch(request, call_next)
|
||||
|
||||
assert request.state.store is None
|
||||
assert request.state.store_context == store_context
|
||||
assert request.state.clean_path == "/api/v1/shop/cart"
|
||||
call_next.assert_called_once_with(request)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_middleware_shop_api_without_referer(self):
|
||||
"""Test middleware handles shop API request without Referer header."""
|
||||
middleware = StoreContextMiddleware(app=None)
|
||||
|
||||
request = Mock(spec=Request)
|
||||
request.headers = {"host": "localhost"}
|
||||
request.url = Mock(path="/api/v1/shop/products")
|
||||
request.state = Mock()
|
||||
|
||||
call_next = AsyncMock(return_value=Mock())
|
||||
|
||||
with (
|
||||
patch.object(StoreContextManager, "is_admin_request", return_value=False),
|
||||
patch.object(
|
||||
StoreContextManager, "is_static_file_request", return_value=False
|
||||
),
|
||||
patch.object(
|
||||
StoreContextManager, "is_shop_api_request", return_value=True
|
||||
),
|
||||
patch.object(
|
||||
StoreContextManager, "extract_store_from_referer", return_value=None
|
||||
),
|
||||
):
|
||||
await middleware.dispatch(request, call_next)
|
||||
|
||||
assert request.state.store is None
|
||||
assert request.state.store_context is None
|
||||
assert request.state.clean_path == "/api/v1/shop/products"
|
||||
call_next.assert_called_once_with(request)
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
@pytest.mark.stores
|
||||
|
||||
Reference in New Issue
Block a user