operationRun = $operationRun; } /** * @return array */ public function middleware(): array { return [new TrackOperationRun]; } public function handle(OnboardingEvidenceWriter $evidence, OperationRunService $runs): void { $tenant = Tenant::query()->find($this->tenantId); if (! $tenant instanceof Tenant) { throw new RuntimeException('Tenant not found.'); } $user = User::query()->find($this->userId); if (! $user instanceof User) { throw new RuntimeException('User not found.'); } $session = OnboardingSession::query() ->where('tenant_id', $tenant->getKey()) ->find($this->onboardingSessionId); if (! $session instanceof OnboardingSession) { throw new RuntimeException('OnboardingSession not found.'); } $connection = ProviderConnection::query() ->where('tenant_id', $tenant->getKey()) ->find($this->providerConnectionId); if (! $connection instanceof ProviderConnection) { throw new RuntimeException('ProviderConnection not found.'); } $connected = (string) ($connection->status ?? 'unknown') === 'connected'; $evidenceStatus = $connected ? 'ok' : 'blocked'; $reasonCode = $connected ? null : 'provider.not_connected'; $message = $connected ? 'Prerequisites for initial sync look good.' : 'Provider connection is not connected. Resolve consent/credentials first.'; $evidence->record( tenant: $tenant, taskType: OnboardingTaskType::InitialSync, status: $evidenceStatus, reasonCode: $reasonCode, message: $message, payload: [ 'provider_connection_status' => $connection->status, ], session: $session, providerConnection: $connection, operationRun: $this->operationRun, recordedBy: $user, ); if (! $this->operationRun instanceof OperationRun) { return; } if ($evidenceStatus === 'ok') { $runs->updateRun( $this->operationRun, status: OperationRunStatus::Completed->value, outcome: OperationRunOutcome::Succeeded->value, ); return; } $runs->updateRun( $this->operationRun, status: OperationRunStatus::Completed->value, outcome: OperationRunOutcome::Failed->value, failures: [[ 'code' => 'onboarding.initial_sync.blocked', 'reason_code' => $reasonCode ?? 'initial_sync.unknown', 'message' => $message, ]], ); } }