Skip to content

Commit

Permalink
slim Ordering::Order aggregate
Browse files Browse the repository at this point in the history
let's try another approach to reduce Ordering::Order complexity

Now, Ordering::OrderPlaced is a pivotal event that changes the rules.
After an order is placed it never backs to the previous state.
The new logic (Fulfillment rules) applies.
  • Loading branch information
pjurewicz committed May 6, 2024
1 parent 851983b commit f5f7ecd
Show file tree
Hide file tree
Showing 31 changed files with 48 additions and 309 deletions.
2 changes: 2 additions & 0 deletions ecommerce/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require_relative "shipping/lib/shipping"
require_relative "invoicing/lib/invoicing"
require_relative "taxes/lib/taxes"
require_relative "fulfillment/lib/fulfillment"
require_relative "processes/lib/processes"

module Ecommerce
Expand Down Expand Up @@ -38,6 +39,7 @@ def configure_bounded_contexts(event_store, command_bus)
Pricing::Configuration.new,
Taxes::Configuration.new(@available_vat_rates),
ProductCatalog::Configuration.new,
Fulfillment::Configuration.new
].each { |c| c.call(event_store, command_bus) }
end

Expand Down
6 changes: 0 additions & 6 deletions ecommerce/ordering/lib/ordering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
require_relative "ordering/events/item_removed_from_basket"
require_relative "ordering/events/order_placed"
require_relative "ordering/events/order_expired"
require_relative "ordering/events/order_confirmed"
require_relative "ordering/events/order_cancelled"
require_relative "ordering/events/order_submitted"
require_relative "ordering/events/order_rejected"
require_relative "ordering/commands/add_item_to_basket"
require_relative "ordering/commands/remove_item_from_basket"
require_relative "ordering/commands/submit_order"
require_relative "ordering/commands/set_order_as_expired"
require_relative "ordering/commands/confirm_order"
require_relative "ordering/commands/cancel_order"
require_relative "ordering/commands/accept_order"
require_relative "ordering/commands/reject_order"
require_relative "ordering/fake_number_generator"
Expand All @@ -34,8 +30,6 @@ def call(event_store, command_bus)
command_bus.register(AddItemToBasket, OnAddItemToBasket.new(event_store))
command_bus.register(RemoveItemFromBasket, OnRemoveItemFromBasket.new(event_store))
command_bus.register(SetOrderAsExpired, OnSetOrderAsExpired.new(event_store))
command_bus.register(ConfirmOrder, OnConfirmOrder.new(event_store))
command_bus.register(CancelOrder, OnCancelOrder.new(event_store))
command_bus.register(AcceptOrder, OnAcceptOrder.new(event_store))
command_bus.register(RejectOrder, OnRejectOrder.new(event_store))
end
Expand Down
7 changes: 0 additions & 7 deletions ecommerce/ordering/lib/ordering/commands/cancel_order.rb

This file was deleted.

7 changes: 0 additions & 7 deletions ecommerce/ordering/lib/ordering/commands/confirm_order.rb

This file was deleted.

5 changes: 0 additions & 5 deletions ecommerce/ordering/lib/ordering/events/order_cancelled.rb

This file was deleted.

5 changes: 0 additions & 5 deletions ecommerce/ordering/lib/ordering/events/order_confirmed.rb

This file was deleted.

20 changes: 0 additions & 20 deletions ecommerce/ordering/lib/ordering/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ class Order

InvalidState = Class.new(StandardError)
AlreadySubmitted = Class.new(InvalidState)
AlreadyConfirmed = Class.new(InvalidState)
NotPlaced = Class.new(InvalidState)
OrderHasExpired = Class.new(InvalidState)

Expand Down Expand Up @@ -46,11 +45,6 @@ def reject
)
end

def confirm
raise NotPlaced unless @state.equal?(:placed)
apply OrderConfirmed.new(data: { order_id: @id })
end

def expire
raise AlreadySubmitted unless @state.equal?(:draft)
apply OrderExpired.new(data: { order_id: @id })
Expand All @@ -71,28 +65,14 @@ def remove_item(product_id)
apply ItemRemovedFromBasket.new(data: { order_id: @id, product_id: product_id })
end

def cancel
raise AlreadyConfirmed if @state.equal?(:confirmed)
raise NotPlaced unless @state.equal?(:placed)
apply OrderCancelled.new(data: { order_id: @id })
end

