From 9185269874168e5180669ea6175d0fb0cd500ec2 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Sat, 4 Oct 2025 11:10:53 +0200 Subject: [PATCH] fix: prevent deleting last address for each kind --- .../Controllers/Shop/CustomerController.php | 21 ++++++++++++-- resources/shop/js/site.js | 28 ++++++++++++++++++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/Shop/CustomerController.php b/app/Http/Controllers/Shop/CustomerController.php index 76615cbe..35e1ab53 100644 --- a/app/Http/Controllers/Shop/CustomerController.php +++ b/app/Http/Controllers/Shop/CustomerController.php @@ -58,8 +58,25 @@ class CustomerController extends Controller public function delete_address($id) { - $ret = CustomerAddresses::destroy($id); + $address = CustomerAddresses::get($id); - return redirect()->route('Shop.Customers.edit'); + if (! $address || (int) $address->customer_id !== (int) Customers::getId()) { + abort(404); + } + + $remaining = CustomerAddresses::getModel() + ->byCustomer($address->customer_id) + ->byType($address->type) + ->count(); + + if ($remaining <= 1) { + return redirect()->route('Shop.Customers.edit') + ->with('growl', [__('Vous devez conserver au moins une adresse par type.'), 'warning']); + } + + CustomerAddresses::destroy($id); + + return redirect()->route('Shop.Customers.edit') + ->with('growl', [__('Adresse supprimée.'), 'success']); } } diff --git a/resources/shop/js/site.js b/resources/shop/js/site.js index f6eb0e3b..aa3b8c98 100644 --- a/resources/shop/js/site.js +++ b/resources/shop/js/site.js @@ -1,8 +1,34 @@ +// Simple notification helper used by blade templates (fallback to Bootstrap alerts) +window.growl = function(message, type) { + var alertTypes = { + success: 'alert-success', + error: 'alert-danger', + warning: 'alert-warning', + info: 'alert-info' + }; + var cssClass = alertTypes[type] || alertTypes.info; + var $container = $('#growl-container'); + + if (!$container.length) { + $container = $('
'); + $('body').append($container); + } + + var $alert = $(''); + $alert.append($('').text(message)); + $alert.append(''); + $container.append($alert); + + setTimeout(function() { + $alert.alert('close'); + }, 4000); +}; + // Prevent closing from click inside dropdown $(document).on('click', '.dropdown-menu', function (e) { e.stopPropagation(); }); - + // make it as accordion for smaller screens if ($(window).width() < 992) { $('.dropdown-menu a').click(function(e) {