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:
@@ -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,
|
||||
]);
|
||||
|
||||
// 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 {
|
||||
// Create a mock request object for the controller
|
||||
$request = new \Illuminate\Http\Request();
|
||||
Log::debug('Calling courier API to create shipment', [
|
||||
'order_uuid' => $order->uuid,
|
||||
]);
|
||||
|
||||
// Call the shipping controller to create shipment
|
||||
$this->shippingController->createShipment($request, $event->order);
|
||||
// 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' => $event->order->uuid,
|
||||
'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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user