feat: registration section preferences with show_in_registration filtering and deduplication

Add show_in_registration and registration_description columns to festival_sections.
Registration form now shows deduplicated sections by name (across sub-events),
filtered by show_in_registration=true, grouped by category with card-based UI.
Section preferences use section_name instead of section_id.
Add GET/PUT registration-settings endpoints for festival-level bulk management.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-10 20:03:54 +02:00
parent 3400e4cc7e
commit c21bc085e9
22 changed files with 1443 additions and 104 deletions

View File

@@ -8,6 +8,7 @@ use App\Http\Controllers\Controller;
use App\Http\Requests\Api\V1\ReorderFestivalSectionsRequest;
use App\Http\Requests\Api\V1\StoreFestivalSectionRequest;
use App\Http\Requests\Api\V1\UpdateFestivalSectionRequest;
use App\Http\Requests\Api\V1\UpdateRegistrationSettingsRequest;
use App\Http\Resources\Api\V1\FestivalSectionResource;
use App\Models\Event;
use App\Models\FestivalSection;
@@ -94,6 +95,82 @@ final class FestivalSectionController extends Controller
return response()->json(null, 204);
}
public function registrationSettings(Event $event): JsonResponse
{
Gate::authorize('viewAny', [FestivalSection::class, $event]);
$sections = $this->getFestivalSections($event);
$grouped = $sections->groupBy('name')->map(function ($group) {
$first = $group->first();
return [
'name' => $first->name,
'category' => $first->category,
'icon' => $first->icon,
'show_in_registration' => $group->contains('show_in_registration', true),
'registration_description' => $group->whereNotNull('registration_description')->first()?->registration_description,
'section_count' => $group->count(),
'section_ids' => $group->pluck('id')->values()->toArray(),
];
})->values();
return response()->json(['data' => $grouped]);
}
public function updateRegistrationSettings(UpdateRegistrationSettingsRequest $request, Event $event): JsonResponse
{
Gate::authorize('create', [FestivalSection::class, $event]);
$validated = $request->validated();
$sections = $this->getFestivalSections($event);
$matching = $sections->where('name', $validated['name']);
if ($matching->isEmpty()) {
return $this->error('Sectie niet gevonden.', 404);
}
FestivalSection::whereIn('id', $matching->pluck('id'))
->update([
'show_in_registration' => $validated['show_in_registration'],
'registration_description' => $validated['registration_description'],
]);
activity('section_management')
->performedOn($event)
->causedBy(auth()->user())
->withProperties([
'section_name' => $validated['name'],
'show_in_registration' => $validated['show_in_registration'],
'sections_updated' => $matching->count(),
])
->log('section.registration_settings_updated');
// Return updated settings
return $this->registrationSettings($event);
}
/**
* Get all sections across the festival context (parent + children).
*/
private function getFestivalSections(Event $event): \Illuminate\Support\Collection
{
$eventIds = collect([$event->id]);
if ($event->isSubEvent()) {
$parentId = $event->parent_event_id;
$eventIds = Event::where('parent_event_id', $parentId)
->orWhere('id', $parentId)
->pluck('id');
} elseif ($event->hasChildren()) {
$childIds = $event->children()->pluck('id');
$eventIds = $childIds->push($event->id);
}
return FestivalSection::whereIn('event_id', $eventIds)->ordered()->get();
}
public function reorder(ReorderFestivalSectionsRequest $request, Event $event): JsonResponse
{
Gate::authorize('reorder', [FestivalSection::class, $event]);

View File

@@ -24,26 +24,30 @@ final class PublicRegistrationDataController extends Controller
$festivalEvent = $event->isSubEvent() ? $event->parent : $event;
$sectionQuery = FestivalSection::where('event_id', $festivalEvent->id)
->where(function ($query) {
$query->where('type', '!=', 'cross_event')
->orWhereNull('type');
})
->ordered();
if ($festivalEvent->isFestival() || $festivalEvent->hasChildren()) {
// Festival: get child event sections only (skip parent operational sections)
$childIds = Event::where('parent_event_id', $festivalEvent->id)->pluck('id');
if ($festivalEvent->isFestival()) {
$childIds = $festivalEvent->children()->pluck('id');
$sectionQuery->orWhere(function ($query) use ($childIds) {
$query->whereIn('event_id', $childIds)
->where(function ($q) {
$q->where('type', '!=', 'cross_event')
->orWhereNull('type');
});
});
$sections = FestivalSection::whereIn('event_id', $childIds)
->where('show_in_registration', true)
->where('type', 'standard')
->select('id', 'name', 'category', 'icon', 'registration_description')
->orderBy('category')
->orderBy('sort_order')
->get()
->unique('name')
->values();
} else {
// Flat event: all sections of the event
$sections = FestivalSection::where('event_id', $festivalEvent->id)
->where('show_in_registration', true)
->where('type', 'standard')
->select('id', 'name', 'category', 'icon', 'registration_description')
->orderBy('category')
->orderBy('sort_order')
->get();
}
$sections = $sectionQuery->get(['id', 'name', 'category', 'icon']);
$timeSlots = $festivalEvent->getAllRelevantTimeSlots()
->where('person_type', 'VOLUNTEER')
->values();
@@ -62,6 +66,7 @@ final class PublicRegistrationDataController extends Controller
'name' => $section->name,
'category' => $section->category,
'icon' => $section->icon,
'registration_description' => $section->registration_description,
]),
'time_slots' => $timeSlots->map(fn (TimeSlot $slot) => [
'id' => $slot->id,

View File

@@ -31,6 +31,8 @@ final class StoreFestivalSectionRequest extends FormRequest
'crew_accreditation_level' => ['nullable', 'string', 'max:50'],
'public_form_accreditation_level' => ['nullable', 'string', 'max:50'],
'timed_accreditations' => ['nullable', 'boolean'],
'show_in_registration' => ['nullable', 'boolean'],
'registration_description' => ['nullable', 'string', 'max:500'],
];
}

