Skip to content

Commit 1287773

Browse files
OfferDrafted is the new event that starts the lifecycle of an offer
So far we didn't have such event, adding items was the first event. Explicit event feels right to have. It will allow easier multitenancy, as we now can register such offer within StoresBC.
1 parent a75af75 commit 1287773

File tree

6 files changed

+56
-0
lines changed

6 files changed

+56
-0
lines changed

ecommerce/pricing/lib/pricing.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ module Pricing
1515
class Configuration
1616
def call(event_store, command_bus)
1717

18+
command_bus.register(
19+
DraftOffer,
20+
DraftOfferHandler.new(event_store)
21+
)
1822
command_bus.register(
1923
AddPriceItem,
2024
OnAddItemToBasket.new(event_store)

ecommerce/pricing/lib/pricing/commands.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
module Pricing
2+
class DraftOffer < Infra::Command
3+
attribute :order_id, Infra::Types::UUID
4+
5+
alias aggregate_id order_id
6+
end
7+
28
class AddPriceItem < Infra::Command
39
attribute :order_id, Infra::Types::UUID
410
attribute :product_id, Infra::Types::UUID

ecommerce/pricing/lib/pricing/events.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
module Pricing
2+
class OfferDrafted < Infra::Event
3+
attribute :order_id, Infra::Types::UUID
4+
end
5+
26
class CouponRegistered < Infra::Event
37
attribute :coupon_id, Infra::Types::UUID
48
attribute :name, Infra::Types::String

ecommerce/pricing/lib/pricing/offer.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ def initialize(id)
1212
@state = :draft
1313
end
1414

15+
def draft
16+
apply OfferDrafted.new(data: { order_id: @id })
17+
end
18+
1519
def add_item(product_id, base_price)
1620
price = @discounts.inject(Discounts::NoPercentageDiscount.new, :add).apply(base_price)
1721
apply PriceItemAdded.new(
@@ -125,6 +129,9 @@ def expire
125129

126130
private
127131

132+
on OfferDrafted do |event|
133+
end
134+
128135
on PriceItemAdded do |event|
129136
@list.add_item(event.data.fetch(:product_id), event.data.fetch(:base_price), event.data.fetch(:price))
130137
end

ecommerce/pricing/lib/pricing/services.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ class FreeProductAlreadyMade < StandardError
1414
class FreeProductNotExists < StandardError
1515
end
1616

17+
class DraftOfferHandler
18+
def initialize(event_store)
19+
@repository = Infra::AggregateRootRepository.new(event_store)
20+
end
21+
22+
def call(cmd)
23+
@repository.with_aggregate(Offer, cmd.aggregate_id) do |offer|
24+
offer.draft
25+
end
26+
end
27+
end
28+
1729
class SetPercentageDiscountHandler
1830
def initialize(event_store)
1931
@repository = Infra::AggregateRootRepository.new(event_store)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require_relative "test_helper"
2+
3+
module Pricing
4+
class DraftOfferTest < Test
5+
cover "Pricing*"
6+
7+
def test_draft_offer
8+
order_id = SecureRandom.uuid
9+
stream = "Pricing::Offer$#{order_id}"
10+
11+
assert_events(
12+
stream,
13+
OfferDrafted.new(data: { order_id: order_id })
14+
) { draft_offer(order_id) }
15+
end
16+
17+
private
18+
19+
def draft_offer(order_id)
20+
run_command(DraftOffer.new(order_id: order_id))
21+
end
22+
end
23+
end

0 commit comments

Comments
 (0)