*/ public array $inputs = []; /** @var array */ public array $draft = []; public int $steps = 1; public ?int $runId = null; public static function canAccess(array $parameters = []): bool { $user = auth()->user(); return $user instanceof User && $user->hasRole(UserRole::Admin); } public function mount(int|string $record): void { $this->record = $this->resolveRecord($record); $intakes = array_map(fn (IntakeKind $intake): string => $intake->value, app(ChannelProcessConfiguration::class)->intakes($this->record)); $requested = (string) request()->query('intake', ''); $this->intake = in_array($requested, $intakes, true) ? $requested : ($intakes[0] ?? 'inquiry'); $this->draft = (array) session()->get($this->draftKey(), []); if ($this->draft === []) { $current = app(ChannelProcessConfiguration::class)->current($this->record, IntakeKind::from($this->intake)); $stored = (array) (($this->record->process_overrides ?? [])[$this->intake] ?? []); $this->draft = ['model' => $current['model'], 'variant' => $current['variant'], 'steps' => (array) ($stored['steps'] ?? []), 'templates' => $current['templates'], 'settings' => []]; } $this->scenario = (string) array_key_first($this->scenarios()); $this->resetInputs(); } public function draftKey(): string { return 'process-tester.draft.'.$this->record->id.'.'.$this->intake; } /** * @return array> */ public function scenarios(): array { return ScenarioCatalogue::for(IntakeKind::from($this->intake), $this->record->isWebsite()); } public function updatedScenario(): void { $this->resetInputs(); $this->runId = null; $this->steps = 1; } private function resetInputs(): void { $class = $this->scenarios()[$this->scenario] ?? null; $this->inputs = $class === null ? [] : array_map(fn (array $input): mixed => $input['type'] === 'date' ? now()->modify((string) $input['default'])->toDateString() : $input['default'], $class::inputs()); } /** * @return list */ public function requirements(): array { return ProcessSandbox::requirements(); } public function start(): void { $this->steps = 1; $this->play(); } public function next(): void { $this->steps++; $this->play(); } private function play(): void { try { $run = app(ProcessSandbox::class)->run($this->record, IntakeKind::from($this->intake), $this->draft, $this->scenario, $this->inputs, $this->steps, auth()->user()); $this->runId = $run->id; } catch (Throwable $exception) { Notification::make()->title('Test nicht möglich')->body($exception->getMessage())->danger()->persistent()->send(); } } public function run(): ?ProcessTestRun { return $this->runId === null ? null : ProcessTestRun::query()->find($this->runId); } /** * @return iterable */ public function recent(): iterable { return ProcessTestRun::query()->where('channel_id', $this->record->id)->latest()->limit(5)->get(); } protected function getHeaderActions(): array { return [ Action::make('back')->label('Zurück zum Ablauf')->color('gray')->url(fn (): string => ChannelResource::getUrl('process', ['record' => $this->record])), ]; } }