View File

@@ -30,6 +30,8 @@ final class UpdateFestivalSectionRequest extends FormRequest
'timed_accreditations' => ['sometimes', 'boolean'],
'crew_accreditation_level' => ['nullable', 'string', 'max:50'],
'public_form_accreditation_level' => ['nullable', 'string', 'max:50'],
'show_in_registration' => ['sometimes', 'boolean'],
'registration_description' => ['sometimes', 'nullable', 'string', 'max:500'],
];
}
}

View File

@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace App\Http\Requests\Api\V1;
use Illuminate\Foundation\Http\FormRequest;
final class UpdateRegistrationSettingsRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
/** @return array<string, mixed> */
public function rules(): array
{
return [
'name' => ['required', 'string', 'max:255'],
'show_in_registration' => ['required', 'boolean'],
'registration_description' => ['nullable', 'string', 'max:500'],
];
}
}

View File

@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace App\Http\Requests\Api\V1;
use Illuminate\Foundation\Http\FormRequest;
final class VolunteerRegistrationRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
protected function prepareForValidation(): void
{
$user = auth('sanctum')->user();
if ($user) {
$this->merge([
'name' => $user->name,
'email' => $user->email,
'_authenticated' => true,
]);
}
}
/** @return array<string, mixed> */
public function rules(): array
{
return [
'name' => ['required_without:_authenticated', 'string', 'max:255'],
'email' => ['required_without:_authenticated', 'email', 'max:255'],
'phone' => ['nullable', 'string', 'max:50'],
'tshirt_size' => ['nullable', 'string', 'in:XS,S,M,L,XL,XXL,XXXL'],
'first_aid' => ['nullable', 'boolean'],
'allergies' => ['nullable', 'string', 'max:500'],
'access_requirements' => ['nullable', 'string', 'max:500'],
'driving_licence' => ['nullable', 'boolean'],
'motivation' => ['nullable', 'string', 'max:1000'],
'motivation_other' => ['nullable', 'string', 'max:500'],
'section_preferences' => ['nullable', 'array', 'max:5'],
'section_preferences.*.section_name' => ['required', 'string', 'max:255'],
'section_preferences.*.priority' => ['required', 'integer', 'min:1', 'max:5'],
'availabilities' => ['nullable', 'array'],
'availabilities.*.time_slot_id' => ['required', 'ulid', 'exists:time_slots,id'],
'availabilities.*.preference_level' => ['nullable', 'integer', 'min:1', 'max:5'],
];
}
}

View File

@@ -27,6 +27,8 @@ final class FestivalSectionResource extends JsonResource
'crew_accreditation_level' => $this->crew_accreditation_level,
'public_form_accreditation_level' => $this->public_form_accreditation_level,
'timed_accreditations' => $this->timed_accreditations,
'show_in_registration' => $this->show_in_registration,
'registration_description' => $this->registration_description,
'created_at' => $this->created_at->toIso8601String(),
'shifts_count' => $this->whenCounted('shifts'),
];

View File

@@ -33,6 +33,8 @@ final class FestivalSection extends Model
'crew_accreditation_level',
'public_form_accreditation_level',
'timed_accreditations',
'show_in_registration',
'registration_description',
];
protected function casts(): array
@@ -43,6 +45,7 @@ final class FestivalSection extends Model
'added_to_timeline' => 'boolean',
'responder_self_checkin' => 'boolean',
'timed_accreditations' => 'boolean',
'show_in_registration' => 'boolean',
];
}

View File

