fix: use metrics provider pattern for merchant dashboard stats

The merchant dashboard was showing subscription count as "Total Stores".
Add get_merchant_metrics() to MetricsProviderProtocol and implement it
in tenancy, billing, and customer providers. Dashboard now fetches real
stats from a new /merchants/core/dashboard/stats endpoint and displays
4 cards: active subscriptions, total stores, customers, team members.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-22 21:28:59 +01:00
parent 42b894094a
commit ff852f1ab3
9 changed files with 372 additions and 15 deletions

View File

@@ -108,6 +108,45 @@ class BillingMetricsProvider:
logger.warning(f"Failed to get billing platform metrics: {e}")
return []
def get_merchant_metrics(
self,
db: Session,
merchant_id: int,
context: MetricsContext | None = None,
) -> list[MetricValue]:
"""
Get subscription metrics for a specific merchant.
Provides:
- Active subscriptions (active + trial)
"""
from app.modules.billing.models import MerchantSubscription
try:
active_subs = (
db.query(func.count(MerchantSubscription.id))
.filter(
MerchantSubscription.merchant_id == merchant_id,
MerchantSubscription.status.in_(["active", "trial"]),
)
.scalar()
or 0
)
return [
MetricValue(
key="billing.active_subscriptions",
value=active_subs,
label="Active Subscriptions",
category="billing",
icon="clipboard-list",
description="Active or trial subscriptions for this merchant",
),
]
except Exception as e:
logger.warning(f"Failed to get billing merchant metrics: {e}")
return []
# Singleton instance
billing_metrics_provider = BillingMetricsProvider()