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 */ 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 */ private function emptyState(): array { return [ 'tenant' => null, 'pack' => null, 'statusEnum' => null, 'canView' => false, 'canManage' => false, 'downloadUrl' => null, 'failedReason' => null, ]; } }