@@ -0,0 +1,193 @@
<?php
declare(strict_types=1);
namespace App\Services;
use App\Enums\PersonStatus;
use App\Models\CrowdType;
use App\Models\Event;
use App\Models\Person;
use App\Models\User;
use App\Models\VolunteerAvailability;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
use Illuminate\Validation\ValidationException;
final class VolunteerRegistrationService
{
public function __construct(
private readonly PersonIdentityService $identityService,
) {}
/**
* @param array<string, mixed> $validated
*
* @throws ValidationException
*/
public function register(Event $event, array $validated, ?User $user): Person
{
if ($event->status !== 'registration_open') {
throw ValidationException::withMessages([
'event' => ['This event is not accepting registrations.'],
]);
}
$festivalEvent = $this->resolveFestivalEvent($event);
$email = $user?->email ?? $validated['email'];
$this->checkDuplicateRegistration($festivalEvent, $email);
$volunteerCrowdType = $this->resolveVolunteerCrowdType($event);
return DB::transaction(function () use ($festivalEvent, $validated, $user, $email, $volunteerCrowdType): Person {
$person = Person::updateOrCreate(
[
'event_id' => $festivalEvent->id,
'email' => $email,
],
[
'user_id' => $user?->id,
'crowd_type_id' => $volunteerCrowdType->id,
'name' => $user?->name ?? $validated['name'],
'phone' => $validated['phone'] ?? null,
'status' => PersonStatus::PENDING,
'custom_fields' => [
'tshirt_size' => $validated['tshirt_size'] ?? null,
'first_aid' => $validated['first_aid'] ?? false,
'allergies' => $validated['allergies'] ?? null,
'access_requirements' => $validated['access_requirements'] ?? null,
'driving_licence' => $validated['driving_licence'] ?? false,
'motivation' => $validated['motivation'] ?? null,
'motivation_other' => $validated['motivation_other'] ?? null,
'section_preferences' => collect($validated['section_preferences'] ?? [])
->map(fn ($pref) => [
'section_name' => $pref['section_name'],
'priority' => $pref['priority'],
])->toArray(),
],
]
);
$this->syncAvailabilities($person, $festivalEvent, $validated['availabilities'] ?? []);
if ($user === null) {
$this->detectIdentityMatch($person);
}
$source = $user !== null ? 'authenticated_form' : 'public_form';
$activityLogger = activity('volunteer_registration')
->performedOn($person)
->withProperties([
'source' => $source,
'event_id' => $festivalEvent->id,
'person_id' => $person->id,
'email' => $email,
]);
if ($user !== null) {
$activityLogger->causedBy($user);
}
$activityLogger->log('person.registered');
return $person;
});
}
private function resolveFestivalEvent(Event $event): Event
{
if ($event->isSubEvent()) {
return $event->parent;
}
return $event;
}
/**
* @throws ValidationException
*/
private function checkDuplicateRegistration(Event $festivalEvent, string $email): void
{
$existing = Person::where('event_id', $festivalEvent->id)
->where('email', $email)
->first();
if ($existing === null) {
return;
}
if ($existing->status !== PersonStatus::REJECTED) {
throw ValidationException::withMessages([
'email' => ['Already registered for this event.'],
]);
}
}
/**
* @throws \RuntimeException
*/
private function resolveVolunteerCrowdType(Event $event): CrowdType
{
$crowdType = CrowdType::where('organisation_id', $event->organisation_id)
->where('system_type', 'VOLUNTEER')
->first();
if ($crowdType === null) {
Log::error('No volunteer crowd type configured', [
'organisation_id' => $event->organisation_id,
'event_id' => $event->id,
]);
abort(500, 'No volunteer crowd type configured for this organisation.');
}
return $crowdType;
}
/**
* @param array<int, array<string, mixed>> $availabilities
*/
private function syncAvailabilities(Person $person, Event $festivalEvent, array $availabilities): void
{
if (empty($availabilities)) {
return;
}
VolunteerAvailability::where('person_id', $person->id)->delete();
$validTimeSlotIds = $festivalEvent->getAllRelevantTimeSlots()
->where('person_type', 'VOLUNTEER')
->pluck('id')
->toArray();
foreach ($availabilities as $availability) {
if (! in_array($availability['time_slot_id'], $validTimeSlotIds, true)) {
continue;
}
VolunteerAvailability::create([
'person_id' => $person->id,
'time_slot_id' => $availability['time_slot_id'],
'preference_level' => $availability['preference_level'] ?? 3,
'submitted_at' => now(),
]);
}
}
private function detectIdentityMatch(Person $person): void
{
if (! Schema::hasTable('person_identity_matches')) {
activity('volunteer_registration')
->performedOn($person)
->withProperties(['email' => $person->email])
->log('person.identity_match_skipped_table_missing');
return;
}
$this->identityService->detectMatchForPerson($person);
}
}