Skip to content

Commit

Permalink
Pricing::Offer better represents the domain meaning than Order
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejkrzywda committed Nov 23, 2023
1 parent f247339 commit 8b0c5cc
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 37 deletions.
2 changes: 1 addition & 1 deletion ecommerce/pricing/lib/pricing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require_relative "pricing/commands"
require_relative "pricing/events"
require_relative "pricing/services"
require_relative "pricing/order"
require_relative "pricing/offer"
require_relative "pricing/price_change"
require_relative "pricing/pricing_catalog"
require_relative "pricing/time_promotion"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Pricing
class Order
class Offer
include AggregateRoot

def initialize(id)
Expand Down
2 changes: 1 addition & 1 deletion ecommerce/pricing/lib/pricing/pricing_catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def initialize(event_store)

def price_for(product)
case product
when Order::FreeProduct
when Offer::FreeProduct
0
else
price_by_product_id(product.id)
Expand Down
18 changes: 9 additions & 9 deletions ecommerce/pricing/lib/pricing/services.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def initialize(event_store)
end

def call(cmd)
@repository.with_aggregate(Order, cmd.aggregate_id) do |order|
@repository.with_aggregate(Offer, cmd.aggregate_id) do |order|
order.apply_discount(Discounts::PercentageDiscount.new(cmd.amount))
end
end
Expand All @@ -32,7 +32,7 @@ def initialize(event_store)
end

def call(cmd)
@repository.with_aggregate(Order, cmd.aggregate_id) do |order|
@repository.with_aggregate(Offer, cmd.aggregate_id) do |order|
order.reset_discount
end
end
Expand All @@ -44,7 +44,7 @@ def initialize(event_store)
end

def call(cmd)
@repository.with_aggregate(Order, cmd.aggregate_id) do |order|
@repository.with_aggregate(Offer, cmd.aggregate_id) do |order|
order.change_discount(Discounts::PercentageDiscount.new(cmd.amount))
end
end
Expand Down Expand Up @@ -95,7 +95,7 @@ def initialize(event_store)
end

def call(command)
@repository.with_aggregate(Order, command.aggregate_id) do |order|
@repository.with_aggregate(Offer, command.aggregate_id) do |order|
order.add_item(command.product_id)
end
end
Expand All @@ -107,7 +107,7 @@ def initialize(event_store)
end

def call(command)
@repository.with_aggregate(Order, command.aggregate_id) do |order|
@repository.with_aggregate(Offer, command.aggregate_id) do |order|
order.remove_item(command.product_id)
end
end
Expand All @@ -120,7 +120,7 @@ def initialize(event_store)
end

def call(command)
@repository.with_aggregate(Order, command.aggregate_id) do |order|
@repository.with_aggregate(Offer, command.aggregate_id) do |order|
order.calculate_total_value(PricingCatalog.new(@event_store), time_promotions_discount)
end
rescue RubyEventStore::WrongExpectedEventVersion
Expand All @@ -130,7 +130,7 @@ def call(command)


def calculate_sub_amounts(command)
@repository.with_aggregate(Order, command.aggregate_id) do |order|
@repository.with_aggregate(Offer, command.aggregate_id) do |order|
order.calculate_sub_amounts(PricingCatalog.new(@event_store), time_promotions_discount)
end
rescue RubyEventStore::WrongExpectedEventVersion
Expand Down Expand Up @@ -163,7 +163,7 @@ def initialize(event_store)
end

def call(command)
@repository.with_aggregate(Order, command.aggregate_id) do |order|
@repository.with_aggregate(Offer, command.aggregate_id) do |order|
order.make_product_free(command.order_id, command.product_id)
end
end
Expand All @@ -175,7 +175,7 @@ def initialize(event_store)
end

def call(command)
@repository.with_aggregate(Order, command.aggregate_id) do |order|
@repository.with_aggregate(Offer, command.aggregate_id) do |order|
order.remove_free_product(command.order_id, command.product_id)
end
end
Expand Down
18 changes: 10 additions & 8 deletions ecommerce/pricing/test/free_products_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ def test_making_product_free_possible_when_order_is_eligible
add_item(order_id, product_1_id)
add_item(order_id, product_1_id)
add_item(order_id, product_1_id)
stream = "Pricing::Order$#{order_id}"

