-
Notifications
You must be signed in to change notification settings - Fork 79
Adding/Removing items to refunds #425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c229df3
Create draft refund
marlena-b 2021130
Add/remove item from refund
marlena-b 13b9cf7
Add projection and read models
marlena-b e617197
CR remarks
marlena-b ad9e65d
Pass refundable_products when initializing a refund
marlena-b 0074aa2
Correct reversed params in asserts
marlena-b File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
8 changes: 8 additions & 0 deletions
8
ecommerce/ordering/lib/ordering/commands/create_draft_refund.rb
This file contains hidden or 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,8 @@ | ||
| module Ordering | ||
| class CreateDraftRefund < Infra::Command | ||
| attribute :refund_id, Infra::Types::UUID | ||
| attribute :order_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 hidden or 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 |
12 changes: 12 additions & 0 deletions
12
ecommerce/ordering/lib/ordering/events/draft_refund_created.rb
This file contains hidden or 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,12 @@ | ||
| module Ordering | ||
| class DraftRefundCreated < Infra::Event | ||
| attribute :refund_id, Infra::Types::UUID | ||
| attribute :order_id, Infra::Types::UUID | ||
| attribute :refundable_products, Infra::Types::Array.of( | ||
| Infra::Types::Hash.schema( | ||
| product_id: Infra::Types::UUID, | ||
| quantity: Infra::Types::Integer | ||
| ) | ||
| ) | ||
| end | ||
| end |
7 changes: 7 additions & 0 deletions
7
ecommerce/ordering/lib/ordering/events/item_added_to_refund.rb
This file contains hidden or 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 hidden or 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 hidden or 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,72 @@ | ||
| module Ordering | ||
| class Refund | ||
| include AggregateRoot | ||
|
|
||
| ExceedsOrderQuantityError = Class.new(StandardError) | ||
| RefundHaveNotBeenRequestedForThisProductError = Class.new(StandardError) | ||
|
|
||
| def initialize(id) | ||
| @id = id | ||
| @refund_items = ItemsList.new | ||
| end | ||
|
|
||
| def create_draft(order_id, refundable_products) | ||
| apply DraftRefundCreated.new(data: { refund_id: @id, order_id: order_id, refundable_products: refundable_products }) | ||
| end | ||
|
|
||
| def add_item(product_id) | ||
| raise ExceedsOrderQuantityError unless enough_items?(product_id) | ||
| apply ItemAddedToRefund.new(data: { refund_id: @id, order_id: @order_id, product_id: product_id }) | ||
| end | ||
|
|
||
| def remove_item(product_id) | ||
| raise RefundHaveNotBeenRequestedForThisProductError unless @refund_items.quantity(product_id).positive? | ||
| apply ItemRemovedFromRefund.new(data: { refund_id: @id, order_id: @order_id, product_id: product_id }) | ||
| end | ||
|
|
||
| on DraftRefundCreated do |event| | ||
| @order_id = event.data[:order_id] | ||
| @refundable_products = event.data[:refundable_products] | ||
| end | ||
|
|
||
| on ItemAddedToRefund do |event| | ||
| @refund_items.increase_quantity(event.data[:product_id]) | ||
| end | ||
|
|
||
| on ItemRemovedFromRefund do |event| | ||
| @refund_items.decrease_quantity(event.data[:product_id]) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def enough_items?(product_id) | ||
| @refund_items.quantity(product_id) < refundable_quantity(product_id) | ||
| end | ||
|
|
||
| def refundable_quantity(product_id) | ||
| product = @refundable_products.find { |product| product.fetch(:product_id) == product_id } | ||
| product.fetch(:quantity) | ||
| end | ||
| end | ||
|
|
||
| class ItemsList | ||
| attr_reader :refund_items | ||
|
|
||
| def initialize | ||
| @refund_items = Hash.new(0) | ||
| end | ||
|
|
||
| def increase_quantity(product_id) | ||
| refund_items[product_id] = quantity(product_id) + 1 | ||
| end | ||
|
|
||
| def decrease_quantity(product_id) | ||
| refund_items[product_id] -= 1 | ||
| refund_items.delete(product_id) if quantity(product_id).equal?(0) | ||
| end | ||
|
|
||
| def quantity(product_id) | ||
| refund_items[product_id] | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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,32 @@ | ||
| module Ordering | ||
| class RefundableProducts | ||
| class << self | ||
| def call(order_id) | ||
| RubyEventStore::Projection | ||
| .from_stream("Ordering::Order$#{order_id}") | ||
| .init(-> { [] }) | ||
| .when(ItemAddedToBasket, -> (state, event) { increase_quantity(state, event.data.fetch(:product_id)) }) | ||
| .when(ItemRemovedFromBasket, -> (state, event) { decrease_quantity(state, event.data.fetch(:product_id)) }) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def increase_quantity(state, product_id) | ||
| prod_quantity = state.find { |prod_quantity| prod_quantity.fetch(:product_id) == product_id } | ||
|
|
||
| if prod_quantity | ||
| prod_quantity[:quantity] += 1 | ||
| else | ||
| state << { product_id: product_id, quantity: 1 } | ||
| end | ||
| end | ||
|
|
||
| def decrease_quantity(state, product_id) | ||
| prod_quantity = state.find { |prod_quantity| prod_quantity.fetch(:product_id) == product_id } | ||
|
|
||
| prod_quantity[:quantity] -= 1 | ||
| state.delete(prod_quantity) if prod_quantity.fetch(:quantity).zero? | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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 AddItemToRefundTest < Test | ||
| cover "Ordering::OnAddItemToRefund*" | ||
|
|
||
| def test_add_item_to_refund | ||
| order_id = SecureRandom.uuid | ||
| aggregate_id = SecureRandom.uuid | ||
| product_1_id = SecureRandom.uuid | ||
| product_2_id = SecureRandom.uuid | ||
| product_3_id = SecureRandom.uuid | ||
| stream = "Ordering::Refund$#{aggregate_id}" | ||
|
|
||
| arrange( | ||
| AddItemToBasket.new(order_id: order_id, product_id: product_1_id), | ||
| AddItemToBasket.new(order_id: order_id, product_id: product_2_id), | ||
| AddItemToBasket.new(order_id: order_id, product_id: product_2_id), | ||
| AddItemToBasket.new(order_id: order_id, product_id: product_3_id), | ||
| CreateDraftRefund.new(refund_id: aggregate_id, order_id: order_id), | ||
| AddItemToRefund.new( | ||
| refund_id: aggregate_id, | ||
| order_id: order_id, | ||
| product_id: product_2_id | ||
| ) | ||
| ) | ||
|
|
||
| expected_events = [ | ||
| ItemAddedToRefund.new( | ||
| data: { | ||
| refund_id: aggregate_id, | ||
| order_id: order_id, | ||
| product_id: product_2_id | ||
| } | ||
| ) | ||
| ] | ||
|
|
||
| assert_events(stream, *expected_events) do | ||
| act( | ||
| AddItemToRefund.new( | ||
| refund_id: aggregate_id, | ||
| order_id: order_id, | ||
| product_id: product_2_id | ||
| ) | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| def test_add_item_raises_exceeds_order_quantity_error | ||
| aggregate_id = SecureRandom.uuid | ||
| order_id = SecureRandom.uuid | ||
| product_id = SecureRandom.uuid | ||
|
|
||
| arrange( | ||
| AddItemToBasket.new(order_id: order_id, product_id: product_id), | ||
| CreateDraftRefund.new(refund_id: aggregate_id, order_id: order_id), | ||
| AddItemToRefund.new( | ||
| refund_id: aggregate_id, | ||
| order_id: order_id, | ||
| product_id: product_id | ||
| ) | ||
| ) | ||
|
|
||
| assert_raises(Ordering::Refund::ExceedsOrderQuantityError) do | ||
| act(AddItemToRefund.new(refund_id: aggregate_id, order_id: order_id, product_id: product_id)) | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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,32 @@ | ||
| require_relative "test_helper" | ||
|
|
||
| module Ordering | ||
| class CreateDraftRefundTest < Test | ||
| cover "Ordering::OnCreateDraftRefund*" | ||
|
|
||
| def test_draft_refund_created | ||
| order_id = SecureRandom.uuid | ||
| aggregate_id = SecureRandom.uuid | ||
| stream = "Ordering::Refund$#{aggregate_id}" | ||
|
|
||
| expected_events = [ | ||
| DraftRefundCreated.new( | ||
| data: { | ||
| refund_id: aggregate_id, | ||
| order_id: order_id, | ||
| refundable_products: [] | ||
| } | ||
| ) | ||
| ] | ||
|
|
||
| assert_events(stream, *expected_events) do | ||
| act( | ||
| CreateDraftRefund.new( | ||
| refund_id: aggregate_id, | ||
| order_id: order_id | ||
| ) | ||
| ) | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.