<?php
 namespace App\Jobs; use App\Events\ProductProvisioningCompleted; use App\Events\ProductProvisioningFailed; use App\Events\ProductProvisioningProgress; use App\Events\ProductProvisioningStarted; use App\Models\ClientProduct; use App\Models\OrderItem; use App\Services\ProductProvisioningService; use App\Services\ProvisioningStatusService; use Illuminate\Bus\Queueable as BusQueueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; class ProcessProductCreateJob implements ShouldQueue { use BusQueueable, Dispatchable, InteractsWithQueue, SerializesModels; public int $clientProductId; public ?string $provisioningId; public ?int $orderId; public int $tries = 1; public int $timeout = 120; public function __construct(int $clientProductId, ?string $provisioningId = null, ?int $orderId = null) { $this->clientProductId = $clientProductId; $this->provisioningId = $provisioningId; $this->orderId = $orderId; } public function handle(ProvisioningStatusService $provisioningStatus, ProductProvisioningService $provisioningService): void { $clientProduct = ClientProduct::with(['product'])->find($this->clientProductId); if (! $clientProduct) { return; } if (! $this->provisioningId) { $this->provisioningId = $provisioningStatus->create( actionType: ProvisioningStatusService::ACTION_CREATE, entityType: ProvisioningStatusService::ENTITY_PRODUCT, entityId: $clientProduct->id, moduleSlug: strtolower($clientProduct->module ?? $clientProduct->product->module ?? 'unknown'), requestData: $this->orderId ? ['order_id' => $this->orderId] : [], message: $this->orderId ? "Sipariş #{$this->orderId} için otomatik provisioning başlatıldı" : 'Otomatik provisioning başlatıldı...' ); } $moduleName = strtolower($clientProduct->module ?? $clientProduct->product->module ?? 'unknown'); $provisioningStatus->update($this->provisioningId, ['module_slug' => $moduleName]); event(new ProductProvisioningStarted($this->provisioningId)); event(new ProductProvisioningProgress($this->provisioningId, 20, 'Provisioning servisi başlatılıyor...')); $result = $provisioningService->processProductProvisioning( $this->clientProductId, null, $this->orderId, $this->provisioningId ); if ($result['status'] === 'success') { $provisioningStatus->update($this->provisioningId, [ 'response_data' => $result['data'] ?? [], ]); event(new ProductProvisioningCompleted($this->provisioningId, $result['data'] ?? [], $this->clientProductId, 'create')); } else { $provisioningStatus->update($this->provisioningId, [ 'response_data' => ['error' => $result['message']], ]); event(new ProductProvisioningFailed( $this->provisioningId, $result['message'], ['client_product_id' => $this->clientProductId, 'order_id' => $this->orderId], $this->clientProductId, 'create' )); if (isset($result['error']) && $result['error'] instanceof \Exception) { throw $result['error']; } throw new \Exception($result['message'] ?? 'Product provisioning failed'); } } } 