-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
rails_application/app/services/client/orders/submit_service.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
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 | ||
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 |
68 changes: 68 additions & 0 deletions
68
rails_application/test/services/client/orders/submit_service_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
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)) | ||
|
||
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 | ||
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 |