Class: Delivery::RetrieveShippingCosts

Inherits:
BaseService show all
Defined in:
app/services/delivery/retrieve_shipping_costs.rb

Overview

Retrieves shipping costs from carriers for one delivery and persists them as
ShippingCost rows — the quote step of the shipping pipeline.

Runs under a per-delivery advisory lock (non-blocking) so concurrent
requests can't double-quote. Three paths:

  1. Service-only / warehouse-pickup deliveries → a single override
    ShippingCost (0.0, or the warehouse-pickup fee on the first delivery).
  2. Zero-charge dropship → a single override ShippingCost at 0.0.
  3. Everything else → builds the WyShipping option payload
    (CalculateShippingOptions), resolves the effective ship date,
    adds marketplace (Walmart/Amazon) rate info, calls
    WyShipping.calculate_shipping_from_options, batch-inserts the returned
    rates, and maintains the override placeholder row. When no valid rates
    come back the delivery falls back to an override cost so the rep can sort
    it out.

Extracted from Delivery#retrieve_shipping_costs (god-object decomposition);
the model keeps a thin delegator for its ~35 callers.

Examples:

Delivery::RetrieveShippingCosts.new(delivery).process
Delivery::RetrieveShippingCosts.new(delivery, rate_ship_date: date).process

Instance Attribute Summary

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods inherited from BaseService

#log_debug, #log_error, #log_info, #log_warning, #logger, #tagged_logger

Constructor Details

#initialize(delivery, rate_ship_date: nil) ⇒ RetrieveShippingCosts

Returns a new instance of RetrieveShippingCosts.



28
29
30
31
32
# File 'app/services/delivery/retrieve_shipping_costs.rb', line 28

def initialize(delivery, rate_ship_date: nil)
  super()
  @delivery = delivery
  @rate_ship_date = rate_ship_date
end

Instance Method Details

#processHash

Returns { code:, message:, packages:, created_at: } — or
{ code: :already_processing, ... } when another request holds the lock.

Returns:

  • (Hash)

    { code:, message:, packages:, created_at: } — or
    { code: :already_processing, ... } when another request holds the lock.



36
37
38
39
40
41
42
43
# File 'app/services/delivery/retrieve_shipping_costs.rb', line 36

def process
  lock_key = "delivery|#{delivery.id}|retrieve_shipping_costs"
  result = Delivery.with_advisory_lock_result(lock_key, timeout_seconds: 0) { retrieve! }

  return { code: :already_processing, message: 'Shipping costs are already being retrieved', packages: '' } unless result.lock_was_acquired?

  result.result
end