This commit is contained in:
C committed 2026-01-04 12:34:43 -08:00
1 parent da382942d3
commit 5f4bbb2565
1 file changed
+19 -18
+19 -18
View File
@@ -54,6 +54,8 @@ async def get_item_by_id(item_id: str) -> dict:
""" """
Retrieve full item details by Square catalog item ID. Retrieve full item details by Square catalog item ID.
Uses Square's batch retrieve API for direct lookup by ID.
Args: Args:
item_id: Square catalog item ID item_id: Square catalog item ID
@@ -61,30 +63,21 @@ async def get_item_by_id(item_id: str) -> dict:
dict: Item details including name, category_id, variations, etc. dict: Item details including name, category_id, variations, etc.
Raises: Raises:
Exception: If item not found or API error ValueError: If item not found
Exception: If API error
""" """
try: try:
# Search for item by searching all items and filtering # Use batch retrieve to get item directly by ID
# This is not ideal but the async client lacks direct retrieval result = await client.catalog.batch_retrieve(
result = await client.catalog.search( object_ids=[item_id],
object_types=["ITEM"], include_related_objects=False,
limit=1000, # Broad search
) )
result_json = json.loads(result.model_dump_json()) result_json = json.loads(result.model_dump_json())
if not result_json.get("objects"): if not result_json.get("objects"):
raise ValueError(f"No items found") raise ValueError(f"Item {item_id} not found (may have been deleted)")
# Find the specific item by ID
obj = None
for item in result_json["objects"]:
if item["id"] == item_id:
obj = item
break
if not obj:
raise ValueError(f"Item {item_id} not found")
obj = result_json["objects"][0]
item_data = obj.get("item_data", {}) item_data = obj.get("item_data", {})
return { return {
@@ -97,6 +90,9 @@ async def get_item_by_id(item_id: str) -> dict:
"version": obj.get("version"), "version": obj.get("version"),
} }
except ValueError:
# Re-raise ValueError as-is for clear error messages
raise
except Exception as e: except Exception as e:
print(f"[@ERROR] Failed to get item {item_id}: {e}") print(f"[@ERROR] Failed to get item {item_id}: {e}")
raise raise
@@ -240,7 +236,12 @@ async def update_itm(item_id: str, lego_set: dict, item_data: dict = None):
print( print(
f"[@DEBUG] Fetching fresh item data before update to get current version..." f"[@DEBUG] Fetching fresh item data before update to get current version..."
) )
fresh_item = await get_item_by_id(item_id) try:
fresh_item = await get_item_by_id(item_id)
except ValueError as e:
# Item was deleted between webhook and enrichment
print(f"[@WARNING] {e} - skipping update (item may have been deleted)")
return
variations = fresh_item.get("variations", []) variations = fresh_item.get("variations", [])