Major changes: - Add AuditProvider protocol for cross-module audit logging - Move customer order operations to orders module (dependency inversion) - Add customer order metrics via MetricsProvider pattern - Fix missing db parameter in get_admin_context() calls - Move ProductMedia relationship to catalog module (proper ownership) - Add marketplace breakdown stats to marketplace_widgets New files: - contracts/audit.py - AuditProviderProtocol - core/services/audit_aggregator.py - Aggregates audit providers - monitoring/services/audit_provider.py - Monitoring audit implementation - orders/services/customer_order_service.py - Customer order operations - orders/routes/api/vendor_customer_orders.py - Customer order endpoints - catalog/services/product_media_service.py - Product media service - Architecture documentation for patterns Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
290 lines
9.1 KiB
Python
290 lines
9.1 KiB
Python
# app/modules/marketplace/services/marketplace_widgets.py
|
|
"""
|
|
Marketplace dashboard widget provider.
|
|
|
|
Provides widgets for marketplace-related data on vendor and admin dashboards.
|
|
Implements the DashboardWidgetProviderProtocol.
|
|
|
|
Widgets provided:
|
|
- recent_imports: List of recent import jobs with status
|
|
"""
|
|
|
|
import logging
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.modules.contracts.widgets import (
|
|
BreakdownWidget,
|
|
DashboardWidget,
|
|
DashboardWidgetProviderProtocol,
|
|
ListWidget,
|
|
WidgetBreakdownItem,
|
|
WidgetContext,
|
|
WidgetListItem,
|
|
)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class MarketplaceWidgetProvider:
|
|
"""
|
|
Widget provider for marketplace module.
|
|
|
|
Provides dashboard widgets for import jobs and other marketplace data.
|
|
"""
|
|
|
|
@property
|
|
def widgets_category(self) -> str:
|
|
return "marketplace"
|
|
|
|
def _map_status_to_display(self, status: str) -> str:
|
|
"""Map job status to widget status indicator."""
|
|
status_map = {
|
|
"pending": "neutral",
|
|
"processing": "warning",
|
|
"completed": "success",
|
|
"completed_with_errors": "warning",
|
|
"failed": "error",
|
|
}
|
|
return status_map.get(status, "neutral")
|
|
|
|
def get_vendor_widgets(
|
|
self,
|
|
db: Session,
|
|
vendor_id: int,
|
|
context: WidgetContext | None = None,
|
|
) -> list[DashboardWidget]:
|
|
"""
|
|
Get marketplace widgets for a vendor dashboard.
|
|
|
|
Args:
|
|
db: Database session
|
|
vendor_id: ID of the vendor
|
|
context: Optional filtering/scoping context
|
|
|
|
Returns:
|
|
List of DashboardWidget objects for the vendor
|
|
"""
|
|
from app.modules.marketplace.models import MarketplaceImportJob
|
|
|
|
limit = context.limit if context else 5
|
|
|
|
# Get recent imports for this vendor
|
|
jobs = (
|
|
db.query(MarketplaceImportJob)
|
|
.filter(MarketplaceImportJob.vendor_id == vendor_id)
|
|
.order_by(MarketplaceImportJob.created_at.desc())
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
|
|
items = [
|
|
WidgetListItem(
|
|
id=job.id,
|
|
title=f"Import #{job.id}",
|
|
subtitle=f"{job.marketplace} - {job.language.upper()}",
|
|
status=self._map_status_to_display(job.status),
|
|
timestamp=job.created_at,
|
|
url=f"/vendor/marketplace/imports/{job.id}",
|
|
metadata={
|
|
"total_processed": job.total_processed or 0,
|
|
"imported_count": job.imported_count or 0,
|
|
"error_count": job.error_count or 0,
|
|
"status": job.status,
|
|
},
|
|
)
|
|
for job in jobs
|
|
]
|
|
|
|
# Get total count for "view all" indicator
|
|
total_count = (
|
|
db.query(MarketplaceImportJob)
|
|
.filter(MarketplaceImportJob.vendor_id == vendor_id)
|
|
.count()
|
|
)
|
|
|
|
return [
|
|
DashboardWidget(
|
|
key="marketplace.recent_imports",
|
|
widget_type="list",
|
|
title="Recent Imports",
|
|
category="marketplace",
|
|
data=ListWidget(
|
|
items=items,
|
|
total_count=total_count,
|
|
view_all_url="/vendor/marketplace/imports",
|
|
),
|
|
icon="download",
|
|
description="Latest product import jobs",
|
|
order=20,
|
|
)
|
|
]
|
|
|
|
def get_platform_widgets(
|
|
self,
|
|
db: Session,
|
|
platform_id: int,
|
|
context: WidgetContext | None = None,
|
|
) -> list[DashboardWidget]:
|
|
"""
|
|
Get marketplace widgets for the admin/platform dashboard.
|
|
|
|
Args:
|
|
db: Database session
|
|
platform_id: ID of the platform
|
|
context: Optional filtering/scoping context
|
|
|
|
Returns:
|
|
List of DashboardWidget objects for the platform
|
|
"""
|
|
from sqlalchemy.orm import joinedload
|
|
|
|
from app.modules.marketplace.models import MarketplaceImportJob
|
|
from app.modules.tenancy.models import Vendor, VendorPlatform
|
|
|
|
limit = context.limit if context else 5
|
|
|
|
# Get vendor IDs for this platform
|
|
vendor_ids_subquery = (
|
|
db.query(VendorPlatform.vendor_id)
|
|
.filter(VendorPlatform.platform_id == platform_id)
|
|
.subquery()
|
|
)
|
|
|
|
# Get recent imports across all vendors in the platform
|
|
jobs = (
|
|
db.query(MarketplaceImportJob)
|
|
.options(joinedload(MarketplaceImportJob.vendor))
|
|
.filter(MarketplaceImportJob.vendor_id.in_(vendor_ids_subquery))
|
|
.order_by(MarketplaceImportJob.created_at.desc())
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
|
|
items = [
|
|
WidgetListItem(
|
|
id=job.id,
|
|
title=f"Import #{job.id}",
|
|
subtitle=job.vendor.name if job.vendor else "Unknown Vendor",
|
|
status=self._map_status_to_display(job.status),
|
|
timestamp=job.created_at,
|
|
url=f"/admin/marketplace/imports/{job.id}",
|
|
metadata={
|
|
"vendor_id": job.vendor_id,
|
|
"vendor_code": job.vendor.vendor_code if job.vendor else None,
|
|
"marketplace": job.marketplace,
|
|
"total_processed": job.total_processed or 0,
|
|
"imported_count": job.imported_count or 0,
|
|
"error_count": job.error_count or 0,
|
|
"status": job.status,
|
|
},
|
|
)
|
|
for job in jobs
|
|
]
|
|
|
|
# Get total count for platform
|
|
total_count = (
|
|
db.query(MarketplaceImportJob)
|
|
.filter(MarketplaceImportJob.vendor_id.in_(vendor_ids_subquery))
|
|
.count()
|
|
)
|
|
|
|
widgets = [
|
|
DashboardWidget(
|
|
key="marketplace.recent_imports",
|
|
widget_type="list",
|
|
title="Recent Imports",
|
|
category="marketplace",
|
|
data=ListWidget(
|
|
items=items,
|
|
total_count=total_count,
|
|
view_all_url="/admin/marketplace/letzshop",
|
|
),
|
|
icon="download",
|
|
description="Latest product import jobs across all vendors",
|
|
order=20,
|
|
)
|
|
]
|
|
|
|
# Add marketplace breakdown widget
|
|
breakdown_widget = self._get_marketplace_breakdown_widget(db)
|
|
if breakdown_widget:
|
|
widgets.append(breakdown_widget)
|
|
|
|
return widgets
|
|
|
|
def _get_marketplace_breakdown_widget(
|
|
self,
|
|
db: Session,
|
|
) -> DashboardWidget | None:
|
|
"""
|
|
Get a breakdown widget showing statistics per marketplace.
|
|
|
|
Returns:
|
|
DashboardWidget with BreakdownWidget data, or None if no data
|
|
"""
|
|
from sqlalchemy import func
|
|
|
|
from app.modules.marketplace.models import MarketplaceProduct
|
|
|
|
try:
|
|
marketplace_stats = (
|
|
db.query(
|
|
MarketplaceProduct.marketplace,
|
|
func.count(MarketplaceProduct.id).label("total_products"),
|
|
func.count(func.distinct(MarketplaceProduct.vendor_name)).label(
|
|
"unique_vendors"
|
|
),
|
|
func.count(func.distinct(MarketplaceProduct.brand)).label(
|
|
"unique_brands"
|
|
),
|
|
)
|
|
.filter(MarketplaceProduct.marketplace.isnot(None))
|
|
.group_by(MarketplaceProduct.marketplace)
|
|
.all()
|
|
)
|
|
|
|
if not marketplace_stats:
|
|
return None
|
|
|
|
total_products = sum(stat.total_products for stat in marketplace_stats)
|
|
|
|
breakdown_items = [
|
|
WidgetBreakdownItem(
|
|
label=stat.marketplace or "Unknown",
|
|
value=stat.total_products,
|
|
secondary_value=stat.unique_vendors,
|
|
percentage=(
|
|
round(stat.total_products / total_products * 100, 1)
|
|
if total_products > 0
|
|
else 0
|
|
),
|
|
icon="globe",
|
|
)
|
|
for stat in marketplace_stats
|
|
]
|
|
|
|
return DashboardWidget(
|
|
key="marketplace.breakdown",
|
|
widget_type="breakdown",
|
|
title="Products by Marketplace",
|
|
category="marketplace",
|
|
data=BreakdownWidget(
|
|
items=breakdown_items,
|
|
total=total_products,
|
|
),
|
|
icon="chart-pie",
|
|
description="Product distribution across marketplaces",
|
|
order=30,
|
|
)
|
|
|
|
except Exception as e:
|
|
logger.warning(f"Failed to get marketplace breakdown widget: {e}")
|
|
return None
|
|
|
|
|
|
# Singleton instance
|
|
marketplace_widget_provider = MarketplaceWidgetProvider()
|
|
|
|
__all__ = ["MarketplaceWidgetProvider", "marketplace_widget_provider"]
|