Skip to content

Commit 253b767

Browse files
Being explicit which parts of event we pass to apply method
This code around extracting data from event feels a bit boilerplate
1 parent 93ca8a2 commit 253b767

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed

rails_application/app/processes/processes/invoice_generation.rb

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ def apply(event)
3030
@order_id = event.data.fetch(:order_id)
3131
case event
3232
when Pricing::PriceItemAdded
33-
apply_price_item_added(event)
33+
apply_price_item_added(event.data.fetch(:product_id), event.data.fetch(:base_price), event.data.fetch(:price))
3434
when Pricing::PriceItemRemoved
35-
apply_price_item_removed(event)
35+
apply_price_item_removed(event.data.fetch(:product_id))
3636
when Pricing::PercentageDiscountSet
37-
apply_percentage_discount_set(event)
37+
apply_percentage_discount_set(event.data.fetch(:type), event.data.fetch(:amount))
3838
when Pricing::PercentageDiscountChanged
39-
apply_percentage_discount_changed(event)
39+
apply_percentage_discount_changed(event.data.fetch(:type), event.data.fetch(:amount))
4040
when Pricing::PercentageDiscountRemoved
41-
apply_percentage_discount_removed(event)
41+
apply_percentage_discount_removed(event.data.fetch(:type))
4242
when Fulfillment::OrderRegistered
4343
apply_order_registered
4444
end
@@ -50,14 +50,14 @@ def calculate_sub_amounts
5050
sub_amounts_total = state.sub_amounts_total
5151
sub_amounts_total.each_pair do |product_id, h|
5252
create_invoice_items_for_product(
53-
product_id: product_id,
54-
quantity: h.fetch(:quantity),
55-
discounted_amount: h.fetch(:amount)
53+
product_id,
54+
h.fetch(:quantity),
55+
h.fetch(:amount)
5656
)
5757
end
5858
end
5959

60-
def create_invoice_items_for_product(product_id:, quantity:, discounted_amount:)
60+
def create_invoice_items_for_product(product_id, quantity, discounted_amount)
6161
vat_rate = @vat_rate_catalog.vat_rate_for(product_id)
6262
unit_prices = MoneySplitter.new(discounted_amount, quantity).call
6363
unit_prices.tally.each do |unit_price, quantity|
@@ -73,42 +73,33 @@ def create_invoice_items_for_product(product_id:, quantity:, discounted_amount:)
7373
end
7474
end
7575

76-
def apply_price_item_added(event)
77-
product_id = event.data.fetch(:product_id)
78-
base_price = event.data.fetch(:base_price)
79-
price = event.data.fetch(:price)
76+
def apply_price_item_added(product_id, base_price, price)
8077
lines = (state.lines + [{ product_id:, base_price:, price: }])
8178
state.with(lines:)
8279
end
8380

84-
def apply_price_item_removed(event)
85-
product_id = event.data.fetch(:product_id)
81+
def apply_price_item_removed(product_id)
8682
lines = state.lines.dup
8783
index_to_remove = lines.find_index { |line| line.fetch(:product_id) == product_id}
8884
lines.delete_at(index_to_remove)
8985
state.with(lines:)
9086
end
9187

92-
def apply_percentage_discount_set(event)
93-
discount_type = event.data.fetch(:type)
94-
discount_amount = event.data.fetch(:amount)
88+
def apply_percentage_discount_set(discount_type, discount_amount)
9589
discounts = state.discounts.reject { |d| d.fetch(:type) == discount_type }
9690
discounts = discounts + [{ type: discount_type, amount: discount_amount }]
9791
new_state = state.with(discounts:)
9892
apply_discounts_to_existing_lines(new_state)
9993
end
10094

101-
def apply_percentage_discount_changed(event)
102-
discount_type = event.data.fetch(:type)
103-
discount_amount = event.data.fetch(:amount)
95+
def apply_percentage_discount_changed(discount_type, discount_amount)
10496
discounts = state.discounts.reject { |d| d.fetch(:type) == discount_type }
10597
discounts = discounts + [{ amount: discount_amount }]
10698
new_state = state.with(discounts: discounts)
10799
apply_discounts_to_existing_lines(new_state)
108100
end
109101

110-
def apply_percentage_discount_removed(event)
111-
discount_type = event.data.fetch(:type)
102+
def apply_percentage_discount_removed(discount_type)
112103
discounts = state.discounts.reject { |d| d.fetch(:type) == discount_type }
113104
new_state = state.with(discounts:)
114105
apply_discounts_to_existing_lines(new_state)

0 commit comments

Comments
 (0)