fixing DQ issues

This commit is contained in:
2025-09-14 15:47:38 +02:00
parent 3eb18ef91e
commit 0ce708cf09
27 changed files with 430 additions and 214 deletions

View File

@@ -1,3 +1,12 @@
# app/services/admin_service.py
"""Summary description ....
This module provides classes and functions for:
- ....
- ....
- ....
"""
import logging
from datetime import datetime
from typing import List, Optional, Tuple
@@ -12,17 +21,17 @@ logger = logging.getLogger(__name__)
class AdminService:
"""Service class for admin operations following the application's service pattern"""
"""Service class for admin operations following the application's service pattern."""
def get_all_users(self, db: Session, skip: int = 0, limit: int = 100) -> List[User]:
"""Get paginated list of all users"""
"""Get paginated list of all users."""
return db.query(User).offset(skip).limit(limit).all()
def toggle_user_status(
self, db: Session, user_id: int, current_admin_id: int
) -> Tuple[User, str]:
"""
Toggle user active status
Toggle user active status.
Args:
db: Database session
@@ -59,7 +68,7 @@ class AdminService:
self, db: Session, skip: int = 0, limit: int = 100
) -> Tuple[List[Shop], int]:
"""
Get paginated list of all shops with total count
Get paginated list of all shops with total count.
Args:
db: Database session
@@ -75,7 +84,7 @@ class AdminService:
def verify_shop(self, db: Session, shop_id: int) -> Tuple[Shop, str]:
"""
Toggle shop verification status
Toggle shop verification status.
Args:
db: Database session
@@ -102,7 +111,7 @@ class AdminService:
def toggle_shop_status(self, db: Session, shop_id: int) -> Tuple[Shop, str]:
"""
Toggle shop active status
Toggle shop active status.
Args:
db: Database session
@@ -137,7 +146,7 @@ class AdminService:
limit: int = 100,
) -> List[MarketplaceImportJobResponse]:
"""
Get filtered and paginated marketplace import jobs
Get filtered and paginated marketplace import jobs.
Args:
db: Database session
@@ -190,19 +199,19 @@ class AdminService:
]
def get_user_by_id(self, db: Session, user_id: int) -> Optional[User]:
"""Get user by ID"""
"""Get user by ID."""
return db.query(User).filter(User.id == user_id).first()
def get_shop_by_id(self, db: Session, shop_id: int) -> Optional[Shop]:
"""Get shop by ID"""
"""Get shop by ID."""
return db.query(Shop).filter(Shop.id == shop_id).first()
def user_exists(self, db: Session, user_id: int) -> bool:
"""Check if user exists by ID"""
"""Check if user exists by ID."""
return db.query(User).filter(User.id == user_id).first() is not None
def shop_exists(self, db: Session, shop_id: int) -> bool:
"""Check if shop exists by ID"""
"""Check if shop exists by ID."""
return db.query(Shop).filter(Shop.id == shop_id).first() is not None

View File

@@ -1,3 +1,12 @@
# app/services/auth_service.py
"""Summary description ....
This module provides classes and functions for:
- ....
- ....
- ....
"""
import logging
from typing import Any, Dict, Optional
@@ -12,9 +21,10 @@ logger = logging.getLogger(__name__)
class AuthService:
"""Service class for authentication operations following the application's service pattern"""
"""Service class for authentication operations following the application's service pattern."""
def __init__(self):
"""Class constructor."""
self.auth_manager = AuthManager()
def register_user(self, db: Session, user_data: UserRegister) -> User:
@@ -62,7 +72,7 @@ class AuthService:
def login_user(self, db: Session, user_credentials: UserLogin) -> Dict[str, Any]:
"""
Login user and return JWT token with user data
Login user and return JWT token with user data.
Args:
db: Database session
@@ -90,33 +100,33 @@ class AuthService:
return {"token_data": token_data, "user": user}
def get_user_by_email(self, db: Session, email: str) -> Optional[User]:
"""Get user by email"""
"""Get user by email."""
return db.query(User).filter(User.email == email).first()
def get_user_by_username(self, db: Session, username: str) -> Optional[User]:
"""Get user by username"""
"""Get user by username."""
return db.query(User).filter(User.username == username).first()
def email_exists(self, db: Session, email: str) -> bool:
"""Check if email already exists"""
"""Check if email already exists."""
return db.query(User).filter(User.email == email).first() is not None
def username_exists(self, db: Session, username: str) -> bool:
"""Check if username already exists"""
"""Check if username already exists."""
return db.query(User).filter(User.username == username).first() is not None
def authenticate_user(
self, db: Session, username: str, password: str
) -> Optional[User]:
"""Authenticate user with username/password"""
"""Authenticate user with username/password."""
return self.auth_manager.authenticate_user(db, username, password)
def create_access_token(self, user: User) -> Dict[str, Any]:
"""Create access token for user"""
"""Create access token for user."""
return self.auth_manager.create_access_token(user)
def hash_password(self, password: str) -> str:
"""Hash password"""
"""Hash password."""
return self.auth_manager.hash_password(password)

View File

@@ -1,3 +1,12 @@
# app/services/marketplace_service.py
"""Summary description ....
This module provides classes and functions for:
- ....
- ....
- ....
"""
import logging
from datetime import datetime
from typing import List, Optional
@@ -14,6 +23,7 @@ logger = logging.getLogger(__name__)
class MarketplaceService:
def __init__(self):
"""Class constructor."""
pass
def validate_shop_access(self, db: Session, shop_code: str, user: User) -> Shop:
@@ -37,7 +47,7 @@ class MarketplaceService:
def create_import_job(
self, db: Session, request: MarketplaceImportRequest, user: User
) -> MarketplaceImportJob:
"""Create a new marketplace import job"""
"""Create a new marketplace import job."""
# Validate shop access first
shop = self.validate_shop_access(db, request.shop_code, user)
@@ -65,7 +75,7 @@ class MarketplaceService:
def get_import_job_by_id(
self, db: Session, job_id: int, user: User
) -> MarketplaceImportJob:
"""Get a marketplace import job by ID with access control"""
"""Get a marketplace import job by ID with access control."""
job = (
db.query(MarketplaceImportJob)
.filter(MarketplaceImportJob.id == job_id)
@@ -89,7 +99,7 @@ class MarketplaceService:
skip: int = 0,
limit: int = 50,
) -> List[MarketplaceImportJob]:
"""Get marketplace import jobs with filtering and access control"""
"""Get marketplace import jobs with filtering and access control."""
query = db.query(MarketplaceImportJob)
# Users can only see their own jobs, admins can see all
@@ -117,7 +127,7 @@ class MarketplaceService:
def update_job_status(
self, db: Session, job_id: int, status: str, **kwargs
) -> MarketplaceImportJob:
"""Update marketplace import job status and other fields"""
"""Update marketplace import job status and other fields."""
job = (
db.query(MarketplaceImportJob)
.filter(MarketplaceImportJob.id == job_id)
@@ -151,7 +161,7 @@ class MarketplaceService:
return job
def get_job_stats(self, db: Session, user: User) -> dict:
"""Get statistics about marketplace import jobs for a user"""
"""Get statistics about marketplace import jobs for a user."""
query = db.query(MarketplaceImportJob)
# Users can only see their own jobs, admins can see all
@@ -177,7 +187,7 @@ class MarketplaceService:
def convert_to_response_model(
self, job: MarketplaceImportJob
) -> MarketplaceImportJobResponse:
"""Convert database model to API response model"""
"""Convert database model to API response model."""
return MarketplaceImportJobResponse(
job_id=job.id,
status=job.status,
@@ -200,7 +210,7 @@ class MarketplaceService:
def cancel_import_job(
self, db: Session, job_id: int, user: User
) -> MarketplaceImportJob:
"""Cancel a pending or running import job"""
"""Cancel a pending or running import job."""
job = self.get_import_job_by_id(db, job_id, user)
if job.status not in ["pending", "running"]:
@@ -216,7 +226,7 @@ class MarketplaceService:
return job
def delete_import_job(self, db: Session, job_id: int, user: User) -> bool:
"""Delete a marketplace import job"""
"""Delete a marketplace import job."""
job = self.get_import_job_by_id(db, job_id, user)
# Only allow deletion of completed, failed, or cancelled jobs

View File

@@ -1,3 +1,12 @@
# app/services/product_service.py
"""Summary description ....
This module provides classes and functions for:
- ....
- ....
- ....
"""
import logging
from datetime import datetime
from typing import Generator, List, Optional
@@ -15,11 +24,12 @@ logger = logging.getLogger(__name__)
class ProductService:
def __init__(self):
"""Class constructor."""
self.gtin_processor = GTINProcessor()
self.price_processor = PriceProcessor()
def create_product(self, db: Session, product_data: ProductCreate) -> Product:
"""Create a new product with validation"""
"""Create a new product with validation."""
try:
# Process and validate GTIN if provided
if product_data.gtin:
@@ -59,7 +69,7 @@ class ProductService:
raise
def get_product_by_id(self, db: Session, product_id: str) -> Optional[Product]:
"""Get a product by its ID"""
"""Get a product by its ID."""
return db.query(Product).filter(Product.product_id == product_id).first()
def get_products_with_filters(
@@ -74,7 +84,7 @@ class ProductService:
shop_name: Optional[str] = None,
search: Optional[str] = None,
) -> tuple[List[Product], int]:
"""Get products with filtering and pagination"""
"""Get products with filtering and pagination."""
query = db.query(Product)
# Apply filters
@@ -106,7 +116,7 @@ class ProductService:
def update_product(
self, db: Session, product_id: str, product_update: ProductUpdate
) -> Product:
"""Update product with validation"""
"""Update product with validation."""
product = db.query(Product).filter(Product.product_id == product_id).first()
if not product:
raise ValueError("Product not found")
@@ -141,7 +151,7 @@ class ProductService:
return product
def delete_product(self, db: Session, product_id: str) -> bool:
"""Delete product and associated stock"""
"""Delete product and associated stock."""
product = db.query(Product).filter(Product.product_id == product_id).first()
if not product:
raise ValueError("Product not found")
@@ -157,7 +167,7 @@ class ProductService:
return True
def get_stock_info(self, db: Session, gtin: str) -> Optional[StockSummaryResponse]:
"""Get stock information for a product by GTIN"""
"""Get stock information for a product by GTIN."""
stock_entries = db.query(Stock).filter(Stock.gtin == gtin).all()
if not stock_entries:
return None
@@ -178,7 +188,7 @@ class ProductService:
marketplace: Optional[str] = None,
shop_name: Optional[str] = None,
) -> Generator[str, None, None]:
"""Generate CSV export with streaming for memory efficiency"""
"""Generate CSV export with streaming for memory efficiency."""
# CSV header
yield (
"product_id,title,description,link,image_link,availability,price,currency,brand,"
@@ -214,7 +224,7 @@ class ProductService:
offset += batch_size
def product_exists(self, db: Session, product_id: str) -> bool:
"""Check if product exists by ID"""
"""Check if product exists by ID."""
return (
db.query(Product).filter(Product.product_id == product_id).first()
is not None

View File

@@ -1,6 +1,15 @@
# app/services/shop_service.py
"""Summary description ....
This module provides classes and functions for:
- ....
- ....
- ....
"""
import logging
from datetime import datetime
from typing import Any, Dict, List, Optional, Tuple
from typing import List, Optional, Tuple
from fastapi import HTTPException
from sqlalchemy import func
@@ -13,7 +22,7 @@ logger = logging.getLogger(__name__)
class ShopService:
"""Service class for shop operations following the application's service pattern"""
"""Service class for shop operations following the application's service pattern."""
def create_shop(
self, db: Session, shop_data: ShopCreate, current_user: User
@@ -75,7 +84,7 @@ class ShopService:
verified_only: bool = False,
) -> Tuple[List[Shop], int]:
"""
Get shops with filtering
Get shops with filtering.
Args:
db: Database session
@@ -110,7 +119,7 @@ class ShopService:
def get_shop_by_code(self, db: Session, shop_code: str, current_user: User) -> Shop:
"""
Get shop by shop code with access control
Get shop by shop code with access control.
Args:
db: Database session
@@ -145,7 +154,7 @@ class ShopService:
self, db: Session, shop: Shop, shop_product: ShopProductCreate
) -> ShopProduct:
"""
Add existing product to shop catalog with shop-specific settings
Add existing product to shop catalog with shop-specific settings.
Args:
db: Database session
@@ -211,7 +220,7 @@ class ShopService:
featured_only: bool = False,
) -> Tuple[List[ShopProduct], int]:
"""
Get products in shop catalog with filtering
Get products in shop catalog with filtering.
Args:
db: Database session

