Files
orion/app/modules/catalog/routes/api/admin.py
Samir Boulahtit f20266167d
Some checks failed
CI / ruff (push) Failing after 7s
CI / pytest (push) Failing after 1s
CI / architecture (push) Failing after 9s
CI / dependency-scanning (push) Successful in 27s
CI / audit (push) Successful in 8s
CI / docs (push) Has been skipped
fix(lint): auto-fix ruff violations and tune lint rules
- Auto-fixed 4,496 lint issues (import sorting, modern syntax, etc.)
- Added ignore rules for patterns intentional in this codebase:
  E402 (late imports), E712 (SQLAlchemy filters), B904 (raise from),
  SIM108/SIM105/SIM117 (readability preferences)
- Added per-file ignores for tests and scripts
- Excluded broken scripts/rename_terminology.py (has curly quotes)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-12 23:10:42 +01:00

162 lines
5.4 KiB
Python

# app/modules/catalog/routes/api/admin.py
"""
Admin store product catalog endpoints.
Provides management of store-specific product catalogs:
- Browse products in store catalogs
- View product details with override info
- Create/update/remove products from catalog
All routes require module access control for the 'catalog' module.
"""
import logging
from fastapi import APIRouter, Depends, Query
from sqlalchemy.orm import Session
from app.api.deps import get_current_admin_api, require_module_access
from app.core.database import get_db
from app.modules.billing.services.subscription_service import subscription_service
from app.modules.catalog.schemas import (
CatalogStore,
CatalogStoresResponse,
RemoveProductResponse,
StoreProductCreate,
StoreProductCreateResponse,
StoreProductDetail,
StoreProductListItem,
StoreProductListResponse,
StoreProductStats,
StoreProductUpdate,
)
from app.modules.catalog.services.store_product_service import store_product_service
from app.modules.enums import FrontendType
from models.schema.auth import UserContext
admin_router = APIRouter(
prefix="/store-products",
dependencies=[Depends(require_module_access("catalog", FrontendType.ADMIN))],
)
logger = logging.getLogger(__name__)
# ============================================================================
# Endpoints
# ============================================================================
@admin_router.get("", response_model=StoreProductListResponse)
def get_store_products(
skip: int = Query(0, ge=0),
limit: int = Query(50, ge=1, le=500),
search: str | None = Query(None, description="Search by title or SKU"),
store_id: int | None = Query(None, description="Filter by store"),
is_active: bool | None = Query(None, description="Filter by active status"),
is_featured: bool | None = Query(None, description="Filter by featured status"),
language: str = Query("en", description="Language for title lookup"),
db: Session = Depends(get_db),
current_admin: UserContext = Depends(get_current_admin_api),
):
"""
Get all products in store catalogs with filtering.
This endpoint allows admins to browse products that have been
copied to store catalogs from the marketplace repository.
"""
products, total = store_product_service.get_products(
db=db,
skip=skip,
limit=limit,
search=search,
store_id=store_id,
is_active=is_active,
is_featured=is_featured,
language=language,
)
return StoreProductListResponse(
products=[StoreProductListItem(**p) for p in products],
total=total,
skip=skip,
limit=limit,
)
@admin_router.get("/stats", response_model=StoreProductStats)
def get_store_product_stats(
store_id: int | None = Query(None, description="Filter stats by store ID"),
db: Session = Depends(get_db),
current_admin: UserContext = Depends(get_current_admin_api),
):
"""Get store product statistics for admin dashboard."""
stats = store_product_service.get_product_stats(db, store_id=store_id)
return StoreProductStats(**stats)
@admin_router.get("/stores", response_model=CatalogStoresResponse)
def get_catalog_stores(
db: Session = Depends(get_db),
current_admin: UserContext = Depends(get_current_admin_api),
):
"""Get list of stores with products in their catalogs."""
stores = store_product_service.get_catalog_stores(db)
return CatalogStoresResponse(stores=[CatalogStore(**v) for v in stores])
@admin_router.get("/{product_id}", response_model=StoreProductDetail)
def get_store_product_detail(
product_id: int,
db: Session = Depends(get_db),
current_admin: UserContext = Depends(get_current_admin_api),
):
"""Get detailed store product information including override info."""
product = store_product_service.get_product_detail(db, product_id)
return StoreProductDetail(**product)
@admin_router.post("", response_model=StoreProductCreateResponse)
def create_store_product(
data: StoreProductCreate,
db: Session = Depends(get_db),
current_admin: UserContext = Depends(get_current_admin_api),
):
"""Create a new store product."""
# Check product limit before creating
subscription_service.check_product_limit(db, data.store_id)
product = store_product_service.create_product(db, data.model_dump())
db.commit()
return StoreProductCreateResponse(
id=product.id, message="Product created successfully"
)
@admin_router.patch("/{product_id}", response_model=StoreProductDetail)
def update_store_product(
product_id: int,
data: StoreProductUpdate,
db: Session = Depends(get_db),
current_admin: UserContext = Depends(get_current_admin_api),
):
"""Update a store product."""
# Only include fields that were explicitly set
update_data = data.model_dump(exclude_unset=True)
store_product_service.update_product(db, product_id, update_data)
db.commit()
# Return the updated product detail
product = store_product_service.get_product_detail(db, product_id)
return StoreProductDetail(**product)
@admin_router.delete("/{product_id}", response_model=RemoveProductResponse)
def remove_store_product(
product_id: int,
db: Session = Depends(get_db),
current_admin: UserContext = Depends(get_current_admin_api),
):
"""Remove a product from store catalog."""
result = store_product_service.remove_product(db, product_id)
db.commit()
return RemoveProductResponse(**result)