on OrderPlaced do |event|
@state = :placed
end

on OrderConfirmed do |event|
@state = :confirmed
end

on OrderExpired do |event|
@state = :expired
end

on OrderCancelled do |event|
@state = :cancelled
end

on ItemAddedToBasket do |event|
@basket.increase_quantity(event.data[:product_id])
end
Expand Down
24 changes: 0 additions & 24 deletions ecommerce/ordering/lib/ordering/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,6 @@ def call(command)
end
end

class OnCancelOrder
def initialize(event_store)
@repository = Infra::AggregateRootRepository.new(event_store)
end

def call(command)
@repository.with_aggregate(Order, command.aggregate_id) do |order|
order.cancel
end
end
end

class OnConfirmOrder
def initialize(event_store)
@repository = Infra::AggregateRootRepository.new(event_store)
end

def call(command)
@repository.with_aggregate(Order, command.aggregate_id) do |order|
order.confirm
end
end
end

class OnSetOrderAsExpired
def initialize(event_store)
@repository = Infra::AggregateRootRepository.new(event_store)
Expand Down
72 changes: 0 additions & 72 deletions ecommerce/ordering/test/cancel_order_test.rb

This file was deleted.

48 changes: 0 additions & 48 deletions ecommerce/ordering/test/confirm_order_test.rb

This file was deleted.

36 changes: 0 additions & 36 deletions ecommerce/ordering/test/order_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,51 +40,27 @@ def test_disallowed_order_state_transitions
draft_order.submit(NumberGenerator.new.call)
assert_raises(Order::InvalidState) { draft_order.accept }
assert_raises(Order::InvalidState) { draft_order.reject }
assert_raises(Order::InvalidState) { draft_order.cancel }
assert_raises(Order::InvalidState) { draft_order.confirm }
draft_order.expire

assert_raises(Order::InvalidState) { submitted_order.submit(NumberGenerator.new.call) }
submitted_order.accept
submitted_order.reject
assert_raises(Order::InvalidState) { submitted_order.cancel }
assert_raises(Order::InvalidState) { submitted_order.confirm }
assert_raises(Order::InvalidState) { submitted_order.expire }

placed_order.submit(NumberGenerator.new.call)
assert_raises(Order::InvalidState) { placed_order.accept }
assert_raises(Order::InvalidState) { placed_order.reject }
placed_order.cancel
placed_order.confirm
assert_raises(Order::InvalidState) { placed_order.expire }

confirmed_order.submit(NumberGenerator.new.call)
assert_raises(Order::InvalidState) { confirmed_order.accept }
assert_raises(Order::InvalidState) { confirmed_order.reject }
assert_raises(Order::InvalidState) { confirmed_order.cancel }
assert_raises(Order::InvalidState) { confirmed_order.confirm }
assert_raises(Order::InvalidState) { confirmed_order.expire }

assert_raises(Order::InvalidState) { expired_order.submit(NumberGenerator.new.call) }
assert_raises(Order::InvalidState) { rejected_order.accept }
assert_raises(Order::InvalidState) { rejected_order.reject }
assert_raises(Order::InvalidState) { rejected_order.cancel }
assert_raises(Order::InvalidState) { rejected_order.confirm }
rejected_order.expire

assert_raises(Order::InvalidState) { expired_order.submit(NumberGenerator.new.call) }
assert_raises(Order::InvalidState) { expired_order.accept }
assert_raises(Order::InvalidState) { expired_order.reject }
assert_raises(Order::InvalidState) { expired_order.cancel }
assert_raises(Order::InvalidState) { expired_order.confirm }
assert_raises(Order::InvalidState) { expired_order.expire }

cancelled_order.submit(NumberGenerator.new.call)
assert_raises(Order::InvalidState) { cancelled_order.accept }
assert_raises(Order::InvalidState) { cancelled_order.reject }
assert_raises(Order::InvalidState) { cancelled_order.confirm }
assert_raises(Order::InvalidState) { cancelled_order.cancel }
cancelled_order.expire
end

def draft_order
Expand All @@ -109,22 +85,10 @@ def rejected_order
end
end

def confirmed_order
placed_order.tap do |order|
order.confirm
end
end

def expired_order
draft_order.tap do |order|
order.expire
end
end

def cancelled_order
submitted_order.tap do |order|
order.reject
end
end
end
end
Loading

0 comments on commit f5f7ecd

Please sign in to comment.