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,47 @@ class CustomerMetricsProvider:
logger.warning(f"Failed to get customer platform metrics: {e}")
return []
def get_merchant_metrics(
self,
db: Session,
merchant_id: int,
context: MetricsContext | None = None,
) -> list[MetricValue]:
"""
Get customer metrics scoped to a merchant.
Aggregates customer counts across all stores owned by the merchant.
"""
from app.modules.customers.models import Customer
from app.modules.tenancy.models import Store
try:
merchant_store_ids = (
db.query(Store.id)
.filter(Store.merchant_id == merchant_id)
.subquery()
)
total_customers = (
db.query(Customer)
.filter(Customer.store_id.in_(merchant_store_ids))
.count()
)
return [
MetricValue(
key="customers.total",
value=total_customers,
label="Total Customers",
category="customers",
icon="users",
description="Total customers across all merchant stores",
),
]
except Exception as e:
logger.warning(f"Failed to get customer merchant metrics: {e}")
return []
# Singleton instance
customer_metrics_provider = CustomerMetricsProvider()