Skip to content

Commit 2f6748c

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

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

rails_application/app/processes/processes/total_order_value.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ class TotalOrderValue
55
subscribes_to(
66
Pricing::PriceItemAdded,
77
Pricing::PriceItemRemoved,
8-
Pricing::PercentageDiscountSet
8+
Pricing::PercentageDiscountSet,
9+
Pricing::PercentageDiscountChanged
910
)
1011

1112
private
@@ -27,6 +28,8 @@ def apply(event)
2728
state.with(lines:)
2829
when Pricing::PercentageDiscountSet
2930
state.with(discount_amount: event.data.fetch(:amount))
31+
when Pricing::PercentageDiscountChanged
32+
state.with(discount_amount: event.data.fetch(:amount))
3033
else
3134
state
3235
end

rails_application/test/processes/total_order_value_test.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,37 @@ def test_with_discount
124124
end
125125
end
126126

127+
def test_changed_discount
128+
process = TotalOrderValue.new(event_store, command_bus)
129+
product_id = SecureRandom.uuid
130+
event_1 = price_item_added(product_id)
131+
event_2 = Pricing::PercentageDiscountSet.new(data: {
132+
order_id: order_id,
133+
type: "test_discount",
134+
amount: 10
135+
})
136+
event_3 = Pricing::PercentageDiscountChanged.new(data: {
137+
order_id: order_id,
138+
type: "test_discount",
139+
amount: 20
140+
})
141+
event_store.append(event_1)
142+
event_store.append(event_2)
143+
event_store.append(event_3)
144+
process.call(event_1)
145+
process.call(event_2)
146+
assert_events_contain(
147+
"Processes::TotalOrderValue$#{event_3.data[:order_id]}",
148+
Processes::TotalOrderValueUpdated.new(
149+
data: {
150+
total_value: 80,
151+
order_id: order_id
152+
}
153+
)) do
154+
process.call(event_3)
155+
end
156+
end
157+
127158

128159

129160
private

0 commit comments

Comments
 (0)