diff --git a/ecommerce/ordering/README.md b/ecommerce/ordering/README.md index 0624dbefc..2e614af54 100644 --- a/ecommerce/ordering/README.md +++ b/ecommerce/ordering/README.md @@ -48,24 +48,6 @@ It doesn't have any impact on the state machine. It's only needed to some read models, which can retrieve it from elsewhere - most notably the `Pricing` The implementation of `Basket`, because of this, seems duplicated to Pricing or Inventory. -#### Checking availability in the controllers - -```ruby -def add_item - read_model = Orders::OrderLine.where(order_uid: params[:id], product_id: params[:product_id]).first - if Availability::Product.exists?(["uid = ? and available < ?", params[:product_id], (read_model&.quantity || 0) + 1]) - redirect_to edit_order_path(params[:id]), - alert: "Product not available in requested quantity!" and return - end - ActiveRecord::Base.transaction do - command_bus.(Ordering::AddItemToBasket.new(order_id: params[:id], product_id: params[:product_id])) - end - head :ok - end -``` - -This code is duplicated for admin creating orders and clients creating orders. - #### Mapping of events between domains UI -> Ordering::SubmitOrder -> Ordering::Submitted -> Reservation(process) -> Ordering::AcceptOrder diff --git a/rails_application/app/controllers/client/orders_controller.rb b/rails_application/app/controllers/client/orders_controller.rb index 90b7e4859..a7faff757 100644 --- a/rails_application/app/controllers/client/orders_controller.rb +++ b/rails_application/app/controllers/client/orders_controller.rb @@ -32,7 +32,7 @@ def edit def add_item read_model = ClientOrders::OrderLine.where(order_uid: params[:id], product_id: params[:product_id]).first - if Availability::Product.exists?(["uid = ? and available < ?", params[:product_id], (read_model&.product_quantity || 0) + 1]) + unless Availability.approximately_available?(params[:product_id], (read_model&.quantity || 0) + 1) redirect_to edit_client_order_path(params[:id]), alert: "Product not available in requested quantity!" and return end diff --git a/rails_application/app/controllers/orders_controller.rb b/rails_application/app/controllers/orders_controller.rb index dfa768fea..cda74715b 100644 --- a/rails_application/app/controllers/orders_controller.rb +++ b/rails_application/app/controllers/orders_controller.rb @@ -55,7 +55,7 @@ def reset_discount def add_item read_model = Orders::OrderLine.where(order_uid: params[:id], product_id: params[:product_id]).first - if Availability::Product.exists?(["uid = ? and available < ?", params[:product_id], (read_model&.quantity || 0) + 1]) + unless Availability.approximately_available?(params[:product_id], (read_model&.quantity || 0) + 1) redirect_to edit_order_path(params[:id]), alert: "Product not available in requested quantity!" and return end diff --git a/rails_application/app/read_models/availability/configuration.rb b/rails_application/app/read_models/availability/configuration.rb index 55ebf72a6..bb223126c 100644 --- a/rails_application/app/read_models/availability/configuration.rb +++ b/rails_application/app/read_models/availability/configuration.rb @@ -3,6 +3,12 @@ class Product < ApplicationRecord self.table_name = "availability_products" end + # private_constant :Product + + def self.approximately_available?(product_id, desired_quantity) + !Product.exists?(["uid = ? and available < ?", product_id, desired_quantity]) + end + class UpdateAvailability < Infra::EventHandler def call(event) order = Product.find_or_create_by!(uid: event.data.fetch(:product_id)) diff --git a/rails_application/test/availability/update_availability_test.rb b/rails_application/test/availability/update_availability_test.rb index 80f1edb98..8435f43bf 100644 --- a/rails_application/test/availability/update_availability_test.rb +++ b/rails_application/test/availability/update_availability_test.rb @@ -10,12 +10,10 @@ def test_availability_updates event_store.publish(Inventory::AvailabilityChanged.new(data: { product_id: product_id, available: 0 })) - product = Availability::Product.find_by(uid: product_id) - assert_equal 0, product.available + refute Availability.approximately_available?(product_id, 1) event_store.publish(Inventory::AvailabilityChanged.new(data: { product_id: product_id, available: 1 })) - product.reload - assert_equal 1, product.available + assert Availability.approximately_available?(product_id, 1) end private