Class: SpreeDelhivery::ShipmentTracker

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree_delhivery/shipment_tracker.rb

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize(shipment) ⇒ ShipmentTracker

Returns a new instance of ShipmentTracker.



6
7
8
9
# File 'app/services/spree_delhivery/shipment_tracker.rb', line 6

def initialize(shipment)
  @shipment = shipment
  @client = SpreeDelhivery::Client.new
end

Instance Method Details

#callObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/services/spree_delhivery/shipment_tracker.rb', line 11

def call
  unless @shipment.delhivery_waybill.present?
    return Result.new(false, "No Waybill found for Shipment #{@shipment.number}")
  end
  
  begin
    response = @client.track_shipment(@shipment.delhivery_waybill)
    
    # Safe navigation to extract the Shipment object
    # Delhivery structure: { "ShipmentData" => [ { "Shipment" => { ... } } ] }
    shipment_data = response.dig('ShipmentData', 0, 'Shipment')
  
    if shipment_data.present?
      # 1. Extract Status safely
      # "Status" object contains keys like "Status", "StatusDateTime", "RecievedBy"
      current_status = shipment_data.dig('Status', 'Status') || "Unknown"
  
      # 2. Update Database efficiently
      # usage of update_columns avoids triggering callbacks/validations, which is preferred for background sync
      @shipment.update_columns(
        delhivery_response_data: shipment_data,
        tracking_status: current_status
      )
  
      Rails.logger.info "[Delhivery] Synced Shipment #{@shipment.number}: #{current_status}"
      
      return Result.new(true, current_status, shipment_data)
    else
      error_msg = "No Shipment Data found in Delhivery response"
      Rails.logger.warn "[Delhivery] Tracking Failed for #{@shipment.number}: #{error_msg}"
      return Result.new(false, error_msg)
    end
  
  rescue StandardError => e
    Rails.logger.error "[Delhivery] Exception tracking #{@shipment.number}: #{e.message}"
    return Result.new(false, e.message)
  end
end