Skip to content

Commit 98adb88

Browse files
committed
Extend PercentageDiscountRemoved event by base_total_value
another step to get rid of OrderTotalValueCalculated
1 parent c708d64 commit 98adb88

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

ecommerce/pricing/lib/pricing/events.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ class PriceItemRemoved < Infra::Event
6262
class PercentageDiscountRemoved < Infra::Event
6363
attribute :order_id, Infra::Types::UUID
6464
attribute :type, Infra::Types::String
65+
attribute :base_total_value, Infra::Types::Price
66+
attribute :total_value, Infra::Types::Price
6567
end
6668

6769
class PercentageDiscountChanged < Infra::Event

ecommerce/pricing/lib/pricing/offer.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,17 @@ def change_discount(discount)
7575

7676
def remove_discount(type)
7777
raise NotPossibleToRemoveWithoutDiscount unless discount_exists?(type)
78+
discount = @discounts.find { |d| d.type == type }
79+
80+
discounted_value = @list.base_sum * (discount.value / 100)
81+
82+
7883
apply PercentageDiscountRemoved.new(
7984
data: {
8085
order_id: @id,
81-
type: type
86+
type: type,
87+
base_total_value: @list.base_sum,
88+
total_value: @list.actual_sum + discounted_value,
8289
}
8390
)
8491
end
@@ -175,6 +182,7 @@ def expire
175182

176183
on PriceItemAdded do |event|
177184
@list.add_item(event.data.fetch(:product_id), event.data.fetch(:base_price), event.data.fetch(:price))
185+
@list.apply_discounts(@discounts)
178186
end
179187

180188
on PriceItemRemoved do |event|

ecommerce/pricing/test/apply_time_promotion_test.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ def percentage_discount_removed_event(order_id)
9292
PercentageDiscountRemoved.new(
9393
data: {
9494
order_id: order_id,
95-
type: Pricing::Discounts::TIME_PROMOTION_DISCOUNT
95+
type: Pricing::Discounts::TIME_PROMOTION_DISCOUNT,
96+
base_total_value: 1000,
97+
total_value: 1000
9698
}
9799
)
98100
end

ecommerce/pricing/test/pricing_test.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ def test_removes_time_promotion_discount
130130
PercentageDiscountRemoved.new(
131131
data: {
132132
order_id: order_id,
133-
type: Discounts::TIME_PROMOTION_DISCOUNT
133+
type: Discounts::TIME_PROMOTION_DISCOUNT,
134+
base_total_value: 0,
135+
total_value: 0
134136
}
135137
)
136138
) { remove_time_promotion_discount(order_id) }
@@ -207,7 +209,9 @@ def test_calculates_total_value_with_discount
207209
PercentageDiscountRemoved.new(
208210
data: {
209211
order_id: order_id,
210-
type: Pricing::Discounts::GENERAL_DISCOUNT
212+
type: Pricing::Discounts::GENERAL_DISCOUNT,
213+
base_total_value: 20,
214+
total_value: 20
211215
}
212216
),
213217
OrderTotalValueCalculated.new(
@@ -408,7 +412,9 @@ def test_removing_discount_possible_when_discount_has_been_set_and_then_changed
408412
PercentageDiscountRemoved.new(
409413
data: {
410414
order_id: order_id,
411-
type: Discounts::GENERAL_DISCOUNT
415+
type: Discounts::GENERAL_DISCOUNT,
416+
base_total_value: 20,
417+
total_value: 20
412418
}
413419
),
414420
OrderTotalValueCalculated.new(

0 commit comments

Comments
 (0)