Files
fewo-stommer/app/Actions/ConfigureChannelProcess.php
T
LarsandClaude Sonnet 5.5 8442d0a3d8 V2.1: Kanalfähigkeiten, Ausführungsvarianten, Dokumentenentkopplung, Portalbestätigung
Modellwahl über Kanalfähigkeiten mit erklärten Einschränkungen, Abgleich alter Werte,
Ausführungsvarianten je Modul, getrennte Vertrags-/Buchungsinformation, Warten auf
die Portalbestätigung mit Eskalation und Wiederaufnahme (Fälle C und D getestet).

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

101 lines
5.1 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\Actions;
use App\Enums\IntakeKind;
use App\Enums\ProcessSetting;
use App\Models\Channel;
use App\Process\ChannelCapabilities;
use App\Process\ChannelProcessConfiguration;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
/**
* Saves the process of a channel for one intake kind (Kanal › Ablauf, docs/Prozessarchitektur/Architektur/03 and V2.1/02):
* the confirmed capabilities of the portal, the chosen model and variant, the step switches and the step settings – in
* channels.settings and channels.process_overrides[intake], next to the existing settings, so their inheritance stays
* as it is. Refuses everything the configuration does not admit (mandatory steps off, responsibilities the model does not
* allow, a model the capabilities do not fit, tester-only variants). Stored settings that the chosen model overrides are
* removed only after the landlord confirmed that, and the change is kept in the channel's history. Changes apply to new
* cases only (decision E3): running cases keep their frozen plan.
*/
class ConfigureChannelProcess
{
public const HISTORY_LIMIT = 20;
public function __construct(private ChannelProcessConfiguration $configuration, private ChannelCapabilities $capabilities) {}
/**
* @param array{model: string, variant?: string, steps?: array<string, array{on?: bool}>, settings?: array<string, mixed>, capabilities?: array<string, bool|string|null>} $draft
* @param bool $acknowledgeConflicts the landlord confirmed that stored settings the model overrides are removed
*
* @throws ValidationException
*/
public function handle(Channel $channel, IntakeKind $intake, array $draft, ?string $by = null, bool $acknowledgeConflicts = false): Channel
{
$problems = $this->configuration->problems($channel, $intake, $draft);
if ($problems !== []) {
throw ValidationException::withMessages(['process' => $problems]);
}
$conflicts = $this->configuration->conflicts($channel, $intake, (string) $draft['model']);
if ($conflicts !== [] && ! $acknowledgeConflicts) {
throw ValidationException::withMessages(['process' => [
'Bisherige Einstellungen widersprechen dem gewählten Modell und würden still überstimmt. Bitte ausdrücklich bestätigen, dass sie entfernt werden:',
...array_column($conflicts, 'text'),
]]);
}
return DB::transaction(function () use ($channel, $intake, $draft, $by, $conflicts): Channel {
$channel = Channel::query()->lockForUpdate()->findOrFail($channel->id);
$by ??= auth()->user()?->name;
$overrides = (array) ($channel->process_overrides ?? []);
$current = (array) ($overrides[$intake->value] ?? []);
$removed = array_map(fn (array $conflict): string => $conflict['setting']->value, $conflicts);
$settings = [];
foreach (ProcessSetting::cases() as $setting) {
if (in_array($setting->value, $removed, true)) {
continue;
}
$value = array_key_exists($setting->value, (array) ($draft['settings'] ?? [])) ? $setting->normalize($draft['settings'][$setting->value]) : $setting->normalize($current[$setting->value] ?? null);
if ($value !== null && in_array($intake, $setting->channelIntakes(), true)) {
$settings[$setting->value] = $value;
}
}
$steps = [];
foreach ((array) ($draft['steps'] ?? []) as $module => $step) {
if (array_key_exists('on', $step) && $step['on'] === false) {
$steps[$module] = ['on' => false];
}
}
$overrides[$intake->value] = [
...$settings,
'model' => $draft['model'],
'variant' => $draft['variant'] ?? 'standard',
'steps' => $steps,
'configured_at' => now()->toIso8601String(),
'configured_by' => $by,
];
$channelSettings = (array) ($channel->settings ?? []);
$changes = [];
if (! $channel->isWebsite() && filled($draft['capabilities'] ?? null)) {
$confirmed = $this->capabilities->confirm($channel, (array) $draft['capabilities'], $by);
$channelSettings['capabilities'] = $confirmed['capabilities'];
$changes = $confirmed['changes'];
}
$channelSettings['process_history'] = array_slice([...(array) ($channelSettings['process_history'] ?? []), [
'at' => now()->toIso8601String(), 'by' => $by, 'intake' => $intake->value, 'model' => $draft['model'], 'variant' => $draft['variant'] ?? 'standard',
'capabilities' => $changes, 'removed_settings' => array_map(fn (array $conflict): string => $conflict['text'], $conflicts),
]], -self::HISTORY_LIMIT);
$channel->update(['process_overrides' => $overrides, 'settings' => $channelSettings]);
return $channel;
});
}
}