View File

@@ -1,21 +1,29 @@
# app/services/stats_service.py
"""Summary description ....
This module provides classes and functions for:
- ....
- ....
- ....
"""
import logging
from typing import Any, Dict, List
from sqlalchemy import func
from sqlalchemy.orm import Session
from models.api_models import MarketplaceStatsResponse, StatsResponse
from models.database_models import Product, Stock, User
from models.database_models import Product, Stock
logger = logging.getLogger(__name__)
class StatsService:
"""Service class for statistics operations following the application's service pattern"""
"""Service class for statistics operations following the application's service pattern."""
def get_comprehensive_stats(self, db: Session) -> Dict[str, Any]:
"""
Get comprehensive statistics with marketplace data
Get comprehensive statistics with marketplace data.
Args:
db: Database session
@@ -79,7 +87,7 @@ class StatsService:
def get_marketplace_breakdown_stats(self, db: Session) -> List[Dict[str, Any]]:
"""
Get statistics broken down by marketplace
Get statistics broken down by marketplace.
Args:
db: Database session
@@ -116,11 +124,11 @@ class StatsService:
return stats_list
def get_product_count(self, db: Session) -> int:
"""Get total product count"""
"""Get total product count."""
return db.query(Product).count()
def get_unique_brands_count(self, db: Session) -> int:
"""Get count of unique brands"""
"""Get count of unique brands."""
return (
db.query(Product.brand)
.filter(Product.brand.isnot(None), Product.brand != "")
@@ -129,7 +137,7 @@ class StatsService:
)
def get_unique_categories_count(self, db: Session) -> int:
"""Get count of unique categories"""
"""Get count of unique categories."""
return (
db.query(Product.google_product_category)
.filter(
@@ -141,7 +149,7 @@ class StatsService:
)
def get_unique_marketplaces_count(self, db: Session) -> int:
"""Get count of unique marketplaces"""
"""Get count of unique marketplaces."""
return (
db.query(Product.marketplace)
.filter(Product.marketplace.isnot(None), Product.marketplace != "")
@@ -150,7 +158,7 @@ class StatsService:
)
def get_unique_shops_count(self, db: Session) -> int:
"""Get count of unique shops"""
"""Get count of unique shops."""
return (
db.query(Product.shop_name)
.filter(Product.shop_name.isnot(None), Product.shop_name != "")
@@ -160,7 +168,7 @@ class StatsService:
def get_stock_statistics(self, db: Session) -> Dict[str, int]:
"""
Get stock-related statistics
Get stock-related statistics.
Args:
db: Database session
@@ -177,7 +185,7 @@ class StatsService:
}
def get_brands_by_marketplace(self, db: Session, marketplace: str) -> List[str]:
"""Get unique brands for a specific marketplace"""
"""Get unique brands for a specific marketplace."""
brands = (
db.query(Product.brand)
.filter(
@@ -191,7 +199,7 @@ class StatsService:
return [brand[0] for brand in brands]
def get_shops_by_marketplace(self, db: Session, marketplace: str) -> List[str]:
"""Get unique shops for a specific marketplace"""
"""Get unique shops for a specific marketplace."""
shops = (
db.query(Product.shop_name)
.filter(
@@ -205,7 +213,7 @@ class StatsService:
return [shop[0] for shop in shops]
def get_products_by_marketplace(self, db: Session, marketplace: str) -> int:
"""Get product count for a specific marketplace"""
"""Get product count for a specific marketplace."""
return db.query(Product).filter(Product.marketplace == marketplace).count()

View File

@@ -1,6 +1,15 @@
# app/services/stock_service.py
"""Summary description ....
This module provides classes and functions for:
- ....
- ....
- ....
"""
import logging
from datetime import datetime
from typing import List, Optional, Tuple
from typing import List, Optional
from sqlalchemy.orm import Session
@@ -13,15 +22,17 @@ logger = logging.getLogger(__name__)
class StockService:
"""Service class for stock operations following the application's service pattern."""
def __init__(self):
"""Class constructor."""
self.gtin_processor = GTINProcessor()
def normalize_gtin(self, gtin_value) -> Optional[str]:
"""Normalize GTIN format using the GTINProcessor"""
"""Normalize GTIN format using the GTINProcessor."""
return self.gtin_processor.normalize(gtin_value)
def set_stock(self, db: Session, stock_data: StockCreate) -> Stock:
"""Set exact stock quantity for a GTIN at a specific location (replaces existing quantity)"""
"""Set exact stock quantity for a GTIN at a specific location (replaces existing quantity)."""
normalized_gtin = self.normalize_gtin(stock_data.gtin)
if not normalized_gtin:
raise ValueError("Invalid GTIN format")
@@ -60,7 +71,7 @@ class StockService:
return new_stock
def add_stock(self, db: Session, stock_data: StockAdd) -> Stock:
"""Add quantity to existing stock for a GTIN at a specific location (adds to existing quantity)"""
"""Add quantity to existing stock for a GTIN at a specific location (adds to existing quantity)."""
normalized_gtin = self.normalize_gtin(stock_data.gtin)
if not normalized_gtin:
raise ValueError("Invalid GTIN format")
@@ -82,7 +93,8 @@ class StockService:
db.commit()
db.refresh(existing_stock)
logger.info(
f"Added stock for GTIN {normalized_gtin} at {location}: {old_quantity} + {stock_data.quantity} = {existing_stock.quantity}"
f"Added stock for GTIN {normalized_gtin} at {location}: "
f"{old_quantity} + {stock_data.quantity} = {existing_stock.quantity}"
)
return existing_stock
else:
@@ -99,7 +111,7 @@ class StockService:
return new_stock
def remove_stock(self, db: Session, stock_data: StockAdd) -> Stock:
"""Remove quantity from existing stock for a GTIN at a specific location"""
"""Remove quantity from existing stock for a GTIN at a specific location."""
normalized_gtin = self.normalize_gtin(stock_data.gtin)
if not normalized_gtin:
raise ValueError("Invalid GTIN format")
@@ -131,12 +143,13 @@ class StockService:
db.commit()
db.refresh(existing_stock)
logger.info(
f"Removed stock for GTIN {normalized_gtin} at {location}: {old_quantity} - {stock_data.quantity} = {existing_stock.quantity}"
f"Removed stock for GTIN {normalized_gtin} at {location}: "
f"{old_quantity} - {stock_data.quantity} = {existing_stock.quantity}"
)
return existing_stock
def get_stock_by_gtin(self, db: Session, gtin: str) -> StockSummaryResponse:
"""Get all stock locations and total quantity for a specific GTIN"""
"""Get all stock locations and total quantity for a specific GTIN."""
normalized_gtin = self.normalize_gtin(gtin)
if not normalized_gtin:
raise ValueError("Invalid GTIN format")
@@ -169,7 +182,7 @@ class StockService:
)
def get_total_stock(self, db: Session, gtin: str) -> dict:
"""Get total quantity in stock for a specific GTIN"""
"""Get total quantity in stock for a specific GTIN."""
normalized_gtin = self.normalize_gtin(gtin)
if not normalized_gtin:
raise ValueError("Invalid GTIN format")
@@ -196,7 +209,7 @@ class StockService:
location: Optional[str] = None,
gtin: Optional[str] = None,
) -> List[Stock]:
"""Get all stock entries with optional filtering"""
"""Get all stock entries with optional filtering."""
query = db.query(Stock)
if location:
@@ -212,7 +225,7 @@ class StockService:
def update_stock(
self, db: Session, stock_id: int, stock_update: StockUpdate
) -> Stock:
"""Update stock quantity for a specific stock entry"""
"""Update stock quantity for a specific stock entry."""
stock_entry = db.query(Stock).filter(Stock.id == stock_id).first()
if not stock_entry:
raise ValueError("Stock entry not found")
@@ -228,7 +241,7 @@ class StockService:
return stock_entry
def delete_stock(self, db: Session, stock_id: int) -> bool:
"""Delete a stock entry"""
"""Delete a stock entry."""
stock_entry = db.query(Stock).filter(Stock.id == stock_id).first()
if not stock_entry:
raise ValueError("Stock entry not found")
@@ -242,7 +255,7 @@ class StockService:
return True
def get_stock_by_id(self, db: Session, stock_id: int) -> Optional[Stock]:
"""Get a stock entry by its ID"""
"""Get a stock entry by its ID."""
return db.query(Stock).filter(Stock.id == stock_id).first()