From c6d4365dc596ee7a25ccc4365c06cab0e7e470ef Mon Sep 17 00:00:00 2001 From: marlena-b Date: Tue, 10 Sep 2024 19:14:13 +0200 Subject: [PATCH 1/3] Split Orders Submit Service --- .../controllers/client/orders_controller.rb | 2 +- .../services/client/orders/submit_service.rb | 44 +++++++++++++ .../client/orders/submit_service_test.rb | 63 +++++++++++++++++++ 3 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 rails_application/app/services/client/orders/submit_service.rb create mode 100644 rails_application/test/services/client/orders/submit_service_test.rb diff --git a/rails_application/app/controllers/client/orders_controller.rb b/rails_application/app/controllers/client/orders_controller.rb index 37addc53..ffc59a5b 100644 --- a/rails_application/app/controllers/client/orders_controller.rb +++ b/rails_application/app/controllers/client/orders_controller.rb @@ -12,7 +12,7 @@ def new end def create - Orders::SubmitService.new(order_id: params[:order_id], customer_id: cookies[:client_id]).call + Client::Orders::SubmitService.new(order_id: params[:order_id], customer_id: cookies[:client_id]).call rescue Orders::OrderHasUnavailableProducts => e unavailable_products = e.unavailable_products.join(", ") redirect_to edit_client_order_path(params[:order_id]), alert: "Order can not be submitted! #{unavailable_products} not available in requested quantity!" diff --git a/rails_application/app/services/client/orders/submit_service.rb b/rails_application/app/services/client/orders/submit_service.rb new file mode 100644 index 00000000..2b618341 --- /dev/null +++ b/rails_application/app/services/client/orders/submit_service.rb @@ -0,0 +1,44 @@ +module Client + module Orders + class OrderHasUnavailableProducts < StandardError + attr_reader :unavailable_products + + def initialize(unavailable_products) + @unavailable_products = unavailable_products + end + end + + class SubmitService < ApplicationService + def initialize(order_id:, customer_id:) + @order_id = order_id + @customer_id = customer_id + end + + def call + unavailable_products = [] + event_store + .within { submit_order } + .subscribe(to: Processes::ReservationProcessFailed) do |event| + unavailable_products = Products::Product.where(id: event.data.fetch(:unavailable_products)).pluck(:name) + end + .call + + if unavailable_products.any? + raise OrderHasUnavailableProducts.new(unavailable_products) + end + true + end + + private + + attr_reader :order_id, :customer_id + + def submit_order + ActiveRecord::Base.transaction do + command_bus.(Ordering::SubmitOrder.new(order_id: order_id)) + command_bus.(Crm::AssignCustomerToOrder.new(order_id: order_id, customer_id: customer_id)) + end + end + end + end +end diff --git a/rails_application/test/services/client/orders/submit_service_test.rb b/rails_application/test/services/client/orders/submit_service_test.rb new file mode 100644 index 00000000..dcc7eafe --- /dev/null +++ b/rails_application/test/services/client/orders/submit_service_test.rb @@ -0,0 +1,63 @@ +require "test_helper" + +module Client + module Orders + class SubmitServiceTest < InMemoryTestCase + cover Client::Orders::SubmitService + + def test_successful_order_submission + order_id = SecureRandom.uuid + customer_id = SecureRandom.uuid + product_id = SecureRandom.uuid + + run_command(Crm::RegisterCustomer.new(customer_id: customer_id, name: "John Doe")) + prepare_product(product_id, "Async Remote", 49) + run_command(Ordering::AddItemToBasket.new(order_id: order_id, product_id: product_id)) + + assert_equal true, SubmitService.new(order_id: order_id, customer_id: customer_id).call + end + + def test_order_submission_with_unavailable_products + order_id = SecureRandom.uuid + customer_id = SecureRandom.uuid + product_id = SecureRandom.uuid + another_product_id = SecureRandom.uuid + + run_command(Crm::RegisterCustomer.new(customer_id: customer_id, name: "John Doe")) + prepare_product(product_id, "Async Remote", 49) + run_command(Inventory::Supply.new(product_id: product_id, quantity: 1)) + run_command(Inventory::Reserve.new(product_id: product_id, quantity: 1)) + run_command(Ordering::AddItemToBasket.new(order_id: order_id, product_id: product_id)) + prepare_product(another_product_id, "Fearless Refactoring", 49) + run_command(Ordering::AddItemToBasket.new(order_id: order_id, product_id: another_product_id)) + + error = assert_raises(OrderHasUnavailableProducts) do + SubmitService.new(order_id: order_id, customer_id: customer_id).call + end + + assert_equal ["Async Remote"], error.unavailable_products + end + + private + + def event_store + Rails.configuration.event_store + end + + def prepare_product(product_id, name, price) + run_command( + ProductCatalog::RegisterProduct.new( + product_id: product_id, + ) + ) + run_command( + ProductCatalog::NameProduct.new( + product_id: product_id, + name: name + ) + ) + run_command(Pricing::SetPrice.new(product_id: product_id, price: price)) + end + end + end +end From 949e7c8d333349831e933e130292d0aa1207832f Mon Sep 17 00:00:00 2001 From: marlena-b Date: Mon, 16 Sep 2024 17:59:04 +0200 Subject: [PATCH 2/3] Do not return true in service --- .../app/services/client/orders/submit_service.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/rails_application/app/services/client/orders/submit_service.rb b/rails_application/app/services/client/orders/submit_service.rb index 2b618341..3def1ce2 100644 --- a/rails_application/app/services/client/orders/submit_service.rb +++ b/rails_application/app/services/client/orders/submit_service.rb @@ -23,10 +23,9 @@ def call end .call - if unavailable_products.any? - raise OrderHasUnavailableProducts.new(unavailable_products) - end - true + if unavailable_products.any? + raise OrderHasUnavailableProducts.new(unavailable_products) + end end private From cb0eab86190d790f3f9bd96acf685e87fd10c0d3 Mon Sep 17 00:00:00 2001 From: marlena-b Date: Mon, 16 Sep 2024 18:11:31 +0200 Subject: [PATCH 3/3] Fix test --- .../test/services/client/orders/submit_service_test.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/rails_application/test/services/client/orders/submit_service_test.rb b/rails_application/test/services/client/orders/submit_service_test.rb index dcc7eafe..412351d2 100644 --- a/rails_application/test/services/client/orders/submit_service_test.rb +++ b/rails_application/test/services/client/orders/submit_service_test.rb @@ -14,7 +14,12 @@ def test_successful_order_submission prepare_product(product_id, "Async Remote", 49) run_command(Ordering::AddItemToBasket.new(order_id: order_id, product_id: product_id)) - assert_equal true, SubmitService.new(order_id: order_id, customer_id: customer_id).call + Client::Orders::SubmitService.new(order_id: order_id, customer_id: customer_id).call + + order = ClientOrders::Order.find_by!(order_uid: order_id) + + assert_equal "Submitted", order.state + assert_equal customer_id, order.client_uid end def test_order_submission_with_unavailable_products