fix(billing): resolve 3 IMPORT-001 architecture violations in billing module
Replace direct imports from optional modules (catalog, orders, analytics) with provider pattern calls (stats_aggregator, feature_aggregator) and move usage_service from analytics to billing where it belongs. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -16,13 +16,14 @@ from decimal import Decimal
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.modules.catalog.models import Product
|
||||
from app.modules.billing.models import (
|
||||
CapacitySnapshot,
|
||||
MerchantSubscription,
|
||||
SubscriptionStatus,
|
||||
)
|
||||
from app.modules.tenancy.models import Store, StoreUser
|
||||
from app.modules.contracts.metrics import MetricsContext
|
||||
from app.modules.core.services.stats_aggregator import stats_aggregator
|
||||
from app.modules.tenancy.models import Platform, Store, StoreUser
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -87,8 +88,17 @@ class CapacityForecastService:
|
||||
or 0
|
||||
)
|
||||
|
||||
# Resource metrics
|
||||
total_products = db.query(func.count(Product.id)).scalar() or 0
|
||||
# Resource metrics via provider pattern (avoids direct catalog/orders 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
|
||||
|
||||
stats = stats_aggregator.get_admin_stats_flat(
|
||||
db, platform_id,
|
||||
context=MetricsContext(date_from=start_of_month),
|
||||
)
|
||||
|
||||
total_products = stats.get("catalog.total_products", 0)
|
||||
total_team = (
|
||||
db.query(func.count(StoreUser.id))
|
||||
.filter(StoreUser.is_active == True) # noqa: E712
|
||||
@@ -96,15 +106,8 @@ class CapacityForecastService:
|
||||
or 0
|
||||
)
|
||||
|
||||
# Orders this month
|
||||
start_of_month = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
|
||||
from app.modules.orders.models import Order
|
||||
|
||||
total_orders = (
|
||||
db.query(func.count(Order.id))
|
||||
.filter(Order.created_at >= start_of_month)
|
||||
.scalar() or 0
|
||||
)
|
||||
# Orders this month (from stats aggregator)
|
||||
total_orders = stats.get("orders.in_period", 0)
|
||||
|
||||
# Storage metrics
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user