commit 893b229cc6b35c09181a84050f34fb79024e41c2 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 22:14:08 2025 -0500 j commit 06f539aea2f4fff9da7038d43d0de553c4423796 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:55:30 2025 -0500 fk commit d0c2960ec9f334448d2eb3573b9d7817482abf46 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:50:53 2025 -0500 frick commit 6b1362c166fc5f51c3bcf316a99116f0d11074a5 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:49:40 2025 -0500 database commit 8cadc6df4c817d9d05503807e56287fd00e5e939 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:38:09 2025 -0500 asdf commit 1ca6f9868452e34143b8df4a412be35e6902a31e Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:32:50 2025 -0500 fffff commit 8bb337a9c35e830ef9ce3dac0a0f2df3fe9bc5a0 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:31:13 2025 -0500 ffff commit 65aba280c55fa09c6a37f688f485efab1f70792b Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:26:16 2025 -0500 aa commit 59ef03a59ee4a15c30e080a1aef7c31c0214a2e3 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:24:21 2025 -0500 asdf commit f44d5740fc9315ccb0792ecac3e8ec9f28f171be Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:23:32 2025 -0500 aaa commit 13c96b164316b4908d9d01e454cbdc9103157558 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:18:54 2025 -0500 sdf commit 949c795fd13d93c9618613740fb093f6bb7b7710 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 21:17:53 2025 -0500 asdf commit 8c3cd423fe228e8aff112a050170246a5fc9f8bd Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:56:01 2025 -0500 app2 commit 78eafc739ebb7f100f657964b3ad8f4937a4046b Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:54:55 2025 -0500 app commit dc47eced143e77ebec415bdfbe209d9466b7bcf1 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:43:15 2025 -0500 asdfasdfasdf commit e24bcae88cf8c14ea543f49b639b2976c627d201 Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:39:44 2025 -0500 a commit c894451bfe790c97ac0e01085615d7c7288a39da Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:38:20 2025 -0500 req commit 3d09869562a96b5adc7c4be279bc8c003bbb37b2 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 commit 4c93a1271b8aea159cf53f8d7879b00513886d6f Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 20:29:39 2025 -0500 q commit 1f5361da88fe3903a1e92a345fa56bb390f69d92 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 commit 511b070cbbcd29b4e784e9a09d58481e50e6e82f Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 13:52:28 2025 -0500 pricey worky commit 964fdd641b63530c59e038ebc7d1e01e9570d75c Author: zman <joshua.k.rzemien@gmail.com> Date: Fri Feb 7 11:37:29 2025 -0500 prep for pricing service work commit a78c3bcba303c2605b6277c1db33b155abe4db1b Author: zman <joshua.k.rzemien@gmail.com> Date: Wed Feb 5 21:51:22 2025 -0500 more stuff yay commit bd9cfca7a95c89b2140eec57bf52bc84432b9a4e Author: zman <joshua.k.rzemien@gmail.com> Date: Tue Feb 4 22:30:33 2025 -0500 GIGA FIXED EVERYTHING OMG commit 85510a46713e0ac660e70c7befb4e94ccf11912e Author: zman <joshua.k.rzemien@gmail.com> Date: Tue Feb 4 00:01:34 2025 -0500 data model change and some new services
202 lines
7.5 KiB
Python
202 lines
7.5 KiB
Python
from datetime import datetime
|
|
from typing import Dict, List
|
|
from uuid import uuid4
|
|
from sqlalchemy import or_
|
|
from sqlalchemy.orm import Session
|
|
import logging
|
|
|
|
from app.db.models import (
|
|
Box,
|
|
File,
|
|
StagedFileProduct,
|
|
Product,
|
|
OpenBoxCard,
|
|
OpenBox,
|
|
TCGPlayerGroups,
|
|
Inventory
|
|
)
|
|
from app.db.utils import db_transaction
|
|
from app.schemas.box import CreateBoxRequest, UpdateBoxRequest, CreateOpenBoxRequest
|
|
from app.services.inventory import InventoryService
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
VALID_BOX_TYPES = {"collector", "play", "draft", "set", "commander"}
|
|
|
|
class BoxService:
|
|
def __init__(self, db: Session, inventory_service: InventoryService):
|
|
self.db = db
|
|
self.inventory_service = inventory_service
|
|
|
|
def get_staged_product_data(self, file_ids: List[str]) -> List[StagedFileProduct]:
|
|
"""Retrieve staged product data for given file IDs."""
|
|
return self.db.query(StagedFileProduct).filter(
|
|
StagedFileProduct.file_id.in_(file_ids)
|
|
).all()
|
|
|
|
def aggregate_staged_product_data(self, staged_product_data: List[StagedFileProduct]) -> Dict[Product, int]:
|
|
"""Aggregate staged product data by product and quantity."""
|
|
product_data = {}
|
|
for row in staged_product_data:
|
|
product = self.db.query(Product).filter(Product.id == row.product_id).first()
|
|
if product:
|
|
product_data[product] = product_data.get(product, 0) + row.quantity
|
|
return product_data
|
|
|
|
def add_products_to_open_box(self, open_box: OpenBox, product_data: Dict[Product, int]) -> None:
|
|
"""Add products to an open box."""
|
|
for product, quantity in product_data.items():
|
|
open_box_card = OpenBoxCard(
|
|
id=str(uuid4()),
|
|
open_box_id=open_box.id,
|
|
card_id=product.id,
|
|
quantity=quantity
|
|
)
|
|
self.db.add(open_box_card)
|
|
|
|
def validate_box_type(self, box_type: str) -> bool:
|
|
"""Validate if the box type is supported."""
|
|
return box_type in VALID_BOX_TYPES
|
|
|
|
def validate_set_code(self, set_code: str) -> bool:
|
|
"""Validate if the set code exists in TCGPlayer groups."""
|
|
return self.db.query(TCGPlayerGroups).filter(
|
|
TCGPlayerGroups.abbreviation == set_code
|
|
).first() is not None
|
|
|
|
def create_box(self, create_box_data: CreateBoxRequest) -> Box:
|
|
"""Create a new box."""
|
|
if not self.validate_box_type(create_box_data.type):
|
|
raise ValueError("Invalid box type")
|
|
if not self.validate_set_code(create_box_data.set_code):
|
|
raise ValueError("Invalid set code")
|
|
|
|
existing_box = self.db.query(Box).filter(
|
|
Box.type == create_box_data.type,
|
|
Box.set_code == create_box_data.set_code,
|
|
or_(Box.sku == create_box_data.sku, Box.sku.is_(None))
|
|
).first()
|
|
|
|
if existing_box:
|
|
return existing_box, False
|
|
else:
|
|
with db_transaction(self.db):
|
|
product = Product(
|
|
id=str(uuid4()),
|
|
type='box',
|
|
product_line='mtg'
|
|
)
|
|
box = Box(
|
|
product_id=product.id,
|
|
type=create_box_data.type,
|
|
set_code=create_box_data.set_code,
|
|
sku=create_box_data.sku,
|
|
num_cards_expected=create_box_data.num_cards_expected
|
|
)
|
|
self.db.add(product)
|
|
self.db.add(box)
|
|
|
|
return box, True
|
|
|
|
def update_box(self, box_id: str, update_box_data: UpdateBoxRequest) -> Box:
|
|
"""Update an existing box."""
|
|
box = self.db.query(Box).filter(Box.product_id == box_id).first()
|
|
if not box:
|
|
raise ValueError("Box not found")
|
|
|
|
update_data = update_box_data.dict(exclude_unset=True)
|
|
|
|
# Validate box type if it's being updated
|
|
if "type" in update_data and update_data["type"] is not None:
|
|
if not self.validate_box_type(update_data["type"]):
|
|
raise ValueError(f"Invalid box type: {update_data['type']}")
|
|
|
|
# Validate set code if it's being updated
|
|
if "set_code" in update_data and update_data["set_code"] is not None:
|
|
if not self.validate_set_code(update_data["set_code"]):
|
|
raise ValueError(f"Invalid set code: {update_data['set_code']}")
|
|
|
|
with db_transaction(self.db):
|
|
for field, value in update_data.items():
|
|
if value is not None: # Only update non-None values
|
|
setattr(box, field, value)
|
|
|
|
return box
|
|
|
|
def delete_box(self, box_id: str) -> Box:
|
|
"""Delete a box."""
|
|
box = self.db.query(Box).filter(Box.product_id == box_id).first()
|
|
product = self.db.query(Product).filter(Product.id == box_id).first()
|
|
if not box:
|
|
raise ValueError("Box not found")
|
|
|
|
with db_transaction(self.db):
|
|
self.db.delete(box)
|
|
self.db.delete(product)
|
|
return box
|
|
|
|
def open_box(self, box_id: str, box_data: CreateOpenBoxRequest) -> OpenBox:
|
|
"""Open a box and process its contents."""
|
|
box = self.db.query(Box).filter(Box.product_id == box_id).first()
|
|
if not box:
|
|
raise ValueError("Box not found")
|
|
|
|
with db_transaction(self.db):
|
|
open_box = OpenBox(
|
|
id=str(uuid4()),
|
|
product_id=box_id,
|
|
num_cards_actual=box_data.num_cards_actual,
|
|
date_opened=datetime.strptime(box_data.date_opened, "%Y-%m-%d") if box_data.date_opened else datetime.now()
|
|
)
|
|
self.db.add(open_box)
|
|
|
|
staged_product_data = self.get_staged_product_data(box_data.file_ids)
|
|
product_data = self.aggregate_staged_product_data(staged_product_data)
|
|
self.inventory_service.process_staged_products(product_data)
|
|
self.add_products_to_open_box(open_box, product_data)
|
|
|
|
# Update file box IDs
|
|
self.db.query(File).filter(File.id.in_(box_data.file_ids)).update(
|
|
{"box_id": open_box.id}, synchronize_session=False
|
|
)
|
|
|
|
return open_box
|
|
|
|
def delete_open_box(self, box_id: str) -> OpenBox:
|
|
# Fetch open box and related cards in one query
|
|
open_box = (
|
|
self.db.query(OpenBox)
|
|
.filter(OpenBox.id == box_id)
|
|
.first()
|
|
)
|
|
if not open_box:
|
|
raise ValueError("Open box not found")
|
|
|
|
# Get all open box cards and related inventory items in one query
|
|
open_box_cards = (
|
|
self.db.query(OpenBoxCard, Inventory)
|
|
.join(
|
|
Inventory,
|
|
OpenBoxCard.card_id == Inventory.product_id
|
|
)
|
|
.filter(OpenBoxCard.open_box_id == open_box.id)
|
|
.all()
|
|
)
|
|
|
|
# Process inventory adjustments
|
|
for open_box_card, inventory_item in open_box_cards:
|
|
if open_box_card.quantity > inventory_item.quantity:
|
|
raise ValueError("Open box quantity exceeds inventory quantity")
|
|
|
|
inventory_item.quantity -= open_box_card.quantity
|
|
if inventory_item.quantity == 0:
|
|
self.db.delete(inventory_item)
|
|
|
|
# Delete the open box card
|
|
self.db.delete(open_box_card)
|
|
|
|
# Execute all database operations in a single transaction
|
|
with db_transaction(self.db):
|
|
self.db.delete(open_box)
|
|
|
|
return open_box |