keys()->filter( fn (string $carrier) => $this->fulfillmentService($carrier) instanceof SupportsTracking ); if ($trackableCarriers->isEmpty()) { return; } Shipment::query() ->whereIn('carrier', $trackableCarriers) ->whereNull('cancelled_at') ->whereDoesntHave('shipmentInfo', function ($query) { $query->whereIn('status', [ TrackingStatus::Delivered->value, TrackingStatus::Returned->value, TrackingStatus::Cancelled->value, ]); }) ->chunkById(50, function ($shipments) { $shipments->groupBy('carrier')->each( fn ($group, $carrier) => $this->pollCarrierShipments($carrier, $group) ); }); } private function pollCarrierShipments(string $carrier, $shipments): void { $service = $this->fulfillmentService($carrier); if (! $service instanceof SupportsTracking) { return; } foreach ($shipments as $shipment) { $this->recordNewCheckpoints($shipment, $service->trackShipment($shipment)); } } private function recordNewCheckpoints(Shipment $shipment, $checkpoints): void { $existing = $shipment->shipmentInfo() ->get(['status', 'occurred_at']) ->map(fn ($info) => $info->status->value.'|'.$info->occurred_at->toIso8601String()) ->flip(); foreach ($checkpoints as $checkpoint) { $fingerprint = $checkpoint->status->value.'|'.$checkpoint->occurredAt->toIso8601String(); if ($existing->has($fingerprint)) { continue; } $info = ShipmentInfo::create([ 'shipment_id' => $shipment->id, 'status' => $checkpoint->status, 'carrier_status' => $checkpoint->carrierStatus, 'message' => $checkpoint->message, 'location' => $checkpoint->location, 'occurred_at' => $checkpoint->occurredAt, 'meta' => $checkpoint->meta, ]); ShipmentStatusUpdatedByCarrier::dispatch($info); } } private function fulfillmentService(string $carrier): ?CarrierFulfillmentInterface { return app(CarrierFulfillmentInterface::class, ['carrier' => $carrier]); } }