2a10f9af38
**Shiplogic API Integration:**
- Fixed API base URL configuration (removed /api suffix)
- Implemented comprehensive request/response logging for rates and shipments endpoints
- Fixed PDF fetching: API returns S3 URLs, now downloads actual PDFs from S3
- Added tests and mock API responses for local development (routes/shiplogic-mock.php)
**Courier Service Enhancements:**
- Added redownloadShipmentPdfs() public method for re-downloading corrupted PDFs
- Enhanced error logging with full request/response bodies for debugging
- Proper binary PDF storage using Laravel Storage facade
- URL and S3 download handling for Shiplogic API responses
**Workflow & Operations:**
- Changed to manual "Ready for Collection" button instead of automatic move
- Operators now: scan QR → apply labels → click "Ready for Collection" → moves to Awaiting Collection
- Removed duplicate PDF attachments to Trello (was adding twice from two listeners)
- Fixed NotifySlackOnShipmentCreated to only handle Slack notifications
**Mobile-Optimized Ops Page:**
- Removed QR code display from order detail page
- Implemented responsive single-column layout for mobile phones
- Large touch-friendly buttons (full width, increased padding)
- Bold typography for better readability on small screens
- Larger input fields and tracking number displays
- Clear step-by-step instructions for warehouse operators
- Re-download PDF button for damaged/corrupted labels
**New Features:**
- POST /ops/orders/{uuid}/ready-for-collection endpoint
- Re-download PDFs functionality accessible from awaiting_collection and in_transit states
- Full audit logging for all operations via ops interface
- Proper error handling and user feedback
**Testing:**
- Added ShipmentCreationTest with mock HTTP client
- Created comprehensive testing guide (SHIPLOGIC_TESTING.md)
- Mock API routes for local development without hitting live API
224 lines
7.6 KiB
PHP
224 lines
7.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Events\ReadyToShipIntent;
|
|
use App\Events\OrderMovedToPrep;
|
|
use App\Events\OrderMovedToAwaitingApproval;
|
|
use App\Events\OrderMovedToReadyForPrint;
|
|
use App\Events\OrderMovedToPrinting;
|
|
use App\Events\OrderMovedToInspection;
|
|
use App\Models\Order;
|
|
use App\Models\CustomOrder;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class TrelloWebhookController extends Controller
|
|
{
|
|
/**
|
|
* Handle incoming Trello webhooks
|
|
*
|
|
* POST /api/webhooks/trello
|
|
*/
|
|
public function handle(Request $request)
|
|
{
|
|
// Trello validation ping or real event - both return 200
|
|
Log::info('Trello webhook received', [
|
|
'method' => $request->method(),
|
|
'content_length' => strlen($request->getContent()),
|
|
]);
|
|
|
|
// If empty body or validation ping, just return success
|
|
if ($request->getContent() === '' || $request->method() === 'HEAD') {
|
|
return response()->json(['ok' => true], 200);
|
|
}
|
|
|
|
try {
|
|
$payload = $request->json()->all();
|
|
|
|
Log::info('Trello action received', [
|
|
'action' => $payload['action']['type'] ?? 'unknown',
|
|
'card' => $payload['action']['data']['card']['name'] ?? 'unknown',
|
|
]);
|
|
|
|
match ($payload['action']['type'] ?? null) {
|
|
'updateCard' => $this->handleCardUpdate($payload),
|
|
'updateCheckItem' => $this->handleChecklistUpdate($payload),
|
|
default => Log::debug('Unhandled Trello action', [
|
|
'type' => $payload['action']['type'] ?? 'unknown',
|
|
]),
|
|
};
|
|
|
|
return response()->json(['success' => true], 200);
|
|
} catch (\Throwable $e) {
|
|
Log::error('Error processing Trello webhook', [
|
|
'error' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString(),
|
|
]);
|
|
|
|
return response()->json(['success' => true], 200);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Handle card movement between lists
|
|
*/
|
|
private function handleCardUpdate(array $payload): void
|
|
{
|
|
$action = $payload['action'] ?? [];
|
|
$cardData = $action['data']['card'] ?? [];
|
|
$cardId = $cardData['id'] ?? null;
|
|
$cardName = $cardData['name'] ?? null;
|
|
$listAfter = $action['data']['listAfter'] ?? [];
|
|
$listName = $listAfter['name'] ?? null;
|
|
|
|
if (! $cardId || ! $cardName || ! $listName) {
|
|
Log::warning('Trello card update missing required data', [
|
|
'card_id' => $cardId,
|
|
'card_name' => $cardName,
|
|
'list_name' => $listName,
|
|
]);
|
|
return;
|
|
}
|
|
|
|
// Extract order number from card name (e.g., "Order #1043" or "Order #ORD-20260102-ABC123")
|
|
if (! preg_match('/Order #([A-Za-z0-9\-]+)/', $cardName, $matches)) {
|
|
Log::debug('Could not extract order number from card name', ['card_name' => $cardName]);
|
|
return;
|
|
}
|
|
|
|
$orderNumber = $matches[1];
|
|
|
|
// Try to find the order (could be standard or custom)
|
|
$order = Order::where('order_number', $orderNumber)->first()
|
|
?? CustomOrder::where('order_number', $orderNumber)->first();
|
|
|
|
if (! $order) {
|
|
Log::warning('Order not found for Trello card', [
|
|
'order_number' => $orderNumber,
|
|
'card_name' => $cardName,
|
|
]);
|
|
return;
|
|
}
|
|
|
|
Log::info('Trello card moved', [
|
|
'order_uuid' => $order->uuid,
|
|
'order_number' => $orderNumber,
|
|
'list' => $listName,
|
|
]);
|
|
|
|
// Handle list-specific actions
|
|
match ($listName) {
|
|
'Prep/Design' => (function () use ($order) {
|
|
$order->update(['status' => 'prep']);
|
|
OrderMovedToPrep::dispatch($order);
|
|
})(),
|
|
'Awaiting Customer Approval' => (function () use ($order) {
|
|
$order->update(['status' => 'awaiting_approval']);
|
|
OrderMovedToAwaitingApproval::dispatch($order);
|
|
})(),
|
|
'Ready for Print' => (function () use ($order) {
|
|
$order->update(['status' => 'ready_for_print']);
|
|
OrderMovedToReadyForPrint::dispatch($order);
|
|
})(),
|
|
'Printing' => (function () use ($order) {
|
|
$order->update(['status' => 'printing']);
|
|
OrderMovedToPrinting::dispatch($order);
|
|
})(),
|
|
'Inspection' => (function () use ($order) {
|
|
$order->update(['status' => 'inspection']);
|
|
OrderMovedToInspection::dispatch($order);
|
|
})(),
|
|
'Packing' => $order->update(['status' => 'packing']),
|
|
'Ready to Ship' => (function () use ($order, $cardId) {
|
|
$order->update(['status' => 'ready_to_ship']);
|
|
$this->handleReadyToShipIntent($order, $cardId);
|
|
})(),
|
|
'Awaiting Collection' => (function () use ($order, $cardId) {
|
|
$order->update(['status' => 'awaiting_collection']);
|
|
$this->handleAwaitingCollectionIntent($order, $cardId);
|
|
})(),
|
|
'In Transit' => $order->update(['status' => 'in_transit']),
|
|
'Done' => $order->update(['status' => 'completed']),
|
|
default => Log::debug('Card moved to list', [
|
|
'order_uuid' => $order->uuid,
|
|
'list' => $listName,
|
|
]),
|
|
};
|
|
|
|
Log::info('Order status updated from Trello', [
|
|
'order_uuid' => $order->uuid,
|
|
'list' => $listName,
|
|
'status' => $order->status,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Handle checklist item completion
|
|
*/
|
|
private function handleChecklistUpdate(array $payload): void
|
|
{
|
|
$action = $payload['action'] ?? [];
|
|
$cardData = $action['data']['card'] ?? [];
|
|
$cardName = $cardData['name'] ?? null;
|
|
$itemName = $action['data']['checkItem']['name'] ?? null;
|
|
$itemState = $action['data']['checkItem']['state'] ?? null;
|
|
|
|
if ($itemState !== 'complete' || ! $cardName || ! $itemName) {
|
|
return;
|
|
}
|
|
|
|
Log::debug('Trello checklist item completed', [
|
|
'card' => $cardName,
|
|
'item' => $itemName,
|
|
]);
|
|
|
|
// TODO: Map checklist completions to domain events if needed
|
|
// For now, just log for visibility
|
|
}
|
|
|
|
/**
|
|
* Handle "Ready to Ship" intent
|
|
*
|
|
* Emit event to trigger shipment creation flow
|
|
*/
|
|
private function handleReadyToShipIntent($order, string $cardId): void
|
|
{
|
|
Log::info('Ready to Ship intent from Trello', [
|
|
'order_uuid' => $order->uuid,
|
|
'card_id' => $cardId,
|
|
]);
|
|
|
|
// Emit event so ShippingController can validate and create shipment
|
|
ReadyToShipIntent::dispatch($order);
|
|
}
|
|
|
|
/**
|
|
* Handle "Awaiting Collection" intent
|
|
*
|
|
* Verify shipment exists before accepting transition
|
|
*/
|
|
private function handleAwaitingCollectionIntent($order, string $cardId): void
|
|
{
|
|
Log::info('Awaiting Collection intent from Trello', [
|
|
'order_uuid' => $order->uuid,
|
|
'card_id' => $cardId,
|
|
]);
|
|
|
|
// Check if order has a waybill (shipment was created)
|
|
if (! $order->courier_waybill_id) {
|
|
Log::warning('Cannot move to Awaiting Collection - no shipment created', [
|
|
'order_uuid' => $order->uuid,
|
|
]);
|
|
return;
|
|
}
|
|
|
|
Log::info('Order ready for collection', [
|
|
'order_uuid' => $order->uuid,
|
|
'waybill_id' => $order->courier_waybill_id,
|
|
]);
|
|
}
|
|
}
|