feat: add vendor API endpoints for inventory transaction history

Adds three new endpoints for viewing stock movement history:
- GET /inventory/transactions - paginated list with filters
- GET /inventory/transactions/product/{id} - product-specific history
- GET /inventory/transactions/order/{id} - order-specific history

Creates InventoryTransactionService to encapsulate query logic
following architecture patterns. Includes response schemas with
product details for rich UI display.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-01 18:20:12 +01:00
parent 049e3319c3
commit 159243066c
3 changed files with 452 additions and 0 deletions

View File

@@ -14,6 +14,7 @@ from sqlalchemy.orm import Session
from app.api.deps import get_current_vendor_api
from app.core.database import get_db
from app.services.inventory_service import inventory_service
from app.services.inventory_transaction_service import inventory_transaction_service
from models.database.user import User
from models.schema.inventory import (
InventoryAdjust,
@@ -22,8 +23,12 @@ from models.schema.inventory import (
InventoryMessageResponse,
InventoryReserve,
InventoryResponse,
InventoryTransactionListResponse,
InventoryTransactionWithProduct,
InventoryUpdate,
OrderTransactionHistoryResponse,
ProductInventorySummary,
ProductTransactionHistoryResponse,
)
router = APIRouter()
@@ -159,3 +164,94 @@ def delete_inventory(
inventory_service.delete_inventory(db, current_user.token_vendor_id, inventory_id)
db.commit()
return InventoryMessageResponse(message="Inventory deleted successfully")
# ============================================================================
# Inventory Transaction History Endpoints
# ============================================================================
@router.get("/inventory/transactions", response_model=InventoryTransactionListResponse)
def get_inventory_transactions(
skip: int = Query(0, ge=0),
limit: int = Query(50, ge=1, le=200),
product_id: int | None = Query(None, description="Filter by product"),
transaction_type: str | None = Query(None, description="Filter by type"),
current_user: User = Depends(get_current_vendor_api),
db: Session = Depends(get_db),
):
"""
Get inventory transaction history for the vendor.
Returns a paginated list of all stock movements with product details.
Use filters to narrow down by product or transaction type.
"""
transactions, total = inventory_transaction_service.get_vendor_transactions(
db=db,
vendor_id=current_user.token_vendor_id,
skip=skip,
limit=limit,
product_id=product_id,
transaction_type=transaction_type,
)
return InventoryTransactionListResponse(
transactions=[InventoryTransactionWithProduct(**tx) for tx in transactions],
total=total,
skip=skip,
limit=limit,
)
@router.get(
"/inventory/transactions/product/{product_id}",
response_model=ProductTransactionHistoryResponse,
)
def get_product_transaction_history(
product_id: int,
limit: int = Query(50, ge=1, le=200),
current_user: User = Depends(get_current_vendor_api),
db: Session = Depends(get_db),
):
"""
Get transaction history for a specific product.
Returns recent stock movements with current inventory status.
"""
result = inventory_transaction_service.get_product_history(
db=db,
vendor_id=current_user.token_vendor_id,
product_id=product_id,
limit=limit,
)
return ProductTransactionHistoryResponse(**result)
@router.get(
"/inventory/transactions/order/{order_id}",
response_model=OrderTransactionHistoryResponse,
)
def get_order_transaction_history(
order_id: int,
current_user: User = Depends(get_current_vendor_api),
db: Session = Depends(get_db),
):
"""
Get all inventory transactions for a specific order.
Shows all stock movements (reserve, fulfill, release) related to an order.
"""
result = inventory_transaction_service.get_order_history(
db=db,
vendor_id=current_user.token_vendor_id,
order_id=order_id,
)
return OrderTransactionHistoryResponse(
order_id=result["order_id"],
order_number=result["order_number"],
transactions=[
InventoryTransactionWithProduct(**tx) for tx in result["transactions"]
],
)