TenantAtlas/app/Services/Tenants/TenantOperabilityService.php
ahmido 440e63edff feat: implement tenant action taxonomy lifecycle visibility (#174)
## Summary

Implements Spec 145 for tenant action taxonomy and lifecycle-safe visibility.

This PR:
- adds a central tenant action policy surface and supporting value objects
- aligns tenant list, detail, edit, onboarding, and widget surfaces around lifecycle-safe actions
- standardizes operator-facing lifecycle wording around View, Resume onboarding, Archive, Restore, and Complete onboarding
- tightens onboarding and tenant lifecycle authorization semantics, including honest 404 vs 403 behavior
- updates related regression coverage and spec artifacts for Spec 145
- fixes follow-on full-suite regressions uncovered during validation, including onboarding browser flows, provider consent fixtures, workspace redirect DI expectations, and critical table/action/UI expectation drift

## Validation

Executed and passed:
- vendor/bin/sail bin pint --dirty --format agent
- vendor/bin/sail artisan test --compact

Result:
- 2581 passed
- 8 skipped
- 13534 assertions

## Notes

- Base branch: dev
- Feature branch commit: a33a41b
- Filament v5 / Livewire v4 compliance preserved
- No panel provider registration changes; Laravel 12 provider registration remains in bootstrap/providers.php
- No new globally searchable resource behavior added in this slice
- Destructive lifecycle actions remain confirmation-gated and authorization-protected

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #174
2026-03-16 00:57:17 +00:00

92 lines
2.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\Tenants;
use App\Models\Tenant;
use App\Support\Tenants\TenantLifecycle;
use App\Support\Tenants\TenantOperabilityDecision;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
class TenantOperabilityService
{
public function decisionFor(Tenant $tenant): TenantOperabilityDecision
{
$lifecycle = TenantLifecycle::fromTenant($tenant);
$isArchived = $tenant->trashed() || $lifecycle === TenantLifecycle::Archived;
return new TenantOperabilityDecision(
lifecycle: $lifecycle,
canViewTenantSurface: $lifecycle->canViewTenantSurface(),
canSelectAsContext: ! $tenant->trashed() && $lifecycle->canSelectAsContext(),
canOperate: ! $tenant->trashed() && $lifecycle->canOperate(),
canArchive: ! $isArchived && $lifecycle->canArchive(),
canRestore: $isArchived || $lifecycle->canRestore(),
canResumeOnboarding: ! $tenant->trashed() && $lifecycle->canResumeOnboarding(),
canReferenceInWorkspaceMonitoring: $lifecycle->canReferenceInWorkspaceMonitoring(),
);
}
public function lifecycleFor(Tenant $tenant): TenantLifecycle
{
return $this->decisionFor($tenant)->lifecycle;
}
public function canSelectAsContext(Tenant $tenant): bool
{
return $this->decisionFor($tenant)->canSelectAsContext;
}
public function canViewTenantSurface(Tenant $tenant): bool
{
return $this->decisionFor($tenant)->canViewTenantSurface;
}
public function canResumeOnboarding(Tenant $tenant): bool
{
return $this->decisionFor($tenant)->canResumeOnboarding;
}
public function canArchive(Tenant $tenant): bool
{
return $this->decisionFor($tenant)->canArchive;
}
public function canRestore(Tenant $tenant): bool
{
return $this->decisionFor($tenant)->canRestore;
}
public function primaryManagementActionKey(Tenant $tenant, bool $preferOnboarding = false): ?string
{
return $this->decisionFor($tenant)->primaryManagementActionKey($preferOnboarding);
}
public function canReferenceInWorkspaceMonitoring(Tenant $tenant): bool
{
return $this->decisionFor($tenant)->canReferenceInWorkspaceMonitoring;
}
/**
* @param Collection<int, Tenant> $tenants
* @return Collection<int, Tenant>
*/
public function filterSelectable(Collection $tenants): Collection
{
return $tenants
->filter(fn (mixed $tenant): bool => $tenant instanceof Tenant && $this->canSelectAsContext($tenant))
->values();
}
public function applySelectableScope(Builder $query, ?string $table = null): Builder
{
$prefix = $table !== null && $table !== '' ? "{$table}." : '';
return $query
->whereNull("{$prefix}deleted_at")
->where("{$prefix}status", TenantLifecycle::Active->value);
}
}