From c8ac094bf432953a2668876fbc1bb1cf3011679f Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Thu, 30 Jan 2025 19:42:56 +0100 Subject: [PATCH] Prevent non-number amounts for shopping list items on database level (closes #2657) --- changelog/78_UNRELEASED_xxxx-xx-xx.md | 2 +- migrations/0250.sql | 35 +++++++++++++++++++++++++++ views/shoppinglist.blade.php | 3 --- 3 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 migrations/0250.sql diff --git a/changelog/78_UNRELEASED_xxxx-xx-xx.md b/changelog/78_UNRELEASED_xxxx-xx-xx.md index 65fdb2f2..6bb9ed42 100644 --- a/changelog/78_UNRELEASED_xxxx-xx-xx.md +++ b/changelog/78_UNRELEASED_xxxx-xx-xx.md @@ -16,7 +16,7 @@ ### Shopping list -- xxx +- Optimized that non-number amounts are now prevented on database level (was infamously a problem when using specific community contributed alternative clients) ### Recipes diff --git a/migrations/0250.sql b/migrations/0250.sql new file mode 100644 index 00000000..40e58201 --- /dev/null +++ b/migrations/0250.sql @@ -0,0 +1,35 @@ +DROP TRIGGER shopping_list_qu_id_default; + +CREATE TRIGGER shopping_list_defaults_INS AFTER INSERT ON shopping_list +BEGIN + UPDATE shopping_list + SET qu_id = (SELECT qu_id_purchase FROM products WHERE id = product_id) + WHERE IFNULL(qu_id, '') = '' + AND id = NEW.id; + + UPDATE shopping_list + SET amount = 1 + WHERE TYPEOF(amount) NOT IN ('integer', 'real') + AND id = NEW.id; +END; + +CREATE TRIGGER shopping_list_defaults_UPD AFTER UPDATE ON shopping_list +BEGIN + UPDATE shopping_list + SET qu_id = (SELECT qu_id_purchase FROM products WHERE id = product_id) + WHERE IFNULL(qu_id, '') = '' + AND id = NEW.id; + + UPDATE shopping_list + SET amount = 1 + WHERE TYPEOF(amount) NOT IN ('integer', 'real') + AND id = NEW.id; +END; + +UPDATE shopping_list +SET qu_id = (SELECT qu_id_purchase FROM products WHERE id = product_id) +WHERE IFNULL(qu_id, '') = ''; + +UPDATE shopping_list +SET amount = 1 +WHERE TYPEOF(amount) NOT IN ('integer', 'real'); diff --git a/views/shoppinglist.blade.php b/views/shoppinglist.blade.php index b6a97d8f..be378164 100644 --- a/views/shoppinglist.blade.php +++ b/views/shoppinglist.blade.php @@ -264,11 +264,8 @@ $productQuConversion = FindObjectInArrayByPropertyValue($productQuConversions, 'to_qu_id', $listItem->qu_id); if ($productQuConversion) { - if (is_numeric($listItem->amount) && is_numeric($productQuConversion->factor)) // However people manage to have a non-number here, happened at least a trilion times for Grocy Mobile (iOS) users - { $listItem->amount = $listItem->amount * $productQuConversion->factor; } - } if(boolval($userSettings['shopping_list_round_up'])) {