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

@@ -196,6 +196,68 @@ class StatsAggregatorService:
categorized = self.get_store_dashboard_stats(db, store_id, platform_id, context)
return self._flatten_metrics(categorized)
def get_merchant_dashboard_stats(
self,
db: Session,
merchant_id: int,
platform_id: int,
context: MetricsContext | None = None,
) -> dict[str, list[MetricValue]]:
"""
Get all metrics for a merchant, grouped by category.
Called by the merchant dashboard to display merchant-scoped statistics.
Args:
db: Database session
merchant_id: ID of the merchant to get metrics for
platform_id: Platform ID (for module enablement check)
context: Optional filtering/scoping context
Returns:
Dict mapping category name to list of MetricValue objects
"""
providers = self._get_enabled_providers(db, platform_id)
result: dict[str, list[MetricValue]] = {}
for module, provider in providers:
if not hasattr(provider, "get_merchant_metrics"):
continue
try:
metrics = provider.get_merchant_metrics(db, merchant_id, context)
if metrics:
result[provider.metrics_category] = metrics
except Exception as e:
logger.warning(
f"Failed to get merchant metrics from module {module.code}: {e}"
)
return result
def get_merchant_stats_flat(
self,
db: Session,
merchant_id: int,
platform_id: int,
context: MetricsContext | None = None,
) -> dict[str, Any]:
"""
Get merchant metrics as a flat dictionary.
Args:
db: Database session
merchant_id: ID of the merchant to get metrics for
platform_id: Platform ID (for module enablement check)
context: Optional filtering/scoping context
Returns:
Flat dict mapping metric keys to values
"""
categorized = self.get_merchant_dashboard_stats(
db, merchant_id, platform_id, context
)
return self._flatten_metrics(categorized)
def get_admin_stats_flat(
self,
db: Session,