From 9075c73a1f810a3a6857f089f34951f2cda44fb7 Mon Sep 17 00:00:00 2001 From: marlena-b Date: Mon, 25 Nov 2024 17:24:25 +0100 Subject: [PATCH] Refactor discounts --- ecommerce/pricing/lib/pricing.rb | 14 +++---- .../lib/pricing/apply_time_promotion.rb | 4 +- ecommerce/pricing/lib/pricing/commands.rb | 4 +- ecommerce/pricing/lib/pricing/discounts.rb | 5 ++- ecommerce/pricing/lib/pricing/events.rb | 2 +- ecommerce/pricing/lib/pricing/offer.rb | 37 +++++++++++-------- ecommerce/pricing/lib/pricing/services.rb | 16 ++++---- .../pricing/test/apply_time_promotion_test.rb | 2 +- ecommerce/pricing/test/pricing_test.rb | 28 +++++++------- ecommerce/pricing/test/test_helper.rb | 4 +- .../app/controllers/orders_controller.rb | 4 +- .../client_orders/configuration.rb | 2 +- .../client_orders/order_handlers.rb | 2 +- .../app/read_models/orders/configuration.rb | 4 +- .../{reset_discount.rb => remove_discount.rb} | 2 +- ...e.rb => remove_time_promotion_discount.rb} | 2 +- .../app/views/orders/edit.html.erb | 2 +- rails_application/config/routes.rb | 2 +- .../test/client_orders/discount_test.rb | 9 ++--- .../test/integration/discount_test.rb | 10 ++--- .../test/orders/discount_test.rb | 12 +++--- ...=> remove_time_promotion_discount_test.rb} | 8 ++-- 22 files changed, 90 insertions(+), 85 deletions(-) rename rails_application/app/read_models/orders/{reset_discount.rb => remove_discount.rb} (94%) rename rails_application/app/read_models/orders/{reset_time_promotion_discount_value.rb => remove_time_promotion_discount.rb} (87%) rename rails_application/test/orders/{reset_time_promotion_discount_value_test.rb => remove_time_promotion_discount_test.rb} (90%) diff --git a/ecommerce/pricing/lib/pricing.rb b/ecommerce/pricing/lib/pricing.rb index adb28218..9f47418e 100644 --- a/ecommerce/pricing/lib/pricing.rb +++ b/ecommerce/pricing/lib/pricing.rb @@ -64,8 +64,8 @@ def call(event_store, command_bus) SetPercentageDiscountHandler.new(event_store) ) command_bus.register( - ResetPercentageDiscount, - ResetPercentageDiscountHandler.new(event_store) + RemovePercentageDiscount, + RemovePercentageDiscountHandler.new(event_store) ) command_bus.register( ChangePercentageDiscount, @@ -96,14 +96,14 @@ def call(event_store, command_bus) SetTimePromotionDiscountHandler.new(event_store) ) command_bus.register( - ResetTimePromotionDiscount, - ResetTimePromotionDiscountHandler.new(event_store) + RemoveTimePromotionDiscount, + RemoveTimePromotionDiscountHandler.new(event_store) ) event_store.subscribe(ApplyTimePromotion, to: [ PriceItemAdded, PriceItemRemoved, PercentageDiscountSet, - PercentageDiscountReset, + PercentageDiscountRemoved, PercentageDiscountChanged, ProductMadeFreeForOrder, FreeProductRemovedFromOrder @@ -112,7 +112,7 @@ def call(event_store, command_bus) PriceItemAdded, PriceItemRemoved, PercentageDiscountSet, - PercentageDiscountReset, + PercentageDiscountRemoved, PercentageDiscountChanged, ProductMadeFreeForOrder, FreeProductRemovedFromOrder @@ -121,7 +121,7 @@ def call(event_store, command_bus) PriceItemAdded, PriceItemRemoved, PercentageDiscountSet, - PercentageDiscountReset, + PercentageDiscountRemoved, PercentageDiscountChanged, ProductMadeFreeForOrder, FreeProductRemovedFromOrder diff --git a/ecommerce/pricing/lib/pricing/apply_time_promotion.rb b/ecommerce/pricing/lib/pricing/apply_time_promotion.rb index 3281919c..01491515 100644 --- a/ecommerce/pricing/lib/pricing/apply_time_promotion.rb +++ b/ecommerce/pricing/lib/pricing/apply_time_promotion.rb @@ -6,10 +6,10 @@ def call(event) if discount.exists? command_bus.(SetTimePromotionDiscount.new(order_id: event.data.fetch(:order_id), amount: discount.value)) else - command_bus.(ResetTimePromotionDiscount.new(order_id: event.data.fetch(:order_id))) + command_bus.(RemoveTimePromotionDiscount.new(order_id: event.data.fetch(:order_id))) end - rescue NotPossibleToAssignDiscountTwice, NotPossibleToResetWithoutDiscount + rescue NotPossibleToAssignDiscountTwice, NotPossibleToRemoveWithoutDiscount end private diff --git a/ecommerce/pricing/lib/pricing/commands.rb b/ecommerce/pricing/lib/pricing/commands.rb index 832f9473..37824c8f 100644 --- a/ecommerce/pricing/lib/pricing/commands.rb +++ b/ecommerce/pricing/lib/pricing/commands.rb @@ -40,7 +40,7 @@ class SetPercentageDiscount < Infra::Command alias aggregate_id order_id end - class ResetPercentageDiscount < Infra::Command + class RemovePercentageDiscount < Infra::Command attribute :order_id, Infra::Types::UUID alias aggregate_id order_id end @@ -52,7 +52,7 @@ class SetTimePromotionDiscount < Infra::Command alias aggregate_id order_id end - class ResetTimePromotionDiscount < Infra::Command + class RemoveTimePromotionDiscount < Infra::Command attribute :order_id, Infra::Types::UUID alias aggregate_id order_id diff --git a/ecommerce/pricing/lib/pricing/discounts.rb b/ecommerce/pricing/lib/pricing/discounts.rb index 97e99cfb..e72c4976 100644 --- a/ecommerce/pricing/lib/pricing/discounts.rb +++ b/ecommerce/pricing/lib/pricing/discounts.rb @@ -17,12 +17,13 @@ def self.build(discount) end class PercentageDiscount - attr_reader :value + attr_reader :value, :type - def initialize(value) + def initialize(type = GENERAL_DISCOUNT, value) raise UnacceptableDiscountRange if value <= 0 raise UnacceptableDiscountRange if value > 100 + @type = type @value = value end diff --git a/ecommerce/pricing/lib/pricing/events.rb b/ecommerce/pricing/lib/pricing/events.rb index a985f765..583b1175 100644 --- a/ecommerce/pricing/lib/pricing/events.rb +++ b/ecommerce/pricing/lib/pricing/events.rb @@ -48,7 +48,7 @@ class PriceItemRemoved < Infra::Event attribute :product_id, Infra::Types::UUID end - class PercentageDiscountReset < Infra::Event + class PercentageDiscountRemoved < Infra::Event attribute :order_id, Infra::Types::UUID attribute :type, Infra::Types::String end diff --git a/ecommerce/pricing/lib/pricing/offer.rb b/ecommerce/pricing/lib/pricing/offer.rb index dca2f3da..6e78332e 100644 --- a/ecommerce/pricing/lib/pricing/offer.rb +++ b/ecommerce/pricing/lib/pricing/offer.rb @@ -5,7 +5,7 @@ class Offer def initialize(id) @id = id @list = List.new - @discounts = {} + @discounts = [] end def add_item(product_id) @@ -26,31 +26,31 @@ def remove_item(product_id) ) end - def apply_discount(type, discount) - raise NotPossibleToAssignDiscountTwice if @discounts.include?(type) + def apply_discount(discount) + raise NotPossibleToAssignDiscountTwice if discount_exists?(discount.type) apply PercentageDiscountSet.new( data: { order_id: @id, - type: type, + type: discount.type, amount: discount.value } ) end - def change_discount(type, discount) - raise NotPossibleToChangeDiscount unless @discounts.include?(type) + def change_discount(discount) + raise NotPossibleToChangeDiscount unless discount_exists?(discount.type) apply PercentageDiscountChanged.new( data: { order_id: @id, - type: type, + type: discount.type, amount: discount.value } ) end - def reset_discount(type) - raise NotPossibleToResetWithoutDiscount unless @discounts.include?(type) - apply PercentageDiscountReset.new( + def remove_discount(type) + raise NotPossibleToRemoveWithoutDiscount unless discount_exists?(type) + apply PercentageDiscountRemoved.new( data: { order_id: @id, type: type @@ -80,7 +80,7 @@ def remove_free_product(order_id, product_id) def calculate_total_value(pricing_catalog) total_value = @list.base_sum(pricing_catalog) - discounted_value = @discounts.values.inject(Discounts::NoPercentageDiscount.new, :add).apply(total_value) + discounted_value = @discounts.inject(Discounts::NoPercentageDiscount.new, :add).apply(total_value) apply( OrderTotalValueCalculated.new( @@ -141,15 +141,16 @@ def use_coupon(coupon_id, discount) end on PercentageDiscountSet do |event| - @discounts[event.data.fetch(:type)] = Discounts::PercentageDiscount.new(event.data.fetch(:amount)) + @discounts << Discounts::PercentageDiscount.new(event.data.fetch(:type), event.data.fetch(:amount)) end on PercentageDiscountChanged do |event| - @discounts[event.data.fetch(:type)] = Discounts::PercentageDiscount.new(event.data.fetch(:amount)) + @discounts.delete_if { |discount| discount.type == event.data.fetch(:type) } + @discounts << Discounts::PercentageDiscount.new(event.data.fetch(:type), event.data.fetch(:amount)) end - on PercentageDiscountReset do |event| - @discounts.delete(event.data.fetch(:type)) + on PercentageDiscountRemoved do |event| + @discounts.delete_if { |discount| discount.type == event.data.fetch(:type) } end on ProductMadeFreeForOrder do |event| @@ -167,6 +168,10 @@ def calculate_total_sub_discounts(pricing_catalog) on CouponUsed do |event| end + def discount_exists?(type) + @discounts.find { |discount| discount.type == type } + end + class List def initialize @@ -215,7 +220,7 @@ def sub_amounts_total(pricing_catalog) def sub_discounts(pricing_catalog, discounts) @products_quantities.map do |product, quantity| catalog_price_for_single = pricing_catalog.price_for(product) - with_total_discount_single = discounts.values.inject(Discounts::NoPercentageDiscount.new, :add).apply(catalog_price_for_single) + with_total_discount_single = discounts.inject(Discounts::NoPercentageDiscount.new, :add).apply(catalog_price_for_single) quantity * (catalog_price_for_single - with_total_discount_single) end end diff --git a/ecommerce/pricing/lib/pricing/services.rb b/ecommerce/pricing/lib/pricing/services.rb index 18f744b2..9bbd59cf 100644 --- a/ecommerce/pricing/lib/pricing/services.rb +++ b/ecommerce/pricing/lib/pricing/services.rb @@ -2,7 +2,7 @@ module Pricing class NotPossibleToAssignDiscountTwice < StandardError end - class NotPossibleToResetWithoutDiscount < StandardError + class NotPossibleToRemoveWithoutDiscount < StandardError end class NotPossibleToChangeDiscount < StandardError @@ -21,19 +21,19 @@ def initialize(event_store) def call(cmd) @repository.with_aggregate(Offer, cmd.aggregate_id) do |order| - order.apply_discount(Discounts::GENERAL_DISCOUNT, Discounts::PercentageDiscount.new(cmd.amount)) + order.apply_discount(Discounts::PercentageDiscount.new(cmd.amount)) end end end - class ResetPercentageDiscountHandler + class RemovePercentageDiscountHandler def initialize(event_store) @repository = Infra::AggregateRootRepository.new(event_store) end def call(cmd) @repository.with_aggregate(Offer, cmd.aggregate_id) do |order| - order.reset_discount(Discounts::GENERAL_DISCOUNT) + order.remove_discount(Discounts::GENERAL_DISCOUNT) end end end @@ -45,7 +45,7 @@ def initialize(event_store) def call(cmd) @repository.with_aggregate(Offer, cmd.aggregate_id) do |order| - order.change_discount(Discounts::GENERAL_DISCOUNT, Discounts::PercentageDiscount.new(cmd.amount)) + order.change_discount(Discounts::PercentageDiscount.new(cmd.amount)) end end end @@ -57,19 +57,19 @@ def initialize(event_store) def call(cmd) @repository.with_aggregate(Offer, cmd.aggregate_id) do |order| - order.apply_discount(Discounts::TIME_PROMOTION_DISCOUNT, Discounts::PercentageDiscount.new(cmd.amount)) + order.apply_discount(Discounts::PercentageDiscount.new(Discounts::TIME_PROMOTION_DISCOUNT, cmd.amount)) end end end - class ResetTimePromotionDiscountHandler + class RemoveTimePromotionDiscountHandler def initialize(event_store) @repository = Infra::AggregateRootRepository.new(event_store) end def call(cmd) @repository.with_aggregate(Offer, cmd.aggregate_id) do |order| - order.reset_discount(Discounts::TIME_PROMOTION_DISCOUNT) + order.remove_discount(Discounts::TIME_PROMOTION_DISCOUNT) end end end diff --git a/ecommerce/pricing/test/apply_time_promotion_test.rb b/ecommerce/pricing/test/apply_time_promotion_test.rb index b8dbeaea..adaddd06 100644 --- a/ecommerce/pricing/test/apply_time_promotion_test.rb +++ b/ecommerce/pricing/test/apply_time_promotion_test.rb @@ -80,7 +80,7 @@ def percentage_discount_set_event(order_id, amount) end def percentage_discount_reset_event(order_id) - PercentageDiscountReset.new( + PercentageDiscountRemoved.new( data: { order_id: order_id, type: Pricing::Discounts::TIME_PROMOTION_DISCOUNT diff --git a/ecommerce/pricing/test/pricing_test.rb b/ecommerce/pricing/test/pricing_test.rb index 2067b5a9..174d6c28 100644 --- a/ecommerce/pricing/test/pricing_test.rb +++ b/ecommerce/pricing/test/pricing_test.rb @@ -125,19 +125,19 @@ def test_resets_time_promotion_discount assert_events_contain( stream, - PercentageDiscountReset.new( + PercentageDiscountRemoved.new( data: { order_id: order_id, type: Discounts::TIME_PROMOTION_DISCOUNT } ) - ) { reset_time_promotion_discount(order_id) } + ) { remove_time_promotion_discount(order_id) } end - def test_does_not_reset_time_promotion_discount_if_there_is_none + def test_does_not_remove_time_promotion_discount_if_there_is_none order_id = SecureRandom.uuid - assert_raises(NotPossibleToResetWithoutDiscount) { reset_time_promotion_discount(order_id) } + assert_raises(NotPossibleToRemoveWithoutDiscount) { remove_time_promotion_discount(order_id) } end def test_calculates_total_value_with_discount @@ -200,7 +200,7 @@ def test_calculates_total_value_with_discount end assert_events_contain( stream, - PercentageDiscountReset.new( + PercentageDiscountRemoved.new( data: { order_id: order_id, type: Pricing::Discounts::GENERAL_DISCOUNT @@ -215,7 +215,7 @@ def test_calculates_total_value_with_discount ) ) do run_command( - Pricing::ResetPercentageDiscount.new(order_id: order_id, type: Pricing::Discounts::GENERAL_DISCOUNT) + Pricing::RemovePercentageDiscount.new(order_id: order_id, type: Pricing::Discounts::GENERAL_DISCOUNT) ) end end @@ -305,7 +305,7 @@ def test_changing_discount_not_possible_when_discount_is_reset Pricing::SetPercentageDiscount.new(order_id: order_id, amount: 10) ) run_command( - Pricing::ResetPercentageDiscount.new(order_id: order_id) + Pricing::RemovePercentageDiscount.new(order_id: order_id) ) assert_raises NotPossibleToChangeDiscount do @@ -399,7 +399,7 @@ def test_resetting_discount_possible_when_discount_has_been_set_and_then_changed assert_events_contain( stream, - PercentageDiscountReset.new( + PercentageDiscountRemoved.new( data: { order_id: order_id, type: Discounts::GENERAL_DISCOUNT @@ -414,7 +414,7 @@ def test_resetting_discount_possible_when_discount_has_been_set_and_then_changed ) ) do run_command( - Pricing::ResetPercentageDiscount.new(order_id: order_id, type: Discounts::GENERAL_DISCOUNT) + Pricing::RemovePercentageDiscount.new(order_id: order_id, type: Discounts::GENERAL_DISCOUNT) ) end end @@ -424,20 +424,20 @@ def test_resetting_with_missing_discount_not_possible set_price(product_1_id, 20) order_id = SecureRandom.uuid add_item(order_id, product_1_id) - assert_raises NotPossibleToResetWithoutDiscount do + assert_raises NotPossibleToRemoveWithoutDiscount do run_command( - Pricing::ResetPercentageDiscount.new(order_id: order_id) + Pricing::RemovePercentageDiscount.new(order_id: order_id) ) end run_command( Pricing::SetPercentageDiscount.new(order_id: order_id, amount: 10) ) run_command( - Pricing::ResetPercentageDiscount.new(order_id: order_id) + Pricing::RemovePercentageDiscount.new(order_id: order_id) ) - assert_raises NotPossibleToResetWithoutDiscount do + assert_raises NotPossibleToRemoveWithoutDiscount do run_command( - Pricing::ResetPercentageDiscount.new(order_id: order_id) + Pricing::RemovePercentageDiscount.new(order_id: order_id) ) end end diff --git a/ecommerce/pricing/test/test_helper.rb b/ecommerce/pricing/test/test_helper.rb index 66b1c8b5..21bb9dca 100644 --- a/ecommerce/pricing/test/test_helper.rb +++ b/ecommerce/pricing/test/test_helper.rb @@ -46,8 +46,8 @@ def set_time_promotion_discount(order_id, amount) run_command(SetTimePromotionDiscount.new(order_id: order_id, amount: amount)) end - def reset_time_promotion_discount(order_id) - run_command(ResetTimePromotionDiscount.new(order_id: order_id)) + def remove_time_promotion_discount(order_id) + run_command(RemoveTimePromotionDiscount.new(order_id: order_id)) end def fake_name diff --git a/rails_application/app/controllers/orders_controller.rb b/rails_application/app/controllers/orders_controller.rb index 76bc55bc..6211c2f7 100644 --- a/rails_application/app/controllers/orders_controller.rb +++ b/rails_application/app/controllers/orders_controller.rb @@ -49,9 +49,9 @@ def update_discount redirect_to edit_order_path(@order_id) end - def reset_discount + def remove_discount @order_id = params[:id] - command_bus.(Pricing::ResetPercentageDiscount.new(order_id: @order_id)) + command_bus.(Pricing::RemovePercentageDiscount.new(order_id: @order_id)) redirect_to edit_order_path(@order_id) end diff --git a/rails_application/app/read_models/client_orders/configuration.rb b/rails_application/app/read_models/client_orders/configuration.rb index 974867dd..47df64fb 100644 --- a/rails_application/app/read_models/client_orders/configuration.rb +++ b/rails_application/app/read_models/client_orders/configuration.rb @@ -56,7 +56,7 @@ def call(event_store) event_store.subscribe(ProductHandlers::RegisterProduct, to: [ProductCatalog::ProductRegistered]) event_store.subscribe(ProductHandlers::UpdateProductAvailability, to: [Inventory::AvailabilityChanged]) event_store.subscribe(OrderHandlers::UpdateDiscount, to: [Pricing::PercentageDiscountSet, Pricing::PercentageDiscountChanged]) - event_store.subscribe(OrderHandlers::ResetDiscount, to: [Pricing::PercentageDiscountReset]) + event_store.subscribe(OrderHandlers::RemoveDiscount, to: [Pricing::PercentageDiscountRemoved]) event_store.subscribe(OrderHandlers::UpdateOrderTotalValue, to: [Pricing::OrderTotalValueCalculated]) event_store.subscribe(OrderHandlers::UpdatePaidOrdersSummary, to: [Fulfillment::OrderConfirmed]) end diff --git a/rails_application/app/read_models/client_orders/order_handlers.rb b/rails_application/app/read_models/client_orders/order_handlers.rb index a96b3f01..33d6a1ce 100644 --- a/rails_application/app/read_models/client_orders/order_handlers.rb +++ b/rails_application/app/read_models/client_orders/order_handlers.rb @@ -88,7 +88,7 @@ def call(event) end end - class ResetDiscount + class RemoveDiscount def call(event) order = Order.find_by(order_uid: event.data.fetch(:order_id)) order.percentage_discount = nil diff --git a/rails_application/app/read_models/orders/configuration.rb b/rails_application/app/read_models/orders/configuration.rb index fb09677a..5980fdd6 100644 --- a/rails_application/app/read_models/orders/configuration.rb +++ b/rails_application/app/read_models/orders/configuration.rb @@ -35,7 +35,7 @@ def call(event_store) event_store.subscribe(AddItemToOrder.new, to: [Pricing::PriceItemAdded]) event_store.subscribe(RemoveItemFromOrder.new, to: [Pricing::PriceItemRemoved]) event_store.subscribe(UpdateDiscount.new, to: [Pricing::PercentageDiscountSet, Pricing::PercentageDiscountChanged]) - event_store.subscribe(ResetDiscount.new, to: [Pricing::PercentageDiscountReset]) + event_store.subscribe(RemoveDiscount.new, to: [Pricing::PercentageDiscountRemoved]) event_store.subscribe(UpdateOrderTotalValue.new, to: [Pricing::OrderTotalValueCalculated]) event_store.subscribe(RegisterProduct.new, to: [ProductCatalog::ProductRegistered]) event_store.subscribe(ChangeProductName.new, to: [ProductCatalog::ProductNamed]) @@ -47,7 +47,7 @@ def call(event_store) event_store.subscribe(ConfirmOrder.new, to: [Fulfillment::OrderConfirmed]) event_store.subscribe(CancelOrder.new, to: [Fulfillment::OrderCancelled]) event_store.subscribe(UpdateTimePromotionDiscountValue.new, to: [Pricing::PercentageDiscountSet]) - event_store.subscribe(ResetTimePromotionDiscountValue.new, to: [Pricing::PercentageDiscountReset]) + event_store.subscribe(RemoveTimePromotionDiscount.new, to: [Pricing::PercentageDiscountRemoved]) subscribe( ->(event) { broadcast_order_state_change(event.data.fetch(:order_id), 'Submitted') }, diff --git a/rails_application/app/read_models/orders/reset_discount.rb b/rails_application/app/read_models/orders/remove_discount.rb similarity index 94% rename from rails_application/app/read_models/orders/reset_discount.rb rename to rails_application/app/read_models/orders/remove_discount.rb index 42037348..e4afcc25 100644 --- a/rails_application/app/read_models/orders/reset_discount.rb +++ b/rails_application/app/read_models/orders/remove_discount.rb @@ -1,5 +1,5 @@ module Orders - class ResetDiscount + class RemoveDiscount def call(event) return unless event.data.fetch(:type) == Pricing::Discounts::GENERAL_DISCOUNT diff --git a/rails_application/app/read_models/orders/reset_time_promotion_discount_value.rb b/rails_application/app/read_models/orders/remove_time_promotion_discount.rb similarity index 87% rename from rails_application/app/read_models/orders/reset_time_promotion_discount_value.rb rename to rails_application/app/read_models/orders/remove_time_promotion_discount.rb index c4741ab5..b2871bd5 100644 --- a/rails_application/app/read_models/orders/reset_time_promotion_discount_value.rb +++ b/rails_application/app/read_models/orders/remove_time_promotion_discount.rb @@ -1,5 +1,5 @@ module Orders - class ResetTimePromotionDiscountValue + class RemoveTimePromotionDiscount def call(event) return unless event.data.fetch(:type) == Pricing::Discounts::TIME_PROMOTION_DISCOUNT diff --git a/rails_application/app/views/orders/edit.html.erb b/rails_application/app/views/orders/edit.html.erb index cb9e4c5b..a4511bbf 100644 --- a/rails_application/app/views/orders/edit.html.erb +++ b/rails_application/app/views/orders/edit.html.erb @@ -68,7 +68,7 @@ General discount "><%= percentage_discount %>% - <%= button_to "Reset", reset_discount_order_path(id: @order_id), {method: :post, class: "hover:underline text-blue-500"} %> + <%= button_to "Remove", remove_discount_order_path(id: @order_id), {method: :post, class: "hover:underline text-blue-500"} %> <% end %> <% if @time_promotions.present? %> diff --git a/rails_application/config/routes.rb b/rails_application/config/routes.rb index 2269eaac..e52f6d35 100644 --- a/rails_application/config/routes.rb +++ b/rails_application/config/routes.rb @@ -12,7 +12,7 @@ post :cancel get :edit_discount post :update_discount - post :reset_discount + post :remove_discount end resource :shipping_address, only: [:edit, :update] resource :billing_address, only: [:edit, :update] diff --git a/rails_application/test/client_orders/discount_test.rb b/rails_application/test/client_orders/discount_test.rb index 29e3990c..0b5ba5e5 100644 --- a/rails_application/test/client_orders/discount_test.rb +++ b/rails_application/test/client_orders/discount_test.rb @@ -37,7 +37,7 @@ def test_discount_changed assert_equal 1, order.percentage_discount end - def test_reset_discount + def test_remove_discount customer_id = SecureRandom.uuid product_id = SecureRandom.uuid order_id = SecureRandom.uuid @@ -46,7 +46,7 @@ def test_reset_discount item_added_to_basket(order_id, product_id) set_percentage_discount(order_id) - reset_percentage_discount(order_id) + remove_percentage_discount(order_id) order = Order.find_by(order_uid: order_id) assert_equal(50, order.total_value) @@ -56,8 +56,8 @@ def test_reset_discount private - def reset_percentage_discount(order_id) - run_command(Pricing::ResetPercentageDiscount.new(order_id: order_id)) + def remove_percentage_discount(order_id) + run_command(Pricing::RemovePercentageDiscount.new(order_id: order_id)) end def set_percentage_discount(order_id) @@ -96,4 +96,3 @@ def event_store end end end - diff --git a/rails_application/test/integration/discount_test.rb b/rails_application/test/integration/discount_test.rb index 4ec99d1e..3fbc5b7c 100644 --- a/rails_application/test/integration/discount_test.rb +++ b/rails_application/test/integration/discount_test.rb @@ -6,7 +6,7 @@ def setup add_available_vat_rate(10) end - def test_reset_discount + def test_remove_discount register_customer("Shopify") order_id = SecureRandom.uuid @@ -17,15 +17,15 @@ def test_reset_discount post "/orders/#{order_id}/add_item?product_id=#{async_remote_id}" get "/orders/#{order_id}/edit" assert_select("td", "$137.00") - assert_select("a", count: 0, text: "Reset") + assert_select("a", count: 0, text: "Remove") apply_discount_10_percent(order_id) - assert_select("button", "Reset") - post "/orders/#{order_id}/reset_discount" + assert_select("button", "Remove") + post "/orders/#{order_id}/remove_discount" follow_redirect! assert_select("td", "$137.00") - assert_select("a", count: 0, text: "Reset") + assert_select("a", count: 0, text: "Remove") end private diff --git a/rails_application/test/orders/discount_test.rb b/rails_application/test/orders/discount_test.rb index 37184b60..00fae46b 100644 --- a/rails_application/test/orders/discount_test.rb +++ b/rails_application/test/orders/discount_test.rb @@ -41,7 +41,7 @@ def test_discount_changed assert event_store.event_in_stream?(event_store.read.of_type([Pricing::PercentageDiscountChanged]).last.event_id, "Orders$all") end - def test_reset_discount + def test_remove_discount customer_id = SecureRandom.uuid product_id = SecureRandom.uuid order_id = SecureRandom.uuid @@ -50,16 +50,16 @@ def test_reset_discount item_added_to_basket(order_id, product_id) set_percentage_discount(order_id) - reset_percentage_discount(order_id) + remove_percentage_discount(order_id) order = Order.find_by(uid: order_id) assert_equal(50, order.total_value) assert_equal(50, order.discounted_value) assert_nil(order.percentage_discount) - assert event_store.event_in_stream?(event_store.read.of_type([Pricing::PercentageDiscountReset]).last.event_id, "Orders$all") + assert event_store.event_in_stream?(event_store.read.of_type([Pricing::PercentageDiscountRemoved]).last.event_id, "Orders$all") end - def test_does_not_reset_percentage_discount_when_time_promotion_reset + def test_does_not_remove_percentage_discount_when_time_promotion_reset customer_id = SecureRandom.uuid product_id = SecureRandom.uuid order_id = SecureRandom.uuid @@ -108,8 +108,8 @@ def test_newest_event_is_always_applied private - def reset_percentage_discount(order_id) - run_command(Pricing::ResetPercentageDiscount.new(order_id: order_id)) + def remove_percentage_discount(order_id) + run_command(Pricing::RemovePercentageDiscount.new(order_id: order_id)) end def set_percentage_discount(order_id) diff --git a/rails_application/test/orders/reset_time_promotion_discount_value_test.rb b/rails_application/test/orders/remove_time_promotion_discount_test.rb similarity index 90% rename from rails_application/test/orders/reset_time_promotion_discount_value_test.rb rename to rails_application/test/orders/remove_time_promotion_discount_test.rb index a835282a..b6fd2d29 100644 --- a/rails_application/test/orders/reset_time_promotion_discount_value_test.rb +++ b/rails_application/test/orders/remove_time_promotion_discount_test.rb @@ -1,7 +1,7 @@ require "test_helper" module Orders - class ResetTimePromotionDiscountValueTest < InMemoryTestCase + class RemoveTimePromotionDiscountTest < InMemoryTestCase cover "Orders*" def test_resets_time_promotion_discount_value @@ -31,7 +31,7 @@ def test_does_not_reset_time_promotion_when_general_discount_reset set_percentage_discount(order_id) assert_no_changes -> { Orders::Order.find_by(uid: order_id).time_promotion_discount_value } do - reset_percentage_discount(order_id) + remove_percentage_discount(order_id) end end @@ -82,9 +82,9 @@ def set_percentage_discount(order_id) ) end - def reset_percentage_discount(order_id) + def remove_percentage_discount(order_id) run_command( - Pricing::ResetPercentageDiscount.new(order_id: order_id, type: Pricing::Discounts::GENERAL_DISCOUNT) + Pricing::RemovePercentageDiscount.new(order_id: order_id, type: Pricing::Discounts::GENERAL_DISCOUNT) ) end end