Refactoring code for modular approach
This commit is contained in:
202
app/services/marketplace_service.py
Normal file
202
app/services/marketplace_service.py
Normal file
@@ -0,0 +1,202 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from models.database_models import MarketplaceImportJob, Shop, User
|
||||
from models.api_models import MarketplaceImportRequest, MarketplaceImportJobResponse
|
||||
from typing import Optional, List
|
||||
from datetime import datetime
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class MarketplaceService:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def validate_shop_access(self, db: Session, shop_code: str, user: User) -> Shop:
|
||||
"""Validate that the shop exists and user has access to it"""
|
||||
shop = db.query(Shop).filter(Shop.shop_code == shop_code).first()
|
||||
if not shop:
|
||||
raise ValueError("Shop not found")
|
||||
|
||||
# Check permissions: admin can import for any shop, others only for their own
|
||||
if user.role != "admin" and shop.owner_id != user.id:
|
||||
raise PermissionError("Access denied to this shop")
|
||||
|
||||
return shop
|
||||
|
||||
def create_import_job(
|
||||
self,
|
||||
db: Session,
|
||||
request: MarketplaceImportRequest,
|
||||
user: User
|
||||
) -> MarketplaceImportJob:
|
||||
"""Create a new marketplace import job"""
|
||||
# Validate shop access first
|
||||
shop = self.validate_shop_access(db, request.shop_code, user)
|
||||
|
||||
# Create marketplace import job record
|
||||
import_job = MarketplaceImportJob(
|
||||
status="pending",
|
||||
source_url=request.url,
|
||||
marketplace=request.marketplace,
|
||||
shop_code=request.shop_code,
|
||||
user_id=user.id,
|
||||
created_at=datetime.utcnow()
|
||||
)
|
||||
|
||||
db.add(import_job)
|
||||
db.commit()
|
||||
db.refresh(import_job)
|
||||
|
||||
logger.info(
|
||||
f"Created marketplace import job {import_job.id}: {request.marketplace} -> {request.shop_code} by user {user.username}")
|
||||
|
||||
return import_job
|
||||
|
||||
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"""
|
||||
job = db.query(MarketplaceImportJob).filter(MarketplaceImportJob.id == job_id).first()
|
||||
if not job:
|
||||
raise ValueError("Marketplace import job not found")
|
||||
|
||||
# Users can only see their own jobs, admins can see all
|
||||
if user.role != "admin" and job.user_id != user.id:
|
||||
raise PermissionError("Access denied to this import job")
|
||||
|
||||
return job
|
||||
|
||||
def get_import_jobs(
|
||||
self,
|
||||
db: Session,
|
||||
user: User,
|
||||
marketplace: Optional[str] = None,
|
||||
shop_name: Optional[str] = None,
|
||||
skip: int = 0,
|
||||
limit: int = 50
|
||||
) -> List[MarketplaceImportJob]:
|
||||
"""Get marketplace import jobs with filtering and access control"""
|
||||
query = db.query(MarketplaceImportJob)
|
||||
|
||||
# Users can only see their own jobs, admins can see all
|
||||
if user.role != "admin":
|
||||
query = query.filter(MarketplaceImportJob.user_id == 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 jobs
|
||||
|
||||
def update_job_status(
|
||||
self,
|
||||
db: Session,
|
||||
job_id: int,
|
||||
status: str,
|
||||
**kwargs
|
||||
) -> MarketplaceImportJob:
|
||||
"""Update marketplace import job status and other fields"""
|
||||
job = db.query(MarketplaceImportJob).filter(MarketplaceImportJob.id == job_id).first()
|
||||
if not job:
|
||||
raise ValueError("Marketplace import job not found")
|
||||
|
||||
job.status = status
|
||||
|
||||
# Update optional fields if provided
|
||||
if 'imported_count' in kwargs:
|
||||
job.imported_count = kwargs['imported_count']
|
||||
if 'updated_count' in kwargs:
|
||||
job.updated_count = kwargs['updated_count']
|
||||
if 'total_processed' in kwargs:
|
||||
job.total_processed = kwargs['total_processed']
|
||||
if 'error_count' in kwargs:
|
||||
job.error_count = kwargs['error_count']
|
||||
if 'error_message' in kwargs:
|
||||
job.error_message = kwargs['error_message']
|
||||
if 'started_at' in kwargs:
|
||||
job.started_at = kwargs['started_at']
|
||||
if 'completed_at' in kwargs:
|
||||
job.completed_at = kwargs['completed_at']
|
||||
|
||||
db.commit()
|
||||
db.refresh(job)
|
||||
|
||||
logger.info(f"Updated marketplace import job {job_id} status to {status}")
|
||||
return job
|
||||
|
||||
def get_job_stats(self, db: Session, user: User) -> dict:
|
||||
"""Get statistics about marketplace import jobs for a user"""
|
||||
query = db.query(MarketplaceImportJob)
|
||||
|
||||
# Users can only see their own jobs, admins can see all
|
||||
if user.role != "admin":
|
||||
query = query.filter(MarketplaceImportJob.user_id == user.id)
|
||||
|
||||
total_jobs = query.count()
|
||||
pending_jobs = query.filter(MarketplaceImportJob.status == "pending").count()
|
||||
running_jobs = query.filter(MarketplaceImportJob.status == "running").count()
|
||||
completed_jobs = query.filter(MarketplaceImportJob.status == "completed").count()
|
||||
failed_jobs = query.filter(MarketplaceImportJob.status == "failed").count()
|
||||
|
||||
return {
|
||||
"total_jobs": total_jobs,
|
||||
"pending_jobs": pending_jobs,
|
||||
"running_jobs": running_jobs,
|
||||
"completed_jobs": completed_jobs,
|
||||
"failed_jobs": failed_jobs
|
||||
}
|
||||
|
||||
def convert_to_response_model(self, job: MarketplaceImportJob) -> MarketplaceImportJobResponse:
|
||||
"""Convert database model to API response model"""
|
||||
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
|
||||
)
|
||||
|
||||
def cancel_import_job(self, db: Session, job_id: int, user: User) -> MarketplaceImportJob:
|
||||
"""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"]:
|
||||
raise ValueError(f"Cannot cancel job with status: {job.status}")
|
||||
|
||||
job.status = "cancelled"
|
||||
job.completed_at = datetime.utcnow()
|
||||
|
||||
db.commit()
|
||||
db.refresh(job)
|
||||
|
||||
logger.info(f"Cancelled marketplace import job {job_id}")
|
||||
return job
|
||||
|
||||
def delete_import_job(self, db: Session, job_id: int, user: User) -> bool:
|
||||
"""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
|
||||
if job.status in ["pending", "running"]:
|
||||
raise ValueError(f"Cannot delete job with status: {job.status}. Cancel it first.")
|
||||
|
||||
db.delete(job)
|
||||
db.commit()
|
||||
|
||||
logger.info(f"Deleted marketplace import job {job_id}")
|
||||
return True
|
||||
|
||||
|
||||
# Create service instance
|
||||
marketplace_service = MarketplaceService()
|
||||
@@ -1,8 +1,9 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from models.database_models import Product
|
||||
from models.api_models import ProductCreate
|
||||
from models.database_models import Product, Stock
|
||||
from models.api_models import ProductCreate, ProductUpdate, StockLocationResponse, StockSummaryResponse
|
||||
from utils.data_processing import GTINProcessor, PriceProcessor
|
||||
from typing import Optional, List
|
||||
from typing import Optional, List, Generator
|
||||
from datetime import datetime
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -41,6 +42,10 @@ class ProductService:
|
||||
logger.info(f"Created product {db_product.product_id}")
|
||||
return db_product
|
||||
|
||||
def get_product_by_id(self, db: Session, product_id: str) -> Optional[Product]:
|
||||
"""Get a product by its ID"""
|
||||
return db.query(Product).filter(Product.product_id == product_id).first()
|
||||
|
||||
def get_products_with_filters(
|
||||
self,
|
||||
db: Session,
|
||||
@@ -48,7 +53,9 @@ class ProductService:
|
||||
limit: int = 100,
|
||||
brand: Optional[str] = None,
|
||||
category: Optional[str] = None,
|
||||
availability: Optional[str] = None,
|
||||
marketplace: Optional[str] = None,
|
||||
shop_name: Optional[str] = None,
|
||||
search: Optional[str] = None
|
||||
) -> tuple[List[Product], int]:
|
||||
"""Get products with filtering and pagination"""
|
||||
@@ -59,14 +66,20 @@ class ProductService:
|
||||
query = query.filter(Product.brand.ilike(f"%{brand}%"))
|
||||
if category:
|
||||
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, 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.marketplace.ilike(search_term)) |
|
||||
(Product.shop_name.ilike(search_term))
|
||||
)
|
||||
|
||||
total = query.count()
|
||||
@@ -74,6 +87,114 @@ class ProductService:
|
||||
|
||||
return products, total
|
||||
|
||||
def update_product(self, db: Session, product_id: str, product_update: ProductUpdate) -> Product:
|
||||
"""Update product with validation"""
|
||||
product = db.query(Product).filter(Product.product_id == product_id).first()
|
||||
if not product:
|
||||
raise ValueError("Product not found")
|
||||
|
||||
# Update fields
|
||||
update_data = product_update.dict(exclude_unset=True)
|
||||
|
||||
# Validate GTIN if being updated
|
||||
if "gtin" in update_data and update_data["gtin"]:
|
||||
normalized_gtin = self.gtin_processor.normalize(update_data["gtin"])
|
||||
if not normalized_gtin:
|
||||
raise ValueError("Invalid GTIN format")
|
||||
update_data["gtin"] = normalized_gtin
|
||||
|
||||
# Process price if being updated
|
||||
if "price" in update_data and update_data["price"]:
|
||||
parsed_price, currency = self.price_processor.parse_price_currency(update_data["price"])
|
||||
if parsed_price:
|
||||
update_data["price"] = parsed_price
|
||||
update_data["currency"] = currency
|
||||
|
||||
for key, value in update_data.items():
|
||||
setattr(product, key, value)
|
||||
|
||||
product.updated_at = datetime.utcnow()
|
||||
db.commit()
|
||||
db.refresh(product)
|
||||
|
||||
logger.info(f"Updated product {product_id}")
|
||||
return product
|
||||
|
||||
def delete_product(self, db: Session, product_id: str) -> bool:
|
||||
"""Delete product and associated stock"""
|
||||
product = db.query(Product).filter(Product.product_id == product_id).first()
|
||||
if not product:
|
||||
raise ValueError("Product not found")
|
||||
|
||||
# Delete associated stock entries if GTIN exists
|
||||
if product.gtin:
|
||||
db.query(Stock).filter(Stock.gtin == product.gtin).delete()
|
||||
|
||||
db.delete(product)
|
||||
db.commit()
|
||||
|
||||
logger.info(f"Deleted product {product_id}")
|
||||
return True
|
||||
|
||||
def get_stock_info(self, db: Session, gtin: str) -> Optional[StockSummaryResponse]:
|
||||
"""Get stock information for a product by GTIN"""
|
||||
stock_entries = db.query(Stock).filter(Stock.gtin == gtin).all()
|
||||
if not stock_entries:
|
||||
return None
|
||||
|
||||
total_quantity = sum(entry.quantity for entry in stock_entries)
|
||||
locations = [
|
||||
StockLocationResponse(location=entry.location, quantity=entry.quantity)
|
||||
for entry in stock_entries
|
||||
]
|
||||
|
||||
return StockSummaryResponse(
|
||||
gtin=gtin,
|
||||
total_quantity=total_quantity,
|
||||
locations=locations
|
||||
)
|
||||
|
||||
def generate_csv_export(
|
||||
self,
|
||||
db: Session,
|
||||
marketplace: Optional[str] = None,
|
||||
shop_name: Optional[str] = None
|
||||
) -> Generator[str, None, None]:
|
||||
"""Generate CSV export with streaming for memory efficiency"""
|
||||
# CSV header
|
||||
yield ("product_id,title,description,link,image_link,availability,price,currency,brand,"
|
||||
"gtin,marketplace,shop_name\n")
|
||||
|
||||
batch_size = 1000
|
||||
offset = 0
|
||||
|
||||
while True:
|
||||
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 with marketplace fields
|
||||
row = (f'"{product.product_id}","{product.title or ""}","{product.description or ""}",'
|
||||
f'"{product.link or ""}","{product.image_link or ""}","{product.availability or ""}",'
|
||||
f'"{product.price or ""}","{product.currency or ""}","{product.brand or ""}",'
|
||||
f'"{product.gtin or ""}","{product.marketplace or ""}","{product.shop_name or ""}"\n')
|
||||
yield row
|
||||
|
||||
offset += batch_size
|
||||
|
||||
def product_exists(self, db: Session, product_id: str) -> bool:
|
||||
"""Check if product exists by ID"""
|
||||
return db.query(Product).filter(Product.product_id == product_id).first() is not None
|
||||
|
||||
|
||||
# Create service instance
|
||||
product_service = ProductService()
|
||||
|
||||
@@ -0,0 +1,235 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from models.database_models import Stock, Product
|
||||
from models.api_models import StockCreate, StockAdd, StockUpdate, StockLocationResponse, StockSummaryResponse
|
||||
from utils.data_processing import GTINProcessor
|
||||
from typing import Optional, List, Tuple
|
||||
from datetime import datetime
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class StockService:
|
||||
def __init__(self):
|
||||
self.gtin_processor = GTINProcessor()
|
||||
|
||||
def normalize_gtin(self, gtin_value) -> Optional[str]:
|
||||
"""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)"""
|
||||
normalized_gtin = self.normalize_gtin(stock_data.gtin)
|
||||
if not normalized_gtin:
|
||||
raise ValueError("Invalid GTIN format")
|
||||
|
||||
location = stock_data.location.strip().upper()
|
||||
|
||||
# Check if stock entry already exists for this GTIN and location
|
||||
existing_stock = db.query(Stock).filter(
|
||||
Stock.gtin == normalized_gtin,
|
||||
Stock.location == location
|
||||
).first()
|
||||
|
||||
if existing_stock:
|
||||
# Update existing stock (SET to exact quantity)
|
||||
old_quantity = existing_stock.quantity
|
||||
existing_stock.quantity = stock_data.quantity
|
||||
existing_stock.updated_at = datetime.utcnow()
|
||||
db.commit()
|
||||
db.refresh(existing_stock)
|
||||
logger.info(
|
||||
f"Updated stock for GTIN {normalized_gtin} at {location}: {old_quantity} → {stock_data.quantity}")
|
||||
return existing_stock
|
||||
else:
|
||||
# Create new stock entry
|
||||
new_stock = Stock(
|
||||
gtin=normalized_gtin,
|
||||
location=location,
|
||||
quantity=stock_data.quantity
|
||||
)
|
||||
db.add(new_stock)
|
||||
db.commit()
|
||||
db.refresh(new_stock)
|
||||
logger.info(f"Created new stock for GTIN {normalized_gtin} at {location}: {stock_data.quantity}")
|
||||
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)"""
|
||||
normalized_gtin = self.normalize_gtin(stock_data.gtin)
|
||||
if not normalized_gtin:
|
||||
raise ValueError("Invalid GTIN format")
|
||||
|
||||
location = stock_data.location.strip().upper()
|
||||
|
||||
# Check if stock entry already exists for this GTIN and location
|
||||
existing_stock = db.query(Stock).filter(
|
||||
Stock.gtin == normalized_gtin,
|
||||
Stock.location == location
|
||||
).first()
|
||||
|
||||
if existing_stock:
|
||||
# Add to existing stock
|
||||
old_quantity = existing_stock.quantity
|
||||
existing_stock.quantity += stock_data.quantity
|
||||
existing_stock.updated_at = datetime.utcnow()
|
||||
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}")
|
||||
return existing_stock
|
||||
else:
|
||||
# Create new stock entry with the quantity
|
||||
new_stock = Stock(
|
||||
gtin=normalized_gtin,
|
||||
location=location,
|
||||
quantity=stock_data.quantity
|
||||
)
|
||||
db.add(new_stock)
|
||||
db.commit()
|
||||
db.refresh(new_stock)
|
||||
logger.info(f"Created new stock for GTIN {normalized_gtin} at {location}: {stock_data.quantity}")
|
||||
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"""
|
||||
normalized_gtin = self.normalize_gtin(stock_data.gtin)
|
||||
if not normalized_gtin:
|
||||
raise ValueError("Invalid GTIN format")
|
||||
|
||||
location = stock_data.location.strip().upper()
|
||||
|
||||
# Find existing stock entry
|
||||
existing_stock = db.query(Stock).filter(
|
||||
Stock.gtin == normalized_gtin,
|
||||
Stock.location == location
|
||||
).first()
|
||||
|
||||
if not existing_stock:
|
||||
raise ValueError(f"No stock found for GTIN {normalized_gtin} at location {location}")
|
||||
|
||||
# Check if we have enough stock to remove
|
||||
if existing_stock.quantity < stock_data.quantity:
|
||||
raise ValueError(
|
||||
f"Insufficient stock. Available: {existing_stock.quantity}, Requested to remove: {stock_data.quantity}")
|
||||
|
||||
# Remove from existing stock
|
||||
old_quantity = existing_stock.quantity
|
||||
existing_stock.quantity -= stock_data.quantity
|
||||
existing_stock.updated_at = datetime.utcnow()
|
||||
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}")
|
||||
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"""
|
||||
normalized_gtin = self.normalize_gtin(gtin)
|
||||
if not normalized_gtin:
|
||||
raise ValueError("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 ValueError(f"No stock found for GTIN: {gtin}")
|
||||
|
||||
# Calculate total quantity and build locations list
|
||||
total_quantity = 0
|
||||
locations = []
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
def get_total_stock(self, db: Session, gtin: str) -> dict:
|
||||
"""Get total quantity in stock for a specific GTIN"""
|
||||
normalized_gtin = self.normalize_gtin(gtin)
|
||||
if not normalized_gtin:
|
||||
raise ValueError("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)
|
||||
}
|
||||
|
||||
def get_all_stock(
|
||||
self,
|
||||
db: Session,
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
location: Optional[str] = None,
|
||||
gtin: Optional[str] = None
|
||||
) -> List[Stock]:
|
||||
"""Get all stock entries with optional filtering"""
|
||||
query = db.query(Stock)
|
||||
|
||||
if location:
|
||||
query = query.filter(Stock.location.ilike(f"%{location}%"))
|
||||
|
||||
if gtin:
|
||||
normalized_gtin = self.normalize_gtin(gtin)
|
||||
if normalized_gtin:
|
||||
query = query.filter(Stock.gtin == normalized_gtin)
|
||||
|
||||
return query.offset(skip).limit(limit).all()
|
||||
|
||||
def update_stock(self, db: Session, stock_id: int, stock_update: StockUpdate) -> Stock:
|
||||
"""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")
|
||||
|
||||
stock_entry.quantity = stock_update.quantity
|
||||
stock_entry.updated_at = datetime.utcnow()
|
||||
db.commit()
|
||||
db.refresh(stock_entry)
|
||||
|
||||
logger.info(f"Updated stock entry {stock_id} to quantity {stock_update.quantity}")
|
||||
return stock_entry
|
||||
|
||||
def delete_stock(self, db: Session, stock_id: int) -> bool:
|
||||
"""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")
|
||||
|
||||
gtin = stock_entry.gtin
|
||||
location = stock_entry.location
|
||||
db.delete(stock_entry)
|
||||
db.commit()
|
||||
|
||||
logger.info(f"Deleted stock entry {stock_id} for GTIN {gtin} at {location}")
|
||||
return True
|
||||
|
||||
def get_stock_by_id(self, db: Session, stock_id: int) -> Optional[Stock]:
|
||||
"""Get a stock entry by its ID"""
|
||||
return db.query(Stock).filter(Stock.id == stock_id).first()
|
||||
|
||||
|
||||
# Create service instance
|
||||
stock_service = StockService()
|
||||
|
||||
Reference in New Issue
Block a user