File tree Expand file tree Collapse file tree 4 files changed +23
-5
lines changed
Expand file tree Collapse file tree 4 files changed +23
-5
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 |
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 (
You can’t perform that action at this time.
0 commit comments