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:
193
api/app/Services/VolunteerRegistrationService.php
Normal file
193
api/app/Services/VolunteerRegistrationService.php
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user