refactor: switch to full auto-discovery for module API routes

- Enhanced route discovery system with ROUTE_CONFIG support for custom
  prefix, tags, and priority
- Added get_admin_api_routes() and get_vendor_api_routes() helpers that
  return routes sorted by priority
- Added fallback discovery for routes/{frontend}.py when routes/api/
  doesn't exist
- Updated CMS module with ROUTE_CONFIG (prefix: /content-pages,
  priority: 100) to register last for catch-all routes
- Moved customers routes from routes/ to routes/api/ directory
- Updated orders module to aggregate exception routers into main routers
- Removed manual module router imports from admin and vendor API init
  files, replaced with auto-discovery loop

Modules now auto-discovered: billing, inventory, orders, marketplace,
cms, customers, analytics, loyalty, messaging, monitoring, dev-tools

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-31 12:42:25 +01:00
parent 9fb3588030
commit db56b34894
14 changed files with 1230 additions and 155 deletions

View File

@@ -14,43 +14,32 @@ Routes can be module-gated using require_module_access() dependency.
For multi-tenant apps, module enablement is checked at request time
based on platform context (not at route registration time).
Extracted modules (app/modules/{module}/routes/):
Self-contained modules (auto-discovered from app/modules/{module}/routes/api/vendor.py):
- billing: Subscription tiers, vendor billing, invoices
- inventory: Stock management, inventory tracking
- orders: Order management, fulfillment, exceptions
- marketplace: Letzshop integration, product sync
Module extraction pattern:
1. Create app/modules/{module}/ directory
2. Create routes/vendor.py with require_module_access("{module}") dependency
3. Import module router here and include it
4. Comment out legacy router include
- cms: Content pages management
- customers: Customer management
"""
from fastapi import APIRouter
# Import all sub-routers (JSON API only)
# Import all sub-routers (legacy routes that haven't been migrated to modules)
from . import (
analytics,
auth,
billing,
# content_pages - moved to app.modules.cms.routes.api.vendor
# customers - moved to app.modules.customers.routes.vendor
dashboard,
email_settings,
email_templates,
features,
info,
inventory,
invoices,
letzshop,
marketplace,
media,
messages,
notifications,
onboarding,
order_item_exceptions,
orders,
payments,
products,
profile,
@@ -59,21 +48,6 @@ from . import (
usage,
)
# Import extracted module routers
# NOTE: Import directly from vendor.py files to avoid circular imports through __init__.py
from app.modules.billing.routes.api.vendor import vendor_router as billing_vendor_router
from app.modules.inventory.routes.vendor import vendor_router as inventory_vendor_router
from app.modules.orders.routes.vendor import vendor_router as orders_vendor_router
from app.modules.orders.routes.vendor import vendor_exceptions_router as orders_exceptions_router
from app.modules.marketplace.routes.api.vendor import vendor_router as marketplace_vendor_router
from app.modules.marketplace.routes.api.vendor import vendor_letzshop_router as letzshop_vendor_router
# CMS module router
from app.modules.cms.routes.api.vendor import router as cms_vendor_router
# Customers module router
from app.modules.customers.routes.vendor import vendor_router as customers_vendor_router
# Create vendor router
router = APIRouter()
@@ -97,50 +71,44 @@ router.include_router(email_templates.router, tags=["vendor-email-templates"])
router.include_router(email_settings.router, tags=["vendor-email-settings"])
router.include_router(onboarding.router, tags=["vendor-onboarding"])
# Business operations (with prefixes: /products/*, /orders/*, etc.)
# Business operations (with prefixes: /products/*, etc.)
router.include_router(products.router, tags=["vendor-products"])
# Include orders module router (with module access control)
router.include_router(orders_vendor_router, tags=["vendor-orders"])
router.include_router(orders_exceptions_router, tags=["vendor-order-exceptions"])
# Legacy: router.include_router(orders.router, tags=["vendor-orders"])
# Legacy: router.include_router(order_item_exceptions.router, tags=["vendor-order-exceptions"])
router.include_router(invoices.router, tags=["vendor-invoices"])
# Include customers module router (with module access control)
router.include_router(customers_vendor_router, tags=["vendor-customers"])
# Legacy: router.include_router(customers.router, tags=["vendor-customers"])
router.include_router(team.router, tags=["vendor-team"])
# Include inventory module router (with module access control)
router.include_router(inventory_vendor_router, tags=["vendor-inventory"])
# Legacy: router.include_router(inventory.router, tags=["vendor-inventory"])
# Include marketplace module router (with module access control)
router.include_router(marketplace_vendor_router, tags=["vendor-marketplace"])
router.include_router(letzshop_vendor_router, tags=["vendor-letzshop"])
# Legacy: router.include_router(marketplace.router, tags=["vendor-marketplace"])
# Legacy: router.include_router(letzshop.router, tags=["vendor-letzshop"])
# Services (with prefixes: /payments/*, /media/*, etc.)
router.include_router(payments.router, tags=["vendor-payments"])
router.include_router(media.router, tags=["vendor-media"])
router.include_router(notifications.router, tags=["vendor-notifications"])
router.include_router(messages.router, tags=["vendor-messages"])
router.include_router(analytics.router, tags=["vendor-analytics"])
# Include billing module router (with module access control)
router.include_router(billing_vendor_router, tags=["vendor-billing"])
# Legacy: router.include_router(billing.router, tags=["vendor-billing"])
router.include_router(features.router, tags=["vendor-features"])
router.include_router(usage.router, tags=["vendor-usage"])
# CMS module router (self-contained module)
router.include_router(cms_vendor_router, tags=["vendor-content-pages"])
# Legacy: content_pages.router moved to app.modules.cms.routes.api.vendor
# ============================================================================
# Auto-discovered Module Routes
# ============================================================================
# Routes from self-contained modules are auto-discovered and registered.
# Modules include: billing, inventory, orders, marketplace, cms, customers
# Routes are sorted by priority, so catch-all routes (CMS) come last.
from app.modules.routes import get_vendor_api_routes
for route_info in get_vendor_api_routes():
# Only pass prefix if custom_prefix is set (router already has internal prefix)
if route_info.custom_prefix:
router.include_router(
route_info.router,
prefix=route_info.custom_prefix,
tags=route_info.tags,
)
else:
router.include_router(
route_info.router,
tags=route_info.tags,
)
# Vendor info endpoint - MUST BE LAST! Has catch-all GET /{vendor_code}
router.include_router(info.router, tags=["vendor-info"])