feat: Implement Slack, Trello, and Courier (ShipLogic) integration
- Add 14 domain events for order lifecycle (OrderCreated, OrderPacked, ShipmentCreated, ParcelDelivered, etc.)
- Create SlackNotifierService with channels for orders, design, production, shipping, ops-alerts
- Create TrelloService to create cards, move cards between lists, attach files, check items
- Create CourierService to integrate with ShipLogic API for shipment creation and document retrieval
- Create PackingController to explicitly capture packing dimensions and weight
- Create ShippingController with multi-layer guards: packing validation, payment/approval verification, idempotency
- Create TrelloWebhookController to handle incoming Trello webhooks as intent signals
- Create CourierWebhookController to handle Shiplogic status updates
- Create event listeners for Slack notifications and Trello card updates
- Create EventServiceProvider to register all events and listeners
- Add database migration for packing, courier, and Trello data columns
- Create config files for slack, trello, and courier integration
- Update .env with integration secrets placeholders
- Add routes for /orders/{id}/pack, /orders/{id}/ship, /api/webhooks/trello, /api/webhooks/courier
Key architectural decisions:
- Packing is explicit ops action (not automatic from status)
- Shipment creation only after: Trello intent + packing confirmed + payment/approval rules met
- Courier API failures keep order in Ready to Ship state (safe retry)
- Trello and Slack are mirrors of backend state, not decision makers
- All side effects flow through event listeners, maintaining separation of concerns
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Events\OrderPacked;
|
||||
use App\Models\Order;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class PackingController extends Controller
|
||||
{
|
||||
/**
|
||||
* Confirm order packing with dimensions and weight
|
||||
*
|
||||
* POST /orders/{id}/pack
|
||||
* POST /custom-orders/{id}/pack
|
||||
*/
|
||||
public function confirmPacked(Request $request, Order $order)
|
||||
{
|
||||
// Validate packing input
|
||||
$validated = $request->validate([
|
||||
'width' => 'required|numeric|min:0.01',
|
||||
'length' => 'required|numeric|min:0.01',
|
||||
'weight' => 'required|numeric|min:0.01',
|
||||
]);
|
||||
|
||||
// Check: Order must exist and not already be packed
|
||||
if (! $order) {
|
||||
return response()->json(['error' => 'Order not found'], 404);
|
||||
}
|
||||
|
||||
if ($order->packing_completed_at !== null) {
|
||||
return response()->json(['error' => 'Order already packed'], 409);
|
||||
}
|
||||
|
||||
// Check: Order must be in Inspection state
|
||||
if ($order->status !== 'inspection') {
|
||||
return response()->json([
|
||||
'error' => 'Order must be in Inspection state before packing',
|
||||
'current_status' => $order->status,
|
||||
], 409);
|
||||
}
|
||||
|
||||
try {
|
||||
// Save packing data
|
||||
$order->update([
|
||||
'packing_width' => $validated['width'],
|
||||
'packing_length' => $validated['length'],
|
||||
'packing_weight' => $validated['weight'],
|
||||
'packing_completed_at' => now(),
|
||||
'packed_by' => Auth::id(),
|
||||
'status' => 'packing',
|
||||
]);
|
||||
|
||||
Log::info('Order packed', [
|
||||
'order_id' => $order->id,
|
||||
'width' => $validated['width'],
|
||||
'length' => $validated['length'],
|
||||
'weight' => $validated['weight'],
|
||||
'packed_by' => Auth::id(),
|
||||
]);
|
||||
|
||||
// Emit event to trigger Trello update, Slack notification
|
||||
OrderPacked::dispatch(
|
||||
$order,
|
||||
(float) $validated['width'],
|
||||
(float) $validated['length'],
|
||||
(float) $validated['weight'],
|
||||
Auth::id(),
|
||||
);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Order packed successfully',
|
||||
'order_id' => $order->id,
|
||||
'packing_completed_at' => $order->packing_completed_at,
|
||||
'dimensions' => [
|
||||
'width' => $validated['width'],
|
||||
'length' => $validated['length'],
|
||||
'weight' => $validated['weight'],
|
||||
],
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Error packing order', [
|
||||
'order_id' => $order->id,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'error' => 'Failed to pack order',
|
||||
'message' => $e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user