getKey() : (int) $draft; $resolvedDraft = TenantOnboardingSession::query() ->with(['tenant', 'startedByUser', 'updatedByUser']) ->whereKey($draftId) ->first(); if (! $resolvedDraft instanceof TenantOnboardingSession) { throw new NotFoundHttpException; } if ((int) $resolvedDraft->workspace_id !== (int) $workspace->getKey()) { throw new NotFoundHttpException; } Gate::forUser($user)->authorize('view', $resolvedDraft); $resolvedDraft = $this->lifecycleService ->syncPersistedLifecycle($resolvedDraft) ->loadMissing(['tenant', 'startedByUser', 'updatedByUser']); $normalizedTenant = $this->lifecycleService->syncLinkedTenantAfterCancellation($resolvedDraft); if ($normalizedTenant !== null) { $this->auditLogger->logTenantLifecycleAction( tenant: $normalizedTenant, action: AuditActionId::TenantReturnedToDraft, actor: $user, context: [ 'metadata' => [ 'source' => 'onboarding_draft_resolver', 'onboarding_session_id' => (int) $resolvedDraft->getKey(), ], ], ); $resolvedDraft->setRelation('tenant', $normalizedTenant); } return $resolvedDraft; } /** * @return Collection */ public function resumableDraftsFor(User $user, Workspace $workspace): Collection { $drafts = TenantOnboardingSession::query() ->with(['tenant', 'startedByUser', 'updatedByUser']) ->where('workspace_id', (int) $workspace->getKey()) ->resumable() ->orderByDesc('updated_at') ->get(); $resolvedDrafts = []; foreach ($drafts as $draft) { try { Gate::forUser($user)->authorize('view', $draft); } catch (AuthorizationException) { continue; } $resolvedDraft = $this->lifecycleService ->syncPersistedLifecycle($draft) ->loadMissing(['tenant', 'startedByUser', 'updatedByUser']); if (! $this->lifecycleService->canResumeDraft($resolvedDraft)) { continue; } $resolvedDrafts[] = $resolvedDraft; } return new Collection($resolvedDrafts); } }