Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| NuvemshopWebhookController | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
1.00 | |
0.00% |
0 / 1 |
| __invoke | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
1.00 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers\Api; |
| 4 | |
| 5 | use App\Enums\WebhookStatus; |
| 6 | use App\Http\Controllers\Controller; |
| 7 | use App\Jobs\ProcessNuvemshopWebhookJob; |
| 8 | use App\Models\StoreIntegration; |
| 9 | use App\Models\WebhookEvent; |
| 10 | use Illuminate\Http\JsonResponse; |
| 11 | use Illuminate\Http\Request; |
| 12 | |
| 13 | class NuvemshopWebhookController extends Controller |
| 14 | { |
| 15 | public function __invoke(Request $request, ?StoreIntegration $integration = null): JsonResponse |
| 16 | { |
| 17 | $eventName = $request->header('X-Linkedstore-Topic') |
| 18 | ?? $request->header('X-Tiendanube-Topic') |
| 19 | ?? $request->input('event', 'unknown'); |
| 20 | |
| 21 | $event = WebhookEvent::create([ |
| 22 | 'store_integration_id' => $integration?->id, |
| 23 | 'provider_slug' => 'nuvemshop', |
| 24 | 'event' => $eventName, |
| 25 | 'external_id' => (string) ($request->input('id') ?? $request->input('order.id')), |
| 26 | 'headers' => collect($request->headers->all())->map(fn ($v) => implode(',', $v))->toArray(), |
| 27 | 'payload' => $request->all(), |
| 28 | 'status' => WebhookStatus::Received->value, |
| 29 | ]); |
| 30 | |
| 31 | ProcessNuvemshopWebhookJob::dispatch($event->id); |
| 32 | |
| 33 | return response()->json(['received' => true, 'webhook_event_id' => $event->id]); |
| 34 | } |
| 35 | } |