Added marketplace support
This commit is contained in:
565
main.py
565
main.py
@@ -5,7 +5,7 @@ from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
||||
from pydantic import BaseModel, Field, validator
|
||||
from typing import Optional, List, Dict, Any
|
||||
from datetime import datetime, timedelta
|
||||
from sqlalchemy import create_engine, Column, Integer, String, DateTime, text, ForeignKey, Index
|
||||
from sqlalchemy import create_engine, Column, Integer, String, DateTime, text, ForeignKey, Index, func
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker, Session, relationship
|
||||
from contextlib import asynccontextmanager
|
||||
@@ -26,7 +26,7 @@ load_dotenv()
|
||||
from utils.data_processing import GTINProcessor, PriceProcessor
|
||||
from utils.csv_processor import CSVProcessor
|
||||
from utils.database import get_db_engine, get_session_local
|
||||
from models.database_models import Base, Product, Stock, ImportJob, User
|
||||
from models.database_models import Base, Product, Stock, User, MarketplaceImportJob
|
||||
from models.api_models import *
|
||||
from middleware.rate_limiter import RateLimiter
|
||||
from middleware.auth import AuthManager
|
||||
@@ -57,7 +57,7 @@ auth_manager = AuthManager()
|
||||
async def lifespan(app: FastAPI):
|
||||
"""Application lifespan events"""
|
||||
# Startup
|
||||
logger.info("Starting up ecommerce API with authentication")
|
||||
logger.info("Starting up ecommerce API with marketplace import support")
|
||||
|
||||
# Create tables
|
||||
Base.metadata.create_all(bind=engine)
|
||||
@@ -79,16 +79,28 @@ async def lifespan(app: FastAPI):
|
||||
conn.execute(text("CREATE INDEX IF NOT EXISTS idx_user_username ON users(username)"))
|
||||
conn.execute(text("CREATE INDEX IF NOT EXISTS idx_user_role ON users(role)"))
|
||||
|
||||
# Product indexes
|
||||
# Product indexes (including new marketplace indexes)
|
||||
conn.execute(text("CREATE INDEX IF NOT EXISTS idx_product_gtin ON products(gtin)"))
|
||||
conn.execute(text("CREATE INDEX IF NOT EXISTS idx_product_brand ON products(brand)"))
|
||||
conn.execute(text("CREATE INDEX IF NOT EXISTS idx_product_category ON products(google_product_category)"))
|
||||
conn.execute(text("CREATE INDEX IF NOT EXISTS idx_product_availability ON products(availability)"))
|
||||
conn.execute(text("CREATE INDEX IF NOT EXISTS idx_product_marketplace ON products(marketplace)"))
|
||||
conn.execute(text("CREATE INDEX IF NOT EXISTS idx_product_shop_name ON products(shop_name)"))
|
||||
conn.execute(
|
||||
text("CREATE INDEX IF NOT EXISTS idx_product_marketplace_shop ON products(marketplace, shop_name)"))
|
||||
|
||||
# Stock indexes
|
||||
conn.execute(text("CREATE INDEX IF NOT EXISTS idx_stock_gtin_location ON stock(gtin, location)"))
|
||||
conn.execute(text("CREATE INDEX IF NOT EXISTS idx_stock_location ON stock(location)"))
|
||||
|
||||
# Marketplace import job indexes
|
||||
conn.execute(text(
|
||||
"CREATE INDEX IF NOT EXISTS idx_marketplace_import_marketplace ON marketplace_import_jobs(marketplace)"))
|
||||
conn.execute(text(
|
||||
"CREATE INDEX IF NOT EXISTS idx_marketplace_import_shop_name ON marketplace_import_jobs(shop_name)"))
|
||||
conn.execute(
|
||||
text("CREATE INDEX IF NOT EXISTS idx_marketplace_import_user_id ON marketplace_import_jobs(user_id)"))
|
||||
|
||||
conn.commit()
|
||||
logger.info("Database indexes created successfully")
|
||||
except Exception as e:
|
||||
@@ -102,9 +114,9 @@ async def lifespan(app: FastAPI):
|
||||
|
||||
# FastAPI app with lifespan
|
||||
app = FastAPI(
|
||||
title="Ecommerce Backend API",
|
||||
description="Advanced product management system with JWT authentication, CSV import/export and stock management",
|
||||
version="2.1.0",
|
||||
title="Ecommerce Backend API with Marketplace Support",
|
||||
description="Advanced product management system with JWT authentication, marketplace-aware CSV import/export and stock management",
|
||||
version="2.2.0",
|
||||
lifespan=lifespan
|
||||
)
|
||||
|
||||
@@ -233,8 +245,15 @@ def get_current_user_info(current_user: User = Depends(get_current_user)):
|
||||
@app.get("/")
|
||||
def root():
|
||||
return {
|
||||
"message": "Ecommerce Backend API v2.1 with JWT Authentication",
|
||||
"message": "Ecommerce Backend API v2.2 with Marketplace Support",
|
||||
"status": "operational",
|
||||
"features": [
|
||||
"JWT Authentication",
|
||||
"Marketplace-aware product import",
|
||||
"Multi-shop product management",
|
||||
"Stock management with location tracking"
|
||||
],
|
||||
"supported_marketplaces": ["Letzshop", "Amazon", "eBay", "Etsy", "Shopify", "Other"],
|
||||
"auth_required": "Most endpoints require Bearer token authentication"
|
||||
}
|
||||
|
||||
@@ -251,21 +270,26 @@ def health_check(db: Session = Depends(get_db)):
|
||||
raise HTTPException(status_code=503, detail="Service unhealthy")
|
||||
|
||||
|
||||
# Protected Routes (authentication required)
|
||||
@app.post("/import-csv", response_model=ImportJobResponse)
|
||||
@rate_limit(max_requests=10, window_seconds=3600) # Limit CSV imports
|
||||
async def import_csv_from_url(
|
||||
request: CSVImportRequest,
|
||||
# Marketplace Import Routes (Protected)
|
||||
@app.post("/import-from-marketplace", response_model=MarketplaceImportJobResponse)
|
||||
@rate_limit(max_requests=10, window_seconds=3600) # Limit marketplace imports
|
||||
async def import_products_from_marketplace(
|
||||
request: MarketplaceImportRequest,
|
||||
background_tasks: BackgroundTasks,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""Import products from CSV URL with background processing (Protected)"""
|
||||
"""Import products from marketplace CSV with background processing (Protected)"""
|
||||
|
||||
# Create import job record
|
||||
import_job = ImportJob(
|
||||
logger.info(
|
||||
f"Starting marketplace import: {request.marketplace} -> {request.shop_name} by user {current_user.username}")
|
||||
|
||||
# Create marketplace import job record
|
||||
import_job = MarketplaceImportJob(
|
||||
status="pending",
|
||||
source_url=request.url,
|
||||
marketplace=request.marketplace,
|
||||
shop_name=request.shop_name,
|
||||
user_id=current_user.id,
|
||||
created_at=datetime.utcnow()
|
||||
)
|
||||
@@ -275,36 +299,44 @@ async def import_csv_from_url(
|
||||
|
||||
# Process in background
|
||||
background_tasks.add_task(
|
||||
process_csv_import,
|
||||
process_marketplace_import,
|
||||
import_job.id,
|
||||
request.url,
|
||||
request.marketplace,
|
||||
request.shop_name,
|
||||
request.batch_size or 1000
|
||||
)
|
||||
|
||||
return ImportJobResponse(
|
||||
return MarketplaceImportJobResponse(
|
||||
job_id=import_job.id,
|
||||
status="pending",
|
||||
message="CSV import started. Check status with /import-status/{job_id}"
|
||||
marketplace=request.marketplace,
|
||||
shop_name=request.shop_name,
|
||||
message=f"Marketplace import started from {request.marketplace}. Check status with /marketplace-import-status/{import_job.id}"
|
||||
)
|
||||
|
||||
|
||||
async def process_csv_import(job_id: int, url: str, batch_size: int = 1000):
|
||||
"""Background task to process CSV import with batching"""
|
||||
async def process_marketplace_import(job_id: int, url: str, marketplace: str, shop_name: str, batch_size: int = 1000):
|
||||
"""Background task to process marketplace CSV import with batching"""
|
||||
db = SessionLocal()
|
||||
|
||||
try:
|
||||
# Update job status
|
||||
job = db.query(ImportJob).filter(ImportJob.id == job_id).first()
|
||||
job = db.query(MarketplaceImportJob).filter(MarketplaceImportJob.id == job_id).first()
|
||||
if not job:
|
||||
logger.error(f"Import job {job_id} not found")
|
||||
logger.error(f"Marketplace import job {job_id} not found")
|
||||
return
|
||||
|
||||
job.status = "processing"
|
||||
job.started_at = datetime.utcnow()
|
||||
db.commit()
|
||||
|
||||
# Process CSV
|
||||
result = await csv_processor.process_csv_from_url(url, batch_size, db)
|
||||
logger.info(f"Processing marketplace import: Job {job_id}, Marketplace: {marketplace}, Shop: {shop_name}")
|
||||
|
||||
# Process CSV with marketplace and shop information
|
||||
result = await csv_processor.process_marketplace_csv_from_url(
|
||||
url, marketplace, shop_name, batch_size, db
|
||||
)
|
||||
|
||||
# Update job with results
|
||||
job.status = "completed"
|
||||
@@ -319,10 +351,11 @@ async def process_csv_import(job_id: int, url: str, batch_size: int = 1000):
|
||||
job.error_message = f"{result['errors']} rows had errors"
|
||||
|
||||
db.commit()
|
||||
logger.info(f"Import job {job_id} completed successfully")
|
||||
logger.info(
|
||||
f"Marketplace import job {job_id} completed successfully - Imported: {result['imported']}, Updated: {result['updated']}")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Import job {job_id} failed: {e}")
|
||||
logger.error(f"Marketplace import job {job_id} failed: {e}")
|
||||
job.status = "failed"
|
||||
job.completed_at = datetime.utcnow()
|
||||
job.error_message = str(e)
|
||||
@@ -332,20 +365,26 @@ async def process_csv_import(job_id: int, url: str, batch_size: int = 1000):
|
||||
db.close()
|
||||
|
||||
|
||||
@app.get("/import-status/{job_id}", response_model=ImportJobResponse)
|
||||
def get_import_status(job_id: int, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
|
||||
"""Get status of CSV import job (Protected)"""
|
||||
job = db.query(ImportJob).filter(ImportJob.id == job_id).first()
|
||||
@app.get("/marketplace-import-status/{job_id}", response_model=MarketplaceImportJobResponse)
|
||||
def get_marketplace_import_status(
|
||||
job_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""Get status of marketplace import job (Protected)"""
|
||||
job = db.query(MarketplaceImportJob).filter(MarketplaceImportJob.id == job_id).first()
|
||||
if not job:
|
||||
raise HTTPException(status_code=404, detail="Import job not found")
|
||||
raise HTTPException(status_code=404, detail="Marketplace import job not found")
|
||||
|
||||
# Users can only see their own jobs, admins can see all
|
||||
if current_user.role != "admin" and job.user_id != current_user.id:
|
||||
raise HTTPException(status_code=403, detail="Access denied to this import job")
|
||||
|
||||
return ImportJobResponse(
|
||||
return MarketplaceImportJobResponse(
|
||||
job_id=job.id,
|
||||
status=job.status,
|
||||
marketplace=job.marketplace,
|
||||
shop_name=job.shop_name,
|
||||
imported=job.imported_count or 0,
|
||||
updated=job.updated_count or 0,
|
||||
total_processed=job.total_processed or 0,
|
||||
@@ -357,6 +396,51 @@ def get_import_status(job_id: int, db: Session = Depends(get_db), current_user:
|
||||
)
|
||||
|
||||
|
||||
@app.get("/marketplace-import-jobs", response_model=List[MarketplaceImportJobResponse])
|
||||
def get_marketplace_import_jobs(
|
||||
marketplace: Optional[str] = Query(None, description="Filter by marketplace"),
|
||||
shop_name: Optional[str] = Query(None, description="Filter by shop name"),
|
||||
skip: int = Query(0, ge=0),
|
||||
limit: int = Query(50, ge=1, le=100),
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""Get marketplace import jobs with filtering (Protected)"""
|
||||
|
||||
query = db.query(MarketplaceImportJob)
|
||||
|
||||
# Users can only see their own jobs, admins can see all
|
||||
if current_user.role != "admin":
|
||||
query = query.filter(MarketplaceImportJob.user_id == current_user.id)
|
||||
|
||||
# Apply filters
|
||||
if marketplace:
|
||||
query = query.filter(MarketplaceImportJob.marketplace.ilike(f"%{marketplace}%"))
|
||||
if shop_name:
|
||||
query = query.filter(MarketplaceImportJob.shop_name.ilike(f"%{shop_name}%"))
|
||||
|
||||
# Order by creation date (newest first) and apply pagination
|
||||
jobs = query.order_by(MarketplaceImportJob.created_at.desc()).offset(skip).limit(limit).all()
|
||||
|
||||
return [
|
||||
MarketplaceImportJobResponse(
|
||||
job_id=job.id,
|
||||
status=job.status,
|
||||
marketplace=job.marketplace,
|
||||
shop_name=job.shop_name,
|
||||
imported=job.imported_count or 0,
|
||||
updated=job.updated_count or 0,
|
||||
total_processed=job.total_processed or 0,
|
||||
error_count=job.error_count or 0,
|
||||
error_message=job.error_message,
|
||||
created_at=job.created_at,
|
||||
started_at=job.started_at,
|
||||
completed_at=job.completed_at
|
||||
) for job in jobs
|
||||
]
|
||||
|
||||
|
||||
# Enhanced Product Routes with Marketplace Support
|
||||
@app.get("/products", response_model=ProductListResponse)
|
||||
def get_products(
|
||||
skip: int = Query(0, ge=0),
|
||||
@@ -364,11 +448,13 @@ def get_products(
|
||||
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 and search (Protected)"""
|
||||
"""Get products with advanced filtering including marketplace and shop (Protected)"""
|
||||
|
||||
query = db.query(Product)
|
||||
|
||||
@@ -379,12 +465,18 @@ def get_products(
|
||||
query = query.filter(Product.google_product_category.ilike(f"%{category}%"))
|
||||
if availability:
|
||||
query = query.filter(Product.availability == availability)
|
||||
if marketplace:
|
||||
query = query.filter(Product.marketplace.ilike(f"%{marketplace}%"))
|
||||
if shop_name:
|
||||
query = query.filter(Product.shop_name.ilike(f"%{shop_name}%"))
|
||||
if search:
|
||||
# Search in title and description
|
||||
# Search in title, description, and marketplace
|
||||
search_term = f"%{search}%"
|
||||
query = query.filter(
|
||||
(Product.title.ilike(search_term)) |
|
||||
(Product.description.ilike(search_term))
|
||||
(Product.description.ilike(search_term)) |
|
||||
(Product.marketplace.ilike(search_term)) |
|
||||
(Product.shop_name.ilike(search_term))
|
||||
)
|
||||
|
||||
# Get total count for pagination
|
||||
@@ -407,7 +499,7 @@ def create_product(
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""Create a new product with validation (Protected)"""
|
||||
"""Create a new product with validation and marketplace support (Protected)"""
|
||||
|
||||
# Check if product_id already exists
|
||||
existing = db.query(Product).filter(Product.product_id == product.product_id).first()
|
||||
@@ -428,11 +520,17 @@ def create_product(
|
||||
product.price = parsed_price
|
||||
product.currency = currency
|
||||
|
||||
# Set default marketplace if not provided
|
||||
if not product.marketplace:
|
||||
product.marketplace = "Letzshop"
|
||||
|
||||
db_product = Product(**product.dict())
|
||||
db.add(db_product)
|
||||
db.commit()
|
||||
db.refresh(db_product)
|
||||
|
||||
logger.info(
|
||||
f"Created product {db_product.product_id} for marketplace {db_product.marketplace}, shop {db_product.shop_name}")
|
||||
return db_product
|
||||
|
||||
|
||||
@@ -473,7 +571,7 @@ def update_product(
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""Update product with validation (Protected)"""
|
||||
"""Update product with validation and marketplace support (Protected)"""
|
||||
|
||||
product = db.query(Product).filter(Product.product_id == product_id).first()
|
||||
if not product:
|
||||
@@ -529,37 +627,45 @@ def delete_product(
|
||||
|
||||
|
||||
# Stock Management Routes (Protected)
|
||||
@app.post("/stock", response_model=StockResponse)
|
||||
def set_stock(
|
||||
stock: StockCreate,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""Set stock with GTIN validation (Protected)"""
|
||||
# Stock Management Routes
|
||||
|
||||
# Normalize and validate GTIN
|
||||
normalized_gtin = gtin_processor.normalize(stock.gtin)
|
||||
@app.post("/stock", response_model=StockResponse)
|
||||
def set_stock(stock: StockCreate, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
|
||||
"""Set exact stock quantity for a GTIN at a specific location (replaces existing quantity)"""
|
||||
|
||||
# Normalize GTIN
|
||||
def normalize_gtin(gtin_value):
|
||||
if not gtin_value:
|
||||
return None
|
||||
gtin_str = str(gtin_value).strip()
|
||||
if '.' in gtin_str:
|
||||
gtin_str = gtin_str.split('.')[0]
|
||||
gtin_clean = ''.join(filter(str.isdigit, gtin_str))
|
||||
if len(gtin_clean) in [8, 12, 13, 14]:
|
||||
return gtin_clean.zfill(13) if len(gtin_clean) == 13 else gtin_clean.zfill(12)
|
||||
return gtin_clean if gtin_clean else None
|
||||
|
||||
normalized_gtin = normalize_gtin(stock.gtin)
|
||||
if not normalized_gtin:
|
||||
raise HTTPException(status_code=400, detail="Invalid GTIN format")
|
||||
|
||||
# Verify GTIN exists in products
|
||||
product = db.query(Product).filter(Product.gtin == normalized_gtin).first()
|
||||
if not product:
|
||||
logger.warning(f"Setting stock for GTIN {normalized_gtin} without corresponding product")
|
||||
|
||||
# Check existing stock
|
||||
# Check if stock entry already exists for this GTIN and location
|
||||
existing_stock = db.query(Stock).filter(
|
||||
Stock.gtin == normalized_gtin,
|
||||
Stock.location == stock.location.strip().upper()
|
||||
).first()
|
||||
|
||||
if existing_stock:
|
||||
# Update existing stock (SET to exact quantity)
|
||||
old_quantity = existing_stock.quantity
|
||||
existing_stock.quantity = stock.quantity
|
||||
existing_stock.updated_at = datetime.utcnow()
|
||||
db.commit()
|
||||
db.refresh(existing_stock)
|
||||
logger.info(f"Updated stock for GTIN {normalized_gtin} at {stock.location}: {old_quantity} → {stock.quantity}")
|
||||
return existing_stock
|
||||
else:
|
||||
# Create new stock entry
|
||||
new_stock = Stock(
|
||||
gtin=normalized_gtin,
|
||||
location=stock.location.strip().upper(),
|
||||
@@ -568,41 +674,259 @@ def set_stock(
|
||||
db.add(new_stock)
|
||||
db.commit()
|
||||
db.refresh(new_stock)
|
||||
logger.info(f"Created new stock for GTIN {normalized_gtin} at {stock.location}: {stock.quantity}")
|
||||
return new_stock
|
||||
|
||||
|
||||
@app.post("/stock/add", response_model=StockResponse)
|
||||
def add_stock(stock: StockAdd, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
|
||||
"""Add quantity to existing stock for a GTIN at a specific location (adds to existing quantity)"""
|
||||
|
||||
# Normalize GTIN
|
||||
def normalize_gtin(gtin_value):
|
||||
if not gtin_value:
|
||||
return None
|
||||
gtin_str = str(gtin_value).strip()
|
||||
if '.' in gtin_str:
|
||||
gtin_str = gtin_str.split('.')[0]
|
||||
gtin_clean = ''.join(filter(str.isdigit, gtin_str))
|
||||
if len(gtin_clean) in [8, 12, 13, 14]:
|
||||
return gtin_clean.zfill(13) if len(gtin_clean) == 13 else gtin_clean.zfill(12)
|
||||
return gtin_clean if gtin_clean else None
|
||||
|
||||
normalized_gtin = normalize_gtin(stock.gtin)
|
||||
if not normalized_gtin:
|
||||
raise HTTPException(status_code=400, detail="Invalid GTIN format")
|
||||
|
||||
# Check if stock entry already exists for this GTIN and location
|
||||
existing_stock = db.query(Stock).filter(
|
||||
Stock.gtin == normalized_gtin,
|
||||
Stock.location == stock.location.strip().upper()
|
||||
).first()
|
||||
|
||||
if existing_stock:
|
||||
# Add to existing stock
|
||||
old_quantity = existing_stock.quantity
|
||||
existing_stock.quantity += stock.quantity
|
||||
existing_stock.updated_at = datetime.utcnow()
|
||||
db.commit()
|
||||
db.refresh(existing_stock)
|
||||
logger.info(
|
||||
f"Added stock for GTIN {normalized_gtin} at {stock.location}: {old_quantity} + {stock.quantity} = {existing_stock.quantity}")
|
||||
return existing_stock
|
||||
else:
|
||||
# Create new stock entry with the quantity
|
||||
new_stock = Stock(
|
||||
gtin=normalized_gtin,
|
||||
location=stock.location.strip().upper(),
|
||||
quantity=stock.quantity
|
||||
)
|
||||
db.add(new_stock)
|
||||
db.commit()
|
||||
db.refresh(new_stock)
|
||||
logger.info(f"Created new stock for GTIN {normalized_gtin} at {stock.location}: {stock.quantity}")
|
||||
return new_stock
|
||||
|
||||
|
||||
@app.post("/stock/remove", response_model=StockResponse)
|
||||
def remove_stock(stock: StockAdd, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
|
||||
"""Remove quantity from existing stock for a GTIN at a specific location"""
|
||||
|
||||
# Normalize GTIN
|
||||
def normalize_gtin(gtin_value):
|
||||
if not gtin_value:
|
||||
return None
|
||||
gtin_str = str(gtin_value).strip()
|
||||
if '.' in gtin_str:
|
||||
gtin_str = gtin_str.split('.')[0]
|
||||
gtin_clean = ''.join(filter(str.isdigit, gtin_str))
|
||||
if len(gtin_clean) in [8, 12, 13, 14]:
|
||||
return gtin_clean.zfill(13) if len(gtin_clean) == 13 else gtin_clean.zfill(12)
|
||||
return gtin_clean if gtin_clean else None
|
||||
|
||||
normalized_gtin = normalize_gtin(stock.gtin)
|
||||
if not normalized_gtin:
|
||||
raise HTTPException(status_code=400, detail="Invalid GTIN format")
|
||||
|
||||
# Find existing stock entry
|
||||
existing_stock = db.query(Stock).filter(
|
||||
Stock.gtin == normalized_gtin,
|
||||
Stock.location == stock.location.strip().upper()
|
||||
).first()
|
||||
|
||||
if not existing_stock:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"No stock found for GTIN {normalized_gtin} at location {stock.location}"
|
||||
)
|
||||
|
||||
# Check if we have enough stock to remove
|
||||
if existing_stock.quantity < stock.quantity:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"Insufficient stock. Available: {existing_stock.quantity}, Requested to remove: {stock.quantity}"
|
||||
)
|
||||
|
||||
# Remove from existing stock
|
||||
old_quantity = existing_stock.quantity
|
||||
existing_stock.quantity -= stock.quantity
|
||||
existing_stock.updated_at = datetime.utcnow()
|
||||
db.commit()
|
||||
db.refresh(existing_stock)
|
||||
logger.info(
|
||||
f"Removed stock for GTIN {normalized_gtin} at {stock.location}: {old_quantity} - {stock.quantity} = {existing_stock.quantity}")
|
||||
return existing_stock
|
||||
|
||||
|
||||
@app.get("/stock/{gtin}", response_model=StockSummaryResponse)
|
||||
def get_stock_by_gtin(gtin: str, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
|
||||
"""Get stock summary with product validation (Protected)"""
|
||||
"""Get all stock locations and total quantity for a specific GTIN"""
|
||||
|
||||
normalized_gtin = gtin_processor.normalize(gtin)
|
||||
# Normalize GTIN
|
||||
def normalize_gtin(gtin_value):
|
||||
if not gtin_value:
|
||||
return None
|
||||
gtin_str = str(gtin_value).strip()
|
||||
if '.' in gtin_str:
|
||||
gtin_str = gtin_str.split('.')[0]
|
||||
gtin_clean = ''.join(filter(str.isdigit, gtin_str))
|
||||
if len(gtin_clean) in [8, 12, 13, 14]:
|
||||
return gtin_clean.zfill(13) if len(gtin_clean) == 13 else gtin_clean.zfill(12)
|
||||
return gtin_clean if gtin_clean else None
|
||||
|
||||
normalized_gtin = normalize_gtin(gtin)
|
||||
if not normalized_gtin:
|
||||
raise HTTPException(status_code=400, detail="Invalid GTIN format")
|
||||
|
||||
# Get all stock entries for this GTIN
|
||||
stock_entries = db.query(Stock).filter(Stock.gtin == normalized_gtin).all()
|
||||
|
||||
if not stock_entries:
|
||||
raise HTTPException(status_code=404, detail=f"No stock found for GTIN: {gtin}")
|
||||
|
||||
total_quantity = sum(entry.quantity for entry in stock_entries)
|
||||
locations = [
|
||||
StockLocationResponse(location=entry.location, quantity=entry.quantity)
|
||||
for entry in stock_entries
|
||||
]
|
||||
# Calculate total quantity and build locations list
|
||||
total_quantity = 0
|
||||
locations = []
|
||||
|
||||
# Get product info
|
||||
for entry in stock_entries:
|
||||
total_quantity += entry.quantity
|
||||
locations.append(StockLocationResponse(
|
||||
location=entry.location,
|
||||
quantity=entry.quantity
|
||||
))
|
||||
|
||||
# Try to get product title for reference
|
||||
product = db.query(Product).filter(Product.gtin == normalized_gtin).first()
|
||||
product_title = product.title if product else None
|
||||
|
||||
return StockSummaryResponse(
|
||||
gtin=normalized_gtin,
|
||||
total_quantity=total_quantity,
|
||||
locations=locations,
|
||||
product_title=product.title if product else None
|
||||
product_title=product_title
|
||||
)
|
||||
|
||||
|
||||
@app.get("/stock/{gtin}/total")
|
||||
def get_total_stock(gtin: str, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
|
||||
"""Get total quantity in stock for a specific GTIN"""
|
||||
|
||||
# Normalize GTIN
|
||||
def normalize_gtin(gtin_value):
|
||||
if not gtin_value:
|
||||
return None
|
||||
gtin_str = str(gtin_value).strip()
|
||||
if '.' in gtin_str:
|
||||
gtin_str = gtin_str.split('.')[0]
|
||||
gtin_clean = ''.join(filter(str.isdigit, gtin_str))
|
||||
if len(gtin_clean) in [8, 12, 13, 14]:
|
||||
return gtin_clean.zfill(13) if len(gtin_clean) == 13 else gtin_clean.zfill(12)
|
||||
return gtin_clean if gtin_clean else None
|
||||
|
||||
normalized_gtin = normalize_gtin(gtin)
|
||||
if not normalized_gtin:
|
||||
raise HTTPException(status_code=400, detail="Invalid GTIN format")
|
||||
|
||||
# Calculate total stock
|
||||
total_stock = db.query(Stock).filter(Stock.gtin == normalized_gtin).all()
|
||||
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()
|
||||
|
||||
return {
|
||||
"gtin": normalized_gtin,
|
||||
"total_quantity": total_quantity,
|
||||
"product_title": product.title if product else None,
|
||||
"locations_count": len(total_stock)
|
||||
}
|
||||
|
||||
|
||||
@app.get("/stock", response_model=List[StockResponse])
|
||||
def get_all_stock(
|
||||
skip: int = Query(0, ge=0),
|
||||
limit: int = Query(100, ge=1, le=1000),
|
||||
location: Optional[str] = Query(None, description="Filter by location"),
|
||||
gtin: Optional[str] = Query(None, description="Filter by GTIN"),
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""Get all stock entries with optional filtering"""
|
||||
query = db.query(Stock)
|
||||
|
||||
if location:
|
||||
query = query.filter(Stock.location.ilike(f"%{location}%"))
|
||||
|
||||
if gtin:
|
||||
# Normalize GTIN for search
|
||||
def normalize_gtin(gtin_value):
|
||||
if not gtin_value:
|
||||
return None
|
||||
gtin_str = str(gtin_value).strip()
|
||||
if '.' in gtin_str:
|
||||
gtin_str = gtin_str.split('.')[0]
|
||||
gtin_clean = ''.join(filter(str.isdigit, gtin_str))
|
||||
if len(gtin_clean) in [8, 12, 13, 14]:
|
||||
return gtin_clean.zfill(13) if len(gtin_clean) == 13 else gtin_clean.zfill(12)
|
||||
return gtin_clean if gtin_clean else None
|
||||
|
||||
normalized_gtin = normalize_gtin(gtin)
|
||||
if normalized_gtin:
|
||||
query = query.filter(Stock.gtin == normalized_gtin)
|
||||
|
||||
stock_entries = query.offset(skip).limit(limit).all()
|
||||
return stock_entries
|
||||
|
||||
|
||||
@app.put("/stock/{stock_id}", response_model=StockResponse)
|
||||
def update_stock(stock_id: int, stock_update: StockUpdate, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
|
||||
"""Update stock quantity for a specific stock entry"""
|
||||
stock_entry = db.query(Stock).filter(Stock.id == stock_id).first()
|
||||
if not stock_entry:
|
||||
raise HTTPException(status_code=404, detail="Stock entry not found")
|
||||
|
||||
stock_entry.quantity = stock_update.quantity
|
||||
stock_entry.updated_at = datetime.utcnow()
|
||||
db.commit()
|
||||
db.refresh(stock_entry)
|
||||
return stock_entry
|
||||
|
||||
|
||||
@app.delete("/stock/{stock_id}")
|
||||
def delete_stock(stock_id: int, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
|
||||
"""Delete a stock entry"""
|
||||
stock_entry = db.query(Stock).filter(Stock.id == stock_id).first()
|
||||
if not stock_entry:
|
||||
raise HTTPException(status_code=404, detail="Stock entry not found")
|
||||
|
||||
db.delete(stock_entry)
|
||||
db.commit()
|
||||
return {"message": "Stock entry deleted successfully"}
|
||||
|
||||
# Enhanced Statistics with Marketplace Support
|
||||
@app.get("/stats", response_model=StatsResponse)
|
||||
def get_stats(db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
|
||||
"""Get comprehensive statistics (Protected)"""
|
||||
"""Get comprehensive statistics with marketplace data (Protected)"""
|
||||
|
||||
# Use more efficient queries with proper indexes
|
||||
total_products = db.query(Product).count()
|
||||
@@ -617,50 +941,104 @@ def get_stats(db: Session = Depends(get_db), current_user: User = Depends(get_cu
|
||||
Product.google_product_category != ""
|
||||
).distinct().count()
|
||||
|
||||
# Additional stock statistics
|
||||
# New marketplace statistics
|
||||
unique_marketplaces = db.query(Product.marketplace).filter(
|
||||
Product.marketplace.isnot(None),
|
||||
Product.marketplace != ""
|
||||
).distinct().count()
|
||||
|
||||
unique_shops = db.query(Product.shop_name).filter(
|
||||
Product.shop_name.isnot(None),
|
||||
Product.shop_name != ""
|
||||
).distinct().count()
|
||||
|
||||
# Stock statistics
|
||||
total_stock_entries = db.query(Stock).count()
|
||||
total_inventory = db.query(Stock.quantity).scalar() or 0
|
||||
total_inventory = db.query(func.sum(Stock.quantity)).scalar() or 0
|
||||
|
||||
return StatsResponse(
|
||||
total_products=total_products,
|
||||
unique_brands=unique_brands,
|
||||
unique_categories=unique_categories,
|
||||
unique_marketplaces=unique_marketplaces,
|
||||
unique_shops=unique_shops,
|
||||
total_stock_entries=total_stock_entries,
|
||||
total_inventory_quantity=total_inventory
|
||||
)
|
||||
|
||||
|
||||
@app.get("/marketplace-stats", response_model=List[MarketplaceStatsResponse])
|
||||
def get_marketplace_stats(db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
|
||||
"""Get statistics broken down by marketplace (Protected)"""
|
||||
|
||||
# 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')
|
||||
).filter(
|
||||
Product.marketplace.isnot(None)
|
||||
).group_by(Product.marketplace).all()
|
||||
|
||||
return [
|
||||
MarketplaceStatsResponse(
|
||||
marketplace=stat.marketplace,
|
||||
total_products=stat.total_products,
|
||||
unique_shops=stat.unique_shops,
|
||||
unique_brands=stat.unique_brands
|
||||
) for stat in marketplace_stats
|
||||
]
|
||||
|
||||
|
||||
# Export with streaming for large datasets (Protected)
|
||||
@app.get("/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 (Protected)"""
|
||||
"""Export products as CSV with streaming and marketplace filtering (Protected)"""
|
||||
|
||||
def generate_csv():
|
||||
# Stream CSV generation for memory efficiency
|
||||
yield "product_id,title,description,link,image_link,availability,price,currency,brand,gtin\n"
|
||||
yield "product_id,title,description,link,image_link,availability,price,currency,brand,gtin,marketplace,shop_name\n"
|
||||
|
||||
batch_size = 1000
|
||||
offset = 0
|
||||
|
||||
while True:
|
||||
products = db.query(Product).offset(offset).limit(batch_size).all()
|
||||
query = db.query(Product)
|
||||
|
||||
# Apply marketplace filters
|
||||
if marketplace:
|
||||
query = query.filter(Product.marketplace.ilike(f"%{marketplace}%"))
|
||||
if shop_name:
|
||||
query = query.filter(Product.shop_name.ilike(f"%{shop_name}%"))
|
||||
|
||||
products = query.offset(offset).limit(batch_size).all()
|
||||
if not products:
|
||||
break
|
||||
|
||||
for product in products:
|
||||
# Create CSV row
|
||||
row = f'"{product.product_id}","{product.title or ""}","{product.description or ""}","{product.link or ""}","{product.image_link or ""}","{product.availability or ""}","{product.price or ""}","{product.currency or ""}","{product.brand or ""}","{product.gtin or ""}"\n'
|
||||
# Create CSV row with marketplace fields
|
||||
row = f'"{product.product_id}","{product.title or ""}","{product.description or ""}","{product.link or ""}","{product.image_link or ""}","{product.availability or ""}","{product.price or ""}","{product.currency or ""}","{product.brand or ""}","{product.gtin or ""}","{product.marketplace or ""}","{product.shop_name or ""}"\n'
|
||||
yield row
|
||||
|
||||
offset += batch_size
|
||||
|
||||
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": "attachment; filename=products_export.csv"}
|
||||
headers={"Content-Disposition": f"attachment; filename={filename}"}
|
||||
)
|
||||
|
||||
|
||||
@@ -700,6 +1078,49 @@ def toggle_user_status(
|
||||
return {"message": f"User {user.username} has been {status}"}
|
||||
|
||||
|
||||
@app.get("/admin/marketplace-import-jobs", response_model=List[MarketplaceImportJobResponse])
|
||||
def get_all_marketplace_import_jobs(
|
||||
marketplace: Optional[str] = Query(None),
|
||||
shop_name: Optional[str] = Query(None),
|
||||
status: Optional[str] = Query(None),
|
||||
skip: int = Query(0, ge=0),
|
||||
limit: int = Query(100, ge=1, le=100),
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: User = Depends(get_current_admin_user)
|
||||
):
|
||||
"""Get all marketplace import jobs (Admin only)"""
|
||||
|
||||
query = db.query(MarketplaceImportJob)
|
||||
|
||||
# Apply filters
|
||||
if marketplace:
|
||||
query = query.filter(MarketplaceImportJob.marketplace.ilike(f"%{marketplace}%"))
|
||||
if shop_name:
|
||||
query = query.filter(MarketplaceImportJob.shop_name.ilike(f"%{shop_name}%"))
|
||||
if status:
|
||||
query = query.filter(MarketplaceImportJob.status == status)
|
||||
|
||||
# Order by creation date and apply pagination
|
||||
jobs = query.order_by(MarketplaceImportJob.created_at.desc()).offset(skip).limit(limit).all()
|
||||
|
||||
return [
|
||||
MarketplaceImportJobResponse(
|
||||
job_id=job.id,
|
||||
status=job.status,
|
||||
marketplace=job.marketplace,
|
||||
shop_name=job.shop_name,
|
||||
imported=job.imported_count or 0,
|
||||
updated=job.updated_count or 0,
|
||||
total_processed=job.total_processed or 0,
|
||||
error_count=job.error_count or 0,
|
||||
error_message=job.error_message,
|
||||
created_at=job.created_at,
|
||||
started_at=job.started_at,
|
||||
completed_at=job.completed_at
|
||||
) for job in jobs
|
||||
]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
|
||||
@@ -709,4 +1130,4 @@ if __name__ == "__main__":
|
||||
port=8000,
|
||||
reload=True,
|
||||
log_level="info"
|
||||
)
|
||||
)
|
||||
Reference in New Issue
Block a user