TenantAtlas/app/Services/TenantReviews/TenantReviewLifecycleService.php
ahmido 807d574d31 feat: add tenant governance aggregate contract and action surface follow-ups (#199)
## Summary
- amend the operator UI constitution and related SpecKit templates for the new UI/UX governance rules
- add Spec 168 artifacts plus the tenant governance aggregate implementation used by the tenant dashboard, banner, and baseline compare landing surfaces
- normalize Filament action surfaces around clickable-row inspection, grouped secondary actions, and explicit action-surface declarations across enrolled resources and pages
- fix post-suite regressions in membership cache priming, finding workflow state refresh, tenant review derived-state invalidation, and tenant-bound backup-set related navigation

## Commit Series
- `docs: amend operator UI constitution`
- `spec: add tenant governance aggregate contract`
- `feat: add tenant governance aggregate contract`
- `refactor: normalize filament action surfaces`
- `fix: resolve post-suite state regressions`

## Testing
- `vendor/bin/sail artisan test --compact`
- Result: `3176 passed, 8 skipped (17384 assertions)`

## Notes
- Livewire v4 / Filament v5 stack remains unchanged
- no provider registration changes; `bootstrap/providers.php` remains the relevant location
- no new global-search resources or asset-registration changes in this branch

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #199
2026-03-29 21:14:17 +00:00

187 lines
6.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\TenantReviews;
use App\Models\EvidenceSnapshot;
use App\Models\ReviewPack;
use App\Models\Tenant;
use App\Models\TenantReview;
use App\Models\User;
use App\Services\Audit\WorkspaceAuditLogger;
use App\Support\Audit\AuditActionId;
use App\Support\TenantReviewStatus;
use App\Support\Ui\DerivedState\DerivedStateFamily;
use App\Support\Ui\DerivedState\RequestScopedDerivedStateStore;
use Illuminate\Support\Facades\DB;
use InvalidArgumentException;
final class TenantReviewLifecycleService
{
public function __construct(
private readonly TenantReviewReadinessGate $readinessGate,
private readonly TenantReviewService $reviewService,
private readonly WorkspaceAuditLogger $auditLogger,
private readonly RequestScopedDerivedStateStore $derivedStateStore,
) {}
public function publish(TenantReview $review, User $user): TenantReview
{
$review->loadMissing(['tenant', 'sections', 'currentExportReviewPack']);
$tenant = $review->tenant;
if (! $tenant instanceof Tenant) {
throw new InvalidArgumentException('Review tenant could not be resolved.');
}
$blockers = $this->readinessGate->blockersForReview($review);
$beforeStatus = (string) $review->status;
if ($blockers !== []) {
throw new InvalidArgumentException(implode(' ', $blockers));
}
$review->forceFill([
'status' => TenantReviewStatus::Published->value,
'published_at' => now(),
'published_by_user_id' => (int) $user->getKey(),
'summary' => array_merge(is_array($review->summary) ? $review->summary : [], [
'publish_blockers' => [],
]),
])->save();
$this->auditLogger->log(
workspace: $tenant->workspace,
action: AuditActionId::TenantReviewPublished,
context: [
'metadata' => [
'review_id' => (int) $review->getKey(),
'before_status' => $beforeStatus,
'after_status' => TenantReviewStatus::Published->value,
],
],
actor: $user,
resourceType: 'tenant_review',
resourceId: (string) $review->getKey(),
targetLabel: sprintf('Tenant review #%d', (int) $review->getKey()),
tenant: $tenant,
);
$this->invalidateArtifactTruthCache($review);
return $review->refresh()->load(['tenant', 'sections', 'currentExportReviewPack']);
}
public function archive(TenantReview $review, User $user): TenantReview
{
$review->loadMissing('tenant');
$tenant = $review->tenant;
if (! $tenant instanceof Tenant) {
throw new InvalidArgumentException('Review tenant could not be resolved.');
}
$beforeStatus = (string) $review->status;
if ($review->statusEnum()->isTerminal()) {
return $review;
}
$review->forceFill([
'status' => TenantReviewStatus::Archived->value,
'archived_at' => now(),
])->save();
$this->auditLogger->log(
workspace: $tenant->workspace,
action: AuditActionId::TenantReviewArchived,
context: [
'metadata' => [
'review_id' => (int) $review->getKey(),
'before_status' => $beforeStatus,
'after_status' => TenantReviewStatus::Archived->value,
],
],
actor: $user,
resourceType: 'tenant_review',
resourceId: (string) $review->getKey(),
targetLabel: sprintf('Tenant review #%d', (int) $review->getKey()),
tenant: $tenant,
);
$this->invalidateArtifactTruthCache($review);
return $review->refresh()->load(['tenant', 'sections', 'currentExportReviewPack']);
}
public function createNextReview(TenantReview $review, User $user, ?EvidenceSnapshot $snapshot = null): TenantReview
{
$review->loadMissing(['tenant', 'evidenceSnapshot']);
$tenant = $review->tenant;
if (! $tenant instanceof Tenant) {
throw new InvalidArgumentException('Review tenant could not be resolved.');
}
if (! $review->isPublished()) {
throw new InvalidArgumentException('Only published reviews can start the next cycle.');
}
$snapshot ??= $this->reviewService->resolveLatestSnapshot($tenant) ?? $review->evidenceSnapshot;
if (! $snapshot instanceof EvidenceSnapshot) {
throw new InvalidArgumentException('An eligible evidence snapshot is required to create the next review.');
}
$nextReview = DB::transaction(function () use ($review, $user, $snapshot, $tenant): TenantReview {
$nextReview = $this->reviewService->create($tenant, $snapshot, $user);
if ((int) $nextReview->getKey() !== (int) $review->getKey()) {
$review->forceFill([
'status' => TenantReviewStatus::Superseded->value,
'superseded_by_review_id' => (int) $nextReview->getKey(),
])->save();
}
$this->auditLogger->log(
workspace: $tenant->workspace,
action: AuditActionId::TenantReviewSuccessorCreated,
context: [
'metadata' => [
'review_id' => (int) $review->getKey(),
'next_review_id' => (int) $nextReview->getKey(),
'before_status' => TenantReviewStatus::Published->value,
'after_status' => TenantReviewStatus::Superseded->value,
],
],
actor: $user,
resourceType: 'tenant_review',
resourceId: (string) $nextReview->getKey(),
targetLabel: sprintf('Tenant review #%d', (int) $nextReview->getKey()),
tenant: $tenant,
);
return $nextReview->refresh()->load(['tenant', 'evidenceSnapshot', 'sections', 'operationRun', 'initiator', 'publisher']);
});
$this->invalidateArtifactTruthCache($review);
$this->invalidateArtifactTruthCache($nextReview);
return $nextReview;
}
private function invalidateArtifactTruthCache(TenantReview $review): void
{
$this->derivedStateStore->invalidateModel(DerivedStateFamily::ArtifactTruth, $review, 'tenant_review');
$review->loadMissing('currentExportReviewPack');
$pack = $review->currentExportReviewPack;
if ($pack instanceof ReviewPack) {
$this->derivedStateStore->invalidateModel(DerivedStateFamily::ArtifactTruth, $pack, 'review_pack');
}
}
}