Squashed commit of the following:
commit893b229cc6
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 22:14:08 2025 -0500 j commit06f539aea2
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:55:30 2025 -0500 fk commitd0c2960ec9
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:50:53 2025 -0500 frick commit6b1362c166
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:49:40 2025 -0500 database commit8cadc6df4c
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:38:09 2025 -0500 asdf commit1ca6f98684
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:32:50 2025 -0500 fffff commit8bb337a9c3
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:31:13 2025 -0500 ffff commit65aba280c5
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:26:16 2025 -0500 aa commit59ef03a59e
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:24:21 2025 -0500 asdf commitf44d5740fc
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:23:32 2025 -0500 aaa commit13c96b1643
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:18:54 2025 -0500 sdf commit949c795fd1
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:17:53 2025 -0500 asdf commit8c3cd423fe
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:56:01 2025 -0500 app2 commit78eafc739e
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:54:55 2025 -0500 app commitdc47eced14
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:43:15 2025 -0500 asdfasdfasdf commite24bcae88c
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:39:44 2025 -0500 a commitc894451bfe
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:38:20 2025 -0500 req commit3d09869562
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:33:27 2025 -0500 wrong number = code dont work lol i love computers commit4c93a1271b
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:29:39 2025 -0500 q commit1f5361da88
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 18:27:20 2025 -0500 same as original code now -5 days of my life commit511b070cbb
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 13:52:28 2025 -0500 pricey worky commit964fdd641b
Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 11:37:29 2025 -0500 prep for pricing service work commita78c3bcba3
Author: zman <joshua.k.rzemien@gmail.com> Date: Wed Feb 5 21:51:22 2025 -0500 more stuff yay commitbd9cfca7a9
Author: zman <joshua.k.rzemien@gmail.com> Date: Tue Feb 4 22:30:33 2025 -0500 GIGA FIXED EVERYTHING OMG commit85510a4671
Author: zman <joshua.k.rzemien@gmail.com> Date: Tue Feb 4 00:01:34 2025 -0500 data model change and some new services
This commit is contained in:
90
app/services/inventory.py
Normal file
90
app/services/inventory.py
Normal file
@@ -0,0 +1,90 @@
|
||||
from typing import Dict
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from app.db.models import Product, Inventory
|
||||
from app.schemas.inventory import UpdateInventoryResponse
|
||||
from app.db.utils import db_transaction
|
||||
|
||||
|
||||
class InventoryService:
|
||||
"""Service class for managing product inventory operations."""
|
||||
|
||||
def __init__(self, db: Session) -> None:
|
||||
"""
|
||||
Initialize the InventoryService.
|
||||
|
||||
Args:
|
||||
db: SQLAlchemy database session
|
||||
"""
|
||||
self.db = db
|
||||
|
||||
def add_inventory(self, product: Product, quantity: int) -> Inventory:
|
||||
"""
|
||||
Add or update inventory for a product.
|
||||
|
||||
Args:
|
||||
product: Product model instance
|
||||
quantity: Quantity to add to inventory
|
||||
|
||||
Returns:
|
||||
Updated Inventory model instance
|
||||
"""
|
||||
inventory = self.db.query(Inventory).filter(
|
||||
Inventory.product_id == product.id
|
||||
).first()
|
||||
|
||||
if inventory is None:
|
||||
inventory = Inventory(
|
||||
product_id=product.id,
|
||||
quantity=quantity
|
||||
)
|
||||
self.db.add(inventory)
|
||||
else:
|
||||
inventory.quantity += quantity
|
||||
|
||||
return inventory
|
||||
|
||||
def process_staged_products(
|
||||
self,
|
||||
product_data: Dict[Product, int]
|
||||
) -> UpdateInventoryResponse:
|
||||
"""
|
||||
Process multiple products and update their inventory.
|
||||
|
||||
Args:
|
||||
product_data: Dictionary mapping Products to their quantities
|
||||
|
||||
Returns:
|
||||
Response indicating success status
|
||||
"""
|
||||
try:
|
||||
with db_transaction(self.db):
|
||||
for product, quantity in product_data.items():
|
||||
self.add_inventory(product, quantity)
|
||||
return UpdateInventoryResponse(success=True)
|
||||
except SQLAlchemyError:
|
||||
return UpdateInventoryResponse(success=False)
|
||||
|
||||
def add_sealed_box_to_inventory(
|
||||
self,
|
||||
product: Product,
|
||||
quantity: int
|
||||
) -> UpdateInventoryResponse:
|
||||
"""
|
||||
Add sealed box inventory for a single product.
|
||||
|
||||
Args:
|
||||
product: Product model instance
|
||||
quantity: Quantity to add to inventory
|
||||
|
||||
Returns:
|
||||
Response indicating success status
|
||||
"""
|
||||
try:
|
||||
with db_transaction(self.db):
|
||||
self.add_inventory(product, quantity)
|
||||
return UpdateInventoryResponse(success=True)
|
||||
except SQLAlchemyError:
|
||||
return UpdateInventoryResponse(success=False)
|
Reference in New Issue
Block a user