Skip to content

Commit 39725d5

Browse files
TotalOrderValue - handle setting discounts
I'm copying the algorithm from the Offer class to later remove the original one.
1 parent 3d65679 commit 39725d5

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

rails_application/app/processes/processes/total_order_value.rb

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ class TotalOrderValue
44

55
subscribes_to(
66
Pricing::PriceItemAdded,
7-
Pricing::PriceItemRemoved
7+
Pricing::PriceItemRemoved,
8+
Pricing::PercentageDiscountSet
89
)
910

1011
private
1112

1213
def act
13-
@total_value = state.lines.sum { |line| line.fetch(:price) }
14-
publish_total_order_value
14+
value = state.lines.sum { |line| line.fetch(:price) } - state.discount_amount
15+
publish_total_order_value(value)
1516
end
1617

1718
def apply(event)
@@ -24,16 +25,18 @@ def apply(event)
2425
when Pricing::PriceItemRemoved
2526
lines = state.lines.reject { |line| line.fetch(:product_id) == event.data.fetch(:product_id) }
2627
state.with(lines:)
28+
when Pricing::PercentageDiscountSet
29+
state.with(discount_amount: event.data.fetch(:amount))
2730
else
2831
state
2932
end
3033
end
3134

3235
private
3336

34-
def publish_total_order_value
37+
def publish_total_order_value(value)
3538
event_store.publish(
36-
TotalOrderValueUpdated.new(data: { total_value: @total_value, order_id: @order_id }),
39+
TotalOrderValueUpdated.new(data: { total_value: value, order_id: @order_id }),
3740
stream_name: "Processes::TotalOrderValue$#{@order_id}"
3841
)
3942
end
@@ -43,9 +46,9 @@ def fetch_id(event)
4346
end
4447
end
4548

46-
Offer = Data.define(:lines) do
47-
def initialize(lines: [])
48-
super(lines: lines.freeze)
49+
Offer = Data.define(:lines, :discount_amount) do
50+
def initialize(lines: [], discount_amount: 0)
51+
super(lines: lines.freeze, discount_amount:)
4952
end
5053
end
5154

rails_application/test/processes/total_order_value_test.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def test_total_order_value_calculation_for_2_items_of_different_products
6868
process.call(event_2)
6969
end
7070
end
71+
7172
def test_total_order_value_calculation_for_2_items_of_different_products_and_1_item_removed
7273
process = TotalOrderValue.new(event_store, command_bus)
7374
product_id_1 = SecureRandom.uuid
@@ -99,6 +100,30 @@ def test_total_order_value_calculation_for_2_items_of_different_products_and_1_i
99100
end
100101
end
101102

103+
def test_with_discount
104+
process = TotalOrderValue.new(event_store, command_bus)
105+
product_id = SecureRandom.uuid
106+
event_1 = price_item_added(product_id)
107+
event_2 = Pricing::PercentageDiscountSet.new(data: {
108+
order_id: order_id,
109+
type: "test_discount",
110+
amount: 10
111+
})
112+
event_store.append(event_1)
113+
event_store.append(event_2)
114+
process.call(event_1)
115+
assert_events_contain(
116+
"Processes::TotalOrderValue$#{event_2.data[:order_id]}",
117+
Processes::TotalOrderValueUpdated.new(
118+
data: {
119+
total_value: 90,
120+
order_id: order_id
121+
}
122+
)) do
123+
process.call(event_2)
124+
end
125+
end
126+
102127

103128

104129
private

0 commit comments

Comments
 (0)