refactor: fix all architecture validator findings (202 → 0)

Eliminate all 103 errors and 96 warnings from the architecture validator:

Phase 1 - Validator rules & YAML:
- Add NAM-001/NAM-002 exceptions for module-scoped router/service files
- Fix API-004 to detect # public comments on decorator lines
- Add module-specific exception bases to EXC-004 valid_bases
- Exclude storefront files from AUTH-004 store context check
- Add SVC-006 exceptions for loyalty service atomic commits
- Fix _get_rule() to search naming_rules and auth_rules categories
- Use plain # CODE comments instead of # noqa: CODE for custom rules

Phase 2 - Billing module (5 route files):
- Move _resolve_store_to_merchant to subscription_service
- Move tier/feature queries to feature_service, admin_subscription_service
- Extract 22 inline Pydantic schemas to billing/schemas/billing.py
- Replace all HTTPException with domain exceptions

Phase 3 - Loyalty module (4 routes + points_service):
- Add 7 domain exceptions (Apple auth, enrollment, device registration)
- Add service methods to card_service, program_service, apple_wallet_service
- Move all db.query() from routes to service layer
- Fix SVC-001: replace HTTPException in points_service with domain exception

Phase 4 - Remaining modules:
- tenancy: move store stats queries to admin_service
- cms: move platform resolution to content_page_service, add NoPlatformSubscriptionException
- messaging: move user/customer lookups to messaging_service
- Add ConfigDict(from_attributes=True) to ContentPageResponse

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-13 18:49:24 +01:00
parent 9173448645
commit 7c43d6f4a2
48 changed files with 1613 additions and 1039 deletions

View File

@@ -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)