dsadgdfgdsgdsf
This commit is contained in:
1 parent
6d376c3653
commit
6c0d15dec9
1 file changed
+46
-14
+46
-14
@@ -64,16 +64,27 @@ async def get_item_by_id(item_id: str) -> dict:
|
|||||||
Exception: If item not found or API error
|
Exception: If item not found or API error
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
|
# Search for item by searching all items and filtering
|
||||||
|
# This is not ideal but the async client lacks direct retrieval
|
||||||
result = await client.catalog.search(
|
result = await client.catalog.search(
|
||||||
object_types=["ITEM"],
|
object_types=["ITEM"],
|
||||||
query={"exact_query": {"attribute_name": "id", "attribute_value": item_id}},
|
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")
|
||||||
|
|
||||||
|
# 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")
|
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 {
|
||||||
@@ -98,6 +109,9 @@ async def get_item_by_variation_id(variation_id: str) -> dict:
|
|||||||
Webhooks send variation IDs (inventory.count.updated), but we need
|
Webhooks send variation IDs (inventory.count.updated), but we need
|
||||||
the parent item to extract the set number from the name.
|
the parent item to extract the set number from the name.
|
||||||
|
|
||||||
|
Since async Square API lacks direct object retrieval, we search all items
|
||||||
|
in the LEGO category and match variations.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
variation_id: Square catalog item variation ID
|
variation_id: Square catalog item variation ID
|
||||||
|
|
||||||
@@ -108,28 +122,46 @@ async def get_item_by_variation_id(variation_id: str) -> dict:
|
|||||||
Exception: If variation/item not found or API error
|
Exception: If variation/item not found or API error
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
# Get variation object using search
|
# Search LEGO category items to find which one has this variation
|
||||||
|
print(f"[@DEBUG] Searching for variation {variation_id} in LEGO category")
|
||||||
|
|
||||||
result = await client.catalog.search(
|
result = await client.catalog.search(
|
||||||
object_types=["ITEM_VARIATION"],
|
object_types=["ITEM"],
|
||||||
query={
|
query={
|
||||||
"exact_query": {"attribute_name": "id", "attribute_value": variation_id}
|
"exact_query": {
|
||||||
|
"attribute_name": "category_id",
|
||||||
|
"attribute_value": LEGO_CAT_ID,
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
limit=1000,
|
||||||
)
|
)
|
||||||
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"Variation {variation_id} not found")
|
raise ValueError(f"No items found in LEGO category")
|
||||||
|
|
||||||
variation_obj = result_json["objects"][0]
|
# Search through items to find which has this variation
|
||||||
|
for obj in result_json["objects"]:
|
||||||
|
item_data = obj.get("item_data", {})
|
||||||
|
variations = item_data.get("variations", [])
|
||||||
|
|
||||||
# Get parent item ID from variation
|
for variation in variations:
|
||||||
item_id = variation_obj.get("item_variation_data", {}).get("item_id")
|
if variation.get("id") == variation_id:
|
||||||
|
# Found the parent item!
|
||||||
|
print(f"[@DEBUG] Found parent item: {obj['id']}")
|
||||||
|
return {
|
||||||
|
"id": obj["id"],
|
||||||
|
"name": item_data.get("name", ""),
|
||||||
|
"description": item_data.get("description", ""),
|
||||||
|
"category_id": item_data.get("category_id"),
|
||||||
|
"variations": variations,
|
||||||
|
"updated_at": obj.get("updated_at"),
|
||||||
|
"version": obj.get("version"),
|
||||||
|
}
|
||||||
|
|
||||||
if not item_id:
|
raise ValueError(
|
||||||
raise ValueError(f"No parent item_id found for variation {variation_id}")
|
f"Variation {variation_id} not found in any LEGO category item"
|
||||||
|
)
|
||||||
# Retrieve parent item
|
|
||||||
return await get_item_by_id(item_id)
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[@ERROR] Failed to get item from variation {variation_id}: {e}")
|
print(f"[@ERROR] Failed to get item from variation {variation_id}: {e}")
|
||||||
|
|||||||
Reference in new issue
Block a user