Skip to content

Commit

Permalink
Better error when submitting order with out of stock product
Browse files Browse the repository at this point in the history
  • Loading branch information
marlena-b committed Aug 26, 2024
1 parent a784dde commit aa41a9e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ecommerce/processes/lib/processes/reservation_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,4 @@ def product_reserved(product_id)
end
end
end
end
end
8 changes: 7 additions & 1 deletion rails_application/app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ def remove_item

def create
ApplicationRecord.transaction { submit_order(params[:order_id], params[:customer_id]) }
redirect_to order_path(params[:order_id]), notice: "Your order is being submitted"

order = Orders::Order.find_by_uid(params[:order_id])
if order.state == "Submitted"
redirect_to order_path(params[:order_id]), notice: "Your order is being submitted"
else
redirect_to edit_order_path(params[:order_id]), alert: "Order can not be submitted! Product not available in requested quantity!"
end
rescue Ordering::Order::IsEmpty
redirect_to edit_order_path(params[:order_id]), alert: "You can't submit an empty order"
rescue Crm::Customer::NotExists
Expand Down
16 changes: 16 additions & 0 deletions rails_application/test/integration/orders_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,22 @@ def test_empty_order_cannot_be_submitted
assert_select "#alert", "You can't submit an empty order"
end

def test_order_cannot_be_submitted_with_out_of_stock_product
product_id = register_product("Fearless Refactoring", 4, 10)
shopify_id = register_customer("Shopify")

supply_product(product_id, 1)
order_1_id = SecureRandom.uuid
order_2_id = SecureRandom.uuid
post "/orders/#{order_1_id}/add_item?product_id=#{product_id}"
post "/orders/#{order_2_id}/add_item?product_id=#{product_id}"

post "/orders", params: { order_id: order_1_id, customer_id: shopify_id }
post "/orders", params: { order_id: order_2_id, customer_id: shopify_id }

assert_equal "Order can not be submitted! Product not available in requested quantity!", flash[:alert]
end

private

def assert_remove_buttons_visible(async_remote_id, fearless_id, order_id)
Expand Down

0 comments on commit aa41a9e

Please sign in to comment.