TenantAtlas/app/Services/Onboarding/OnboardingDraftResolver.php
ahmido 641bb4afde feat: implement tenant lifecycle operability semantics (#172)
## Summary
- implement Spec 143 tenant lifecycle, operability, and tenant-context semantics across chooser, tenant management, onboarding, and canonical operation viewers
- add centralized tenant lifecycle and operability support types, audit action coverage, and lifecycle-aware badge and action handling
- add feature and unit coverage for tenant chooser eligibility, global search scoping, canonical operation access, onboarding authorization, and lifecycle presentation

## Testing
- vendor/bin/sail artisan test --compact
- vendor/bin/sail bin pint --dirty --format agent

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #172
2026-03-15 09:08:36 +00:00

103 lines
3.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\Onboarding;
use App\Models\TenantOnboardingSession;
use App\Models\User;
use App\Models\Workspace;
use App\Services\Audit\WorkspaceAuditLogger;
use App\Support\Audit\AuditActionId;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Gate;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class OnboardingDraftResolver
{
public function __construct(
private readonly OnboardingLifecycleService $lifecycleService,
private readonly WorkspaceAuditLogger $auditLogger,
) {}
/**
* @throws AuthorizationException
* @throws NotFoundHttpException
*/
public function resolve(TenantOnboardingSession|int|string $draft, User $user, Workspace $workspace): TenantOnboardingSession
{
$draftId = $draft instanceof TenantOnboardingSession
? (int) $draft->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<int, TenantOnboardingSession>
*/
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();
return $drafts
->map(function (TenantOnboardingSession $draft) use ($user): ?TenantOnboardingSession {
try {
Gate::forUser($user)->authorize('view', $draft);
} catch (AuthorizationException) {
return null;
}
return $this->lifecycleService
->syncPersistedLifecycle($draft)
->loadMissing(['tenant', 'startedByUser', 'updatedByUser']);
})
->filter(fn (?TenantOnboardingSession $draft): bool => $draft instanceof TenantOnboardingSession
&& $this->lifecycleService->canResumeDraft($draft))
->values();
}
}