diff --git a/app/controllers/api/products_controller.rb b/app/controllers/api/products_controller.rb index da053d891..30584fd5e 100644 --- a/app/controllers/api/products_controller.rb +++ b/app/controllers/api/products_controller.rb @@ -48,8 +48,12 @@ class API::ProductsController < API::ApiController def destroy authorize @product - ProductService.destroy(@product) - head :no_content + begin + ProductService.destroy(@product) + head :no_content + rescue StandardError => e + render json: e, status: :unprocessable_entity + end end def stock_movements diff --git a/app/frontend/src/javascript/components/store/store-product-item.tsx b/app/frontend/src/javascript/components/store/store-product-item.tsx index 240ae9d79..b9e3c4a15 100644 --- a/app/frontend/src/javascript/components/store/store-product-item.tsx +++ b/app/frontend/src/javascript/components/store/store-product-item.tsx @@ -40,7 +40,9 @@ export const StoreProductItem: React.FC = ({ product, car const addProductToCart = (e: React.BaseSyntheticEvent) => { e.preventDefault(); e.stopPropagation(); - CartAPI.addItem(cart, product.id, 1).then(onSuccessAddProductToCart).catch(onError); + CartAPI.addItem(cart, product.id, 1).then(onSuccessAddProductToCart).catch(() => { + onError(t('app.public.store_product_item.stock_limit')); + }); }; /** diff --git a/app/frontend/src/javascript/components/store/store-product.tsx b/app/frontend/src/javascript/components/store/store-product.tsx index 2b089ba85..64544599e 100644 --- a/app/frontend/src/javascript/components/store/store-product.tsx +++ b/app/frontend/src/javascript/components/store/store-product.tsx @@ -110,7 +110,9 @@ export const StoreProduct: React.FC = ({ productSlug, current CartAPI.addItem(cart, product.id, toCartCount).then(data => { setCart(data); onSuccess(t('app.public.store_product.add_to_cart_success')); - }).catch(onError); + }).catch(() => { + onError(t('app.public.store_product.stock_limit')); + }); } }; diff --git a/config/locales/app.public.en.yml b/config/locales/app.public.en.yml index b17b431c5..d494c1f82 100644 --- a/config/locales/app.public.en.yml +++ b/config/locales/app.public.en.yml @@ -408,6 +408,7 @@ en: documentation: "Documentation" minimum_purchase: "Minimum purchase: " add_to_cart: "Add to cart" + stock_limit: "You have reached the current stock limit" stock_status: available: "Available" limited_stock: "Limited stock" @@ -416,6 +417,7 @@ en: minimum_purchase: "Minimum purchase: " add: "Add" add_to_cart: "Add to cart" + stock_limit: "You have reached the current stock limit" product_price: per_unit: "/ unit" free: "Free" diff --git a/config/locales/app.public.fr.yml b/config/locales/app.public.fr.yml index f4dc7bdcd..7a4df543b 100644 --- a/config/locales/app.public.fr.yml +++ b/config/locales/app.public.fr.yml @@ -408,6 +408,7 @@ fr: documentation: "Documentation" minimum_purchase: "Achat minimum : " add_to_cart: "Ajouter au panier" + stock_limit: "Vous avez atteint la limite actuelle de stock" stock_status: available: "Disponible" limited_stock: "Stock limité" @@ -416,6 +417,7 @@ fr: minimum_purchase: "Achat minimum : " add: "Ajouter" add_to_cart: "Ajouter au panier" + stock_limit: "Vous avez atteint la limite actuelle de stock" product_price: per_unit: "/ unité" free: "Gratuit"