Skip to content

Commit 04adb3b

Browse files
authored
Merge pull request #443 from RailsEventStore/processes-improved-readability
Processes improved readability
2 parents 781477d + 5405d91 commit 04adb3b

File tree

6 files changed

+88
-84
lines changed

6 files changed

+88
-84
lines changed

ecommerce/processes/lib/processes/determine_vat_rates_on_order_placed.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class DetermineVatRatesOnOrderPlaced
33

44
ProcessState = Data.define(:offer_accepted, :order_placed, :order_id, :order_lines) do
55
def initialize(offer_accepted: false, order_placed: false, order_id: nil, order_lines: [])
6-
super(offer_accepted:, order_placed:, order_id:, order_lines: order_lines)
6+
super
77
end
88

99
def placed? = offer_accepted && order_placed
@@ -15,18 +15,7 @@ def placed? = offer_accepted && order_placed
1515
Fulfillment::OrderRegistered
1616
)
1717

18-
def apply(event)
19-
case event
20-
when Pricing::OfferAccepted
21-
state.with(
22-
offer_accepted: true,
23-
order_lines: event.data.fetch(:order_lines),
24-
order_id: event.data.fetch(:order_id)
25-
)
26-
when Fulfillment::OrderRegistered
27-
state.with(order_placed: true)
28-
end
29-
end
18+
private
3019

3120
def act
3221
determine_vat_rates if state.placed?
@@ -40,11 +29,22 @@ def determine_vat_rates
4029
end
4130
end
4231

43-
private
32+
def apply(event)
33+
case event
34+
when Pricing::OfferAccepted
35+
state.with(
36+
offer_accepted: true,
37+
order_lines: event.data.fetch(:order_lines),
38+
order_id: event.data.fetch(:order_id)
39+
)
40+
when Fulfillment::OrderRegistered
41+
state.with(order_placed: true)
42+
end
43+
end
4444

4545
def fetch_id(event)
4646
event.data.fetch(:order_id)
4747
end
4848

4949
end
50-
end
50+
end

ecommerce/processes/lib/processes/order_item_invoicing_process.rb

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class OrderItemInvoicingProcess
33

44
ProcessState = Data.define(:order_id, :product_id, :quantity, :vat_rate, :discounted_amount) do
55
def initialize(order_id: nil, product_id: nil, quantity: nil, vat_rate: nil, discounted_amount: nil)
6-
super(order_id:, product_id:, quantity:, vat_rate:, discounted_amount:)
6+
super
77
end
88

99
def can_create_invoice_item?
@@ -18,6 +18,25 @@ def can_create_invoice_item?
1818
Taxes::VatRateDetermined
1919
)
2020

21+
private
22+
23+
def act
24+
return unless state.can_create_invoice_item?
25+
26+
unit_prices = MoneySplitter.new(state.discounted_amount, state.quantity).call
27+
unit_prices.tally.each do |unit_price, quantity|
28+
command_bus.call(
29+
Invoicing::AddInvoiceItem.new(
30+
invoice_id: state.order_id,
31+
product_id: state.product_id,
32+
vat_rate: state.vat_rate,
33+
quantity: quantity,
34+
unit_price: unit_price
35+
)
36+
)
37+
end
38+
end
39+
2140
def apply(event)
2241
case event
2342
when Pricing::PriceItemValueCalculated
@@ -34,23 +53,6 @@ def apply(event)
3453
end
3554
end
3655

37-
def act
38-
return unless state.can_create_invoice_item?
39-
40-
unit_prices = MoneySplitter.new(state.discounted_amount, state.quantity).call
41-
unit_prices.tally.each do |unit_price, quantity|
42-
command_bus.call(Invoicing::AddInvoiceItem.new(
43-
invoice_id: state.order_id,
44-
product_id: state.product_id,
45-
vat_rate: state.vat_rate,
46-
quantity: quantity,
47-
unit_price: unit_price
48-
))
49-
end
50-
end
51-
52-
private
53-
5456
def fetch_id(event)
5557
"#{event.data.fetch(:order_id)}$#{event.data.fetch(:product_id)}"
5658
end
@@ -70,12 +72,14 @@ def call
7072
distributed_amounts << 0
7173
next
7274
end
75+
7376
p = weight / total_weight
7477
distributed_amount = (p * @amount).round(2)
7578
distributed_amounts << distributed_amount
7679
total_weight -= weight
7780
@amount -= distributed_amount
7881
end
82+
7983
distributed_amounts
8084
end
8185
end

ecommerce/processes/lib/processes/release_payment_process.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class ReleasePaymentProcess
33

44
ProcessState = Data.define(:order, :payment, :order_id) do
55
def initialize(order: :draft, payment: :none, order_id: nil)
6-
super(order:, payment:, order_id:)
6+
super
77
end
88

99
def release?
@@ -21,6 +21,12 @@ def release?
2121
Fulfillment::OrderConfirmed
2222
)
2323

