Skip to content

Commit b0e0f2a

Browse files
committed
Introduce product and order archivization.
- producs and orders can be archived wich make them disappear from the UI.
1 parent 7ff88f6 commit b0e0f2a

File tree

23 files changed

+197
-4
lines changed

23 files changed

+197
-4
lines changed

ecommerce/fulfillment/lib/fulfillment.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
require_relative "fulfillment/events/order_registered"
33
require_relative "fulfillment/events/order_confirmed"
44
require_relative "fulfillment/events/order_cancelled"
5+
require_relative "fulfillment/events/order_archived"
56
require_relative "fulfillment/commands/register_order"
67
require_relative "fulfillment/commands/confirm_order"
78
require_relative "fulfillment/commands/cancel_order"
9+
require_relative "fulfillment/commands/archive_order"
810
require_relative "fulfillment/on_register_order"
911
require_relative "fulfillment/on_cancel_order"
1012
require_relative "fulfillment/on_confirm_order"
13+
require_relative "fulfillment/on_archive_order"
1114
require_relative "fulfillment/order"
1215
require_relative "fulfillment/number_generator"
1316
require_relative "fulfillment/fake_number_generator"
@@ -22,6 +25,7 @@ def call(event_store, command_bus)
2225
command_bus.register(RegisterOrder, OnRegisterOrder.new(event_store, @number_generator.call))
2326
command_bus.register(ConfirmOrder, OnConfirmOrder.new(event_store))
2427
command_bus.register(CancelOrder, OnCancelOrder.new(event_store))
28+
command_bus.register(ArchiveOrder, OnArchiveOrder.new(event_store))
2529
end
2630
end
2731
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
module Fulfillment
4+
class ArchiveOrder < Infra::Command
5+
attribute :order_id, Infra::Types::UUID
6+
7+
alias aggregate_id order_id
8+
end
9+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
module Fulfillment
4+
class OrderArchived < Infra::Event
5+
attribute :order_id, Infra::Types::UUID
6+
end
7+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
module Fulfillment
4+
class OnArchiveOrder
5+
def initialize(event_store)
6+
@repository = Infra::AggregateRootRepository.new(event_store)
7+
end
8+
9+
def call(command)
10+
@repository.with_aggregate(Order, command.aggregate_id) do |order|
11+
order.archive
12+
end
13+
end
14+
end
15+
end

ecommerce/fulfillment/lib/fulfillment/order.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ def cancel
2525
apply OrderCancelled.new(data: { order_id: @id })
2626
end
2727

28+
def archive
29+
apply OrderArchived.new(data: { order_id: @id })
30+
end
31+
2832
on OrderRegistered do |event|
2933
@state = :new
3034
end
@@ -36,5 +40,9 @@ def cancel
3640
on OrderCancelled do |event|
3741
@state = :cancelled
3842
end
43+
44+
on OrderArchived do |event|
45+
@state = :archived
46+
end
3947
end
4048
end
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require_relative "test_helper"
2+
3+
module Fulfillment
4+
class ArchiveOrderTest < Test
5+
cover "Fulfillment::OnArchiveOrder*"
6+
7+
def test_order_can_be_archived
8+
aggregate_id = SecureRandom.uuid
9+
stream = "Fulfillment::Order$#{aggregate_id}"
10+
11+
arrange(
12+
RegisterOrder.new(order_id: aggregate_id)
13+
)
14+
15+
assert_events(
16+
stream,
17+
OrderArchived.new(data: { order_id: aggregate_id })
18+
) { act(ArchiveOrder.new(order_id: aggregate_id)) }
19+
end
20+
21+
def test_not_registered_order_can_be_archived
22+
aggregate_id = SecureRandom.uuid
23+
stream = "Fulfillment::Order$#{aggregate_id}"
24+
25+
assert_events(
26+
stream,
27+
OrderArchived.new(data: { order_id: aggregate_id })
28+
) { act(ArchiveOrder.new(order_id: aggregate_id)) }
29+
end
30+
31+
def test_confirmed_order_can_be_archived
32+
aggregate_id = SecureRandom.uuid
33+
stream = "Fulfillment::Order$#{aggregate_id}"
34+
35+
arrange(
36+
RegisterOrder.new(order_id: aggregate_id),
37+
ConfirmOrder.new(order_id: aggregate_id)
38+
)
39+
40+
assert_events(
41+
stream,
42+
OrderArchived.new(data: { order_id: aggregate_id })
43+
) { act(ArchiveOrder.new(order_id: aggregate_id)) }
44+
end
45+
end
46+
end

ecommerce/product_catalog/lib/product_catalog.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
require_relative "product_catalog/events"
44
require_relative "product_catalog/registration"
55
require_relative "product_catalog/naming"
6+
require_relative "product_catalog/archivization"
67

78
module ProductCatalog
89

910
class Configuration
1011
def call(event_store, command_bus)
1112
command_bus.register(RegisterProduct, Registration.new(event_store))
1213
command_bus.register(NameProduct, Naming.new(event_store))
14+
command_bus.register(ArchiveProduct, Archivization.new(event_store))
1315
end
1416
end
1517
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module ProductCatalog
2+
class Archivization
3+
def initialize(event_store)
4+
@event_store = event_store
5+
end
6+
7+
def call(command)
8+
@event_store.publish(
9+
ProductArchived.new(
10+
data: { product_id: command.product_id }
11+
),
12+
stream_name: "ProductCatalog$#{command.product_id}"
13+
)
14+
end
15+
end
16+
end

ecommerce/product_catalog/lib/product_catalog/commands.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ class NameProduct < Infra::Command
77
attribute :product_id, Infra::Types::UUID
88
attribute :name, Infra::Types::String
99
end
10+
11+
class ArchiveProduct < Infra::Command
12+
attribute :product_id, Infra::Types::UUID
13+
end
1014
end

ecommerce/product_catalog/lib/product_catalog/events.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@ class ProductNamed < Infra::Event
88
attribute :product_id, Infra::Types::String
99
end
1010

11+
class ProductArchived < Infra::Event
12+
attribute :product_id, Infra::Types::UUID
13+
end
14+
1115
end

0 commit comments

Comments
 (0)