## Summary - Fixes misleading “queued / running in background” message when Review Pack generation request reuses an existing ready pack (fingerprint dedupe). - Improves resilience of Filament/Livewire interactions by ensuring the Livewire intercept shim applies after Livewire initializes. - Aligns Review Pack operation notifications with Ops-UX patterns (queued + completed notifications) and removes the old ReviewPackStatusNotification. ## Key Changes - Review Pack generate action now: - Shows queued toast only when a new pack is actually created/queued. - Shows a “Review pack already available” success notification with a link when dedupe returns an existing pack. ## Tests - `vendor/bin/sail artisan test --compact tests/Feature/ReviewPack/ReviewPackGenerationTest.php` - `vendor/bin/sail artisan test --compact tests/Feature/ReviewPack/ReviewPackResourceTest.php` - `vendor/bin/sail artisan test --compact tests/Feature/LivewireInterceptShimTest.php` ## Notes - No global search behavior changes for ReviewPacks (still excluded). - Destructive actions remain confirmation-gated (`->requiresConfirmation()`). Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de> Reviewed-on: #133
159 lines
4.5 KiB
PHP
159 lines
4.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Filament\Widgets\Tenant;
|
|
|
|
use App\Models\ReviewPack;
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
use App\Services\ReviewPackService;
|
|
use App\Support\Auth\Capabilities;
|
|
use App\Support\ReviewPackStatus;
|
|
use Filament\Facades\Filament;
|
|
use Filament\Notifications\Notification;
|
|
use Filament\Widgets\Widget;
|
|
|
|
class TenantReviewPackCard extends Widget
|
|
{
|
|
protected static bool $isLazy = false;
|
|
|
|
protected string $view = 'filament.widgets.tenant.tenant-review-pack-card';
|
|
|
|
public ?Tenant $record = null;
|
|
|
|
private function resolveTenant(): ?Tenant
|
|
{
|
|
$tenant = Filament::getTenant();
|
|
|
|
if ($tenant instanceof Tenant) {
|
|
return $tenant;
|
|
}
|
|
|
|
return $this->record instanceof Tenant ? $this->record : null;
|
|
}
|
|
|
|
public function generatePack(bool $includePii = true, bool $includeOperations = true): void
|
|
{
|
|
$user = auth()->user();
|
|
|
|
if (! $user instanceof User) {
|
|
abort(403);
|
|
}
|
|
|
|
$tenant = $this->resolveTenant();
|
|
|
|
if (! $tenant instanceof Tenant) {
|
|
abort(404);
|
|
}
|
|
|
|
if (! $user->canAccessTenant($tenant)) {
|
|
abort(404);
|
|
}
|
|
|
|
if (! $user->can(Capabilities::REVIEW_PACK_MANAGE, $tenant)) {
|
|
abort(403);
|
|
}
|
|
|
|
/** @var ReviewPackService $service */
|
|
$service = app(ReviewPackService::class);
|
|
|
|
if ($service->checkActiveRun($tenant)) {
|
|
Notification::make()
|
|
->title('Generation already in progress')
|
|
->body('A review pack is currently being generated for this tenant.')
|
|
->warning()
|
|
->send();
|
|
|
|
return;
|
|
}
|
|
|
|
$service->generate($tenant, $user, [
|
|
'include_pii' => $includePii,
|
|
'include_operations' => $includeOperations,
|
|
]);
|
|
|
|
Notification::make()
|
|
->title('Review pack generation started')
|
|
->body('The pack will be generated in the background. You will be notified when it is ready.')
|
|
->success()
|
|
->send();
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
protected function getViewData(): array
|
|
{
|
|
$tenant = $this->resolveTenant();
|
|
|
|
if (! $tenant instanceof Tenant) {
|
|
return $this->emptyState();
|
|
}
|
|
|
|
$user = auth()->user();
|
|
$isTenantMember = $user instanceof User && $user->canAccessTenant($tenant);
|
|
$canView = $isTenantMember && $user->can(Capabilities::REVIEW_PACK_VIEW, $tenant);
|
|
$canManage = $isTenantMember && $user->can(Capabilities::REVIEW_PACK_MANAGE, $tenant);
|
|
|
|
$latestPack = ReviewPack::query()
|
|
->where('tenant_id', (int) $tenant->getKey())
|
|
->orderByDesc('created_at')
|
|
->orderByDesc('id')
|
|
->first();
|
|
|
|
if (! $latestPack instanceof ReviewPack) {
|
|
return [
|
|
'tenant' => $tenant,
|
|
'pack' => null,
|
|
'statusEnum' => null,
|
|
'canView' => $canView,
|
|
'canManage' => $canManage,
|
|
'downloadUrl' => null,
|
|
'failedReason' => null,
|
|
];
|
|
}
|
|
|
|
$statusEnum = ReviewPackStatus::tryFrom((string) $latestPack->status);
|
|
|
|
$downloadUrl = null;
|
|
if ($statusEnum === ReviewPackStatus::Ready && $canView) {
|
|
/** @var ReviewPackService $service */
|
|
$service = app(ReviewPackService::class);
|
|
$downloadUrl = $service->generateDownloadUrl($latestPack);
|
|
}
|
|
|
|
$failedReason = null;
|
|
if ($statusEnum === ReviewPackStatus::Failed && $latestPack->operationRun) {
|
|
$opContext = is_array($latestPack->operationRun->context) ? $latestPack->operationRun->context : [];
|
|
$failedReason = (string) ($opContext['reason_code'] ?? 'Unknown error');
|
|
}
|
|
|
|
return [
|
|
'tenant' => $tenant,
|
|
'pack' => $latestPack,
|
|
'statusEnum' => $statusEnum,
|
|
'canView' => $canView,
|
|
'canManage' => $canManage,
|
|
'downloadUrl' => $downloadUrl,
|
|
'failedReason' => $failedReason,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function emptyState(): array
|
|
{
|
|
return [
|
|
'tenant' => null,
|
|
'pack' => null,
|
|
'statusEnum' => null,
|
|
'canView' => false,
|
|
'canManage' => false,
|
|
'downloadUrl' => null,
|
|
'failedReason' => null,
|
|
];
|
|
}
|
|
}
|