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):
|
if isinstance(inventory_units, dict):
|
||||||
inventory_units = inventory_units.get("nodes", [])
|
inventory_units = inventory_units.get("nodes", [])
|
||||||
|
|
||||||
# Collect all GTINs and validate products exist
|
# Collect all GTINs and check for items without GTINs
|
||||||
gtins = set()
|
gtins = set()
|
||||||
|
has_items_without_gtin = False
|
||||||
for unit in inventory_units:
|
for unit in inventory_units:
|
||||||
variant = unit.get("variant", {}) or {}
|
variant = unit.get("variant", {}) or {}
|
||||||
trade_id = variant.get("tradeId", {}) or {}
|
trade_id = variant.get("tradeId", {}) or {}
|
||||||
gtin = trade_id.get("number")
|
gtin = trade_id.get("number")
|
||||||
if gtin:
|
if gtin:
|
||||||
gtins.add(gtin)
|
gtins.add(gtin)
|
||||||
|
else:
|
||||||
|
has_items_without_gtin = True
|
||||||
|
|
||||||
# Batch query all products by GTIN
|
# Batch query all products by GTIN
|
||||||
products_by_gtin: dict[str, Product] = {}
|
products_by_gtin: dict[str, Product] = {}
|
||||||
@@ -507,13 +510,19 @@ class OrderService:
|
|||||||
# Identify missing GTINs (graceful handling - no exception raised)
|
# Identify missing GTINs (graceful handling - no exception raised)
|
||||||
missing_gtins = gtins - set(products_by_gtin.keys())
|
missing_gtins = gtins - set(products_by_gtin.keys())
|
||||||
placeholder = None
|
placeholder = None
|
||||||
if missing_gtins:
|
if missing_gtins or has_items_without_gtin:
|
||||||
# Get or create placeholder product for unmatched items
|
# Get or create placeholder product for unmatched items or items without GTIN
|
||||||
placeholder = self._get_or_create_placeholder_product(db, vendor_id)
|
placeholder = self._get_or_create_placeholder_product(db, vendor_id)
|
||||||
logger.warning(
|
if missing_gtins:
|
||||||
f"Order {order_number}: {len(missing_gtins)} product(s) not found. "
|
logger.warning(
|
||||||
f"GTINs: {missing_gtins}. Using placeholder and creating exceptions."
|
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
|
# Parse address data
|
||||||
ship_address = order_data.get("shipAddress", {}) or {}
|
ship_address = order_data.get("shipAddress", {}) or {}
|
||||||
|
|||||||
Reference in New Issue
Block a user