assert_events_contain(
stream,
stream_name(order_id),
ProductMadeFreeForOrder.new(
data: {
order_id: order_id,
Expand Down Expand Up @@ -46,10 +45,9 @@ def test_making_only_the_cheapest_product_free
add_item(order_id, product_1_id)
add_item(order_id, product_1_id)
add_item(order_id, cheaper_product)
stream = "Pricing::Order$#{order_id}"

assert_events_contain(
stream,
stream_name(order_id),
ProductMadeFreeForOrder.new(
data: {
order_id: order_id,
Expand Down Expand Up @@ -98,7 +96,6 @@ def test_making_product_free_possible_after_previous_free_product_was_removed
add_item(order_id, product_1_id)
add_item(order_id, product_1_id)
add_item(order_id, product_1_id)
stream = "Pricing::Order$#{order_id}"

run_command(
Pricing::MakeProductFreeForOrder.new(order_id: order_id, product_id: product_1_id)
Expand All @@ -117,7 +114,7 @@ def test_making_product_free_possible_after_previous_free_product_was_removed
)

assert_events_contain(
stream,
stream_name(order_id),
ProductMadeFreeForOrder.new(
data: {
order_id: order_id,
Expand Down Expand Up @@ -146,14 +143,13 @@ def test_removing_free_product_possible_if_it_is_already_set
add_item(order_id, product_1_id)
add_item(order_id, product_1_id)
add_item(order_id, product_1_id)
stream = "Pricing::Order$#{order_id}"

run_command(
Pricing::MakeProductFreeForOrder.new(order_id: order_id, product_id: product_1_id)
)

assert_events_contain(
stream,
stream_name(order_id),
FreeProductRemovedFromOrder.new(
data: {
order_id: order_id,
Expand Down Expand Up @@ -211,5 +207,11 @@ def test_removing_free_product_twice_not_possible
end
end

private

def stream_name(id)
"Pricing::Offer$#{id}"
end

end
end
8 changes: 6 additions & 2 deletions ecommerce/pricing/test/future_prices_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_future_price_is_not_included_when_calculating_total_value
set_future_price(product_1_id, 30, future_date_timestamp)
order_id = SecureRandom.uuid
add_item(order_id, product_1_id)
stream = "Pricing::Order$#{order_id}"
stream = stream_name(order_id)

assert_events(
stream,
Expand All @@ -34,7 +34,7 @@ def test_check_future_price
Timecop.travel(future_date_timestamp + 2137) do
order_id = SecureRandom.uuid
add_item(order_id, product_1_id)
stream = "Pricing::Order$#{order_id}"
stream = stream_name(order_id)

assert_events(
stream,
Expand Down Expand Up @@ -110,6 +110,10 @@ def test_future_prices_catalog_by_product_id

private

def stream_name(order_id)
"Pricing::Offer$#{order_id}"
end

def days_number(n)
3600 * 24 * n
end
Expand Down
6 changes: 3 additions & 3 deletions ecommerce/pricing/test/order_product_test.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require_relative "test_helper"

module Pricing
class Order
class Offer
class ProductTest < Test
cover "Pricing::Order::Product"
cover "Pricing::Offer::Product"

def setup
super
Expand All @@ -24,7 +24,7 @@ def test_hash_equality
end

class FreeProductTest < Test
cover "Pricing::Order::FreeProduct"
cover "Pricing::Offer::FreeProduct"

def setup
super
Expand Down
18 changes: 11 additions & 7 deletions ecommerce/pricing/test/pricing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_calculates_total_value
order_id = SecureRandom.uuid
add_item(order_id, product_1_id)
add_item(order_id, product_2_id)
stream = "Pricing::Order$#{order_id}"
stream = stream_name(order_id)
assert_events(
stream,
OrderTotalValueCalculated.new(
Expand All @@ -39,7 +39,7 @@ def test_calculates_sub_amounts
set_price(product_1_id, 20)
set_price(product_2_id, 30)
order_id = SecureRandom.uuid
stream = "Pricing::Order$#{order_id}"
stream = stream_name(order_id)

assert_events(stream) { calculate_sub_amounts(order_id) }

Expand Down Expand Up @@ -98,7 +98,7 @@ def test_calculates_total_value_with_discount
set_price(product_1_id, 20)
order_id = SecureRandom.uuid
add_item(order_id, product_1_id)
stream = "Pricing::Order$#{order_id}"
stream = stream_name(order_id)
assert_events(
stream,
OrderTotalValueCalculated.new(
Expand Down Expand Up @@ -175,7 +175,7 @@ def test_calculates_total_value_with_100_discount
set_price(product_1_id, 20)
order_id = SecureRandom.uuid
add_item(order_id, product_1_id)
stream = "Pricing::Order$#{order_id}"
stream = stream_name(order_id)
assert_events_contain(
stream,
PercentageDiscountSet.new(
Expand Down Expand Up @@ -269,7 +269,7 @@ def test_changing_discount_possible_when_discount_is_set
set_price(product_1_id, 20)
order_id = SecureRandom.uuid
add_item(order_id, product_1_id)
stream = "Pricing::Order$#{order_id}"
stream = stream_name(order_id)
run_command(
Pricing::SetPercentageDiscount.new(order_id: order_id, amount: 10)
)
Expand Down Expand Up @@ -301,7 +301,7 @@ def test_changing_discount_possible_more_than_once
set_price(product_1_id, 20)
order_id = SecureRandom.uuid
add_item(order_id, product_1_id)
stream = "Pricing::Order$#{order_id}"
stream = stream_name(order_id)
run_command(
Pricing::SetPercentageDiscount.new(order_id: order_id, amount: 10)
)
Expand Down Expand Up @@ -336,7 +336,7 @@ def test_resetting_discount_possible_when_discount_has_been_set_and_then_changed
set_price(product_1_id, 20)
order_id = SecureRandom.uuid
add_item(order_id, product_1_id)
stream = "Pricing::Order$#{order_id}"
stream = stream_name(order_id)
run_command(
Pricing::SetPercentageDiscount.new(order_id: order_id, amount: 10)
)
Expand Down Expand Up @@ -390,6 +390,10 @@ def test_resetting_with_missing_discount_not_possible

private

def stream_name(order_id)
"Pricing::Offer$#{order_id}"
end

def calculate_sub_amounts(order_id)
run_command(CalculateSubAmounts.new(order_id: order_id))
end
Expand Down
2 changes: 1 addition & 1 deletion ecommerce/pricing/test/simple_offer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_removing
set_price(product_1_id, 20)
order_id = SecureRandom.uuid
add_item(order_id, product_1_id)
stream = "Pricing::Order$#{order_id}"
stream = "Pricing::Offer$#{order_id}"
assert_events(
stream,
OrderTotalValueCalculated.new(
Expand Down
2 changes: 1 addition & 1 deletion ecommerce/pricing/test/subamounts_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def price_item_value_calculated_event(amount, discounted_amount)
end

def stream
"Pricing::Order$#{order_id}"
"Pricing::Offer$#{order_id}"
end

def product_id
Expand Down
10 changes: 7 additions & 3 deletions ecommerce/pricing/test/time_promotion_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_calculates_total_value_with_time_promotion
set_price(product_1_id, 20)
order_id = SecureRandom.uuid
add_item(order_id, product_1_id)
stream = "Pricing::Order$#{order_id}"
stream = stream_name(order_id)

assert_events(
stream,
Expand Down Expand Up @@ -110,7 +110,7 @@ def test_calculates_sub_amounts_with_combined_discounts
set_price(product_1_id, 20)
set_price(product_2_id, 30)
order_id = SecureRandom.uuid
stream = "Pricing::Order$#{order_id}"
stream = stream_name(order_id)

assert_events(stream) { calculate_sub_amounts(order_id) }

Expand Down Expand Up @@ -188,7 +188,7 @@ def test_takes_last_values_for_time_promotion
set_price(product_1_id, 20)
order_id = SecureRandom.uuid
add_item(order_id, product_1_id)
stream = "Pricing::Order$#{order_id}"
stream = stream_name(order_id)

assert_events(
stream,
Expand All @@ -205,6 +205,10 @@ def test_takes_last_values_for_time_promotion

private

def stream_name(order_id)
"Pricing::Offer$#{order_id}"
end

def set_time_promotion_range(time_promotion_id, start_time, end_time, discount)
run_command(
CreateTimePromotion.new(time_promotion_id: time_promotion_id, start_time: start_time, end_time: end_time, discount: discount, label: "test")
Expand Down

0 comments on commit 8b0c5cc

Please sign in to comment.