<?php
 namespace App\Jobs; use App\Core\Product\ModuleParamsBuilder; use App\Events\ProductProvisioningCompleted; use App\Events\ProductProvisioningFailed; use App\Events\ProductProvisioningProgress; use App\Events\ProductProvisioningStarted; use App\Core\Hook\HookManager; use App\Models\ClientProduct; 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 ProcessProductUnsuspendJob implements ShouldQueue { use BusQueueable, Dispatchable, InteractsWithQueue, SerializesModels; public int $tries = 1; public int $clientProductId; public ?string $provisioningId; public function __construct(int $clientProductId, ?string $provisioningId = null) { $this->clientProductId = $clientProductId; $this->provisioningId = $provisioningId; } public function handle(ProvisioningStatusService $provisioningStatus): void { $clientProduct = ClientProduct::with(['product', 'server', 'configOptionValues.option', 'configOptionValues.selectedValue', 'configFieldValues.customField'])->find($this->clientProductId); if (! $clientProduct) { return; } if (! $this->provisioningId) { $this->provisioningId = $provisioningStatus->create( actionType: ProvisioningStatusService::ACTION_UNSUSPEND, entityType: ProvisioningStatusService::ENTITY_PRODUCT, entityId: $clientProduct->id, moduleSlug: strtolower($clientProduct->module), requestData: [], message: 'Hesap aktifleştiriliyor...' ); } try { $moduleName = strtolower($clientProduct->module); $provisioningStatus->update($this->provisioningId, ['module_slug' => $moduleName]); event(new ProductProvisioningStarted($this->provisioningId)); $moduleConfig = $clientProduct->product->getModuleConfigArray($clientProduct->module); $moduleInstance = app(\App\Services\ModuleResolver::class)->resolve( $moduleName, $clientProduct->product->type ?? null, $moduleConfig ); if (! $moduleInstance) { throw new \Exception("Modül bulunamadı: {$moduleName}"); } $params = ModuleParamsBuilder::build($clientProduct); $provisioningStatus->update($this->provisioningId, ['request_data' => $params]); event(new ProductProvisioningProgress($this->provisioningId, 50, 'Hesap aktifleştiriliyor...')); $hookResult = app(HookManager::class)->filter('PreModuleUnsuspend', $params); if (is_array($hookResult) && ($hookResult['abortcmd'] ?? false) === true) { event(new ProductProvisioningFailed( $this->provisioningId, 'Hook aborted', [], $this->clientProductId, 'unsuspend' )); return; } $params = is_array($hookResult) ? $hookResult : $params; $result = $moduleInstance->unsuspendAccount($params); $provisioningStatus->update($this->provisioningId, ['response_data' => $result]); if (isset($result['status']) && $result['status'] === 'success') { $clientProduct->update([ 'status' => 'active', 'suspended_at' => null, 'failed_suspend_count' => 0, 'suspend_reason' => null, ]); } event(new ProductProvisioningCompleted($this->provisioningId, $result)); } catch (\Exception $e) { \Log::error('Product unsuspend provisioning failed', [ 'provisioning_id' => $this->provisioningId, 'client_product_id' => $this->clientProductId, 'module_slug' => $moduleName, 'error' => $e->getMessage(), ]); event(new ProductProvisioningFailed( $this->provisioningId, $e->getMessage(), [ 'exception' => get_class($e), 'file' => $e->getFile(), 'line' => $e->getLine(), 'trace' => explode("\n", $e->getTraceAsString()), ], $this->clientProductId, 'unsuspend' )); throw $e; } } } 