diff --git a/src/asquare.py b/src/asquare.py index 00d1a6b..e9e4dec 100644 --- a/src/asquare.py +++ b/src/asquare.py @@ -256,10 +256,15 @@ async def update_itm(item_id: str, lego_set: dict, item_data: dict = None): variation_id = None variation_version = None present_at_location_ids = [] + present_at_all_locations = False else: variation_id = variations[0]["id"] variation_version = variations[0].get("version") - # Extract location IDs from variation + # Check if variation is at all locations + present_at_all_locations = variations[0].get( + "present_at_all_locations", False + ) + # Extract location IDs from variation (only if not at all locations) present_at_location_ids = variations[0].get( "present_at_location_ids", [] ) @@ -271,6 +276,7 @@ async def update_itm(item_id: str, lego_set: dict, item_data: dict = None): print(f"[@DEBUG] Fresh item version: {current_version}") print(f"[@DEBUG] Fresh variation version: {variation_version}") print(f"[@DEBUG] Category ID from fresh_item: {category_id}") + print(f"[@DEBUG] Present at all locations: {present_at_all_locations}") print(f"[@DEBUG] Present at locations: {present_at_location_ids}") # Ensure we have a category_id @@ -300,21 +306,27 @@ async def update_itm(item_id: str, lego_set: dict, item_data: dict = None): if variation_version is not None: variation_obj["version"] = variation_version + # Build item object matching variation's location settings + item_obj = { + "type": "ITEM", + "id": item_id, + "version": current_version, + "present_at_all_locations": present_at_all_locations, + "item_data": { + "name": pw_name, + "description": f"{set_name} - {set_theme}", + "category_id": category_id, + "variations": [variation_obj], + }, + } + + # Only include present_at_location_ids if not at all locations + if not present_at_all_locations and present_at_location_ids: + item_obj["present_at_location_ids"] = present_at_location_ids + upsert_body = { "idempotency_key": str(uuid.uuid4()), - "object": { - "type": "ITEM", - "id": item_id, - "version": current_version, # Include version for conflict prevention - "present_at_all_locations": False, # Required when using specific locations - "present_at_location_ids": present_at_location_ids, # Preserve location assignment - "item_data": { - "name": pw_name, - "description": f"{set_name} - {set_theme}", - "category_id": category_id, # Preserve category assignment - "variations": [variation_obj], - }, - }, + "object": item_obj, } # Remove None values