-
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
306 additions
and
168 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Ordering | ||
class Projections | ||
def self.product_quantity_available_to_refund(order_id, product_id) | ||
RubyEventStore::Projection | ||
.from_stream("Ordering::Order$#{order_id}") | ||
.init(-> { { available: 0 } }) | ||
.when(ItemAddedToBasket, -> (state, event) { state[:available] += 1 if event.data.fetch(:product_id) == product_id }) | ||
.when(ItemRemovedFromBasket, -> (state, event) { state[:available] -= 1 if event.data.fetch(:product_id) == product_id }) | ||
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
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,27 @@ | ||
require_relative "test_helper" | ||
|
||
module Ordering | ||
class ProjectionsTest < Test | ||
cover "Ordering::Projections" | ||
|
||
def test_product_quantity_available_to_refund | ||
order_id = SecureRandom.uuid | ||
product_id = SecureRandom.uuid | ||
another_product_id = SecureRandom.uuid | ||
stream_name = "Ordering::Order$#{order_id}" | ||
projection = Projections.product_quantity_available_to_refund(order_id, product_id) | ||
|
||
event_store = RubyEventStore::Client.new(repository: RubyEventStore::InMemoryRepository.new) | ||
|
||
event_store.publish(ItemAddedToBasket.new(data: { order_id: order_id, product_id: product_id }), stream_name: stream_name) | ||
event_store.publish(ItemAddedToBasket.new(data: { order_id: order_id, product_id: product_id }), stream_name: stream_name) | ||
event_store.publish(ItemRemovedFromBasket.new(data: { order_id: order_id, product_id: product_id }), stream_name: stream_name) | ||
event_store.publish(ItemAddedToBasket.new(data: { order_id: order_id, product_id: another_product_id }), stream_name: stream_name) | ||
event_store.publish(ItemRemovedFromBasket.new(data: { order_id: order_id, product_id: another_product_id }), stream_name: stream_name) | ||
|
||
available_quantity_to_refund = projection.run(event_store) | ||
|
||
assert_equal({ available: 1 }, available_quantity_to_refund) | ||
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,48 @@ | ||
require_relative "test_helper" | ||
|
||
module Ordering | ||
class RefundItemsListTest < Test | ||
|
||
def test_initialize | ||
list = ItemsList.new | ||
|
||
assert_equal 0, list.refund_items.size | ||
end | ||
|
||
def test_increase_item_quantity | ||
product_one_id = SecureRandom.uuid | ||
product_two_id = SecureRandom.uuid | ||
list = ItemsList.new | ||
|
||
list.increase_quantity(product_one_id) | ||
|
||
assert_equal 1, list.refund_items.size | ||
assert_equal 1, list.quantity(product_one_id) | ||
|
||
list.increase_quantity(product_two_id) | ||
|
||
assert_equal 2, list.refund_items.size | ||
assert_equal 1, list.quantity(product_two_id) | ||
end | ||
|
||
def test_decrease_item_quantity | ||
product_id = SecureRandom.uuid | ||
list = ItemsList.new | ||
|
||
list.increase_quantity(product_id) | ||
list.increase_quantity(product_id) | ||
|
||
assert_equal 1, list.refund_items.size | ||
assert_equal 2, list.quantity(product_id) | ||
|
||
list.decrease_quantity(product_id) | ||
|
||
assert_equal 1, list.refund_items.size | ||
assert_equal 1, list.quantity(product_id) | ||
|
||
list.decrease_quantity(product_id) | ||
|
||
assert_equal 0, list.refund_items.size | ||
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
43 changes: 12 additions & 31 deletions
43
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 |
---|---|---|
@@ -1,40 +1,21 @@ | ||
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 | ||
refund = Refund.find_by!(uid: event.data.fetch(:refund_id)) | ||
product = Orders::Product.find_by!(uid: event.data.fetch(:product_id)) | ||
|
||
def event_store | ||
Rails.configuration.event_store | ||
end | ||
item = refund.refund_items.find_or_create_by(product_uid: product.uid) do |item| | ||
item.price = product.price | ||
item.quantity = 0 | ||
end | ||
|
||
def find(refund_id, product_id) | ||
Refund | ||
.find_by_uid(refund_id) | ||
.refund_items | ||
.where(product_uid: product_id) | ||
.first | ||
end | ||
refund.total_value += item.price | ||
item.quantity += 1 | ||
|
||
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 | ||
) | ||
ActiveRecord::Base.transaction do | ||
refund.save! | ||
item.save! | ||
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
24 changes: 8 additions & 16 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 |
---|---|---|
@@ -1,24 +1,16 @@ | ||
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 | ||
refund = Refund.find_by!(uid: event.data.fetch(:refund_id)) | ||
item = refund.refund_items.find_by!(product_uid: event.data.fetch(:product_id)) | ||
|
||
private | ||
def find(order_uid, product_id) | ||
Refund | ||
.find_by_uid(order_uid) | ||
.refund_items | ||
.where(product_uid: product_id) | ||
.first | ||
end | ||
refund.total_value -= item.price | ||
item.quantity -= 1 | ||
|
||
def event_store | ||
Rails.configuration.event_store | ||
ActiveRecord::Base.transaction do | ||
refund.save! | ||
item.quantity > 0 ? item.save! : item.destroy! | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.