fix: Refactor CreateShipmentOnReadyToShip listener to directly call service

- Remove controller dependency and call CourierService directly
- Add proper validation before attempting shipment creation
- Log failures instead of silently succeeding
- Emit ShipmentCreated/ShipmentCreationFailed events properly
- Prevents false success logs when validation fails
This commit is contained in:
twotalesanimation
2026-01-02 17:42:12 +02:00
parent aa30111841
commit 2ddb3dc19f
+77 -16
View File
@@ -3,9 +3,9 @@
namespace App\Listeners; namespace App\Listeners;
use App\Events\ReadyToShipIntent; use App\Events\ReadyToShipIntent;
use App\Http\Controllers\ShippingController; use App\Services\CourierService;
use Illuminate\Contracts\Queue\ShouldQueue; use App\Events\ShipmentCreated;
use Illuminate\Queue\InteractsWithQueue; use App\Events\ShipmentCreationFailed;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
class CreateShipmentOnReadyToShip class CreateShipmentOnReadyToShip
@@ -13,34 +13,95 @@ class CreateShipmentOnReadyToShip
/** /**
* Create the event listener. * Create the event listener.
*/ */
public function __construct(protected ShippingController $shippingController) public function __construct(protected CourierService $courierService)
{ {
} }
/** /**
* Handle the event. * Handle the event - create shipment when Trello card moves to Ready to Ship.
*/ */
public function handle(ReadyToShipIntent $event): void public function handle(ReadyToShipIntent $event): void
{ {
$order = $event->order;
Log::info('Creating shipment from Ready to Ship intent', [ Log::info('Creating shipment from Ready to Ship intent', [
'order_uuid' => $event->order->uuid, 'order_uuid' => $order->uuid,
]); ]);
try { // Validate packing
// Create a mock request object for the controller if (! $order->packing_completed_at || ! $order->packing_width || ! $order->packing_length || ! $order->packing_weight) {
$request = new \Illuminate\Http\Request(); Log::warning('Cannot create shipment - order not packed', [
'order_uuid' => $order->uuid,
// Call the shipping controller to create shipment 'packing_completed_at' => $order->packing_completed_at,
$this->shippingController->createShipment($request, $event->order);
Log::info('Shipment created successfully from webhook', [
'order_uuid' => $event->order->uuid,
]); ]);
return;
}
// Validate payment
if ($order->is_custom_order === false && $order->payment_status !== 'paid') {
Log::warning('Cannot create shipment - order not paid', [
'order_uuid' => $order->uuid,
'payment_status' => $order->payment_status,
]);
return;
}
// Prevent duplicate shipments
if ($order->courier_waybill_id) {
Log::info('Shipment already exists for this order', [
'order_uuid' => $order->uuid,
'waybill_id' => $order->courier_waybill_id,
]);
return;
}
try {
Log::debug('Calling courier API to create shipment', [
'order_uuid' => $order->uuid,
]);
// Call courier API
$shipmentData = $this->courierService->createShipment(
$order->id,
$order->packing_width,
$order->packing_length,
$order->packing_weight,
);
// Save shipment details
$order->update([
'courier_waybill_id' => $shipmentData['waybill_id'],
'courier_tracking_number' => $shipmentData['tracking_number'],
'courier_status' => 'awaiting_collection',
'status' => 'awaiting_collection',
]);
Log::info('Shipment created successfully from webhook', [
'order_uuid' => $order->uuid,
'waybill_id' => $shipmentData['waybill_id'],
'tracking_number' => $shipmentData['tracking_number'],
]);
// Fetch shipping documents
$stickerPath = $this->courierService->fetchSticker($shipmentData['shipment_id'], $order->id);
$waybillPath = $this->courierService->fetchWaybill($shipmentData['shipment_id'], $order->id);
// Emit event to trigger Slack/Trello updates
ShipmentCreated::dispatch(
$order,
$shipmentData['waybill_id'],
$shipmentData['tracking_number'],
$stickerPath,
$waybillPath,
);
} catch (\Exception $e) { } catch (\Exception $e) {
Log::error('Failed to create shipment from Ready to Ship intent', [ Log::error('Failed to create shipment from Ready to Ship intent', [
'order_uuid' => $event->order->uuid, 'order_uuid' => $order->uuid,
'error' => $e->getMessage(), 'error' => $e->getMessage(),
]); ]);
// Emit failure event
ShipmentCreationFailed::dispatch($order, $e->getMessage());
} }
} }
} }