marketplace refactoring

This commit is contained in:
2025-10-04 13:38:10 +02:00
parent 32be301d83
commit c971674ec2
68 changed files with 1102 additions and 1128 deletions

View File

@@ -22,8 +22,8 @@ from app.exceptions import (
ShopVerificationException,
AdminOperationException,
)
from models.schemas.marketplace import MarketplaceImportJobResponse
from models.database.marketplace import MarketplaceImportJob
from models.schemas.marketplace_import_job import MarketplaceImportJobResponse
from models.database.marketplace_import_job import MarketplaceImportJob
from models.database.shop import Shop
from models.database.user import User
@@ -338,5 +338,5 @@ class AdminService:
)
# Create service instance following the same pattern as product_service
# Create service instance following the same pattern as marketplace_product_service
admin_service = AdminService()

View File

@@ -1,4 +1,4 @@
# app/services/marketplace_service.py
# app/services/marketplace_import_job_service.py
"""
Marketplace service for managing import jobs and marketplace integrations.
@@ -24,16 +24,16 @@ from app.exceptions import (
ImportJobCannotBeDeletedException,
ValidationException,
)
from models.schemas.marketplace import (MarketplaceImportJobResponse,
MarketplaceImportRequest)
from models.database.marketplace import MarketplaceImportJob
from models.schemas.marketplace_import_job import (MarketplaceImportJobResponse,
MarketplaceImportJobRequest)
from models.database.marketplace_import_job import MarketplaceImportJob
from models.database.shop import Shop
from models.database.user import User
logger = logging.getLogger(__name__)
class MarketplaceService:
class MarketplaceImportJobService:
"""Service class for Marketplace operations following the application's service pattern."""
def validate_shop_access(self, db: Session, shop_code: str, user: User) -> Shop:
@@ -76,7 +76,7 @@ class MarketplaceService:
raise ValidationException("Failed to validate shop access")
def create_import_job(
self, db: Session, request: MarketplaceImportRequest, user: User
self, db: Session, request: MarketplaceImportJobRequest, user: User
) -> MarketplaceImportJob:
"""
Create a new marketplace import job.
@@ -414,4 +414,4 @@ class MarketplaceService:
# Create service instance
marketplace_service = MarketplaceService()
marketplace_import_job_service = MarketplaceImportJobService()

View File

@@ -1,9 +1,9 @@
# app/services/product_service.py
# app/services/marketplace_product_service.py
"""
Product service for managing product operations and data processing.
MarketplaceProduct service for managing product operations and data processing.
This module provides classes and functions for:
- Product CRUD operations with validation
- MarketplaceProduct CRUD operations with validation
- Advanced product filtering and search
- Stock information integration
- CSV export functionality
@@ -18,37 +18,38 @@ from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import Session
from app.exceptions import (
ProductNotFoundException,
ProductAlreadyExistsException,
InvalidProductDataException,
ProductValidationException,
MarketplaceProductNotFoundException,
MarketplaceProductAlreadyExistsException,
InvalidMarketplaceProductDataException,
MarketplaceProductValidationException,
ValidationException,
)
from models.schemas.product import ProductCreate, ProductUpdate
from app.services.marketplace_import_job_service import marketplace_import_job_service
from models.schemas.marketplace_product import MarketplaceProductCreate, MarketplaceProductUpdate
from models.schemas.stock import StockLocationResponse, StockSummaryResponse
from models.database.product import Product
from models.database.marketplace_product import MarketplaceProduct
from models.database.stock import Stock
from app.utils.data_processing import GTINProcessor, PriceProcessor
logger = logging.getLogger(__name__)
class ProductService:
"""Service class for Product operations following the application's service pattern."""
class MarketplaceProductService:
"""Service class for MarketplaceProduct operations following the application's service pattern."""
def __init__(self):
"""Class constructor."""
self.gtin_processor = GTINProcessor()
self.price_processor = PriceProcessor()
def create_product(self, db: Session, product_data: ProductCreate) -> Product:
def create_product(self, db: Session, product_data: MarketplaceProductCreate) -> MarketplaceProduct:
"""Create a new product with validation."""
try:
# Process and validate GTIN if provided
if product_data.gtin:
normalized_gtin = self.gtin_processor.normalize(product_data.gtin)
if not normalized_gtin:
raise InvalidProductDataException("Invalid GTIN format", field="gtin")
raise InvalidMarketplaceProductDataException("Invalid GTIN format", field="gtin")
product_data.gtin = normalized_gtin
# Process price if provided
@@ -62,67 +63,67 @@ class ProductService:
product_data.currency = currency
except ValueError as e:
# Convert ValueError to domain-specific exception
raise InvalidProductDataException(str(e), field="price")
raise InvalidMarketplaceProductDataException(str(e), field="price")
# Set default marketplace if not provided
if not product_data.marketplace:
product_data.marketplace = "Letzshop"
# Validate required fields
if not product_data.product_id or not product_data.product_id.strip():
raise ProductValidationException("Product ID is required", field="product_id")
if not product_data.marketplace_product_id or not product_data.marketplace_product_id.strip():
raise MarketplaceProductValidationException("MarketplaceProduct ID is required", field="marketplace_product_id")
if not product_data.title or not product_data.title.strip():
raise ProductValidationException("Product title is required", field="title")
raise MarketplaceProductValidationException("MarketplaceProduct title is required", field="title")
db_product = Product(**product_data.model_dump())
db_product = MarketplaceProduct(**product_data.model_dump())
db.add(db_product)
db.commit()
db.refresh(db_product)
logger.info(f"Created product {db_product.product_id}")
logger.info(f"Created product {db_product.marketplace_product_id}")
return db_product
except (InvalidProductDataException, ProductValidationException):
except (InvalidMarketplaceProductDataException, MarketplaceProductValidationException):
db.rollback()
raise # Re-raise custom exceptions
except IntegrityError as e:
db.rollback()
logger.error(f"Database integrity error: {str(e)}")
if "product_id" in str(e).lower() or "unique" in str(e).lower():
raise ProductAlreadyExistsException(product_data.product_id)
if "marketplace_product_id" in str(e).lower() or "unique" in str(e).lower():
raise MarketplaceProductAlreadyExistsException(product_data.marketplace_product_id)
else:
raise ProductValidationException("Data integrity constraint violation")
raise MarketplaceProductValidationException("Data integrity constraint violation")
except Exception as e:
db.rollback()
logger.error(f"Error creating product: {str(e)}")
raise ValidationException("Failed to create product")
def get_product_by_id(self, db: Session, product_id: str) -> Optional[Product]:
def get_product_by_id(self, db: Session, marketplace_product_id: str) -> Optional[MarketplaceProduct]:
"""Get a product by its ID."""
try:
return db.query(Product).filter(Product.product_id == product_id).first()
return db.query(MarketplaceProduct).filter(MarketplaceProduct.marketplace_product_id == marketplace_product_id).first()
except Exception as e:
logger.error(f"Error getting product {product_id}: {str(e)}")
logger.error(f"Error getting product {marketplace_product_id}: {str(e)}")
return None
def get_product_by_id_or_raise(self, db: Session, product_id: str) -> Product:
def get_product_by_id_or_raise(self, db: Session, marketplace_product_id: str) -> MarketplaceProduct:
"""
Get a product by its ID or raise exception.
Args:
db: Database session
product_id: Product ID to find
marketplace_product_id: MarketplaceProduct ID to find
Returns:
Product object
MarketplaceProduct object
Raises:
ProductNotFoundException: If product doesn't exist
MarketplaceProductNotFoundException: If product doesn't exist
"""
product = self.get_product_by_id(db, product_id)
product = self.get_product_by_id(db, marketplace_product_id)
if not product:
raise ProductNotFoundException(product_id)
raise MarketplaceProductNotFoundException(marketplace_product_id)
return product
def get_products_with_filters(
@@ -136,7 +137,7 @@ class ProductService:
marketplace: Optional[str] = None,
shop_name: Optional[str] = None,
search: Optional[str] = None,
) -> Tuple[List[Product], int]:
) -> Tuple[List[MarketplaceProduct], int]:
"""
Get products with filtering and pagination.
@@ -155,27 +156,27 @@ class ProductService:
Tuple of (products_list, total_count)
"""
try:
query = db.query(Product)
query = db.query(MarketplaceProduct)
# Apply filters
if brand:
query = query.filter(Product.brand.ilike(f"%{brand}%"))
query = query.filter(MarketplaceProduct.brand.ilike(f"%{brand}%"))
if category:
query = query.filter(Product.google_product_category.ilike(f"%{category}%"))
query = query.filter(MarketplaceProduct.google_product_category.ilike(f"%{category}%"))
if availability:
query = query.filter(Product.availability == availability)
query = query.filter(MarketplaceProduct.availability == availability)
if marketplace:
query = query.filter(Product.marketplace.ilike(f"%{marketplace}%"))
query = query.filter(MarketplaceProduct.marketplace.ilike(f"%{marketplace}%"))
if shop_name:
query = query.filter(Product.shop_name.ilike(f"%{shop_name}%"))
query = query.filter(MarketplaceProduct.shop_name.ilike(f"%{shop_name}%"))
if search:
# Search in title, description, marketplace, and shop_name
search_term = f"%{search}%"
query = query.filter(
(Product.title.ilike(search_term))
| (Product.description.ilike(search_term))
| (Product.marketplace.ilike(search_term))
| (Product.shop_name.ilike(search_term))
(MarketplaceProduct.title.ilike(search_term))
| (MarketplaceProduct.description.ilike(search_term))
| (MarketplaceProduct.marketplace.ilike(search_term))
| (MarketplaceProduct.shop_name.ilike(search_term))
)
total = query.count()
@@ -187,10 +188,10 @@ class ProductService:
logger.error(f"Error getting products with filters: {str(e)}")
raise ValidationException("Failed to retrieve products")
def update_product(self, db: Session, product_id: str, product_update: ProductUpdate) -> Product:
def update_product(self, db: Session, marketplace_product_id: str, product_update: MarketplaceProductUpdate) -> MarketplaceProduct:
"""Update product with validation."""
try:
product = self.get_product_by_id_or_raise(db, product_id)
product = self.get_product_by_id_or_raise(db, marketplace_product_id)
# Update fields
update_data = product_update.model_dump(exclude_unset=True)
@@ -199,7 +200,7 @@ class ProductService:
if "gtin" in update_data and update_data["gtin"]:
normalized_gtin = self.gtin_processor.normalize(update_data["gtin"])
if not normalized_gtin:
raise InvalidProductDataException("Invalid GTIN format", field="gtin")
raise InvalidMarketplaceProductDataException("Invalid GTIN format", field="gtin")
update_data["gtin"] = normalized_gtin
# Process price if being updated
@@ -213,11 +214,11 @@ class ProductService:
update_data["currency"] = currency
except ValueError as e:
# Convert ValueError to domain-specific exception
raise InvalidProductDataException(str(e), field="price")
raise InvalidMarketplaceProductDataException(str(e), field="price")
# Validate required fields if being updated
if "title" in update_data and (not update_data["title"] or not update_data["title"].strip()):
raise ProductValidationException("Product title cannot be empty", field="title")
raise MarketplaceProductValidationException("MarketplaceProduct title cannot be empty", field="title")
for key, value in update_data.items():
setattr(product, key, value)
@@ -226,33 +227,33 @@ class ProductService:
db.commit()
db.refresh(product)
logger.info(f"Updated product {product_id}")
logger.info(f"Updated product {marketplace_product_id}")
return product
except (ProductNotFoundException, InvalidProductDataException, ProductValidationException):
except (MarketplaceProductNotFoundException, InvalidMarketplaceProductDataException, MarketplaceProductValidationException):
db.rollback()
raise # Re-raise custom exceptions
except Exception as e:
db.rollback()
logger.error(f"Error updating product {product_id}: {str(e)}")
logger.error(f"Error updating product {marketplace_product_id}: {str(e)}")
raise ValidationException("Failed to update product")
def delete_product(self, db: Session, product_id: str) -> bool:
def delete_product(self, db: Session, marketplace_product_id: str) -> bool:
"""
Delete product and associated stock.
Args:
db: Database session
product_id: Product ID to delete
marketplace_product_id: MarketplaceProduct ID to delete
Returns:
True if deletion successful
Raises:
ProductNotFoundException: If product doesn't exist
MarketplaceProductNotFoundException: If product doesn't exist
"""
try:
product = self.get_product_by_id_or_raise(db, product_id)
product = self.get_product_by_id_or_raise(db, marketplace_product_id)
# Delete associated stock entries if GTIN exists
if product.gtin:
@@ -261,14 +262,14 @@ class ProductService:
db.delete(product)
db.commit()
logger.info(f"Deleted product {product_id}")
logger.info(f"Deleted product {marketplace_product_id}")
return True
except ProductNotFoundException:
except MarketplaceProductNotFoundException:
raise # Re-raise custom exceptions
except Exception as e:
db.rollback()
logger.error(f"Error deleting product {product_id}: {str(e)}")
logger.error(f"Error deleting product {marketplace_product_id}: {str(e)}")
raise ValidationException("Failed to delete product")
def get_stock_info(self, db: Session, gtin: str) -> Optional[StockSummaryResponse]:
@@ -330,7 +331,7 @@ class ProductService:
# Write header row
headers = [
"product_id", "title", "description", "link", "image_link",
"marketplace_product_id", "title", "description", "link", "image_link",
"availability", "price", "currency", "brand", "gtin",
"marketplace", "shop_name"
]
@@ -345,13 +346,13 @@ class ProductService:
offset = 0
while True:
query = db.query(Product)
query = db.query(MarketplaceProduct)
# Apply marketplace filters
if marketplace:
query = query.filter(Product.marketplace.ilike(f"%{marketplace}%"))
query = query.filter(MarketplaceProduct.marketplace.ilike(f"%{marketplace}%"))
if shop_name:
query = query.filter(Product.shop_name.ilike(f"%{shop_name}%"))
query = query.filter(MarketplaceProduct.shop_name.ilike(f"%{shop_name}%"))
products = query.offset(offset).limit(batch_size).all()
if not products:
@@ -360,7 +361,7 @@ class ProductService:
for product in products:
# Create CSV row with proper escaping
row_data = [
product.product_id or "",
product.marketplace_product_id or "",
product.title or "",
product.description or "",
product.link or "",
@@ -387,11 +388,11 @@ class ProductService:
logger.error(f"Error generating CSV export: {str(e)}")
raise ValidationException("Failed to generate CSV export")
def product_exists(self, db: Session, product_id: str) -> bool:
def product_exists(self, db: Session, marketplace_product_id: str) -> bool:
"""Check if product exists by ID."""
try:
return (
db.query(Product).filter(Product.product_id == product_id).first()
db.query(MarketplaceProduct).filter(MarketplaceProduct.marketplace_product_id == marketplace_product_id).first()
is not None
)
except Exception as e:
@@ -401,18 +402,18 @@ class ProductService:
# Private helper methods
def _validate_product_data(self, product_data: dict) -> None:
"""Validate product data structure."""
required_fields = ['product_id', 'title']
required_fields = ['marketplace_product_id', 'title']
for field in required_fields:
if field not in product_data or not product_data[field]:
raise ProductValidationException(f"{field} is required", field=field)
raise MarketplaceProductValidationException(f"{field} is required", field=field)
def _normalize_product_data(self, product_data: dict) -> dict:
"""Normalize and clean product data."""
normalized = product_data.copy()
# Trim whitespace from string fields
string_fields = ['product_id', 'title', 'description', 'brand', 'marketplace', 'shop_name']
string_fields = ['marketplace_product_id', 'title', 'description', 'brand', 'marketplace', 'shop_name']
for field in string_fields:
if field in normalized and normalized[field]:
normalized[field] = normalized[field].strip()
@@ -421,4 +422,4 @@ class ProductService:
# Create service instance
product_service = ProductService()
marketplace_product_service = MarketplaceProductService()

View File

@@ -20,13 +20,13 @@ from app.exceptions import (
ShopAlreadyExistsException,
UnauthorizedShopAccessException,
InvalidShopDataException,
ProductNotFoundException,
MarketplaceProductNotFoundException,
ShopProductAlreadyExistsException,
MaxShopsReachedException,
ValidationException,
)
from models.schemas.shop import ShopCreate, ShopProductCreate
from models.database.product import Product
from models.database.marketplace_product import MarketplaceProduct
from models.database.shop import Shop, ShopProduct
from models.database.user import User
@@ -198,22 +198,22 @@ class ShopService:
Created ShopProduct object
Raises:
ProductNotFoundException: If product not found
MarketplaceProductNotFoundException: If product not found
ShopProductAlreadyExistsException: If product already in shop
"""
try:
# Check if product exists
product = self._get_product_by_id_or_raise(db, shop_product.product_id)
marketplace_product = self._get_product_by_id_or_raise(db, shop_product.marketplace_product_id)
# Check if product already in shop
if self._product_in_shop(db, shop.id, product.id):
raise ShopProductAlreadyExistsException(shop.shop_code, shop_product.product_id)
if self._product_in_shop(db, shop.id, marketplace_product.id):
raise ShopProductAlreadyExistsException(shop.shop_code, shop_product.marketplace_product_id)
# Create shop-product association
new_shop_product = ShopProduct(
shop_id=shop.id,
product_id=product.id,
**shop_product.model_dump(exclude={"product_id"}),
marketplace_product_id=marketplace_product.id,
**shop_product.model_dump(exclude={"marketplace_product_id"}),
)
db.add(new_shop_product)
@@ -223,10 +223,10 @@ class ShopService:
# Load the product relationship
db.refresh(new_shop_product)
logger.info(f"Product {shop_product.product_id} added to shop {shop.shop_code}")
logger.info(f"MarketplaceProduct {shop_product.marketplace_product_id} added to shop {shop.shop_code}")
return new_shop_product
except (ProductNotFoundException, ShopProductAlreadyExistsException):
except (MarketplaceProductNotFoundException, ShopProductAlreadyExistsException):
db.rollback()
raise # Re-raise custom exceptions
except Exception as e:
@@ -322,20 +322,20 @@ class ShopService:
.first() is not None
)
def _get_product_by_id_or_raise(self, db: Session, product_id: str) -> Product:
def _get_product_by_id_or_raise(self, db: Session, marketplace_product_id: str) -> MarketplaceProduct:
"""Get product by ID or raise exception."""
product = db.query(Product).filter(Product.product_id == product_id).first()
product = db.query(MarketplaceProduct).filter(MarketplaceProduct.marketplace_product_id == marketplace_product_id).first()
if not product:
raise ProductNotFoundException(product_id)
raise MarketplaceProductNotFoundException(marketplace_product_id)
return product
def _product_in_shop(self, db: Session, shop_id: int, product_id: int) -> bool:
def _product_in_shop(self, db: Session, shop_id: int, marketplace_product_id: int) -> bool:
"""Check if product is already in shop."""
return (
db.query(ShopProduct)
.filter(
ShopProduct.shop_id == shop_id,
ShopProduct.product_id == product_id
ShopProduct.marketplace_product_id == marketplace_product_id
)
.first() is not None
)

View File

@@ -16,7 +16,7 @@ from sqlalchemy import func
from sqlalchemy.orm import Session
from app.exceptions import ValidationException
from models.database.product import Product
from models.database.marketplace_product import MarketplaceProduct
from models.database.stock import Stock
logger = logging.getLogger(__name__)
@@ -85,13 +85,13 @@ class StatsService:
# Query to get stats per marketplace
marketplace_stats = (
db.query(
Product.marketplace,
func.count(Product.id).label("total_products"),
func.count(func.distinct(Product.shop_name)).label("unique_shops"),
func.count(func.distinct(Product.brand)).label("unique_brands"),
MarketplaceProduct.marketplace,
func.count(MarketplaceProduct.id).label("total_products"),
func.count(func.distinct(MarketplaceProduct.shop_name)).label("unique_shops"),
func.count(func.distinct(MarketplaceProduct.brand)).label("unique_brands"),
)
.filter(Product.marketplace.isnot(None))
.group_by(Product.marketplace)
.filter(MarketplaceProduct.marketplace.isnot(None))
.group_by(MarketplaceProduct.marketplace)
.all()
)
@@ -195,13 +195,13 @@ class StatsService:
# Private helper methods
def _get_product_count(self, db: Session) -> int:
"""Get total product count."""
return db.query(Product).count()
return db.query(MarketplaceProduct).count()
def _get_unique_brands_count(self, db: Session) -> int:
"""Get count of unique brands."""
return (
db.query(Product.brand)
.filter(Product.brand.isnot(None), Product.brand != "")
db.query(MarketplaceProduct.brand)
.filter(MarketplaceProduct.brand.isnot(None), MarketplaceProduct.brand != "")
.distinct()
.count()
)
@@ -209,10 +209,10 @@ class StatsService:
def _get_unique_categories_count(self, db: Session) -> int:
"""Get count of unique categories."""
return (
db.query(Product.google_product_category)
db.query(MarketplaceProduct.google_product_category)
.filter(
Product.google_product_category.isnot(None),
Product.google_product_category != "",
MarketplaceProduct.google_product_category.isnot(None),
MarketplaceProduct.google_product_category != "",
)
.distinct()
.count()
@@ -221,8 +221,8 @@ class StatsService:
def _get_unique_marketplaces_count(self, db: Session) -> int:
"""Get count of unique marketplaces."""
return (
db.query(Product.marketplace)
.filter(Product.marketplace.isnot(None), Product.marketplace != "")
db.query(MarketplaceProduct.marketplace)
.filter(MarketplaceProduct.marketplace.isnot(None), MarketplaceProduct.marketplace != "")
.distinct()
.count()
)
@@ -230,8 +230,8 @@ class StatsService:
def _get_unique_shops_count(self, db: Session) -> int:
"""Get count of unique shops."""
return (
db.query(Product.shop_name)
.filter(Product.shop_name.isnot(None), Product.shop_name != "")
db.query(MarketplaceProduct.shop_name)
.filter(MarketplaceProduct.shop_name.isnot(None), MarketplaceProduct.shop_name != "")
.distinct()
.count()
)
@@ -239,16 +239,16 @@ class StatsService:
def _get_products_with_gtin_count(self, db: Session) -> int:
"""Get count of products with GTIN."""
return (
db.query(Product)
.filter(Product.gtin.isnot(None), Product.gtin != "")
db.query(MarketplaceProduct)
.filter(MarketplaceProduct.gtin.isnot(None), MarketplaceProduct.gtin != "")
.count()
)
def _get_products_with_images_count(self, db: Session) -> int:
"""Get count of products with images."""
return (
db.query(Product)
.filter(Product.image_link.isnot(None), Product.image_link != "")
db.query(MarketplaceProduct)
.filter(MarketplaceProduct.image_link.isnot(None), MarketplaceProduct.image_link != "")
.count()
)
@@ -265,11 +265,11 @@ class StatsService:
def _get_brands_by_marketplace(self, db: Session, marketplace: str) -> List[str]:
"""Get unique brands for a specific marketplace."""
brands = (
db.query(Product.brand)
db.query(MarketplaceProduct.brand)
.filter(
Product.marketplace == marketplace,
Product.brand.isnot(None),
Product.brand != "",
MarketplaceProduct.marketplace == marketplace,
MarketplaceProduct.brand.isnot(None),
MarketplaceProduct.brand != "",
)
.distinct()
.all()
@@ -279,11 +279,11 @@ class StatsService:
def _get_shops_by_marketplace(self, db: Session, marketplace: str) -> List[str]:
"""Get unique shops for a specific marketplace."""
shops = (
db.query(Product.shop_name)
db.query(MarketplaceProduct.shop_name)
.filter(
Product.marketplace == marketplace,
Product.shop_name.isnot(None),
Product.shop_name != "",
MarketplaceProduct.marketplace == marketplace,
MarketplaceProduct.shop_name.isnot(None),
MarketplaceProduct.shop_name != "",
)
.distinct()
.all()
@@ -292,7 +292,7 @@ class StatsService:
def _get_products_by_marketplace_count(self, db: Session, marketplace: str) -> int:
"""Get product count for a specific marketplace."""
return db.query(Product).filter(Product.marketplace == marketplace).count()
return db.query(MarketplaceProduct).filter(MarketplaceProduct.marketplace == marketplace).count()
# Create service instance following the same pattern as other services
stats_service = StatsService()

View File

@@ -26,7 +26,7 @@ from app.exceptions import (
)
from models.schemas.stock import (StockAdd, StockCreate, StockLocationResponse,
StockSummaryResponse, StockUpdate)
from models.database.product import Product
from models.database.marketplace_product import MarketplaceProduct
from models.database.stock import Stock
from app.utils.data_processing import GTINProcessor
@@ -261,7 +261,7 @@ class StockService:
)
# Try to get product title for reference
product = db.query(Product).filter(Product.gtin == normalized_gtin).first()
product = db.query(MarketplaceProduct).filter(MarketplaceProduct.gtin == normalized_gtin).first()
product_title = product.title if product else None
return StockSummaryResponse(
@@ -304,7 +304,7 @@ class StockService:
total_quantity = sum(entry.quantity for entry in total_stock)
# Get product info for context
product = db.query(Product).filter(Product.gtin == normalized_gtin).first()
product = db.query(MarketplaceProduct).filter(MarketplaceProduct.gtin == normalized_gtin).first()
return {
"gtin": normalized_gtin,
@@ -491,14 +491,14 @@ class StockService:
low_stock_items = []
for entry in low_stock_entries:
# Get product info if available
product = db.query(Product).filter(Product.gtin == entry.gtin).first()
product = db.query(MarketplaceProduct).filter(MarketplaceProduct.gtin == entry.gtin).first()
low_stock_items.append({
"gtin": entry.gtin,
"location": entry.location,
"current_quantity": entry.quantity,
"product_title": product.title if product else None,
"product_id": product.product_id if product else None,
"marketplace_product_id": product.marketplace_product_id if product else None,
})
return low_stock_items