-
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
17 changed files
with
508 additions
and
12 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
9 changes: 9 additions & 0 deletions
9
ecommerce/ordering/lib/ordering/commands/add_item_to_refund.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,9 @@ | ||
module Ordering | ||
class AddItemToRefund < Infra::Command | ||
attribute :refund_id, Infra::Types::UUID | ||
attribute :order_id, Infra::Types::UUID | ||
attribute :product_id, Infra::Types::UUID | ||
|
||
alias aggregate_id refund_id | ||
end | ||
end |
9 changes: 9 additions & 0 deletions
9
ecommerce/ordering/lib/ordering/commands/remove_item_from_refund.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,9 @@ | ||
module Ordering | ||
class RemoveItemFromRefund < Infra::Command | ||
attribute :refund_id, Infra::Types::UUID | ||
attribute :order_id, Infra::Types::UUID | ||
attribute :product_id, Infra::Types::UUID | ||
|
||
alias aggregate_id refund_id | ||
end | ||
end |
7 changes: 7 additions & 0 deletions
7
ecommerce/ordering/lib/ordering/events/item_added_to_refund.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,7 @@ | ||
module Ordering | ||
class ItemAddedToRefund < Infra::Event | ||
attribute :refund_id, Infra::Types::UUID | ||
attribute :order_id, Infra::Types::UUID | ||
attribute :product_id, Infra::Types::UUID | ||
end | ||
end |
7 changes: 7 additions & 0 deletions
7
ecommerce/ordering/lib/ordering/events/item_removed_from_refund.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,7 @@ | ||
module Ordering | ||
class ItemRemovedFromRefund < Infra::Event | ||
attribute :refund_id, Infra::Types::UUID | ||
attribute :order_id, Infra::Types::UUID | ||
attribute :product_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require_relative "test_helper" | ||
|
||
module Ordering | ||
class AddItemToRefundTest < Test | ||
cover "Ordering::OnAddItemToRefund*" | ||
|
||
def test_add_item_to_refund | ||
order_id = SecureRandom.uuid | ||
aggregate_id = SecureRandom.uuid | ||
product_id = SecureRandom.uuid | ||
stream = "Ordering::Refund$#{aggregate_id}" | ||
|
||
arrange( | ||
CreateDraftRefund.new( | ||
refund_id: aggregate_id, | ||
order_id: order_id | ||
) | ||
) | ||
|
||
expected_events = [ | ||
ItemAddedToRefund.new( | ||
data: { | ||
refund_id: aggregate_id, | ||
order_id: order_id, | ||
product_id: product_id | ||
} | ||
) | ||
] | ||
|
||
assert_events(stream, *expected_events) do | ||
act( | ||
AddItemToRefund.new( | ||
refund_id: aggregate_id, | ||
order_id: order_id, | ||
product_id: product_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,69 @@ | ||
require_relative "test_helper" | ||
|
||
module Ordering | ||
class RemoveItemFromRefundTest < Test | ||
cover "Ordering::OnRemoveItemFromRefund*" | ||
|
||
def test_removing_items_from_refund | ||
order_id = SecureRandom.uuid | ||
aggregate_id = SecureRandom.uuid | ||
product_id = SecureRandom.uuid | ||
stream = "Ordering::Refund$#{aggregate_id}" | ||
|
||
arrange( | ||
CreateDraftRefund.new( | ||
refund_id: aggregate_id, | ||
order_id: order_id | ||
), | ||
AddItemToRefund.new( | ||
refund_id: aggregate_id, | ||
order_id: order_id, | ||
product_id: product_id | ||
) | ||
) | ||
|
||
expected_events = [ | ||
ItemRemovedFromRefund.new( | ||
data: { | ||
refund_id: aggregate_id, | ||
order_id: order_id, | ||
product_id: product_id | ||
} | ||
) | ||
] | ||
|
||
assert_events(stream, *expected_events) do | ||
act( | ||
RemoveItemFromRefund.new( | ||
refund_id: aggregate_id, | ||
order_id: order_id, | ||
product_id: product_id | ||
) | ||
) | ||
end | ||
end | ||
|
||
def test_can_remove_only_added_items | ||
order_id = SecureRandom.uuid | ||
aggregate_id = SecureRandom.uuid | ||
product_id = SecureRandom.uuid | ||
|
||
arrange( | ||
CreateDraftRefund.new( | ||
refund_id: aggregate_id, | ||
order_id: order_id | ||
) | ||
) | ||
|
||
assert_raises(Refund::ProductNotFoundError) do | ||
act( | ||
RemoveItemFromRefund.new( | ||
refund_id: aggregate_id, | ||
order_id: order_id, | ||
product_id: product_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
40 changes: 40 additions & 0 deletions
40
rails_application/app/read_models/refunds/add_item_to_refund.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,40 @@ | ||
module Refunds | ||
class AddItemToRefund | ||
def call(event) | ||
refund_id = event.data.fetch(:refund_id) | ||
Refund.find_or_create_by!(uid: refund_id) | ||
product_id = event.data.fetch(:product_id) | ||
item = | ||
find(refund_id, product_id) || | ||
create(refund_id, product_id) | ||
item.quantity += 1 | ||
item.save! | ||
end | ||
|
||
private | ||
|
||
def event_store | ||
Rails.configuration.event_store | ||
end | ||
|
||
def find(refund_id, product_id) | ||
Refund | ||
.find_by_uid(refund_id) | ||
.refund_items | ||
.where(product_uid: product_id) | ||
.first | ||
end | ||
|
||
def create(refund_id, product_id) | ||
product = Orders::Product.find_by_uid(product_id) | ||
Refund | ||
.find_by(uid: refund_id) | ||
.refund_items | ||
.create( | ||
product_uid: product_id, | ||
price: product.price, | ||
quantity: 0 | ||
) | ||
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
24 changes: 24 additions & 0 deletions
24
rails_application/app/read_models/refunds/remove_item_from_refund.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,24 @@ | ||
module Refunds | ||
class RemoveItemFromRefund | ||
def call(event) | ||
refund_id = event.data.fetch(:refund_id) | ||
product_id = event.data.fetch(:product_id) | ||
item = find(refund_id, product_id) | ||
item.quantity -= 1 | ||
item.quantity > 0 ? item.save! : item.destroy! | ||
end | ||
|
||
private | ||
def find(order_uid, product_id) | ||
Refund | ||
.find_by_uid(order_uid) | ||
.refund_items | ||
.where(product_uid: product_id) | ||
.first | ||
end | ||
|
||
def event_store | ||
Rails.configuration.event_store | ||
end | ||
end | ||
end |
Oops, something went wrong.