From 6c0d15dec9088128fecd9ec5f1f530bc86549a33 Mon Sep 17 00:00:00 2001 From: C <0xicl33n@gmail.com> Date: Sun, 4 Jan 2026 11:41:26 -0800 Subject: [PATCH] dsadgdfgdsgdsf --- src/asquare.py | 60 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 14 deletions(-) diff --git a/src/asquare.py b/src/asquare.py index 76b54b8..cdaaaca 100644 --- a/src/asquare.py +++ b/src/asquare.py @@ -64,16 +64,27 @@ async def get_item_by_id(item_id: str) -> dict: Exception: If item not found or API error """ 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( 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()) 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") - obj = result_json["objects"][0] item_data = obj.get("item_data", {}) 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 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: 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 """ 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( - object_types=["ITEM_VARIATION"], + object_types=["ITEM"], 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()) 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 - item_id = variation_obj.get("item_variation_data", {}).get("item_id") + for variation in variations: + 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(f"No parent item_id found for variation {variation_id}") - - # Retrieve parent item - return await get_item_by_id(item_id) + raise ValueError( + f"Variation {variation_id} not found in any LEGO category item" + ) except Exception as e: print(f"[@ERROR] Failed to get item from variation {variation_id}: {e}")