Files
fewo-stommer/app/Filament/Resources/Channels/Pages/ChannelProcessTest.php
T
LarsandClaude Sonnet 5.5 c46f4a032b Kanalkonfigurator und Prozess-Tester auf Schablonenbasis
Geschäftsentscheidungen zuerst (Schablonen je Bereich, Voreinstellungen A3–A5), Einzelschritte
eingeklappt, Auswirkungsanalyse beim Schablonenwechsel, unbestätigte Portal-Fähigkeiten gekennzeichnet.
Tester-Bericht nennt Schablonen, unabhängig laufende Schritte und fehlende Nachweise; alle vier
Kombinationen sind als Szenario getestet. Doku: SCHABLONEN.md.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-05 10:56:51 +01:00

152 lines
4.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Filament\Resources\Channels\Pages;
use App\Enums\IntakeKind;
use App\Enums\UserRole;
use App\Filament\Resources\Channels\ChannelResource;
use App\Models\Channel;
use App\Models\ProcessTestRun;
use App\Models\User;
use App\Process\ChannelProcessConfiguration;
use App\Process\Tester\ProcessSandbox;
use App\Process\Tester\ScenarioCatalogue;
use App\Process\Tester\Scenarios\Scenario;
use Filament\Actions\Action;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\Concerns\InteractsWithRecord;
use Filament\Resources\Pages\Page;
use Throwable;
/**
* Ablauf testen (docs/Prozessarchitektur/Architektur/04): plays a predefined scenario with the channel's current – also
* unsaved – process configuration in an isolated run and shows the report step by step. Nothing of a run reaches the
* real data, mails, calendars or portals; on a server where the isolation cannot be established the tester is locked
* and says why. Landlord administrators only (decision E8).
*
* @property Channel $record
*/
class ChannelProcessTest extends Page
{
use InteractsWithRecord;
protected static string $resource = ChannelResource::class;
protected static ?string $title = 'Ablauf testen';
protected string $view = 'filament.resources.channels.channel-process-test';
public string $intake = 'inquiry';
public string $scenario = '';
/** @var array<string, mixed> */
public array $inputs = [];
/** @var array<string, mixed> */
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<string, class-string<Scenario>>
*/
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<string>
*/
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<ProcessTestRun>
*/
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])),
];
}
}