24+
private
25+
26+
def act
27+
release_payment if state.release?
28+
end
29+
2430
def apply(event)
2531
case event
2632
when Payments::PaymentAuthorized
@@ -39,12 +45,6 @@ def apply(event)
3945
end
4046
end
4147

42-
def act
43-
release_payment if state.release?
44-
end
45-
46-
private
47-
4848
def release_payment
4949
command_bus.call(Payments::ReleasePayment.new(order_id: state.order_id))
5050
end

ecommerce/processes/lib/processes/reservation_process.rb

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
module Processes
22
class ReservationProcess
3-
class SomeInventoryNotAvailable < StandardError
4-
attr_reader :unavailable_products
5-
6-
def initialize(unavailable_products)
7-
@unavailable_products = unavailable_products
8-
end
9-
end
10-
113
ProcessState = Data.define(:order, :order_lines) do
124
def initialize(order: nil, order_lines: [])
135
super(order:, order_lines: order_lines.freeze)
@@ -24,19 +16,7 @@ def reserved_product_ids = order_lines.keys
2416
Fulfillment::OrderConfirmed
2517
)
2618

27-
def apply(event)
28-
case event
29-
when Pricing::OfferAccepted
30-
state.with(
31-
order: :accepted,
32-
order_lines: event.data.fetch(:order_lines).map { |ol| [ol.fetch(:product_id), ol.fetch(:quantity)] }.to_h
33-
)
34-
when Fulfillment::OrderCancelled
35-
state.with(order: :cancelled)
36-
when Fulfillment::OrderConfirmed
37-
state.with(order: :confirmed)
38-
end
39-
end
19+
private
4020

4121
def act
4222
case state
@@ -56,7 +36,19 @@ def act
5636
end
5737
end
5838

59-
private
39+
def apply(event)
40+
case event
41+
when Pricing::OfferAccepted
42+
state.with(
43+
order: :accepted,
44+
order_lines: event.data.fetch(:order_lines).map { |ol| [ol.fetch(:product_id), ol.fetch(:quantity)] }.to_h
45+
)
46+
when Fulfillment::OrderCancelled
47+
state.with(order: :cancelled)
48+
when Fulfillment::OrderConfirmed
49+
state.with(order: :confirmed)
50+
end
51+
end
6052

6153
def reserve_stock
6254
unavailable_products = []
@@ -99,5 +91,13 @@ def reject_order(unavailable_product_ids)
9991
def fetch_id(event)
10092
event.data.fetch(:order_id)
10193
end
94+
95+
class SomeInventoryNotAvailable < StandardError
96+
attr_reader :unavailable_products
97+
98+
def initialize(unavailable_products)
99+
@unavailable_products = unavailable_products
100+
end
101+
end
102102
end
103103
end

ecommerce/processes/lib/processes/shipment_process.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,6 @@ def initialize(order: nil, shipment: nil) = super
1414

1515
private
1616

17-
def apply(event)
18-
case event
19-
when Shipping::ShippingAddressAddedToShipment
20-
state.with(shipment: :address_set)
21-
when Fulfillment::OrderRegistered
22-
state.with(order: :placed)
23-
when Fulfillment::OrderConfirmed
24-
state.with(order: :confirmed)
25-
end
26-
end
27-
2817
def act
2918
case state
3019
in { shipment: :address_set, order: :placed }
@@ -36,6 +25,17 @@ def act
3625
end
3726
end
3827

28+
def apply(event)
29+
case event
30+
when Shipping::ShippingAddressAddedToShipment
31+
state.with(shipment: :address_set)
32+
when Fulfillment::OrderRegistered
33+
state.with(order: :placed)
34+
when Fulfillment::OrderConfirmed
35+
state.with(order: :confirmed)
36+
end
37+
end
38+
3939
def submit_shipment
4040
command_bus.call(Shipping::SubmitShipment.new(order_id: id))
4141
end

ecommerce/processes/lib/processes/three_plus_one_free.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ def eligible_free_product
2626

2727
private
2828

29+
def act
30+
case [state.free_product, state.eligible_free_product]
31+
in [the_same_product, ^the_same_product]
32+
in [nil, new_free_product]
33+
make_new_product_for_free(new_free_product)
34+
in [old_free_product, *]
35+
remove_old_free_product(old_free_product)
36+
else
37+
end
38+
end
39+
2940
def apply(event)
3041
product_id = event.data.fetch(:product_id)
3142
case event
@@ -44,17 +55,6 @@ def apply(event)
4455
end
4556
end
4657

47-
def act
48-
case [state.free_product, state.eligible_free_product]
49-
in [the_same_product, ^the_same_product]
50-
in [nil, new_free_product]
51-
make_new_product_for_free(new_free_product)
52-
in [old_free_product, *]
53-
remove_old_free_product(old_free_product)
54-
else
55-
end
56-
end
57-
5858
def remove_old_free_product(product_id)
5959
command_bus.call(Pricing::RemoveFreeProductFromOrder.new(order_id: id, product_id:))
6060
end

0 commit comments

Comments
 (0)