Skip to content

Commit

Permalink
Refactor, add OrderManagement::Order::Updater#update_voucher
Browse files Browse the repository at this point in the history
Move the logic to update a voucher and associated order to
`OrderManagement`
  • Loading branch information
rioug committed Oct 29, 2024
1 parent 86c1f76 commit 0a0ae42
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 19 deletions.
3 changes: 1 addition & 2 deletions app/controllers/spree/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ def update
@order.recreate_all_fees! # Enterprise fees on line items and on the order itself

# Re apply the voucher
VoucherAdjustmentsService.new(@order).update
@order.update_totals_and_states
OrderManagement::Order::Updater.new(@order).update_voucher

if @order.complete?
@order.update_payment_fees!
Expand Down
3 changes: 1 addition & 2 deletions app/controllers/voucher_adjustments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ def add_voucher_to_order(voucher)
# calculate_voucher_adjustment
clear_payments

VoucherAdjustmentsService.new(@order).update
@order.update_totals_and_states
OrderManagement::Order::Updater.new(@order).update_voucher

true
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ def after_payment_update(payment)
persist_totals
end

def update_voucher
VoucherAdjustmentsService.new(order).update
update_totals_and_states
end

private

def cancel_payments_requiring_auth
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,26 @@ module Order
end
end

describe "#update_voucher" do
let(:voucher_service) { instance_double(VoucherAdjustmentsService) }

it "calls VoucherAdjustmentsService" do
expect(VoucherAdjustmentsService).to receive(:new).and_return(voucher_service)
expect(voucher_service).to receive(:update)

updater.update_voucher
end

it "calls update_totals_and_states" do
allow(VoucherAdjustmentsService).to receive(:new).and_return(voucher_service)
allow(voucher_service).to receive(:update)

expect(updater).to receive(:update_totals_and_states)

updater.update_voucher
end
end

def update_order_quantity(order)
order.line_items.first.update_attribute(:quantity, 2)
end
Expand Down
3 changes: 1 addition & 2 deletions spec/controllers/checkout_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,7 @@
before do
# Adding voucher to the order
vine_voucher.create_adjustment(vine_voucher.code, order)
VoucherAdjustmentsService.new(order).update
order.update_totals_and_states
OrderManagement::Order::Updater.new(order).update_voucher

allow(Vine::VoucherRedeemerService).to receive(:new).and_return(vine_voucher_redeemer)
end
Expand Down
3 changes: 1 addition & 2 deletions spec/lib/reports/sales_tax_totals_by_order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,7 @@
def add_voucher(order, voucher)
# Add voucher to the order
voucher.create_adjustment(voucher.code, order)
VoucherAdjustmentsService.new(order).update
order.update_totals_and_states
OrderManagement::Order::Updater.new(order).update_voucher

Orders::WorkflowService.new(order).complete!
end
Expand Down
3 changes: 1 addition & 2 deletions spec/requests/spree/admin/payments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,6 @@

def add_voucher_to_order(voucher, order)
voucher.create_adjustment(voucher.code, order)
VoucherAdjustmentsService.new(order).update
order.update_totals_and_states
OrderManagement::Order::Updater.new(order).update_voucher
end
end
3 changes: 1 addition & 2 deletions spec/services/vine/voucher_redeemer_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,7 @@

def add_voucher(voucher)
voucher.create_adjustment(voucher.code, order)
VoucherAdjustmentsService.new(order).update
order.update_totals_and_states
OrderManagement::Order::Updater.new(order).update_voucher
end

def mock_api_response(success:, data: nil, status: 200)
Expand Down
4 changes: 1 addition & 3 deletions spec/system/admin/reports/revenues_by_hub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,6 @@ def apply_voucher(order, voucher)
order.update_shipping_fees!
order.update_order!

VoucherAdjustmentsService.new(order).update

order.update_totals_and_states
OrderManagement::Order::Updater.new(order).update_voucher
end
end
3 changes: 1 addition & 2 deletions spec/system/consumer/checkout/payment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,6 @@

def add_voucher_to_order(voucher, order)
voucher.create_adjustment(voucher.code, order)
VoucherAdjustmentsService.new(order).update
order.update_totals_and_states
OrderManagement::Order::Updater.new(order).update_voucher
end
end
3 changes: 1 addition & 2 deletions spec/system/consumer/checkout/summary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,7 @@ def confirmation_page_expect_paid(paid_state:, paid_amount:)

def add_voucher_to_order(voucher, order)
voucher.create_adjustment(voucher.code, order)
VoucherAdjustmentsService.new(order).update
order.update_totals_and_states
OrderManagement::Order::Updater.new(order).update_voucher
end

def set_up_order(balance, state)
Expand Down

0 comments on commit 0a0ae42

Please sign in to comment.