From 2ddb3dc19f099c68b51eb7bd45685a60f9ab951a Mon Sep 17 00:00:00 2001 From: twotalesanimation <80506065+twotalesanimation@users.noreply.github.com> Date: Fri, 2 Jan 2026 17:42:12 +0200 Subject: [PATCH] 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 --- app/Listeners/CreateShipmentOnReadyToShip.php | 93 +++++++++++++++---- 1 file changed, 77 insertions(+), 16 deletions(-) diff --git a/app/Listeners/CreateShipmentOnReadyToShip.php b/app/Listeners/CreateShipmentOnReadyToShip.php index 9a63036..5d2e5b1 100644 --- a/app/Listeners/CreateShipmentOnReadyToShip.php +++ b/app/Listeners/CreateShipmentOnReadyToShip.php @@ -3,9 +3,9 @@ namespace App\Listeners; use App\Events\ReadyToShipIntent; -use App\Http\Controllers\ShippingController; -use Illuminate\Contracts\Queue\ShouldQueue; -use Illuminate\Queue\InteractsWithQueue; +use App\Services\CourierService; +use App\Events\ShipmentCreated; +use App\Events\ShipmentCreationFailed; use Illuminate\Support\Facades\Log; class CreateShipmentOnReadyToShip @@ -13,34 +13,95 @@ class CreateShipmentOnReadyToShip /** * 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 { + $order = $event->order; + Log::info('Creating shipment from Ready to Ship intent', [ - 'order_uuid' => $event->order->uuid, + 'order_uuid' => $order->uuid, ]); - try { - // Create a mock request object for the controller - $request = new \Illuminate\Http\Request(); - - // Call the shipping controller to create shipment - $this->shippingController->createShipment($request, $event->order); - - Log::info('Shipment created successfully from webhook', [ - 'order_uuid' => $event->order->uuid, + // Validate packing + if (! $order->packing_completed_at || ! $order->packing_width || ! $order->packing_length || ! $order->packing_weight) { + Log::warning('Cannot create shipment - order not packed', [ + 'order_uuid' => $order->uuid, + 'packing_completed_at' => $order->packing_completed_at, ]); + 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) { Log::error('Failed to create shipment from Ready to Ship intent', [ - 'order_uuid' => $event->order->uuid, + 'order_uuid' => $order->uuid, 'error' => $e->getMessage(), ]); + + // Emit failure event + ShipmentCreationFailed::dispatch($order, $e->getMessage()); } } }