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:
@@ -23,6 +23,7 @@ class ShippingController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function createShipment(Request $request, Order $order)
|
public function createShipment(Request $request, Order $order)
|
||||||
{
|
{
|
||||||
|
Log::info('Initiating shipment creation', ['order_uuid' => $order->uuid]);
|
||||||
// Guard 1: Verify packing completed with valid dimensions
|
// Guard 1: Verify packing completed with valid dimensions
|
||||||
if (! $this->validatePackingGate($order)) {
|
if (! $this->validatePackingGate($order)) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
@@ -60,7 +61,7 @@ class ShippingController extends Controller
|
|||||||
|
|
||||||
// Guard 4: Idempotency - if already shipped, return existing shipment
|
// Guard 4: Idempotency - if already shipped, return existing shipment
|
||||||
if ($order->courier_waybill_id) {
|
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([
|
return response()->json([
|
||||||
'success' => true,
|
'success' => true,
|
||||||
@@ -90,7 +91,7 @@ class ShippingController extends Controller
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
Log::info('Shipment created with courier', [
|
Log::info('Shipment created with courier', [
|
||||||
'order_id' => $order->id,
|
'order_uuid' => $order->uuid,
|
||||||
'waybill_id' => $shipmentData['waybill_id'],
|
'waybill_id' => $shipmentData['waybill_id'],
|
||||||
'tracking_number' => $shipmentData['tracking_number'],
|
'tracking_number' => $shipmentData['tracking_number'],
|
||||||
]);
|
]);
|
||||||
@@ -120,7 +121,7 @@ class ShippingController extends Controller
|
|||||||
]);
|
]);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
Log::error('Failed to create shipment', [
|
Log::error('Failed to create shipment', [
|
||||||
'order_id' => $order->id,
|
'order_uuid' => $order->uuid,
|
||||||
'error' => $e->getMessage(),
|
'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(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,8 +13,10 @@ use App\Events\ParcelInTransit;
|
|||||||
use App\Events\ProofApproved;
|
use App\Events\ProofApproved;
|
||||||
use App\Events\ProofRevisionRequested;
|
use App\Events\ProofRevisionRequested;
|
||||||
use App\Events\ProofUploaded;
|
use App\Events\ProofUploaded;
|
||||||
|
use App\Events\ReadyToShipIntent;
|
||||||
use App\Events\ShipmentCreated;
|
use App\Events\ShipmentCreated;
|
||||||
use App\Events\ShipmentCreationFailed;
|
use App\Events\ShipmentCreationFailed;
|
||||||
|
use App\Listeners\CreateShipmentOnReadyToShip;
|
||||||
use App\Listeners\NotifySlackOnOrderCreated;
|
use App\Listeners\NotifySlackOnOrderCreated;
|
||||||
use App\Listeners\NotifySlackOnOrderPacked;
|
use App\Listeners\NotifySlackOnOrderPacked;
|
||||||
use App\Listeners\NotifySlackOnParcelCollected;
|
use App\Listeners\NotifySlackOnParcelCollected;
|
||||||
@@ -42,8 +44,13 @@ class EventServiceProvider extends ServiceProvider
|
|||||||
// Packing events
|
// Packing events
|
||||||
OrderPacked::class => [
|
OrderPacked::class => [
|
||||||
NotifySlackOnOrderPacked::class,
|
NotifySlackOnOrderPacked::class,
|
||||||
|
],Ready to Ship intent (from Trello webhook)
|
||||||
|
ReadyToShipIntent::class => [
|
||||||
|
CreateShipmentOnReadyToShip::class,
|
||||||
],
|
],
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
// Shipment events
|
// Shipment events
|
||||||
ShipmentCreated::class => [
|
ShipmentCreated::class => [
|
||||||
NotifySlackOnShipmentCreated::class,
|
NotifySlackOnShipmentCreated::class,
|
||||||
|
|||||||
Reference in New Issue
Block a user