Skip to content

Commit 8b5f35c

Browse files
committed
Revert "Three plus one as promotion"
This reverts commit e9f8863.
1 parent a2e8b65 commit 8b5f35c

File tree

3 files changed

+28
-131
lines changed

3 files changed

+28
-131
lines changed

ecommerce/pricing/lib/pricing/events.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ class PriceItemRemoved < Infra::Event
5656
attribute :total_value, Infra::Types::Price
5757
end
5858

59+
class DiscountApplied < Infra::Event
60+
attribute :order_id, Infra::Types::UUID
61+
attribute :base_total_value, Infra::Types::Price
62+
attribute :total_value, Infra::Types::Price
63+
end
64+
5965
class PercentageDiscountRemoved < Infra::Event
6066
attribute :order_id, Infra::Types::UUID
6167
attribute :type, Infra::Types::String

ecommerce/pricing/lib/pricing/offer.rb

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,12 @@ def initialize(id)
1010
@list = List.new
1111
@discounts = []
1212
@state = :draft
13-
end
1413

15-
def add_item(product_id, base_price, promotion = Pricing::Discounts::ThreePlusOneGratis.new)
16-
if promotion
17-
price = promotion.apply(@list.quantities, product_id, base_price)
18-
else
19-
price = @discounts.inject(Discounts::NoPercentageDiscount.new, :add).apply(base_price)
20-
end
14+
@three_plus_one_gratis = Discounts::ThreePlusOneGratis.new
15+
end
2116

17+
def add_item(product_id, base_price)
18+
price = @discounts.inject(Discounts::NoPercentageDiscount.new, :add).apply(base_price)
2219
apply PriceItemAdded.new(
2320
data: {
2421
order_id: @id,
@@ -29,6 +26,15 @@ def add_item(product_id, base_price, promotion = Pricing::Discounts::ThreePlusOn
2926
total_value: @list.actual_sum + price
3027
}
3128
)
29+
30+
anything_applied = @three_plus_one_gratis.apply(@list)
31+
apply DiscountApplied.new(
32+
data: {
33+
order_id: @id,
34+
base_total_value: @list.base_sum,
35+
total_value: @list.actual_sum
36+
}
37+
) if anything_applied
3238
end
3339

3440
def remove_item(product_id)
@@ -167,6 +173,10 @@ def expire
167173

168174
private
169175

176+
on DiscountApplied do |event|
177+
# TODO: Something about discount?
178+
end
179+
170180
on PriceItemAdded do |event|
171181
@list.add_item(event.data.fetch(:product_id), event.data.fetch(:base_price), event.data.fetch(:price))
172182
end

ecommerce/pricing/test/three_plus_one_test.rb

Lines changed: 5 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -99,72 +99,6 @@ def test_given_three_items_are_added_when_forth_item_is_added_then_the_last__ite
9999
)
100100
) { add_item(order_id, product_id) }
101101

102-
assert_events(
103-
stream,
104-
PriceItemAdded.new(
105-
data: {
106-
order_id: order_id,
107-
product_id: product_id,
108-
base_price: 20,
109-
price: 0,
110-
base_total_value: 80,
111-
total_value: 60,
112-
}
113-
),
114-
OrderTotalValueCalculated.new(
115-
data: {
116-
order_id: order_id,
117-
discounted_amount: 60,
118-
total_amount: 80
119-
}
120-
),
121-
PriceItemValueCalculated.new(
122-
data: {
123-
order_id: order_id,
124-
product_id: product_id,
125-
quantity: 4,
126-
amount: 80,
127-
discounted_amount: 60,
128-
}
129-
)
130-
) { add_item(order_id, product_id) }
131-
end
132-
133-
def test_given_3_plus_one__when_10_percent_discount_for_offer__then_offer_price_includes_both_discounts
134-
product_id = SecureRandom.uuid
135-
set_price(product_id, 20)
136-
order_id = SecureRandom.uuid
137-
stream = "Pricing::Offer$#{order_id}"
138-
assert_events(
139-
stream,
140-
PriceItemAdded.new(
141-
data: {
142-
order_id: order_id,
143-
product_id: product_id,
144-
base_price: 20,
145-
price: 20,
146-
base_total_value: 20,
147-
total_value: 20,
148-
}
149-
),
150-
OrderTotalValueCalculated.new(
151-
data: {
152-
order_id: order_id,
153-
discounted_amount: 20,
154-
total_amount: 20
155-
}
156-
),
157-
PriceItemValueCalculated.new(
158-
data: {
159-
order_id: order_id,
160-
product_id: product_id,
161-
quantity: 1,
162-
amount: 20,
163-
discounted_amount: 20,
164-
}
165-
)
166-
) { add_item(order_id, product_id) }
167-
168102
assert_events(
169103
stream,
170104
PriceItemAdded.new(
@@ -173,74 +107,21 @@ def test_given_3_plus_one__when_10_percent_discount_for_offer__then_offer_price_
173107
product_id: product_id,
174108
base_price: 20,
175109
price: 20,
176-
base_total_value: 40,
177-
total_value: 40,
178-
}
179-
),
180-
OrderTotalValueCalculated.new(
181-
data: {
182-
order_id: order_id,
183-
discounted_amount: 40,
184-
total_amount: 40
185-
}
186-
),
187-
PriceItemValueCalculated.new(
188-
data: {
189-
order_id: order_id,
190-
product_id: product_id,
191-
quantity: 2,
192-
amount: 40,
193-
discounted_amount: 40,
194-
}
195-
)
196-
) { add_item(order_id, product_id) }
197-
198-
assert_events(
199-
stream,
200-
PriceItemAdded.new(
201-
data: {
202-
order_id: order_id,
203-
product_id: product_id,
204-
base_price: 20,
205-
price: 20,
206-
base_total_value: 60,
207-
total_value: 60,
208-
}
209-
),
210-
OrderTotalValueCalculated.new(
211-
data: {
212-
order_id: order_id,
213-
discounted_amount: 60,
214-
total_amount: 60
110+
base_total_value: 80,
111+
total_value: 80,
215112
}
216113
),
217-
PriceItemValueCalculated.new(
218-
data: {
219-
order_id: order_id,
220-
product_id: product_id,
221-
quantity: 3,
222-
amount: 60,
223-
discounted_amount: 60,
224-
}
225-
)
226-
) { add_item(order_id, product_id) }
227-
228-
assert_events(
229-
stream,
230-
PriceItemAdded.new(
114+
DiscountApplied.new(
231115
data: {
232116
order_id: order_id,
233-
product_id: product_id,
234-
base_price: 20,
235-
price: 0,
236117
base_total_value: 80,
237118
total_value: 60,
238119
}
239120
),
240121
OrderTotalValueCalculated.new(
241122
data: {
242123
order_id: order_id,
243-
discounted_amount: 60,
124+
discounted_amount: 80,
244125
total_amount: 80
245126
}
246127
),
@@ -250,7 +131,7 @@ def test_given_3_plus_one__when_10_percent_discount_for_offer__then_offer_price_
250131
product_id: product_id,
251132
quantity: 4,
252133
amount: 80,
253-
discounted_amount: 60,
134+
discounted_amount: 80,
254135
}
255136
)
256137
) { add_item(order_id, product_id) }

0 commit comments

Comments
 (0)