flag special orders in ui
This commit is contained in:
@ -24,6 +24,7 @@ import csv
|
||||
import io
|
||||
from app.schemas.file import FileInDB
|
||||
from datetime import datetime
|
||||
from sqlalchemy import func
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class OrderManagementService(BaseTCGPlayerService):
|
||||
@ -40,7 +41,34 @@ class OrderManagementService(BaseTCGPlayerService):
|
||||
self.pull_sheet_endpoint = f"/pull-sheets/export{self.API_VERSION}"
|
||||
self.shipping_endpoint = f"/shipping/export{self.API_VERSION}"
|
||||
|
||||
|
||||
async def add_item_quantity(self, db: Session, orders: list[TCGPlayerAPIOrderSummary]) -> list[TCGPlayerAPIOrderSummary]:
|
||||
"""
|
||||
Add item quantity to orders using SQL aggregation for better performance
|
||||
"""
|
||||
# Get order numbers from the input orders
|
||||
order_numbers = [order["orderNumber"] for order in orders]
|
||||
|
||||
# Use SQL aggregation to get the sum of quantities directly from the database
|
||||
quantity_sums = (
|
||||
db.query(
|
||||
TCGPlayerOrderProduct.order_number,
|
||||
func.sum(TCGPlayerOrderProduct.quantity).label('total_quantity')
|
||||
)
|
||||
.filter(TCGPlayerOrderProduct.order_number.in_(order_numbers))
|
||||
.group_by(TCGPlayerOrderProduct.order_number)
|
||||
.all()
|
||||
)
|
||||
|
||||
# Create a lookup dictionary for faster access
|
||||
quantity_lookup = {order_number: total_quantity for order_number, total_quantity in quantity_sums}
|
||||
|
||||
# Update orders with quantities
|
||||
for order in orders:
|
||||
order["itemQuantity"] = quantity_lookup.get(order["orderNumber"], 0)
|
||||
|
||||
return orders
|
||||
|
||||
|
||||
async def get_orders(self, search_range: str = "LastThreeMonths", open_only: bool = False, filter_out: list[str] = [], filter_in: list[str] = []) -> list[TCGPlayerAPIOrderSummary]:
|
||||
"""
|
||||
search range options:
|
||||
@ -79,6 +107,9 @@ class OrderManagementService(BaseTCGPlayerService):
|
||||
orders = [order for order in orders if order.get("orderNumber") not in filter_out]
|
||||
if filter_in:
|
||||
orders = [order for order in orders if order.get("orderNumber") in filter_in]
|
||||
# add item quantity to orders as none
|
||||
for order in orders:
|
||||
order["itemQuantity"] = 0
|
||||
return orders
|
||||
|
||||
async def get_order_ids(self, search_range: str = "LastThreeMonths", open_only: bool = False, filter_out: list[str] = [], filter_in: list[str] = []):
|
||||
|
Reference in New Issue
Block a user