From 608afb086d775abc48eba8cddde9ea5a9ff24887 Mon Sep 17 00:00:00 2001 From: dadaloop82 Date: Mon, 27 Apr 2026 18:14:27 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20bringMigrateNamesInternal=20=E2=80=94=20?= =?UTF-8?q?use=20PUT/remove=20and=20German=20catalog=20keys?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two bugs in the migration function: 1. DELETE endpoint does not exist in Bring! API — must use PUT with 'remove' param (same as the remove-from-list flow elsewhere in the code) 2. Items were added using the Italian shopping_name as the 'purchase' field instead of the German catalog key via italianToBring(shoppingName). This created Italian/German duplicates (e.g. both 'Affettato' and 'Aufschnitt' in the list at the same time). Also add a pre-add duplicate check so existing catalog-key items are not double-added when the old specific item is removed. Manual cleanup run: removed 25 stale/duplicate items, added 8 correct German-key items, ran migration (1 more migrated). List is now clean. --- api/index.php | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/api/index.php b/api/index.php index cba4bcf..37a60f2 100644 --- a/api/index.php +++ b/api/index.php @@ -4240,7 +4240,11 @@ function bringMigrateNamesInternal(PDO $db, array $purchaseItems, string $listUU $shoppingName = $lookup[$key]['shopping_name']; $brand = $lookup[$key]['brand']; - // Already using the generic name → nothing to do + // Resolve to the correct Bring! catalog key (German) + $bringKey = italianToBring($shoppingName); + + // Already using the correct catalog key or the shopping name → nothing to do + if (mb_strtolower($rawName) === mb_strtolower($bringKey)) { $skipped++; continue; } if (mb_strtolower($rawName) === mb_strtolower($shoppingName)) { $skipped++; continue; } if (mb_strtolower($itName) === mb_strtolower($shoppingName)) { $skipped++; continue; } @@ -4250,15 +4254,31 @@ function bringMigrateNamesInternal(PDO $db, array $purchaseItems, string $listUU $newSpec = $itName . ($brand ? " · {$brand}" : '') . ' — ' . $spec; } - // Remove old item, add with generic name - bringRequest('DELETE', "https://api.getbring.com/rest/v2/bringlists/{$listUUID}/{$rawName}"); - $addBody = http_build_query([ - 'uuid' => $listUUID, - 'purchase' => $shoppingName, - 'specification' => $newSpec, - ]); - $result = bringRequest('PUT', "https://api.getbring.com/rest/v2/bringlists/{$listUUID}", $addBody); - if ($result !== false) { $migrated++; } else { $errors++; } + // Check if the correct catalog key is already in the list + $alreadyAdded = false; + foreach ($purchaseItems as $existing) { + if (strcasecmp($existing['name'] ?? '', $bringKey) === 0) { + $alreadyAdded = true; + break; + } + } + + // Remove old item using the correct API (PUT with remove param) + bringRequest('PUT', "https://api.getbring.com/rest/v2/bringlists/{$listUUID}", + http_build_query(['uuid' => $listUUID, 'remove' => $rawName])); + + // Add with the correct German catalog key (unless already present) + if (!$alreadyAdded) { + $addBody = http_build_query([ + 'uuid' => $listUUID, + 'purchase' => $bringKey, + 'specification' => $newSpec, + ]); + $result = bringRequest('PUT', "https://api.getbring.com/rest/v2/bringlists/{$listUUID}", $addBody); + if ($result !== false) { $migrated++; } else { $errors++; } + } else { + $migrated++; // old item removed, correct generic already present + } } return ['migrated' => $migrated, 'skipped' => $skipped, 'errors' => $errors];