Class: Delivery::RetrieveShippingCosts
- Inherits:
-
BaseService
- Object
- BaseService
- Delivery::RetrieveShippingCosts
- 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:
- Service-only / warehouse-pickup deliveries → a single
override
ShippingCost (0.0, or the warehouse-pickup fee on the first delivery). - Zero-charge dropship → a single
overrideShippingCost at 0.0. - 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 theoverrideplaceholder 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.
Instance Attribute Summary
Attributes inherited from BaseService
Instance Method Summary collapse
-
#initialize(delivery, rate_ship_date: nil) ⇒ RetrieveShippingCosts
constructor
A new instance of RetrieveShippingCosts.
-
#process ⇒ Hash
{ code:, message:, packages:, created_at: } — or { code: :already_processing, ... } when another request holds the lock.
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
#process ⇒ Hash
Returns { 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 |