feat: Add CreateShipmentOnReadyToShip listener to handle webhook-triggered shipments

- Create new listener that fires on ReadyToShipIntent event
- Register listener in EventServiceProvider
- Fix log statements in ShippingController to use order UUID instead of ID
- Now when Trello card moves to 'Ready to Ship', backend automatically creates shipment
This commit is contained in:
twotalesanimation
2026-01-02 17:26:11 +02:00
parent 291393d497
commit 1c68fff301
3 changed files with 57 additions and 3 deletions
+4 -3
View File
@@ -23,6 +23,7 @@ class ShippingController extends Controller
*/
public function createShipment(Request $request, Order $order)
{
Log::info('Initiating shipment creation', ['order_uuid' => $order->uuid]);
// Guard 1: Verify packing completed with valid dimensions
if (! $this->validatePackingGate($order)) {
return response()->json([
@@ -60,7 +61,7 @@ class ShippingController extends Controller
// Guard 4: Idempotency - if already shipped, return existing shipment
if ($order->courier_waybill_id) {
Log::info('Shipment already exists, returning existing', ['order_id' => $order->id]);
Log::info('Shipment already exists, returning existing', ['order_uuid' => $order->uuid]);
return response()->json([
'success' => true,
@@ -90,7 +91,7 @@ class ShippingController extends Controller
]);
Log::info('Shipment created with courier', [
'order_id' => $order->id,
'order_uuid' => $order->uuid,
'waybill_id' => $shipmentData['waybill_id'],
'tracking_number' => $shipmentData['tracking_number'],
]);
@@ -120,7 +121,7 @@ class ShippingController extends Controller
]);
} catch (\Exception $e) {
Log::error('Failed to create shipment', [
'order_id' => $order->id,
'order_uuid' => $order->uuid,
'error' => $e->getMessage(),
]);
@@ -0,0 +1,46 @@
<?php
namespace App\Listeners;
use App\Events\ReadyToShipIntent;
use App\Http\Controllers\ShippingController;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;
class CreateShipmentOnReadyToShip
{
/**
* Create the event listener.
*/
public function __construct(protected ShippingController $shippingController)
{
}
/**
* Handle the event.
*/
public function handle(ReadyToShipIntent $event): void
{
Log::info('Creating shipment from Ready to Ship intent', [
'order_uuid' => $event->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,
]);
} catch (\Exception $e) {
Log::error('Failed to create shipment from Ready to Ship intent', [
'order_uuid' => $event->order->uuid,
'error' => $e->getMessage(),
]);
}
}
}
+7
View File
@@ -13,8 +13,10 @@ use App\Events\ParcelInTransit;
use App\Events\ProofApproved;
use App\Events\ProofRevisionRequested;
use App\Events\ProofUploaded;
use App\Events\ReadyToShipIntent;
use App\Events\ShipmentCreated;
use App\Events\ShipmentCreationFailed;
use App\Listeners\CreateShipmentOnReadyToShip;
use App\Listeners\NotifySlackOnOrderCreated;
use App\Listeners\NotifySlackOnOrderPacked;
use App\Listeners\NotifySlackOnParcelCollected;
@@ -42,8 +44,13 @@ class EventServiceProvider extends ServiceProvider
// Packing events
OrderPacked::class => [
NotifySlackOnOrderPacked::class,
],Ready to Ship intent (from Trello webhook)
ReadyToShipIntent::class => [
CreateShipmentOnReadyToShip::class,
],
//
// Shipment events
ShipmentCreated::class => [
NotifySlackOnShipmentCreated::class,