Class: Address::RecalculateShipping
- Inherits:
-
BaseService
- Object
- BaseService
- Address::RecalculateShipping
- Defined in:
- app/services/address/recalculate_shipping.rb
Defined Under Namespace
Classes: Result
Constant Summary collapse
- FIELDS_THAT_TRIGGER_RECALC =
(Address::ADDRESS_FIELDS_TO_RECALCULATE_SHIPPING_FOR + Address::FREIGHT_FIELDS).freeze
Instance Method Summary collapse
Methods inherited from BaseService
#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger
Constructor Details
This class inherits a constructor from BaseService
Instance Method Details
#process(address) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'app/services/address/recalculate_shipping.rb', line 13 def process(address) # Isolate fields of interest address_changes = address.saved_changes.select{|k,v| FIELDS_THAT_TRIGGER_RECALC.include?(k) } log_info "address_changes: #{address_changes}" # Only recalc if the case insensitive comparison of values changed need_to_recalc_shipping = address_changes.any?{|k, (value_was,value_becomes)| !value_was.to_s.casecmp(value_becomes.to_s).zero? } log_info "need_to_recalc_shipping: #{need_to_recalc_shipping}" return Result.new(needed_to_recalc_shipping: false) unless need_to_recalc_shipping # only recalculate shipping for orders and quotes that are open for change, not locked # NOTE: nor in the midst of retrieving shipping. Had to remove this condition (where(retrieving_shipping_costs:false)) # because if there's a failure mid-stream in recalc. Then the recalc never happens and never recovers. orders_to_update = address.shipping_orders.open_for_change.to_a.reject(&:editing_locked?) log_info "orders_to_update (ids): #{orders_to_update.map(&:id)}" orders_to_update.each{|o| o.update_column(:signature_confirmation, true)} if address.require_signature_by_default? orders_to_update.each(&:retrieve_shipping_costs) quotes_to_update = address.shipping_quotes.open_quotes.to_a.reject(&:editing_locked?) log_info "quotes_to_update (ids): #{quotes_to_update.map(&:id).uniq}" quotes_to_update.each{|q| q.update_column(:signature_confirmation, true)} if address.require_signature_by_default? quotes_to_update.each(&:retrieve_shipping_costs) Result.new(needed_to_recalc_shipping: true, need_to_set_signature_confirmation: address.require_signature_by_default?, orders: orders_to_update, quotes: quotes_to_update) end |