refactor(cleanup): delete legacy storefront routes, convert cart to re-exports
Phase 6 of storefront restructure plan - delete legacy files and convert remaining cart files to re-exports. Deleted legacy storefront route files (now served from modules): - app/api/v1/storefront/auth.py (→ customers module) - app/api/v1/storefront/profile.py (→ customers module) - app/api/v1/storefront/addresses.py (→ customers module) - app/api/v1/storefront/carts.py (→ cart module) - app/api/v1/storefront/products.py (→ catalog module) - app/api/v1/storefront/orders.py (→ orders/checkout modules) - app/api/v1/storefront/messages.py (→ messaging module) Converted legacy cart files to re-exports: - models/schema/cart.py → app.modules.cart.schemas - models/database/cart.py → app.modules.cart.models - app/services/cart_service.py → app.modules.cart.services This reduces API-007 violations from 81 to 69 (remaining violations are in admin/vendor routes - separate migration effort). Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,453 +1,13 @@
|
||||
# app/services/cart_service.py
|
||||
"""
|
||||
Shopping cart service.
|
||||
LEGACY LOCATION - This file re-exports from the canonical module location.
|
||||
|
||||
This module provides:
|
||||
- Session-based cart management
|
||||
- Cart item operations (add, update, remove)
|
||||
- Cart total calculations
|
||||
Canonical location: app/modules/cart/services/
|
||||
|
||||
All monetary calculations use integer cents internally for precision.
|
||||
See docs/architecture/money-handling.md for details.
|
||||
This file exists for backward compatibility. New code should import from:
|
||||
from app.modules.cart.services import cart_service
|
||||
"""
|
||||
|
||||
import logging
|
||||
from app.modules.cart.services.cart_service import cart_service, CartService
|
||||
|
||||
from sqlalchemy import and_
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.exceptions import (
|
||||
CartItemNotFoundException,
|
||||
InsufficientInventoryForCartException,
|
||||
InvalidCartQuantityException,
|
||||
ProductNotFoundException,
|
||||
)
|
||||
from app.utils.money import cents_to_euros
|
||||
from models.database.cart import CartItem
|
||||
from models.database.product import Product
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class CartService:
|
||||
"""Service for managing shopping carts."""
|
||||
|
||||
def get_cart(self, db: Session, vendor_id: int, session_id: str) -> dict:
|
||||
"""
|
||||
Get cart contents for a session.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
vendor_id: Vendor ID
|
||||
session_id: Session ID
|
||||
|
||||
Returns:
|
||||
Cart data with items and totals
|
||||
"""
|
||||
logger.info(
|
||||
"[CART_SERVICE] get_cart called",
|
||||
extra={
|
||||
"vendor_id": vendor_id,
|
||||
"session_id": session_id,
|
||||
},
|
||||
)
|
||||
|
||||
# Fetch cart items from database
|
||||
cart_items = (
|
||||
db.query(CartItem)
|
||||
.filter(
|
||||
and_(CartItem.vendor_id == vendor_id, CartItem.session_id == session_id)
|
||||
)
|
||||
.all()
|
||||
)
|
||||
|
||||
logger.info(
|
||||
f"[CART_SERVICE] Found {len(cart_items)} items in database",
|
||||
extra={"item_count": len(cart_items)},
|
||||
)
|
||||
|
||||
# Build response - calculate totals in cents, return euros
|
||||
items = []
|
||||
subtotal_cents = 0
|
||||
|
||||
for cart_item in cart_items:
|
||||
product = cart_item.product
|
||||
line_total_cents = cart_item.line_total_cents
|
||||
|
||||
items.append(
|
||||
{
|
||||
"product_id": product.id,
|
||||
"product_name": product.marketplace_product.get_title("en")
|
||||
if product.marketplace_product
|
||||
else str(product.id),
|
||||
"quantity": cart_item.quantity,
|
||||
"price": cart_item.price_at_add, # Returns euros via property
|
||||
"line_total": cents_to_euros(line_total_cents),
|
||||
"image_url": (
|
||||
product.marketplace_product.image_link
|
||||
if product.marketplace_product
|
||||
else None
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
subtotal_cents += line_total_cents
|
||||
|
||||
# Convert to euros for API response
|
||||
subtotal = cents_to_euros(subtotal_cents)
|
||||
cart_data = {
|
||||
"vendor_id": vendor_id,
|
||||
"session_id": session_id,
|
||||
"items": items,
|
||||
"subtotal": subtotal,
|
||||
"total": subtotal, # Could add tax/shipping later
|
||||
}
|
||||
|
||||
logger.info(
|
||||
f"[CART_SERVICE] get_cart returning: {len(cart_data['items'])} items, total: {cart_data['total']}",
|
||||
extra={"cart": cart_data},
|
||||
)
|
||||
|
||||
return cart_data
|
||||
|
||||
def add_to_cart(
|
||||
self,
|
||||
db: Session,
|
||||
vendor_id: int,
|
||||
session_id: str,
|
||||
product_id: int,
|
||||
quantity: int = 1,
|
||||
) -> dict:
|
||||
"""
|
||||
Add product to cart.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
vendor_id: Vendor ID
|
||||
session_id: Session ID
|
||||
product_id: Product ID
|
||||
quantity: Quantity to add
|
||||
|
||||
Returns:
|
||||
Updated cart
|
||||
|
||||
Raises:
|
||||
ProductNotFoundException: If product not found
|
||||
InsufficientInventoryException: If not enough inventory
|
||||
"""
|
||||
logger.info(
|
||||
"[CART_SERVICE] add_to_cart called",
|
||||
extra={
|
||||
"vendor_id": vendor_id,
|
||||
"session_id": session_id,
|
||||
"product_id": product_id,
|
||||
"quantity": quantity,
|
||||
},
|
||||
)
|
||||
|
||||
# Verify product exists and belongs to vendor
|
||||
product = (
|
||||
db.query(Product)
|
||||
.filter(
|
||||
and_(
|
||||
Product.id == product_id,
|
||||
Product.vendor_id == vendor_id,
|
||||
Product.is_active == True,
|
||||
)
|
||||
)
|
||||
.first()
|
||||
)
|
||||
|
||||
if not product:
|
||||
logger.error(
|
||||
"[CART_SERVICE] Product not found",
|
||||
extra={"product_id": product_id, "vendor_id": vendor_id},
|
||||
)
|
||||
raise ProductNotFoundException(product_id=product_id, vendor_id=vendor_id)
|
||||
|
||||
logger.info(
|
||||
f"[CART_SERVICE] Product found: {product.marketplace_product.title}",
|
||||
extra={
|
||||
"product_id": product_id,
|
||||
"product_name": product.marketplace_product.title,
|
||||
"available_inventory": product.available_inventory,
|
||||
},
|
||||
)
|
||||
|
||||
# Get current price in cents (use sale_price if available, otherwise regular price)
|
||||
current_price_cents = (
|
||||
product.sale_price_cents
|
||||
or product.price_cents
|
||||
or 0
|
||||
)
|
||||
|
||||
# Check if item already exists in cart
|
||||
existing_item = (
|
||||
db.query(CartItem)
|
||||
.filter(
|
||||
and_(
|
||||
CartItem.vendor_id == vendor_id,
|
||||
CartItem.session_id == session_id,
|
||||
CartItem.product_id == product_id,
|
||||
)
|
||||
)
|
||||
.first()
|
||||
)
|
||||
|
||||
if existing_item:
|
||||
# Update quantity
|
||||
new_quantity = existing_item.quantity + quantity
|
||||
|
||||
# Check inventory for new total quantity
|
||||
if product.available_inventory < new_quantity:
|
||||
logger.warning(
|
||||
"[CART_SERVICE] Insufficient inventory for update",
|
||||
extra={
|
||||
"product_id": product_id,
|
||||
"current_in_cart": existing_item.quantity,
|
||||
"adding": quantity,
|
||||
"requested_total": new_quantity,
|
||||
"available": product.available_inventory,
|
||||
},
|
||||
)
|
||||
raise InsufficientInventoryForCartException(
|
||||
product_id=product_id,
|
||||
product_name=product.marketplace_product.title,
|
||||
requested=new_quantity,
|
||||
available=product.available_inventory,
|
||||
)
|
||||
|
||||
existing_item.quantity = new_quantity
|
||||
db.flush()
|
||||
db.refresh(existing_item)
|
||||
|
||||
logger.info(
|
||||
"[CART_SERVICE] Updated existing cart item",
|
||||
extra={"cart_item_id": existing_item.id, "new_quantity": new_quantity},
|
||||
)
|
||||
|
||||
return {
|
||||
"message": "Product quantity updated in cart",
|
||||
"product_id": product_id,
|
||||
"quantity": new_quantity,
|
||||
}
|
||||
# Check inventory for new item
|
||||
if product.available_inventory < quantity:
|
||||
logger.warning(
|
||||
"[CART_SERVICE] Insufficient inventory",
|
||||
extra={
|
||||
"product_id": product_id,
|
||||
"requested": quantity,
|
||||
"available": product.available_inventory,
|
||||
},
|
||||
)
|
||||
raise InsufficientInventoryForCartException(
|
||||
product_id=product_id,
|
||||
product_name=product.marketplace_product.title,
|
||||
requested=quantity,
|
||||
available=product.available_inventory,
|
||||
)
|
||||
|
||||
# Create new cart item (price stored in cents)
|
||||
cart_item = CartItem(
|
||||
vendor_id=vendor_id,
|
||||
session_id=session_id,
|
||||
product_id=product_id,
|
||||
quantity=quantity,
|
||||
price_at_add_cents=current_price_cents,
|
||||
)
|
||||
db.add(cart_item)
|
||||
db.flush()
|
||||
db.refresh(cart_item)
|
||||
|
||||
logger.info(
|
||||
"[CART_SERVICE] Created new cart item",
|
||||
extra={
|
||||
"cart_item_id": cart_item.id,
|
||||
"quantity": quantity,
|
||||
"price_cents": current_price_cents,
|
||||
},
|
||||
)
|
||||
|
||||
return {
|
||||
"message": "Product added to cart",
|
||||
"product_id": product_id,
|
||||
"quantity": quantity,
|
||||
}
|
||||
|
||||
def update_cart_item(
|
||||
self,
|
||||
db: Session,
|
||||
vendor_id: int,
|
||||
session_id: str,
|
||||
product_id: int,
|
||||
quantity: int,
|
||||
) -> dict:
|
||||
"""
|
||||
Update quantity of item in cart.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
vendor_id: Vendor ID
|
||||
session_id: Session ID
|
||||
product_id: Product ID
|
||||
quantity: New quantity (must be >= 1)
|
||||
|
||||
Returns:
|
||||
Success message
|
||||
|
||||
Raises:
|
||||
ValidationException: If quantity < 1
|
||||
ProductNotFoundException: If product not found
|
||||
InsufficientInventoryException: If not enough inventory
|
||||
"""
|
||||
if quantity < 1:
|
||||
raise InvalidCartQuantityException(quantity=quantity, min_quantity=1)
|
||||
|
||||
# Find cart item
|
||||
cart_item = (
|
||||
db.query(CartItem)
|
||||
.filter(
|
||||
and_(
|
||||
CartItem.vendor_id == vendor_id,
|
||||
CartItem.session_id == session_id,
|
||||
CartItem.product_id == product_id,
|
||||
)
|
||||
)
|
||||
.first()
|
||||
)
|
||||
|
||||
if not cart_item:
|
||||
raise CartItemNotFoundException(
|
||||
product_id=product_id, session_id=session_id
|
||||
)
|
||||
|
||||
# Verify product still exists and is active
|
||||
product = (
|
||||
db.query(Product)
|
||||
.filter(
|
||||
and_(
|
||||
Product.id == product_id,
|
||||
Product.vendor_id == vendor_id,
|
||||
Product.is_active == True,
|
||||
)
|
||||
)
|
||||
.first()
|
||||
)
|
||||
|
||||
if not product:
|
||||
raise ProductNotFoundException(str(product_id))
|
||||
|
||||
# Check inventory
|
||||
if product.available_inventory < quantity:
|
||||
raise InsufficientInventoryForCartException(
|
||||
product_id=product_id,
|
||||
product_name=product.marketplace_product.title,
|
||||
requested=quantity,
|
||||
available=product.available_inventory,
|
||||
)
|
||||
|
||||
# Update quantity
|
||||
cart_item.quantity = quantity
|
||||
db.flush()
|
||||
db.refresh(cart_item)
|
||||
|
||||
logger.info(
|
||||
"[CART_SERVICE] Updated cart item quantity",
|
||||
extra={
|
||||
"cart_item_id": cart_item.id,
|
||||
"product_id": product_id,
|
||||
"new_quantity": quantity,
|
||||
},
|
||||
)
|
||||
|
||||
return {
|
||||
"message": "Cart updated",
|
||||
"product_id": product_id,
|
||||
"quantity": quantity,
|
||||
}
|
||||
|
||||
def remove_from_cart(
|
||||
self, db: Session, vendor_id: int, session_id: str, product_id: int
|
||||
) -> dict:
|
||||
"""
|
||||
Remove item from cart.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
vendor_id: Vendor ID
|
||||
session_id: Session ID
|
||||
product_id: Product ID
|
||||
|
||||
Returns:
|
||||
Success message
|
||||
|
||||
Raises:
|
||||
ProductNotFoundException: If product not in cart
|
||||
"""
|
||||
# Find and delete cart item
|
||||
cart_item = (
|
||||
db.query(CartItem)
|
||||
.filter(
|
||||
and_(
|
||||
CartItem.vendor_id == vendor_id,
|
||||
CartItem.session_id == session_id,
|
||||
CartItem.product_id == product_id,
|
||||
)
|
||||
)
|
||||
.first()
|
||||
)
|
||||
|
||||
if not cart_item:
|
||||
raise CartItemNotFoundException(
|
||||
product_id=product_id, session_id=session_id
|
||||
)
|
||||
|
||||
db.delete(cart_item)
|
||||
|
||||
logger.info(
|
||||
"[CART_SERVICE] Removed item from cart",
|
||||
extra={
|
||||
"cart_item_id": cart_item.id,
|
||||
"product_id": product_id,
|
||||
"session_id": session_id,
|
||||
},
|
||||
)
|
||||
|
||||
return {"message": "Item removed from cart", "product_id": product_id}
|
||||
|
||||
def clear_cart(self, db: Session, vendor_id: int, session_id: str) -> dict:
|
||||
"""
|
||||
Clear all items from cart.
|
||||
|
||||
Args:
|
||||
db: Database session
|
||||
vendor_id: Vendor ID
|
||||
session_id: Session ID
|
||||
|
||||
Returns:
|
||||
Success message with count of items removed
|
||||
"""
|
||||
# Delete all cart items for this session
|
||||
deleted_count = (
|
||||
db.query(CartItem)
|
||||
.filter(
|
||||
and_(CartItem.vendor_id == vendor_id, CartItem.session_id == session_id)
|
||||
)
|
||||
.delete()
|
||||
)
|
||||
|
||||
logger.info(
|
||||
"[CART_SERVICE] Cleared cart",
|
||||
extra={
|
||||
"session_id": session_id,
|
||||
"vendor_id": vendor_id,
|
||||
"items_removed": deleted_count,
|
||||
},
|
||||
)
|
||||
|
||||
return {"message": "Cart cleared", "items_removed": deleted_count}
|
||||
|
||||
|
||||
# Create service instance
|
||||
cart_service = CartService()
|
||||
__all__ = ["cart_service", "CartService"]
|
||||
|
||||
Reference in New Issue
Block a user