Additional event informations can be added

This commit is contained in:
2026-08-12 15:01:35 +02:00
parent 554166803b
commit 9581176a0c
28 changed files with 675 additions and 39 deletions
@@ -0,0 +1,111 @@
<?php
namespace App\Domains\Event\Actions\SetParticipationOptions;
use Illuminate\Support\Str;
class SetParticipationOptionsCommand
{
public function __construct(private SetParticipationOptionsRequest $request)
{
}
public function execute(): SetParticipationOptionsResponse
{
$response = new SetParticipationOptionsResponse();
$this->request->event->participation_options = $this->normalize($this->request->questions);
$this->request->event->save();
$response->success = true;
return $response;
}
/**
* Normalisiert die rohe Fragen-Definition: trimmt Labels, generiert stabile Keys/Values und verwirft
* unvollständige Fragen (kein Label oder keine gültige Option). Keys/Values sind innerhalb ihres
* Geltungsbereichs eindeutig, damit Antworten robust referenzieren können.
*
* @param array<int, array<string, mixed>> $questions
* @return array<int, array{key: string, label: string, options: array<int, array{value: string, label: string}>}>
*/
private function normalize(array $questions): array
{
$normalized = [];
$usedKeys = [];
foreach ($questions as $question) {
$label = trim((string)($question['label'] ?? ''));
if ($label === '') {
continue;
}
$options = $this->normalizeOptions($question['options'] ?? []);
if ($options === []) {
continue;
}
$key = $this->uniqueSlug((string)($question['key'] ?? ''), $label, $usedKeys);
$usedKeys[] = $key;
$normalized[] = [
'key' => $key,
'label' => $label,
'options' => $options,
];
}
return $normalized;
}
/**
* @param array<int, array<string, mixed>> $options
* @return array<int, array{value: string, label: string}>
*/
private function normalizeOptions(mixed $options): array
{
if (!is_array($options)) {
return [];
}
$normalized = [];
$usedValues = [];
foreach ($options as $option) {
$label = trim((string)($option['label'] ?? ''));
if ($label === '') {
continue;
}
$value = $this->uniqueSlug((string)($option['value'] ?? ''), $label, $usedValues);
$usedValues[] = $value;
$normalized[] = ['value' => $value, 'label' => $label];
}
return $normalized;
}
/**
* Liefert einen eindeutigen Slug: bevorzugt den bestehenden Wert (Bestandsschutz für schon gespeicherte
* Fragen/Antworten), sonst aus dem Label abgeleitet; Kollisionen werden durchnummeriert.
*
* @param array<int, string> $used
*/
private function uniqueSlug(string $existing, string $label, array $used): string
{
$base = $existing !== '' ? $existing : Str::slug($label);
if ($base === '') {
$base = 'option';
}
$candidate = $base;
$suffix = 2;
while (in_array($candidate, $used, true)) {
$candidate = $base . '-' . $suffix;
$suffix++;
}
return $candidate;
}
}
@@ -0,0 +1,19 @@
<?php
namespace App\Domains\Event\Actions\SetParticipationOptions;
use App\Models\Event;
class SetParticipationOptionsRequest
{
public Event $event;
/** @var array<int, array<string, mixed>> Rohe Fragen-Definition aus dem Admin-Panel. */
public array $questions;
public function __construct(Event $event, array $questions)
{
$this->event = $event;
$this->questions = $questions;
}
}
@@ -0,0 +1,15 @@
<?php
namespace App\Domains\Event\Actions\SetParticipationOptions;
class SetParticipationOptionsResponse
{
public bool $success;
public ?string $message;
public function __construct()
{
$this->success = false;
$this->message = null;
}
}
@@ -27,6 +27,14 @@ class SignUpCommand {
return $response;
}
// Teilnahmeoptionen (event-spezifische Pflicht-Auswahl) gegen die Event-Definition absichern.
$participationOptionRows = $this->buildParticipationOptionRows();
if ($participationOptionRows === null) {
$response->success = false;
$response->message = 'Bitte alle Auswahlfelder ausfüllen.';
return $response;
}
$eatingHabit = match ($this->request->eating_habit) {
'vegan' => EatingHabit::EATING_HABIT_VEGAN,
'vegetarian' => EatingHabit::EATING_HABIT_VEGETARIAN,
@@ -80,8 +88,68 @@ class SignUpCommand {
]
);
$this->persistParticipationOptions($response->participant, $participationOptionRows);
$response->success = true;
return $response;
}
/**
* Validiert die Antworten gegen die am Event definierten Teilnahmeoptionen und erzeugt die zu speichernden
* Zeilen mit Label-Snapshots. Gibt null zurück, wenn eine Pflicht-Frage unbeantwortet oder eine Antwort
* ungültig ist (Absicherung gegen manipulierte Payloads).
*
* @return array<int, array{question_key: string, question_label: string, value: string, value_label: string}>|null
*/
private function buildParticipationOptionRows(): ?array
{
$rows = [];
foreach ($this->request->event->participation_options ?? [] as $question) {
$key = $question['key'] ?? null;
if ($key === null) {
continue;
}
$value = $this->request->participationOptions[$key] ?? null;
if ($value === null || $value === '') {
return null;
}
$option = null;
foreach ($question['options'] ?? [] as $candidate) {
if (($candidate['value'] ?? null) === $value) {
$option = $candidate;
break;
}
}
if ($option === null) {
return null;
}
$rows[] = [
'question_key' => $key,
'question_label' => $question['label'] ?? $key,
'value' => $value,
'value_label' => $option['label'] ?? $value,
];
}
return $rows;
}
/**
* @param array<int, array{question_key: string, question_label: string, value: string, value_label: string}> $rows
*/
private function persistParticipationOptions(\App\Models\EventParticipant $participant, array $rows): void
{
foreach ($rows as $row) {
$participant->selectedOptions()->create(array_merge($row, [
'tenant' => $this->request->event->tenant,
'event_id' => $this->request->event->id,
]));
}
}
}
@@ -46,6 +46,8 @@ class SignUpRequest {
public Amount $amount,
public ?string $paymentMethod,
public array $paymentOptions = [],
/** Antworten auf die Teilnahmeoptionen: [ question_key => value ]. */
public array $participationOptions = [],
) {
}
}