Class: ShipmentFirstCarrierScanHandler

Inherits:
ApplicationJob
  • Object
show all
Includes:
RailsEventStore::AsyncHandler
Defined in:
app/subscribers/shipment_first_carrier_scan_handler.rb

Overview

Auto-ship-confirms a delivery when the carrier reports the package is moving
and the warehouse forgot to mark the delivery as shipped.

Subscribes to: Events::ShipmentFirstCarrierScan

WHY: Deliveries sit in pending_ship_confirm overnight while packages are
already in the carrier network. FedEx International Ground shipments
regularly show a 4-6 hour gap between the carrier's first IT scan (~3 AM)
and the warehouse confirming shipment (next morning). This handler closes
that window by treating a carrier movement scan as a fallback signal.

SAFETY GUARDS:

  • Re-checks pending_ship_confirm? before enqueuing (delivery may have
    already been shipped between event publish and job execution).
  • DeliveryShipConfirmWorker uses lock: :until_executed on delivery_id,
    so duplicate scans cannot trigger duplicate ship-confirms.
  • individual_auto_ship_confirm guards on shipments.completed.any?,
    so warehouse-pickup and shipment-less deliveries are skipped.

Instance Method Summary collapse

Instance Method Details

#perform(event) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/subscribers/shipment_first_carrier_scan_handler.rb', line 26

def perform(event)
  delivery_id = event.data[:delivery_id]
  delivery = Delivery.find_by(id: delivery_id)

  unless delivery
    Rails.logger.warn "[ShipmentFirstCarrierScanHandler] Delivery##{delivery_id} not found, skipping"
    return
  end

  unless delivery.pending_ship_confirm?
    Rails.logger.info "[ShipmentFirstCarrierScanHandler] Delivery##{delivery_id} no longer " \
                      "pending_ship_confirm (state=#{delivery.state}), skipping"
    return
  end

  Rails.logger.info "[ShipmentFirstCarrierScanHandler] Enqueuing DeliveryShipConfirmWorker " \
                    "for Delivery##{delivery_id} (carrier scan: #{event.data[:status_code]} " \
                    "on shipment #{event.data[:shipment_id]})"

  DeliveryShipConfirmWorker.perform_async(delivery_id: delivery_id)
rescue StandardError => e
  ErrorReporting.error(e)
  raise
end