-
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.
Better error when submitting order with out of stock product
- Loading branch information
1 parent
53100a8
commit 690d65d
Showing
17 changed files
with
321 additions
and
51 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
.ruby-version | ||
.tool-versions |
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
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
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
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,10 @@ | ||
module Processes | ||
class ReservationProcessFailed < Infra::Event | ||
attribute :order_id, Infra::Types::UUID | ||
attribute :unavailable_products, Infra::Types::Array.of(Infra::Types::UUID) | ||
end | ||
|
||
class ReservationProcessSuceeded < Infra::Event | ||
attribute :order_id, Infra::Types::UUID | ||
end | ||
end |
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
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
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
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
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
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
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,17 @@ | ||
class ApplicationService | ||
def self.call(...) | ||
new(...).call | ||
end | ||
|
||
def call | ||
raise NotImplementedError | ||
end | ||
|
||
def event_store | ||
Rails.configuration.event_store | ||
end | ||
|
||
def command_bus | ||
Rails.configuration.command_bus | ||
end | ||
end |
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,42 @@ | ||
module Orders | ||
class SubmitService < ApplicationService | ||
def initialize(order_id:, customer_id:) | ||
@order_id = order_id | ||
@customer_id = customer_id | ||
end | ||
|
||
def call | ||
success = true | ||
unavailable_products = nil | ||
|
||
event_store | ||
.within { submit_order } | ||
.subscribe(to: Processes::ReservationProcessFailed) do |event| | ||
success = false | ||
unavailable_products = Products::Product.where(id: event.data.fetch(:unavailable_products)).pluck(:name) | ||
end | ||
.call | ||
|
||
if success | ||
Result.new(:success) | ||
else | ||
Result.new(:products_out_of_stock, unavailable_products) | ||
end | ||
rescue Ordering::Order::IsEmpty | ||
Result.new(:order_is_empty) | ||
rescue Crm::Customer::NotExists | ||
Result.new(:customer_not_exists) | ||
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 |
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,14 @@ | ||
class Result | ||
attr_reader :status, :args | ||
|
||
def initialize(status, *args) | ||
@status = status | ||
@args = args | ||
end | ||
|
||
def path(name, &block) | ||
return unless @status == name.to_sym | ||
|
||
block.call(*@args) | ||
end | ||
end |
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
Oops, something went wrong.