-
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.
Pass refundable_products when initializing a refund
- Loading branch information
Showing
12 changed files
with
125 additions
and
77 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
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
11 changes: 0 additions & 11 deletions
11
ecommerce/ordering/lib/ordering/product_quantity_available_to_refund.rb
This file was deleted.
Oops, something went wrong.
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,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 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
27 changes: 0 additions & 27 deletions
27
ecommerce/ordering/test/product_quantity_available_to_refund_test.rb
This file was deleted.
Oops, something went wrong.
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,29 @@ | ||
require_relative "test_helper" | ||
|
||
module Ordering | ||
class RefundableProductsTest < Test | ||
cover "Ordering::RefundableProducts" | ||
|
||
def test_product_quantity_available_to_refund | ||
order_id = SecureRandom.uuid | ||
product_1_id = SecureRandom.uuid | ||
product_2_id = SecureRandom.uuid | ||
product_3_id = SecureRandom.uuid | ||
stream_name = "Ordering::Order$#{order_id}" | ||
projection = RefundableProducts.call(order_id) | ||
|
||
event_store = RubyEventStore::Client.new(repository: RubyEventStore::InMemoryRepository.new) | ||
|
||
event_store.publish(ItemAddedToBasket.new(data: { order_id: order_id, product_id: product_3_id }), stream_name: stream_name) | ||
event_store.publish(ItemAddedToBasket.new(data: { order_id: order_id, product_id: product_1_id }), stream_name: stream_name) | ||
event_store.publish(ItemAddedToBasket.new(data: { order_id: order_id, product_id: product_2_id }), stream_name: stream_name) | ||
event_store.publish(ItemAddedToBasket.new(data: { order_id: order_id, product_id: product_2_id }), stream_name: stream_name) | ||
event_store.publish(ItemRemovedFromBasket.new(data: { order_id: order_id, product_id: product_2_id }), stream_name: stream_name) | ||
event_store.publish(ItemRemovedFromBasket.new(data: { order_id: order_id, product_id: product_3_id }), stream_name: stream_name) | ||
|
||
refundable_products = projection.run(event_store) | ||
|
||
assert_equal([{ product_id: product_1_id, quantity: 1}, {product_id: product_2_id, quantity: 1 }], refundable_products) | ||
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
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