feat: registration form fields, section preferences, tag sync & schema updates
Implement EAV system for dynamic event-specific registration fields with organisation-level templates, person section preferences with priority ranking, and TagSyncService for deferred tag_picker sync. New tables: registration_field_templates, registration_form_fields, person_field_values, person_section_preferences. New columns: persons.remarks, events.registration_show_section_preferences, events.registration_show_availability. 58 tests, 126 assertions — all 432 tests pass (zero regressions). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Api\V1\UpsertPersonFieldValuesRequest;
|
||||
use App\Http\Resources\Api\V1\PersonFieldValueResource;
|
||||
use App\Models\Event;
|
||||
use App\Models\Person;
|
||||
use App\Services\RegistrationFormFieldService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
|
||||
final class PersonFieldValueController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly RegistrationFormFieldService $service,
|
||||
) {}
|
||||
|
||||
public function index(Event $event, Person $person): AnonymousResourceCollection
|
||||
{
|
||||
Gate::authorize('view', [$person, $event]);
|
||||
|
||||
$values = $this->service->getPersonValues($person);
|
||||
|
||||
return PersonFieldValueResource::collection($values);
|
||||
}
|
||||
|
||||
public function upsert(UpsertPersonFieldValuesRequest $request, Event $event, Person $person): JsonResponse
|
||||
{
|
||||
Gate::authorize('update', [$person, $event]);
|
||||
|
||||
$this->service->upsertPersonValues($person, $request->validated()['values']);
|
||||
|
||||
$values = $this->service->getPersonValues($person);
|
||||
|
||||
return $this->success(PersonFieldValueResource::collection($values));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Api\V1\ReplacePersonSectionPreferencesRequest;
|
||||
use App\Http\Resources\Api\V1\PersonSectionPreferenceResource;
|
||||
use App\Models\Event;
|
||||
use App\Models\Person;
|
||||
use App\Services\PersonSectionPreferenceService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
|
||||
final class PersonSectionPreferenceController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly PersonSectionPreferenceService $service,
|
||||
) {}
|
||||
|
||||
public function index(Event $event, Person $person): AnonymousResourceCollection
|
||||
{
|
||||
Gate::authorize('view', [$person, $event]);
|
||||
|
||||
$preferences = $this->service->getPreferences($person);
|
||||
|
||||
return PersonSectionPreferenceResource::collection($preferences);
|
||||
}
|
||||
|
||||
public function replace(ReplacePersonSectionPreferencesRequest $request, Event $event, Person $person): JsonResponse
|
||||
{
|
||||
Gate::authorize('update', [$person, $event]);
|
||||
|
||||
$this->service->replacePreferences($person, $request->validated()['preferences']);
|
||||
|
||||
$preferences = $this->service->getPreferences($person);
|
||||
|
||||
return $this->success(PersonSectionPreferenceResource::collection($preferences));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Api\V1\StoreRegistrationFieldTemplateRequest;
|
||||
use App\Http\Requests\Api\V1\UpdateRegistrationFieldTemplateRequest;
|
||||
use App\Http\Resources\Api\V1\RegistrationFieldTemplateResource;
|
||||
use App\Models\Organisation;
|
||||
use App\Models\RegistrationFieldTemplate;
|
||||
use App\Services\RegistrationFieldTemplateService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
|
||||
final class RegistrationFieldTemplateController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly RegistrationFieldTemplateService $service,
|
||||
) {}
|
||||
|
||||
public function index(Organisation $organisation): AnonymousResourceCollection
|
||||
{
|
||||
Gate::authorize('viewAny', [RegistrationFieldTemplate::class, $organisation]);
|
||||
|
||||
$templates = $this->service->listForOrganisation($organisation);
|
||||
|
||||
return RegistrationFieldTemplateResource::collection($templates);
|
||||
}
|
||||
|
||||
public function store(StoreRegistrationFieldTemplateRequest $request, Organisation $organisation): JsonResponse
|
||||
{
|
||||
Gate::authorize('create', [RegistrationFieldTemplate::class, $organisation]);
|
||||
|
||||
$template = $this->service->createTemplate($organisation, $request->validated());
|
||||
|
||||
return $this->created(new RegistrationFieldTemplateResource($template));
|
||||
}
|
||||
|
||||
public function update(
|
||||
UpdateRegistrationFieldTemplateRequest $request,
|
||||
Organisation $organisation,
|
||||
RegistrationFieldTemplate $registrationFieldTemplate,
|
||||
): JsonResponse {
|
||||
Gate::authorize('update', [$registrationFieldTemplate, $organisation]);
|
||||
|
||||
$template = $this->service->updateTemplate($registrationFieldTemplate, $request->validated());
|
||||
|
||||
return $this->success(new RegistrationFieldTemplateResource($template));
|
||||
}
|
||||
|
||||
public function destroy(Organisation $organisation, RegistrationFieldTemplate $registrationFieldTemplate): JsonResponse
|
||||
{
|
||||
Gate::authorize('delete', [$registrationFieldTemplate, $organisation]);
|
||||
|
||||
$this->service->deleteTemplate($registrationFieldTemplate);
|
||||
|
||||
return response()->json(null, 204);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Api\V1\ImportFromEventRequest;
|
||||
use App\Http\Requests\Api\V1\ReorderRegistrationFormFieldsRequest;
|
||||
use App\Http\Requests\Api\V1\StoreRegistrationFormFieldRequest;
|
||||
use App\Http\Requests\Api\V1\UpdateRegistrationFormFieldRequest;
|
||||
use App\Http\Resources\Api\V1\RegistrationFormFieldResource;
|
||||
use App\Models\Event;
|
||||
use App\Models\Organisation;
|
||||
use App\Models\RegistrationFieldTemplate;
|
||||
use App\Models\RegistrationFormField;
|
||||
use App\Services\RegistrationFieldTemplateService;
|
||||
use App\Services\RegistrationFormFieldService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
|
||||
final class RegistrationFormFieldController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly RegistrationFormFieldService $service,
|
||||
private readonly RegistrationFieldTemplateService $templateService,
|
||||
) {}
|
||||
|
||||
public function index(Event $event): AnonymousResourceCollection
|
||||
{
|
||||
Gate::authorize('viewAny', [RegistrationFormField::class, $event]);
|
||||
|
||||
$fields = $this->service->listForEvent($event);
|
||||
|
||||
return RegistrationFormFieldResource::collection($fields);
|
||||
}
|
||||
|
||||
public function store(StoreRegistrationFormFieldRequest $request, Event $event): JsonResponse
|
||||
{
|
||||
Gate::authorize('create', [RegistrationFormField::class, $event]);
|
||||
|
||||
$field = $this->service->createField($event, $request->validated());
|
||||
|
||||
return $this->created(new RegistrationFormFieldResource($field));
|
||||
}
|
||||
|
||||
public function update(
|
||||
UpdateRegistrationFormFieldRequest $request,
|
||||
Event $event,
|
||||
RegistrationFormField $registrationField,
|
||||
): JsonResponse {
|
||||
Gate::authorize('update', [$registrationField, $event]);
|
||||
|
||||
$field = $this->service->updateField($registrationField, $request->validated());
|
||||
|
||||
return $this->success(new RegistrationFormFieldResource($field));
|
||||
}
|
||||
|
||||
public function destroy(Event $event, RegistrationFormField $registrationField): JsonResponse
|
||||
{
|
||||
Gate::authorize('delete', [$registrationField, $event]);
|
||||
|
||||
$this->service->deleteField($registrationField);
|
||||
|
||||
return response()->json(null, 204);
|
||||
}
|
||||
|
||||
public function reorder(ReorderRegistrationFormFieldsRequest $request, Event $event): JsonResponse
|
||||
{
|
||||
Gate::authorize('reorder', [RegistrationFormField::class, $event]);
|
||||
|
||||
$this->service->reorderFields($event, $request->validated()['ids']);
|
||||
|
||||
return response()->json(null, 204);
|
||||
}
|
||||
|
||||
public function fromTemplate(Request $request, Event $event): JsonResponse
|
||||
{
|
||||
Gate::authorize('create', [RegistrationFormField::class, $event]);
|
||||
|
||||
$request->validate([
|
||||
'template_id' => ['required', 'ulid', 'exists:registration_field_templates,id'],
|
||||
]);
|
||||
|
||||
$template = RegistrationFieldTemplate::findOrFail($request->input('template_id'));
|
||||
|
||||
if ($template->organisation_id !== $event->organisation_id) {
|
||||
return $this->error('Template does not belong to this organisation.', 422);
|
||||
}
|
||||
|
||||
$field = $this->templateService->createFieldFromTemplate($event, $template);
|
||||
|
||||
return $this->created(new RegistrationFormFieldResource($field));
|
||||
}
|
||||
|
||||
public function importFromEvent(ImportFromEventRequest $request, Event $event): JsonResponse
|
||||
{
|
||||
Gate::authorize('create', [RegistrationFormField::class, $event]);
|
||||
|
||||
$sourceEvent = Event::findOrFail($request->validated()['source_event_id']);
|
||||
|
||||
$fields = $this->service->importFromEvent($event, $sourceEvent);
|
||||
|
||||
return $this->success(RegistrationFormFieldResource::collection($fields));
|
||||
}
|
||||
}
|
||||
41
api/app/Http/Requests/Api/V1/ImportFromEventRequest.php
Normal file
41
api/app/Http/Requests/Api/V1/ImportFromEventRequest.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Api\V1;
|
||||
|
||||
use App\Models\Event;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
final class ImportFromEventRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'source_event_id' => ['required', 'ulid', 'exists:events,id'],
|
||||
];
|
||||
}
|
||||
|
||||
public function withValidator($validator): void
|
||||
{
|
||||
$validator->after(function ($validator) {
|
||||
$sourceEventId = $this->input('source_event_id');
|
||||
if (!$sourceEventId) {
|
||||
return;
|
||||
}
|
||||
|
||||
$sourceEvent = Event::find($sourceEventId);
|
||||
$targetEvent = $this->route('event');
|
||||
|
||||
if ($sourceEvent && $targetEvent && $sourceEvent->organisation_id !== $targetEvent->organisation_id) {
|
||||
$validator->errors()->add('source_event_id', 'Source event must belong to the same organisation.');
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Api\V1;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
final class ReorderRegistrationFormFieldsRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'ids' => ['required', 'array', 'min:1'],
|
||||
'ids.*' => ['required', 'ulid', 'exists:registration_form_fields,id'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Api\V1;
|
||||
|
||||
use App\Models\FestivalSection;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
final class ReplacePersonSectionPreferencesRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'preferences' => ['required', 'array', 'min:1', 'max:5'],
|
||||
'preferences.*.festival_section_id' => ['required', 'ulid'],
|
||||
'preferences.*.priority' => ['required', 'integer', 'min:1', 'max:5'],
|
||||
];
|
||||
}
|
||||
|
||||
public function withValidator($validator): void
|
||||
{
|
||||
$validator->after(function ($validator) {
|
||||
$preferences = $this->input('preferences', []);
|
||||
|
||||
if (!is_array($preferences)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Priorities must be unique
|
||||
$priorities = array_column($preferences, 'priority');
|
||||
if (count($priorities) !== count(array_unique($priorities))) {
|
||||
$validator->errors()->add('preferences', 'Priorities must be unique within the request.');
|
||||
}
|
||||
|
||||
// Validate section IDs belong to the event
|
||||
$event = $this->route('event');
|
||||
$person = $this->route('person');
|
||||
|
||||
if (!$event || !$person) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Valid sections: own event's sections + parent festival's cross_event sections
|
||||
$validSectionIds = FestivalSection::where('event_id', $event->id)
|
||||
->pluck('id');
|
||||
|
||||
if ($event->parent_event_id) {
|
||||
$parentCrossEventSections = FestivalSection::where('event_id', $event->parent_event_id)
|
||||
->where('type', 'cross_event')
|
||||
->pluck('id');
|
||||
$validSectionIds = $validSectionIds->merge($parentCrossEventSections);
|
||||
}
|
||||
|
||||
foreach ($preferences as $index => $pref) {
|
||||
$sectionId = $pref['festival_section_id'] ?? null;
|
||||
if ($sectionId && !$validSectionIds->contains($sectionId)) {
|
||||
$validator->errors()->add(
|
||||
"preferences.{$index}.festival_section_id",
|
||||
'Section does not belong to this event.'
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,8 @@ final class StoreEventRequest extends FormRequest
|
||||
'event_type' => ['nullable', 'in:event,festival,series'],
|
||||
'event_type_label' => ['nullable', 'string', 'max:50'],
|
||||
'sub_event_label' => ['nullable', 'string', 'max:50'],
|
||||
'registration_show_section_preferences' => ['nullable', 'boolean'],
|
||||
'registration_show_availability' => ['nullable', 'boolean'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ final class StorePersonRequest extends FormRequest
|
||||
'phone' => ['nullable', 'string', 'max:30'],
|
||||
'company_id' => ['nullable', 'ulid', 'exists:companies,id'],
|
||||
'status' => ['nullable', 'in:invited,applied,pending,approved,rejected,no_show'],
|
||||
'remarks' => ['nullable', 'string', 'max:5000'],
|
||||
'custom_fields' => ['nullable', 'array'],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Api\V1;
|
||||
|
||||
use App\Enums\RegistrationFieldType;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
final class StoreRegistrationFieldTemplateRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function rules(): array
|
||||
{
|
||||
$fieldType = $this->input('field_type');
|
||||
$type = RegistrationFieldType::tryFrom($fieldType);
|
||||
|
||||
return [
|
||||
'label' => ['required', 'string', 'max:255'],
|
||||
'field_type' => ['required', Rule::in(array_column(RegistrationFieldType::cases(), 'value'))],
|
||||
'options' => [
|
||||
$type?->requiresOptions() ? 'required' : 'nullable',
|
||||
$type?->prohibitsOptions() ? 'prohibited' : 'nullable',
|
||||
'array',
|
||||
],
|
||||
'options.*' => ['string', 'max:255'],
|
||||
'tag_category' => [
|
||||
$type === RegistrationFieldType::TAG_PICKER ? 'nullable' : 'prohibited',
|
||||
'string',
|
||||
'max:50',
|
||||
],
|
||||
'is_required' => ['nullable', 'boolean'],
|
||||
'is_filterable' => ['nullable', 'boolean'],
|
||||
'is_portal_visible' => ['nullable', 'boolean'],
|
||||
'is_admin_only' => ['nullable', 'boolean'],
|
||||
'section' => ['nullable', 'string', 'max:100'],
|
||||
'help_text' => ['nullable', 'string', 'max:5000'],
|
||||
'sort_order' => ['nullable', 'integer', 'min:0'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Api\V1;
|
||||
|
||||
use App\Enums\RegistrationFieldType;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
final class StoreRegistrationFormFieldRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function rules(): array
|
||||
{
|
||||
$fieldType = $this->input('field_type');
|
||||
$type = RegistrationFieldType::tryFrom($fieldType);
|
||||
|
||||
return [
|
||||
'label' => ['required', 'string', 'max:255'],
|
||||
'field_type' => ['required', Rule::in(array_column(RegistrationFieldType::cases(), 'value'))],
|
||||
'options' => [
|
||||
$type?->requiresOptions() ? 'required' : 'nullable',
|
||||
$type?->prohibitsOptions() ? 'prohibited' : 'nullable',
|
||||
'array',
|
||||
],
|
||||
'options.*' => ['string', 'max:255'],
|
||||
'tag_category' => [
|
||||
$type === RegistrationFieldType::TAG_PICKER ? 'nullable' : 'prohibited',
|
||||
'string',
|
||||
'max:50',
|
||||
],
|
||||
'is_required' => ['nullable', 'boolean'],
|
||||
'is_portal_visible' => ['nullable', 'boolean'],
|
||||
'is_admin_only' => ['nullable', 'boolean'],
|
||||
'is_filterable' => ['nullable', 'boolean'],
|
||||
'section' => ['nullable', 'string', 'max:100'],
|
||||
'help_text' => ['nullable', 'string', 'max:5000'],
|
||||
'sort_order' => ['nullable', 'integer', 'min:0'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,8 @@ final class UpdateEventRequest extends FormRequest
|
||||
'event_type_label' => ['nullable', 'string', 'max:50'],
|
||||
'sub_event_label' => ['nullable', 'string', 'max:50'],
|
||||
'registration_welcome_text' => ['nullable', 'string', 'max:1000'],
|
||||
'registration_show_section_preferences' => ['nullable', 'boolean'],
|
||||
'registration_show_availability' => ['nullable', 'boolean'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ final class UpdatePersonRequest extends FormRequest
|
||||
'status' => ['sometimes', 'in:invited,applied,pending,approved,rejected,no_show'],
|
||||
'is_blacklisted' => ['sometimes', 'boolean'],
|
||||
'admin_notes' => ['nullable', 'string'],
|
||||
'remarks' => ['nullable', 'string', 'max:5000'],
|
||||
'custom_fields' => ['nullable', 'array'],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Api\V1;
|
||||
|
||||
use App\Enums\RegistrationFieldType;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
final class UpdateRegistrationFieldTemplateRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'label' => ['sometimes', 'string', 'max:255'],
|
||||
'options' => ['nullable', 'array'],
|
||||
'options.*' => ['string', 'max:255'],
|
||||
'tag_category' => ['nullable', 'string', 'max:50'],
|
||||
'is_required' => ['nullable', 'boolean'],
|
||||
'is_filterable' => ['nullable', 'boolean'],
|
||||
'is_portal_visible' => ['nullable', 'boolean'],
|
||||
'is_admin_only' => ['nullable', 'boolean'],
|
||||
'section' => ['nullable', 'string', 'max:100'],
|
||||
'help_text' => ['nullable', 'string', 'max:5000'],
|
||||
'sort_order' => ['nullable', 'integer', 'min:0'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Api\V1;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
final class UpdateRegistrationFormFieldRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'label' => ['sometimes', 'string', 'max:255'],
|
||||
'options' => ['nullable', 'array'],
|
||||
'options.*' => ['string', 'max:255'],
|
||||
'tag_category' => ['nullable', 'string', 'max:50'],
|
||||
'is_required' => ['nullable', 'boolean'],
|
||||
'is_portal_visible' => ['nullable', 'boolean'],
|
||||
'is_admin_only' => ['nullable', 'boolean'],
|
||||
'is_filterable' => ['nullable', 'boolean'],
|
||||
'section' => ['nullable', 'string', 'max:100'],
|
||||
'help_text' => ['nullable', 'string', 'max:5000'],
|
||||
'sort_order' => ['nullable', 'integer', 'min:0'],
|
||||
];
|
||||
}
|
||||
}
|
||||
133
api/app/Http/Requests/Api/V1/UpsertPersonFieldValuesRequest.php
Normal file
133
api/app/Http/Requests/Api/V1/UpsertPersonFieldValuesRequest.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\Api\V1;
|
||||
|
||||
use App\Enums\RegistrationFieldType;
|
||||
use App\Models\PersonTag;
|
||||
use App\Models\RegistrationFormField;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
final class UpsertPersonFieldValuesRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'values' => ['required', 'array'],
|
||||
];
|
||||
}
|
||||
|
||||
public function withValidator($validator): void
|
||||
{
|
||||
$validator->after(function ($validator) {
|
||||
$values = $this->input('values', []);
|
||||
$event = $this->route('event');
|
||||
|
||||
if (!$event || !is_array($values)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$fields = RegistrationFormField::where('event_id', $event->id)
|
||||
->get()
|
||||
->keyBy('slug');
|
||||
|
||||
$orgId = $event->organisation_id;
|
||||
|
||||
foreach ($values as $slug => $value) {
|
||||
$field = $fields->get($slug);
|
||||
|
||||
if ($field === null) {
|
||||
$validator->errors()->add("values.{$slug}", "Unknown field: {$slug}");
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($field->is_required && ($value === null || $value === '' || $value === [])) {
|
||||
$validator->errors()->add("values.{$slug}", "The {$slug} field is required.");
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($value === null || $value === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
match ($field->field_type) {
|
||||
RegistrationFieldType::TEXT, RegistrationFieldType::TEXTAREA => $this->validateString($validator, $slug, $value),
|
||||
RegistrationFieldType::NUMBER => $this->validateNumber($validator, $slug, $value),
|
||||
RegistrationFieldType::BOOLEAN => $this->validateBoolean($validator, $slug, $value),
|
||||
RegistrationFieldType::SELECT, RegistrationFieldType::RADIO => $this->validateSingleOption($validator, $slug, $value, $field),
|
||||
RegistrationFieldType::MULTISELECT, RegistrationFieldType::CHECKBOX => $this->validateMultiOption($validator, $slug, $value, $field),
|
||||
RegistrationFieldType::TAG_PICKER => $this->validateTagPicker($validator, $slug, $value, $orgId),
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private function validateString($validator, string $slug, mixed $value): void
|
||||
{
|
||||
if (!is_string($value) || mb_strlen($value) > 5000) {
|
||||
$validator->errors()->add("values.{$slug}", "Must be a string (max 5000 characters).");
|
||||
}
|
||||
}
|
||||
|
||||
private function validateNumber($validator, string $slug, mixed $value): void
|
||||
{
|
||||
if (!is_numeric($value)) {
|
||||
$validator->errors()->add("values.{$slug}", "Must be a number.");
|
||||
}
|
||||
}
|
||||
|
||||
private function validateBoolean($validator, string $slug, mixed $value): void
|
||||
{
|
||||
if (!in_array($value, [true, false, 0, 1, '0', '1'], true)) {
|
||||
$validator->errors()->add("values.{$slug}", "Must be a boolean.");
|
||||
}
|
||||
}
|
||||
|
||||
private function validateSingleOption($validator, string $slug, mixed $value, RegistrationFormField $field): void
|
||||
{
|
||||
if (!is_string($value) || !in_array($value, $field->options ?? [], true)) {
|
||||
$validator->errors()->add("values.{$slug}", "Must be one of the defined options.");
|
||||
}
|
||||
}
|
||||
|
||||
private function validateMultiOption($validator, string $slug, mixed $value, RegistrationFormField $field): void
|
||||
{
|
||||
if (!is_array($value)) {
|
||||
$validator->errors()->add("values.{$slug}", "Must be an array.");
|
||||
return;
|
||||
}
|
||||
|
||||
$options = $field->options ?? [];
|
||||
foreach ($value as $item) {
|
||||
if (!in_array($item, $options, true)) {
|
||||
$validator->errors()->add("values.{$slug}", "Invalid option: {$item}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function validateTagPicker($validator, string $slug, mixed $value, string $orgId): void
|
||||
{
|
||||
if (!is_array($value)) {
|
||||
$validator->errors()->add("values.{$slug}", "Must be an array of tag IDs.");
|
||||
return;
|
||||
}
|
||||
|
||||
$validTagIds = PersonTag::where('organisation_id', $orgId)
|
||||
->where('is_active', true)
|
||||
->pluck('id')
|
||||
->all();
|
||||
|
||||
foreach ($value as $tagId) {
|
||||
if (!in_array($tagId, $validTagIds, true)) {
|
||||
$validator->errors()->add("values.{$slug}", "Invalid tag ID: {$tagId}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,8 @@ final class EventResource extends JsonResource
|
||||
'registration_banner_url' => $this->registration_banner_url,
|
||||
'registration_welcome_text' => $this->registration_welcome_text,
|
||||
'registration_logo_url' => $this->registration_logo_url,
|
||||
'registration_show_section_preferences' => $this->registration_show_section_preferences,
|
||||
'registration_show_availability' => $this->registration_show_availability,
|
||||
'is_festival' => $this->resource->isFestival(),
|
||||
'is_sub_event' => $this->resource->isSubEvent(),
|
||||
'is_flat_event' => $this->resource->isFlatEvent(),
|
||||
|
||||
34
api/app/Http/Resources/Api/V1/PersonFieldValueResource.php
Normal file
34
api/app/Http/Resources/Api/V1/PersonFieldValueResource.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources\Api\V1;
|
||||
|
||||
use App\Enums\RegistrationFieldType;
|
||||
use App\Models\PersonTag;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
final class PersonFieldValueResource extends JsonResource
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
$field = $this->registrationFormField;
|
||||
|
||||
return [
|
||||
'field_slug' => $field?->slug,
|
||||
'field_label' => $field?->label,
|
||||
'field_type' => $field?->field_type?->value,
|
||||
'value' => $this->value,
|
||||
'selected_options' => $this->selected_options,
|
||||
'tag_names' => $this->when(
|
||||
$field?->field_type === RegistrationFieldType::TAG_PICKER && !empty($this->selected_options),
|
||||
function () {
|
||||
return PersonTag::whereIn('id', $this->selected_options ?? [])
|
||||
->pluck('name')
|
||||
->all();
|
||||
}
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,7 @@ final class PersonResource extends JsonResource
|
||||
'status' => $this->status,
|
||||
'is_blacklisted' => $this->is_blacklisted,
|
||||
'admin_notes' => $this->admin_notes,
|
||||
'remarks' => $this->remarks,
|
||||
'custom_fields' => $this->custom_fields,
|
||||
'created_at' => $this->created_at->toIso8601String(),
|
||||
'crowd_type' => new CrowdTypeResource($this->whenLoaded('crowdType')),
|
||||
@@ -53,6 +54,8 @@ final class PersonResource extends JsonResource
|
||||
'added_by_user_id' => $this->pivot->added_by_user_id,
|
||||
]
|
||||
),
|
||||
'field_values' => PersonFieldValueResource::collection($this->whenLoaded('fieldValues')),
|
||||
'section_preferences' => PersonSectionPreferenceResource::collection($this->whenLoaded('sectionPreferences')),
|
||||
'tags' => $this->when(
|
||||
$this->user_id && $this->relationLoaded('user'),
|
||||
function () {
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources\Api\V1;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
final class PersonSectionPreferenceResource extends JsonResource
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
$section = $this->whenLoaded('festivalSection');
|
||||
|
||||
return [
|
||||
'festival_section_id' => $this->festival_section_id,
|
||||
'priority' => $this->priority,
|
||||
'section_name' => $this->when(
|
||||
$this->relationLoaded('festivalSection'),
|
||||
fn () => $this->festivalSection?->name
|
||||
),
|
||||
'section_icon' => $this->when(
|
||||
$this->relationLoaded('festivalSection'),
|
||||
fn () => $this->festivalSection?->icon
|
||||
),
|
||||
'section_category' => $this->when(
|
||||
$this->relationLoaded('festivalSection'),
|
||||
fn () => $this->festivalSection?->category
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources\Api\V1;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
final class RegistrationFieldTemplateResource extends JsonResource
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'organisation_id' => $this->organisation_id,
|
||||
'label' => $this->label,
|
||||
'slug' => $this->slug,
|
||||
'field_type' => $this->field_type->value,
|
||||
'options' => $this->options,
|
||||
'tag_category' => $this->tag_category,
|
||||
'is_required' => $this->is_required,
|
||||
'is_filterable' => $this->is_filterable,
|
||||
'is_portal_visible' => $this->is_portal_visible,
|
||||
'is_admin_only' => $this->is_admin_only,
|
||||
'section' => $this->section,
|
||||
'help_text' => $this->help_text,
|
||||
'sort_order' => $this->sort_order,
|
||||
'is_system' => $this->is_system,
|
||||
'is_active' => $this->is_active,
|
||||
'created_at' => $this->created_at->toIso8601String(),
|
||||
'updated_at' => $this->updated_at->toIso8601String(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources\Api\V1;
|
||||
|
||||
use App\Enums\RegistrationFieldType;
|
||||
use App\Models\PersonTag;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
final class RegistrationFormFieldResource extends JsonResource
|
||||
{
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'event_id' => $this->event_id,
|
||||
'label' => $this->label,
|
||||
'slug' => $this->slug,
|
||||
'field_type' => $this->field_type->value,
|
||||
'options' => $this->options,
|
||||
'tag_category' => $this->tag_category,
|
||||
'is_required' => $this->is_required,
|
||||
'is_portal_visible' => $this->is_portal_visible,
|
||||
'is_admin_only' => $this->is_admin_only,
|
||||
'is_filterable' => $this->is_filterable,
|
||||
'section' => $this->section,
|
||||
'help_text' => $this->help_text,
|
||||
'sort_order' => $this->sort_order,
|
||||
'created_at' => $this->created_at->toIso8601String(),
|
||||
'updated_at' => $this->updated_at->toIso8601String(),
|
||||
'available_tags' => $this->when(
|
||||
$this->field_type === RegistrationFieldType::TAG_PICKER,
|
||||
function () {
|
||||
$query = PersonTag::where('organisation_id', $this->event->organisation_id)
|
||||
->where('is_active', true);
|
||||
|
||||
if ($this->tag_category) {
|
||||
$query->where('category', $this->tag_category);
|
||||
}
|
||||
|
||||
return PersonTagResource::collection($query->orderBy('sort_order')->get());
|
||||
}
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user