@@ -17,11 +17,23 @@ def initialize(event_store, command_bus)
1717 )
1818
1919 def act
20- calculate_sub_amounts
20+ create_invoice_items_for_all_products if state . placed?
2121 end
2222
2323 private
2424
25+ def create_invoice_items_for_all_products
26+ product_totals . each do |product_id , quantity , amount |
27+ create_invoice_items_for_product ( product_id , quantity , amount )
28+ end
29+ end
30+
31+ def product_totals
32+ state . sub_amounts_total . map do |product_id , amounts |
33+ [ product_id , amounts . fetch ( :quantity ) , amounts . fetch ( :amount ) ]
34+ end
35+ end
36+
2537 def fetch_id ( event )
2638 event . data . fetch ( :order_id )
2739 end
@@ -30,30 +42,17 @@ def apply(event)
3042 @order_id = event . data . fetch ( :order_id )
3143 case event
3244 when Pricing ::PriceItemAdded
33- apply_price_item_added ( event . data . fetch ( :product_id ) , event . data . fetch ( :base_price ) , event . data . fetch ( :price ) )
45+ state . add_line ( event . data . fetch ( :product_id ) , event . data . fetch ( :base_price ) , event . data . fetch ( :price ) )
3446 when Pricing ::PriceItemRemoved
35- apply_price_item_removed ( event . data . fetch ( :product_id ) )
47+ state . remove_line ( event . data . fetch ( :product_id ) )
3648 when Pricing ::PercentageDiscountSet
37- apply_percentage_discount_set ( event . data . fetch ( :type ) , event . data . fetch ( :amount ) )
49+ state . set_discount ( event . data . fetch ( :type ) , event . data . fetch ( :amount ) )
3850 when Pricing ::PercentageDiscountChanged
39- apply_percentage_discount_changed ( event . data . fetch ( :type ) , event . data . fetch ( :amount ) )
51+ state . set_discount ( event . data . fetch ( :type ) , event . data . fetch ( :amount ) )
4052 when Pricing ::PercentageDiscountRemoved
41- apply_percentage_discount_removed ( event . data . fetch ( :type ) )
53+ state . remove_discount ( event . data . fetch ( :type ) )
4254 when Fulfillment ::OrderRegistered
43- apply_order_registered
44- end
45- end
46-
47- def calculate_sub_amounts
48- return unless state . placed?
49-
50- sub_amounts_total = state . sub_amounts_total
51- sub_amounts_total . each_pair do |product_id , h |
52- create_invoice_items_for_product (
53- product_id ,
54- h . fetch ( :quantity ) ,
55- h . fetch ( :amount )
56- )
55+ state . mark_placed
5756 end
5857 end
5958
@@ -73,30 +72,6 @@ def create_invoice_items_for_product(product_id, quantity, discounted_amount)
7372 end
7473 end
7574
76- def apply_price_item_added ( product_id , base_price , price )
77- state . add_line ( product_id , base_price , price )
78- end
79-
80- def apply_price_item_removed ( product_id )
81- state . remove_line ( product_id )
82- end
83-
84- def apply_percentage_discount_set ( discount_type , discount_amount )
85- state . set_discount ( discount_type , discount_amount )
86- end
87-
88- def apply_percentage_discount_changed ( discount_type , discount_amount )
89- state . set_discount ( discount_type , discount_amount )
90- end
91-
92- def apply_percentage_discount_removed ( discount_type )
93- state . remove_discount ( discount_type )
94- end
95-
96- def apply_order_registered
97- state . mark_placed
98- end
99-
10075 end
10176
10277 Invoice = Data . define ( :lines , :discounts , :order_placed ) do
0 commit comments