fix: handle order items without GTIN (e.g., vouchers)
Items like vouchers may have no GTIN at all. Previously, the placeholder product was only created when there were missing GTINs, causing a NOT NULL constraint violation for items with gtin=None. Now we also check for items without GTIN and create a placeholder for them. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -480,14 +480,17 @@ class OrderService:
|
||||
if isinstance(inventory_units, dict):
|
||||
inventory_units = inventory_units.get("nodes", [])
|
||||
|
||||
# Collect all GTINs and validate products exist
|
||||
# Collect all GTINs and check for items without GTINs
|
||||
gtins = set()
|
||||
has_items_without_gtin = False
|
||||
for unit in inventory_units:
|
||||
variant = unit.get("variant", {}) or {}
|
||||
trade_id = variant.get("tradeId", {}) or {}
|
||||
gtin = trade_id.get("number")
|
||||
if gtin:
|
||||
gtins.add(gtin)
|
||||
else:
|
||||
has_items_without_gtin = True
|
||||
|
||||
# Batch query all products by GTIN
|
||||
products_by_gtin: dict[str, Product] = {}
|
||||
@@ -507,13 +510,19 @@ class OrderService:
|
||||
# Identify missing GTINs (graceful handling - no exception raised)
|
||||
missing_gtins = gtins - set(products_by_gtin.keys())
|
||||
placeholder = None
|
||||
if missing_gtins:
|
||||
# Get or create placeholder product for unmatched items
|
||||
if missing_gtins or has_items_without_gtin:
|
||||
# Get or create placeholder product for unmatched items or items without GTIN
|
||||
placeholder = self._get_or_create_placeholder_product(db, vendor_id)
|
||||
logger.warning(
|
||||
f"Order {order_number}: {len(missing_gtins)} product(s) not found. "
|
||||
f"GTINs: {missing_gtins}. Using placeholder and creating exceptions."
|
||||
)
|
||||
if missing_gtins:
|
||||
logger.warning(
|
||||
f"Order {order_number}: {len(missing_gtins)} product(s) not found. "
|
||||
f"GTINs: {missing_gtins}. Using placeholder and creating exceptions."
|
||||
)
|
||||
if has_items_without_gtin:
|
||||
logger.warning(
|
||||
f"Order {order_number}: Some items have no GTIN. "
|
||||
f"Using placeholder and creating exceptions."
|
||||
)
|
||||
|
||||
# Parse address data
|
||||
ship_address = order_data.get("shipAddress", {}) or {}
|
||||
|
||||
Reference in New Issue
Block a user