From 80542de3334b49417f3ef42e67646834690f6818 Mon Sep 17 00:00:00 2001 From: C <0xicl33n@gmail.com> Date: Sun, 4 Jan 2026 14:29:05 -0800 Subject: [PATCH] d --- src/asquare.py | 95 +++++++++++++++++++++++++------------------------- 1 file changed, 48 insertions(+), 47 deletions(-) diff --git a/src/asquare.py b/src/asquare.py index 2eb9ba1..6da5823 100644 --- a/src/asquare.py +++ b/src/asquare.py @@ -286,30 +286,55 @@ async def update_itm(item_id: str, lego_set: dict, item_data: dict = None): print(f"[@DEBUG] Fresh variation version: {variation_version}") print(f"[@DEBUG] Final category_id for upsert: {category_id}") - # Preserve existing variation and update only what we need - if variations: - # Start with existing variation to preserve all fields - variation_obj = variations[0].copy() - variation_data = variation_obj.get("item_variation_data", {}).copy() + # Get the full existing item_data and modify only what we need + # This preserves ALL existing fields (location settings, tax, etc) + existing_item_data = fresh_item.get("item_data", {}) - # Update only the fields we want to change - variation_data["price_money"] = { - "amount": price_in_cents, - "currency": "USD", - } - variation_data["sku"] = set_number # LEGO set number - variation_data["upc"] = set_upc if set_upc else None # Barcode/GTIN + # Update item-level fields + existing_item_data["name"] = pw_name + existing_item_data["description"] = f"{set_name} - {set_theme}" + existing_item_data["category_id"] = category_id - variation_obj["item_variation_data"] = variation_data + # Update only the specific variation, keep all others unchanged + existing_variations = existing_item_data.get("variations", []) + updated_variations = [] + variation_found = False - # Update version if exists (required for updates) - if variation_version is not None: - variation_obj["version"] = variation_version - else: - # Create new variation if none exists - variation_obj = { + for existing_var in existing_variations: + if existing_var.get("id") == variation_id: + # Preserve all variation fields and only update what we need + updated_var = existing_var.copy() + updated_var_data = existing_var.get( + "item_variation_data", {} + ).copy() + + # Update only specific fields + updated_var_data["price_money"] = { + "amount": price_in_cents, + "currency": "USD", + } + updated_var_data["sku"] = set_number # LEGO set number + updated_var_data["upc"] = ( + set_upc if set_upc else None + ) # Barcode/GTIN + + updated_var["item_variation_data"] = updated_var_data + + # Update version (required for updates) + if variation_version is not None: + updated_var["version"] = variation_version + + updated_variations.append(updated_var) + variation_found = True + else: + # Keep other variations as-is + updated_variations.append(existing_var) + + # If variation wasn't found (shouldn't happen), create new one + if not variation_found: + new_variation = { "type": "ITEM_VARIATION", - "id": "#TEMP_VARIATION_ID", + "id": variation_id if variation_id else "#TEMP_VARIATION_ID", "item_variation_data": { "item_id": item_id, "name": "Regular", @@ -322,33 +347,9 @@ async def update_itm(item_id: str, lego_set: dict, item_data: dict = None): "upc": set_upc if set_upc else None, }, } - - # Get the full existing item_data and modify only what we need - # This preserves ALL existing fields (location settings, tax, etc) - existing_item_data = fresh_item.get("item_data", {}) - - # Update only the fields we want to change - existing_item_data["name"] = pw_name - existing_item_data["description"] = f"{set_name} - {set_theme}" - existing_item_data["category_id"] = category_id - - # Update only the specific variation, keep all others unchanged - existing_variations = existing_item_data.get("variations", []) - updated_variations = [] - variation_found = False - - for existing_var in existing_variations: - if existing_var.get("id") == variation_id: - # Replace this variation with our updated one - updated_variations.append(variation_obj) - variation_found = True - else: - # Keep other variations as-is - updated_variations.append(existing_var) - - # If variation wasn't found (shouldn't happen), add it - if not variation_found and variation_id: - updated_variations.append(variation_obj) + if variation_version is not None: + new_variation["version"] = variation_version + updated_variations.append(new_variation) existing_item_data["variations"] = updated_variations