Extract more OFN fee-handling code from Spree::Order class

This commit is contained in:
Matt-Yorkley
2021-01-29 21:42:55 +00:00
parent 793baca44f
commit 8466ab5675
2 changed files with 14 additions and 14 deletions

View File

@@ -69,7 +69,7 @@ module Spree
delegate :admin_and_handling_total, :payment_fee, :ship_total, to: :adjustments_fetcher
delegate :update_totals, to: :updater
delegate :create_line_item_fees!, :create_order_fees!, :update_order_fees!,
:update_line_item_fees!, to: :fee_handler
:update_line_item_fees!, :recreate_all_fees!, to: :fee_handler
# Needs to happen before save_permalink is called
before_validation :set_currency
@@ -662,19 +662,6 @@ module Spree
end
end
def recreate_all_fees!
# `with_lock` acquires an exclusive row lock on order so no other
# requests can update it until the transaction is commited.
# See https://github.com/rails/rails/blob/3-2-stable/activerecord/lib/active_record/locking/pessimistic.rb#L69
# and https://www.postgresql.org/docs/current/static/sql-select.html#SQL-FOR-UPDATE-SHARE
with_lock do
EnterpriseFee.clear_all_adjustments self
create_line_item_fees!
create_order_fees!
end
end
def set_order_cycle!(order_cycle)
return if self.order_cycle == order_cycle

View File

@@ -9,6 +9,19 @@ class OrderFeesHandler
@order_cycle = order.order_cycle
end
def recreate_all_fees!
# `with_lock` acquires an exclusive row lock on order so no other
# requests can update it until the transaction is commited.
# See https://github.com/rails/rails/blob/3-2-stable/activerecord/lib/active_record/locking/pessimistic.rb#L69
# and https://www.postgresql.org/docs/current/static/sql-select.html#SQL-FOR-UPDATE-SHARE
order.with_lock do
EnterpriseFee.clear_all_adjustments order
create_line_item_fees!
create_order_fees!
end
end
def create_line_item_fees!
order.line_items.includes(variant: :product).each do |line_item|
if provided_by_order_cycle? line_item