marketplace refactoring
This commit is contained in:
@@ -9,7 +9,7 @@ This module provides classes and functions for:
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
from app.api.v1 import admin, auth, marketplace, product, shop, stats, stock
|
||||
from app.api.v1 import admin, auth, marketplace, shop, stats, stock
|
||||
|
||||
api_router = APIRouter()
|
||||
|
||||
@@ -17,7 +17,6 @@ api_router = APIRouter()
|
||||
api_router.include_router(admin.router, tags=["admin"])
|
||||
api_router.include_router(auth.router, tags=["authentication"])
|
||||
api_router.include_router(marketplace.router, tags=["marketplace"])
|
||||
api_router.include_router(product.router, tags=["product"])
|
||||
api_router.include_router(shop.router, tags=["shop"])
|
||||
api_router.include_router(stats.router, tags=["statistics"])
|
||||
api_router.include_router(stock.router, tags=["stock"])
|
||||
|
||||
@@ -19,7 +19,7 @@ from app.api.deps import get_current_admin_user
|
||||
from app.core.database import get_db
|
||||
from app.services.admin_service import admin_service
|
||||
from models.schemas.auth import UserResponse
|
||||
from models.schemas.marketplace import MarketplaceImportJobResponse
|
||||
from models.schemas.marketplace_import_job import MarketplaceImportJobResponse
|
||||
from models.schemas.shop import ShopListResponse
|
||||
from models.database.user import User
|
||||
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
# app/api/v1/marketplace.py
|
||||
# app/api/v1/marketplace_products.py
|
||||
"""
|
||||
Marketplace endpoints - simplified with service-level exception handling.
|
||||
MarketplaceProduct endpoints - simplified with service-level exception handling.
|
||||
|
||||
This module provides classes and functions for:
|
||||
- Product import from marketplace CSV files
|
||||
- MarketplaceProduct CRUD operations with marketplace support
|
||||
- Advanced product filtering and search
|
||||
- MarketplaceProduct export functionality
|
||||
- MarketplaceProduct import from marketplace CSV files
|
||||
- Import job management and monitoring
|
||||
- Import statistics and job cancellation
|
||||
"""
|
||||
@@ -12,25 +15,151 @@ import logging
|
||||
from typing import List, Optional
|
||||
|
||||
from fastapi import APIRouter, BackgroundTasks, Depends, Query
|
||||
from fastapi.responses import StreamingResponse
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_current_user
|
||||
from app.core.database import get_db
|
||||
from app.services.marketplace_service import marketplace_service
|
||||
from app.services.marketplace_import_job_service import marketplace_import_job_service
|
||||
from app.services.marketplace_product_service import marketplace_product_service
|
||||
from app.tasks.background_tasks import process_marketplace_import
|
||||
from middleware.decorators import rate_limit
|
||||
from models.schemas.marketplace import (MarketplaceImportJobResponse,
|
||||
MarketplaceImportRequest)
|
||||
from models.schemas.marketplace_import_job import (MarketplaceImportJobResponse,
|
||||
MarketplaceImportJobRequest)
|
||||
from models.schemas.marketplace_product import (MarketplaceProductCreate, MarketplaceProductDetailResponse,
|
||||
MarketplaceProductListResponse, MarketplaceProductResponse,
|
||||
MarketplaceProductUpdate)
|
||||
from models.database.user import User
|
||||
|
||||
router = APIRouter()
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# PRODUCT ENDPOINTS
|
||||
# ============================================================================
|
||||
|
||||
@router.get("/marketplace/product/export-csv")
|
||||
async def export_csv(
|
||||
marketplace: Optional[str] = Query(None, description="Filter by marketplace"),
|
||||
shop_name: Optional[str] = Query(None, description="Filter by shop name"),
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Export products as CSV with streaming and marketplace filtering (Protected)."""
|
||||
|
||||
def generate_csv():
|
||||
return marketplace_product_service.generate_csv_export(
|
||||
db=db, marketplace=marketplace, shop_name=shop_name
|
||||
)
|
||||
|
||||
filename = "marketplace_products_export"
|
||||
if marketplace:
|
||||
filename += f"_{marketplace}"
|
||||
if shop_name:
|
||||
filename += f"_{shop_name}"
|
||||
filename += ".csv"
|
||||
|
||||
return StreamingResponse(
|
||||
generate_csv(),
|
||||
media_type="text/csv",
|
||||
headers={"Content-Disposition": f"attachment; filename={filename}"},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/marketplace/product", response_model=MarketplaceProductListResponse)
|
||||
def get_products(
|
||||
skip: int = Query(0, ge=0),
|
||||
limit: int = Query(100, ge=1, le=1000),
|
||||
brand: Optional[str] = Query(None),
|
||||
category: Optional[str] = Query(None),
|
||||
availability: Optional[str] = Query(None),
|
||||
marketplace: Optional[str] = Query(None, description="Filter by marketplace"),
|
||||
shop_name: Optional[str] = Query(None, description="Filter by shop name"),
|
||||
search: Optional[str] = Query(None),
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Get products with advanced filtering including marketplace and shop (Protected)."""
|
||||
products, total = marketplace_product_service.get_products_with_filters(
|
||||
db=db,
|
||||
skip=skip,
|
||||
limit=limit,
|
||||
brand=brand,
|
||||
category=category,
|
||||
availability=availability,
|
||||
marketplace=marketplace,
|
||||
shop_name=shop_name,
|
||||
search=search,
|
||||
)
|
||||
|
||||
return MarketplaceProductListResponse(
|
||||
products=products, total=total, skip=skip, limit=limit
|
||||
)
|
||||
|
||||
|
||||
@router.post("/marketplace/product", response_model=MarketplaceProductResponse)
|
||||
def create_product(
|
||||
product: MarketplaceProductCreate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Create a new product with validation and marketplace support (Protected)."""
|
||||
logger.info(f"Starting product creation for ID: {product.marketplace_product_id}")
|
||||
|
||||
db_product = marketplace_product_service.create_product(db, product)
|
||||
logger.info("MarketplaceProduct created successfully")
|
||||
|
||||
return db_product
|
||||
|
||||
|
||||
@router.get("/marketplace/product/{marketplace_product_id}", response_model=MarketplaceProductDetailResponse)
|
||||
def get_product(
|
||||
marketplace_product_id: str,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Get product with stock information (Protected)."""
|
||||
product = marketplace_product_service.get_product_by_id_or_raise(db, marketplace_product_id)
|
||||
|
||||
# Get stock information if GTIN exists
|
||||
stock_info = None
|
||||
if product.gtin:
|
||||
stock_info = marketplace_product_service.get_stock_info(db, product.gtin)
|
||||
|
||||
return MarketplaceProductDetailResponse(product=product, stock_info=stock_info)
|
||||
|
||||
|
||||
@router.put("/marketplace/product/{marketplace_product_id}", response_model=MarketplaceProductResponse)
|
||||
def update_product(
|
||||
marketplace_product_id: str,
|
||||
product_update: MarketplaceProductUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Update product with validation and marketplace support (Protected)."""
|
||||
updated_product = marketplace_product_service.update_product(db, marketplace_product_id, product_update)
|
||||
return updated_product
|
||||
|
||||
|
||||
@router.delete("/marketplace/product/{marketplace_product_id}")
|
||||
def delete_product(
|
||||
marketplace_product_id: str,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Delete product and associated stock (Protected)."""
|
||||
marketplace_product_service.delete_product(db, marketplace_product_id)
|
||||
return {"message": "MarketplaceProduct and associated stock deleted successfully"}
|
||||
|
||||
# ============================================================================
|
||||
# IMPORT JOB ENDPOINTS
|
||||
# ============================================================================
|
||||
|
||||
@router.post("/marketplace/import-product", response_model=MarketplaceImportJobResponse)
|
||||
@rate_limit(max_requests=10, window_seconds=3600) # Limit marketplace imports
|
||||
async def import_products_from_marketplace(
|
||||
request: MarketplaceImportRequest,
|
||||
request: MarketplaceImportJobRequest,
|
||||
background_tasks: BackgroundTasks,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
@@ -41,7 +170,7 @@ async def import_products_from_marketplace(
|
||||
)
|
||||
|
||||
# Create import job through service
|
||||
import_job = marketplace_service.create_import_job(db, request, current_user)
|
||||
import_job = marketplace_import_job_service.create_import_job(db, request, current_user)
|
||||
|
||||
# Process in background
|
||||
background_tasks.add_task(
|
||||
@@ -74,8 +203,8 @@ def get_marketplace_import_status(
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Get status of marketplace import job (Protected)."""
|
||||
job = marketplace_service.get_import_job_by_id(db, job_id, current_user)
|
||||
return marketplace_service.convert_to_response_model(job)
|
||||
job = marketplace_import_job_service.get_import_job_by_id(db, job_id, current_user)
|
||||
return marketplace_import_job_service.convert_to_response_model(job)
|
||||
|
||||
|
||||
@router.get(
|
||||
@@ -90,7 +219,7 @@ def get_marketplace_import_jobs(
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Get marketplace import jobs with filtering (Protected)."""
|
||||
jobs = marketplace_service.get_import_jobs(
|
||||
jobs = marketplace_import_job_service.get_import_jobs(
|
||||
db=db,
|
||||
user=current_user,
|
||||
marketplace=marketplace,
|
||||
@@ -99,7 +228,7 @@ def get_marketplace_import_jobs(
|
||||
limit=limit,
|
||||
)
|
||||
|
||||
return [marketplace_service.convert_to_response_model(job) for job in jobs]
|
||||
return [marketplace_import_job_service.convert_to_response_model(job) for job in jobs]
|
||||
|
||||
|
||||
@router.get("/marketplace/marketplace-import-stats")
|
||||
@@ -107,7 +236,7 @@ def get_marketplace_import_stats(
|
||||
db: Session = Depends(get_db), current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""Get statistics about marketplace import jobs (Protected)."""
|
||||
return marketplace_service.get_job_stats(db, current_user)
|
||||
return marketplace_import_job_service.get_job_stats(db, current_user)
|
||||
|
||||
|
||||
@router.put(
|
||||
@@ -120,8 +249,8 @@ def cancel_marketplace_import_job(
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Cancel a pending or running marketplace import job (Protected)."""
|
||||
job = marketplace_service.cancel_import_job(db, job_id, current_user)
|
||||
return marketplace_service.convert_to_response_model(job)
|
||||
job = marketplace_import_job_service.cancel_import_job(db, job_id, current_user)
|
||||
return marketplace_import_job_service.convert_to_response_model(job)
|
||||
|
||||
|
||||
@router.delete("/marketplace/import-jobs/{job_id}")
|
||||
@@ -131,5 +260,5 @@ def delete_marketplace_import_job(
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Delete a completed marketplace import job (Protected)."""
|
||||
marketplace_service.delete_import_job(db, job_id, current_user)
|
||||
marketplace_import_job_service.delete_import_job(db, job_id, current_user)
|
||||
return {"message": "Marketplace import job deleted successfully"}
|
||||
|
||||
@@ -1,142 +0,0 @@
|
||||
# app/api/v1/product.py
|
||||
"""
|
||||
Product endpoints - simplified with service-level exception handling.
|
||||
|
||||
This module provides classes and functions for:
|
||||
- Product CRUD operations with marketplace support
|
||||
- Advanced product filtering and search
|
||||
- Product export functionality
|
||||
"""
|
||||
|
||||
import logging
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from fastapi.responses import StreamingResponse
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_current_user
|
||||
from app.core.database import get_db
|
||||
from app.services.product_service import product_service
|
||||
from models.schemas.product import (ProductCreate, ProductDetailResponse,
|
||||
ProductListResponse, ProductResponse,
|
||||
ProductUpdate)
|
||||
from models.database.user import User
|
||||
|
||||
router = APIRouter()
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@router.get("/product/export-csv")
|
||||
async def export_csv(
|
||||
marketplace: Optional[str] = Query(None, description="Filter by marketplace"),
|
||||
shop_name: Optional[str] = Query(None, description="Filter by shop name"),
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Export products as CSV with streaming and marketplace filtering (Protected)."""
|
||||
|
||||
def generate_csv():
|
||||
return product_service.generate_csv_export(
|
||||
db=db, marketplace=marketplace, shop_name=shop_name
|
||||
)
|
||||
|
||||
filename = "products_export"
|
||||
if marketplace:
|
||||
filename += f"_{marketplace}"
|
||||
if shop_name:
|
||||
filename += f"_{shop_name}"
|
||||
filename += ".csv"
|
||||
|
||||
return StreamingResponse(
|
||||
generate_csv(),
|
||||
media_type="text/csv",
|
||||
headers={"Content-Disposition": f"attachment; filename={filename}"},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/product", response_model=ProductListResponse)
|
||||
def get_products(
|
||||
skip: int = Query(0, ge=0),
|
||||
limit: int = Query(100, ge=1, le=1000),
|
||||
brand: Optional[str] = Query(None),
|
||||
category: Optional[str] = Query(None),
|
||||
availability: Optional[str] = Query(None),
|
||||
marketplace: Optional[str] = Query(None, description="Filter by marketplace"),
|
||||
shop_name: Optional[str] = Query(None, description="Filter by shop name"),
|
||||
search: Optional[str] = Query(None),
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Get products with advanced filtering including marketplace and shop (Protected)."""
|
||||
products, total = product_service.get_products_with_filters(
|
||||
db=db,
|
||||
skip=skip,
|
||||
limit=limit,
|
||||
brand=brand,
|
||||
category=category,
|
||||
availability=availability,
|
||||
marketplace=marketplace,
|
||||
shop_name=shop_name,
|
||||
search=search,
|
||||
)
|
||||
|
||||
return ProductListResponse(
|
||||
products=products, total=total, skip=skip, limit=limit
|
||||
)
|
||||
|
||||
|
||||
@router.post("/product", response_model=ProductResponse)
|
||||
def create_product(
|
||||
product: ProductCreate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Create a new product with validation and marketplace support (Protected)."""
|
||||
logger.info(f"Starting product creation for ID: {product.product_id}")
|
||||
|
||||
db_product = product_service.create_product(db, product)
|
||||
logger.info("Product created successfully")
|
||||
|
||||
return db_product
|
||||
|
||||
|
||||
@router.get("/product/{product_id}", response_model=ProductDetailResponse)
|
||||
def get_product(
|
||||
product_id: str,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Get product with stock information (Protected)."""
|
||||
product = product_service.get_product_by_id_or_raise(db, product_id)
|
||||
|
||||
# Get stock information if GTIN exists
|
||||
stock_info = None
|
||||
if product.gtin:
|
||||
stock_info = product_service.get_stock_info(db, product.gtin)
|
||||
|
||||
return ProductDetailResponse(product=product, stock_info=stock_info)
|
||||
|
||||
|
||||
@router.put("/product/{product_id}", response_model=ProductResponse)
|
||||
def update_product(
|
||||
product_id: str,
|
||||
product_update: ProductUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Update product with validation and marketplace support (Protected)."""
|
||||
updated_product = product_service.update_product(db, product_id, product_update)
|
||||
return updated_product
|
||||
|
||||
|
||||
@router.delete("/product/{product_id}")
|
||||
def delete_product(
|
||||
product_id: str,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Delete product and associated stock (Protected)."""
|
||||
product_service.delete_product(db, product_id)
|
||||
return {"message": "Product and associated stock deleted successfully"}
|
||||
|
||||
@@ -29,13 +29,13 @@ from .auth import (
|
||||
UserAlreadyExistsException
|
||||
)
|
||||
|
||||
from .product import (
|
||||
ProductNotFoundException,
|
||||
ProductAlreadyExistsException,
|
||||
InvalidProductDataException,
|
||||
ProductValidationException,
|
||||
from .marketplace_product import (
|
||||
MarketplaceProductNotFoundException,
|
||||
MarketplaceProductAlreadyExistsException,
|
||||
InvalidMarketplaceProductDataException,
|
||||
MarketplaceProductValidationException,
|
||||
InvalidGTINException,
|
||||
ProductCSVImportException,
|
||||
MarketplaceProductCSVImportException,
|
||||
)
|
||||
|
||||
from .stock import (
|
||||
@@ -61,7 +61,7 @@ from .shop import (
|
||||
ShopValidationException,
|
||||
)
|
||||
|
||||
from .marketplace import (
|
||||
from .marketplace_import_job import (
|
||||
MarketplaceImportException,
|
||||
ImportJobNotFoundException,
|
||||
ImportJobNotOwnedException,
|
||||
@@ -107,13 +107,13 @@ __all__ = [
|
||||
"AdminRequiredException",
|
||||
"UserAlreadyExistsException",
|
||||
|
||||
# Product exceptions
|
||||
"ProductNotFoundException",
|
||||
"ProductAlreadyExistsException",
|
||||
"InvalidProductDataException",
|
||||
"ProductValidationException",
|
||||
# MarketplaceProduct exceptions
|
||||
"MarketplaceProductNotFoundException",
|
||||
"MarketplaceProductAlreadyExistsException",
|
||||
"InvalidMarketplaceProductDataException",
|
||||
"MarketplaceProductValidationException",
|
||||
"InvalidGTINException",
|
||||
"ProductCSVImportException",
|
||||
"MarketplaceProductCSVImportException",
|
||||
|
||||
# Stock exceptions
|
||||
"StockNotFoundException",
|
||||
@@ -136,7 +136,7 @@ __all__ = [
|
||||
"MaxShopsReachedException",
|
||||
"ShopValidationException",
|
||||
|
||||
# Marketplace exceptions
|
||||
# Marketplace import exceptions
|
||||
"MarketplaceImportException",
|
||||
"ImportJobNotFoundException",
|
||||
"ImportJobNotOwnedException",
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# app/exceptions/marketplace.py
|
||||
# app/exceptions/marketplace_import_job.py
|
||||
"""
|
||||
Marketplace import specific exceptions.
|
||||
"""
|
||||
@@ -1,36 +1,36 @@
|
||||
# app/exceptions/product.py
|
||||
# app/exceptions/marketplace_products.py
|
||||
"""
|
||||
Product management specific exceptions.
|
||||
MarketplaceProduct management specific exceptions.
|
||||
"""
|
||||
|
||||
from typing import Any, Dict, Optional
|
||||
from .base import ResourceNotFoundException, ConflictException, ValidationException, BusinessLogicException
|
||||
|
||||
|
||||
class ProductNotFoundException(ResourceNotFoundException):
|
||||
class MarketplaceProductNotFoundException(ResourceNotFoundException):
|
||||
"""Raised when a product is not found."""
|
||||
|
||||
def __init__(self, product_id: str):
|
||||
def __init__(self, marketplace_product_id: str):
|
||||
super().__init__(
|
||||
resource_type="Product",
|
||||
identifier=product_id,
|
||||
message=f"Product with ID '{product_id}' not found",
|
||||
resource_type="MarketplaceProduct",
|
||||
identifier=marketplace_product_id,
|
||||
message=f"MarketplaceProduct with ID '{marketplace_product_id}' not found",
|
||||
error_code="PRODUCT_NOT_FOUND",
|
||||
)
|
||||
|
||||
|
||||
class ProductAlreadyExistsException(ConflictException):
|
||||
class MarketplaceProductAlreadyExistsException(ConflictException):
|
||||
"""Raised when trying to create a product that already exists."""
|
||||
|
||||
def __init__(self, product_id: str):
|
||||
def __init__(self, marketplace_product_id: str):
|
||||
super().__init__(
|
||||
message=f"Product with ID '{product_id}' already exists",
|
||||
message=f"MarketplaceProduct with ID '{marketplace_product_id}' already exists",
|
||||
error_code="PRODUCT_ALREADY_EXISTS",
|
||||
details={"product_id": product_id},
|
||||
details={"marketplace_product_id": marketplace_product_id},
|
||||
)
|
||||
|
||||
|
||||
class InvalidProductDataException(ValidationException):
|
||||
class InvalidMarketplaceProductDataException(ValidationException):
|
||||
"""Raised when product data is invalid."""
|
||||
|
||||
def __init__(
|
||||
@@ -47,7 +47,7 @@ class InvalidProductDataException(ValidationException):
|
||||
self.error_code = "INVALID_PRODUCT_DATA"
|
||||
|
||||
|
||||
class ProductValidationException(ValidationException):
|
||||
class MarketplaceProductValidationException(ValidationException):
|
||||
"""Raised when product validation fails."""
|
||||
|
||||
def __init__(
|
||||
@@ -80,12 +80,12 @@ class InvalidGTINException(ValidationException):
|
||||
self.error_code = "INVALID_GTIN"
|
||||
|
||||
|
||||
class ProductCSVImportException(BusinessLogicException):
|
||||
class MarketplaceProductCSVImportException(BusinessLogicException):
|
||||
"""Raised when product CSV import fails."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
message: str = "Product CSV import failed",
|
||||
message: str = "MarketplaceProduct CSV import failed",
|
||||
row_number: Optional[int] = None,
|
||||
errors: Optional[Dict[str, Any]] = None,
|
||||
):
|
||||
@@ -98,13 +98,13 @@ class InvalidShopDataException(ValidationException):
|
||||
class ShopProductAlreadyExistsException(ConflictException):
|
||||
"""Raised when trying to add a product that already exists in shop."""
|
||||
|
||||
def __init__(self, shop_code: str, product_id: str):
|
||||
def __init__(self, shop_code: str, marketplace_product_id: str):
|
||||
super().__init__(
|
||||
message=f"Product '{product_id}' already exists in shop '{shop_code}'",
|
||||
message=f"MarketplaceProduct '{marketplace_product_id}' already exists in shop '{shop_code}'",
|
||||
error_code="SHOP_PRODUCT_ALREADY_EXISTS",
|
||||
details={
|
||||
"shop_code": shop_code,
|
||||
"product_id": product_id,
|
||||
"marketplace_product_id": marketplace_product_id,
|
||||
},
|
||||
)
|
||||
|
||||
@@ -112,11 +112,11 @@ class ShopProductAlreadyExistsException(ConflictException):
|
||||
class ShopProductNotFoundException(ResourceNotFoundException):
|
||||
"""Raised when a shop product relationship is not found."""
|
||||
|
||||
def __init__(self, shop_code: str, product_id: str):
|
||||
def __init__(self, shop_code: str, marketplace_product_id: str):
|
||||
super().__init__(
|
||||
resource_type="ShopProduct",
|
||||
identifier=f"{shop_code}/{product_id}",
|
||||
message=f"Product '{product_id}' not found in shop '{shop_code}'",
|
||||
identifier=f"{shop_code}/{marketplace_product_id}",
|
||||
message=f"MarketplaceProduct '{marketplace_product_id}' not found in shop '{shop_code}'",
|
||||
error_code="SHOP_PRODUCT_NOT_FOUND",
|
||||
)
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
@@ -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()
|
||||
@@ -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
|
||||
)
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -11,7 +11,7 @@ import logging
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from app.core.database import SessionLocal
|
||||
from models.database.marketplace import MarketplaceImportJob
|
||||
from models.database.marketplace_import_job import MarketplaceImportJob
|
||||
from app.utils.csv_processor import CSVProcessor
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -17,7 +17,7 @@ import requests
|
||||
from sqlalchemy import literal
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from models.database.product import Product
|
||||
from models.database.marketplace_product import MarketplaceProduct
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -40,15 +40,15 @@ class CSVProcessor:
|
||||
|
||||
COLUMN_MAPPING = {
|
||||
# Standard variations
|
||||
"id": "product_id",
|
||||
"ID": "product_id",
|
||||
"Product ID": "product_id",
|
||||
"id": "marketplace_product_id",
|
||||
"ID": "marketplace_product_id",
|
||||
"MarketplaceProduct ID": "marketplace_product_id",
|
||||
"name": "title",
|
||||
"Name": "title",
|
||||
"product_name": "title",
|
||||
"Product Name": "title",
|
||||
"MarketplaceProduct Name": "title",
|
||||
# Google Shopping feed standard
|
||||
"g:id": "product_id",
|
||||
"g:id": "marketplace_product_id",
|
||||
"g:title": "title",
|
||||
"g:description": "description",
|
||||
"g:link": "link",
|
||||
@@ -266,8 +266,8 @@ class CSVProcessor:
|
||||
product_data["shop_name"] = shop_name
|
||||
|
||||
# Validate required fields
|
||||
if not product_data.get("product_id"):
|
||||
logger.warning(f"Row {index}: Missing product_id, skipping")
|
||||
if not product_data.get("marketplace_product_id"):
|
||||
logger.warning(f"Row {index}: Missing marketplace_product_id, skipping")
|
||||
errors += 1
|
||||
continue
|
||||
|
||||
@@ -278,8 +278,8 @@ class CSVProcessor:
|
||||
|
||||
# Check if product exists
|
||||
existing_product = (
|
||||
db.query(Product)
|
||||
.filter(Product.product_id == literal(product_data["product_id"]))
|
||||
db.query(MarketplaceProduct)
|
||||
.filter(MarketplaceProduct.marketplace_product_id == literal(product_data["marketplace_product_id"]))
|
||||
.first()
|
||||
)
|
||||
|
||||
@@ -293,7 +293,7 @@ class CSVProcessor:
|
||||
existing_product.updated_at = datetime.now(timezone.utc)
|
||||
updated += 1
|
||||
logger.debug(
|
||||
f"Updated product {product_data['product_id']} for "
|
||||
f"Updated product {product_data['marketplace_product_id']} for "
|
||||
f"{marketplace} and shop {shop_name}"
|
||||
)
|
||||
else:
|
||||
@@ -302,13 +302,13 @@ class CSVProcessor:
|
||||
k: v
|
||||
for k, v in product_data.items()
|
||||
if k not in ["id", "created_at", "updated_at"]
|
||||
and hasattr(Product, k)
|
||||
and hasattr(MarketplaceProduct, k)
|
||||
}
|
||||
new_product = Product(**filtered_data)
|
||||
new_product = MarketplaceProduct(**filtered_data)
|
||||
db.add(new_product)
|
||||
imported += 1
|
||||
logger.debug(
|
||||
f"Imported new product {product_data['product_id']} "
|
||||
f"Imported new product {product_data['marketplace_product_id']} "
|
||||
f"for {marketplace} and shop {shop_name}"
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user