Skip to content

Commit

Permalink
Fix expiring order
Browse files Browse the repository at this point in the history
Expiring an order should increase product stock level. The reason is
that the order haven't been paid and the stock will not be send.

Co-authored-by: Tomasz Stolarczyk <[email protected]>
  • Loading branch information
lukaszreszke and stolarczykt committed Sep 24, 2024
1 parent 437ea12 commit e2dba6a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
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 @@ -111,7 +111,13 @@ def create
def expire
Order
.where(status: "Draft")
.find_each { |order| order.status = "Expired"; order.save! }
.find_each do |order|
order.order_items.each do |item|
item.product.increment!(:stock_level)
end
order.status = "Expired"
order.save!
end
redirect_to root_path
end

Expand Down
6 changes: 5 additions & 1 deletion rails_application/test/integration/orders_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ def test_expiring_orders
post "/orders/#{order_id}/add_item?product_id=#{async_remote_id}"
get "/orders"
assert_select("td", "Draft")
post "/orders/expire"

assert_changes -> { Product.find(async_remote_id).stock_level }, from: 9, to: 10 do
post "/orders/expire"
end

follow_redirect!
assert_select("td", "Expired")
end
Expand Down

0 comments on commit e2dba6a

Please sign in to comment.