Skip to content

Commit

Permalink
Use ProductCatalog instead of Product to retrieve stock
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszreszke committed Nov 12, 2024
1 parent b0fc081 commit b06d158
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
6 changes: 3 additions & 3 deletions rails_application/app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,16 @@ def reset_discount
end

def add_item
product = Product.find(params[:product_id])
if product.stock_level < 1
product_catalog = Inventory::ProductCatalog.find_by(product_id: params[:product_id])
if product_catalog.stock_level < 1
redirect_to edit_order_path(params[:id]),
alert: "Product not available in requested quantity!" and return
end

@order = Order.find(params[:id])
ApplicationRecord.transaction do
@order.add_item(Product.find(params[:product_id]))
Inventory::ProductService.new.decrement_stock_level(Inventory::DecreaseStockLevel.new(product_id: product.id))
Inventory::ProductService.new.decrement_stock_level(Inventory::DecreaseStockLevel.new(product_id: product_catalog.product_id))
@order.save!
event_store.publish(Ordering::ItemAdded.new(data: { order_id: @order.id, product_id: params[:product_id] }), stream_name: "Ordering::Order$#{@order.id}")
end
Expand Down
6 changes: 3 additions & 3 deletions rails_application/test/integration/orders_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_happy_path

assert_remove_buttons_not_visible(async_remote_id, fearless_id)

assert_changes -> { Product.find(async_remote_id).stock_level }, from: 10, to: 9 do
assert_changes -> { Inventory::ProductCatalog.find_by(product_id: async_remote_id).stock_level }, from: 10, to: 9 do
post "/orders/#{order_id}/add_item?product_id=#{async_remote_id}"
end
assert_expected_events_in_stream(
Expand All @@ -48,7 +48,7 @@ def test_happy_path
]
)

assert_changes -> { Product.find(fearless_id).stock_level }, from: 10, to: 8 do
assert_changes -> { Inventory::ProductCatalog.find_by(product_id: fearless_id).stock_level }, from: 10, to: 8 do
post "/orders/#{order_id}/add_item?product_id=#{fearless_id}"
post "/orders/#{order_id}/add_item?product_id=#{fearless_id}"
post "/orders/#{order_id}/remove_item?product_id=#{fearless_id}"
Expand Down Expand Up @@ -100,7 +100,7 @@ def test_expiring_orders
get "/orders"
assert_select("td", "Draft")

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

Expand Down
5 changes: 3 additions & 2 deletions rails_application/test/integration/supplies_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ def test_supply_new_product
assert_select "tr#product_#{product.id}" do
assert_select "td", "10"
end
assert_equal(10, product.reload.stock_level)
product_catalog = Inventory::ProductCatalog.find_by(product_id: product.id)
assert_equal(10, product_catalog.stock_level)
end

def test_supply_product_with_existing_stock
create_product
product = Product.find_by(sku:)
increase_stock_level_by_10(product.id)

assert_changes -> { Product.find(product.id).stock_level }, from: 10, to: 20 do
assert_changes -> { Inventory::ProductCatalog.find_by(product_id: product.id).stock_level }, from: 10, to: 20 do
increase_stock_level_by_10(product.id)
end

Expand Down

0 comments on commit b06d158

Please sign in to comment.