S2a: purge legacy Form Builder PHP code and routes
This commit is contained in:
@@ -357,9 +357,6 @@ final class PersonIdentityService
|
||||
]);
|
||||
});
|
||||
|
||||
// Sync registration tags
|
||||
$this->syncRegistrationTags($person);
|
||||
|
||||
activity('identity')
|
||||
->causedBy($resolvedBy)
|
||||
->performedOn($person)
|
||||
@@ -498,9 +495,6 @@ final class PersonIdentityService
|
||||
]);
|
||||
});
|
||||
|
||||
// Sync registration tags
|
||||
$this->syncRegistrationTags($person);
|
||||
|
||||
activity('identity')
|
||||
->causedBy($linkedBy)
|
||||
->performedOn($person)
|
||||
@@ -550,23 +544,4 @@ final class PersonIdentityService
|
||||
return $person->fresh();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync registration tags when identity is confirmed.
|
||||
*/
|
||||
private function syncRegistrationTags(Person $person): void
|
||||
{
|
||||
if ($person->user_id === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
app(TagSyncService::class)->syncFromRegistration($person);
|
||||
} catch (\Exception $e) {
|
||||
Log::warning('Failed to sync registration tags on identity confirm', [
|
||||
'person_id' => $person->id,
|
||||
'user_id' => $person->user_id,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Person;
|
||||
use App\Models\PersonSectionPreference;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
final class PersonSectionPreferenceService
|
||||
{
|
||||
public function getPreferences(Person $person): Collection
|
||||
{
|
||||
return PersonSectionPreference::where('person_id', $person->id)
|
||||
->with('festivalSection')
|
||||
->orderBy('priority')
|
||||
->get();
|
||||
}
|
||||
|
||||
public function replacePreferences(Person $person, array $preferences): void
|
||||
{
|
||||
$old = PersonSectionPreference::where('person_id', $person->id)->get()->toArray();
|
||||
|
||||
DB::transaction(function () use ($person, $preferences): void {
|
||||
PersonSectionPreference::where('person_id', $person->id)->delete();
|
||||
|
||||
foreach ($preferences as $pref) {
|
||||
PersonSectionPreference::create([
|
||||
'person_id' => $person->id,
|
||||
'festival_section_id' => $pref['festival_section_id'],
|
||||
'priority' => $pref['priority'],
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
$activityLogger = activity('section_preferences')
|
||||
->performedOn($person)
|
||||
->withProperties([
|
||||
'old' => $old,
|
||||
'attributes' => $preferences,
|
||||
]);
|
||||
|
||||
if (auth()->user()) {
|
||||
$activityLogger->causedBy(auth()->user());
|
||||
}
|
||||
|
||||
$activityLogger->log('person.section_preferences.replaced');
|
||||
}
|
||||
}
|
||||
@@ -1,215 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Enums\FieldDisplayWidth;
|
||||
use App\Enums\RegistrationFieldType;
|
||||
use App\Models\Event;
|
||||
use App\Models\Organisation;
|
||||
use App\Models\RegistrationFieldTemplate;
|
||||
use App\Models\RegistrationFormField;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
final class RegistrationFieldTemplateService
|
||||
{
|
||||
public function listForOrganisation(Organisation $organisation): Collection
|
||||
{
|
||||
return $organisation->registrationFieldTemplates()
|
||||
->ordered()
|
||||
->get();
|
||||
}
|
||||
|
||||
public function createTemplate(Organisation $organisation, array $data): RegistrationFieldTemplate
|
||||
{
|
||||
$data['slug'] = $this->generateUniqueSlug($organisation, $data['label']);
|
||||
|
||||
if (!isset($data['display_width'])) {
|
||||
$fieldType = RegistrationFieldType::from($data['field_type']);
|
||||
$data['display_width'] = FieldDisplayWidth::defaultForFieldType($fieldType)->value;
|
||||
}
|
||||
|
||||
$template = $organisation->registrationFieldTemplates()->create($data);
|
||||
|
||||
$activityLogger = activity('registration_templates')
|
||||
->performedOn($template)
|
||||
->withProperties(['attributes' => $data]);
|
||||
|
||||
if (auth()->user()) {
|
||||
$activityLogger->causedBy(auth()->user());
|
||||
}
|
||||
|
||||
$activityLogger->log('registration_template.created');
|
||||
|
||||
return $template;
|
||||
}
|
||||
|
||||
public function updateTemplate(RegistrationFieldTemplate $template, array $data): RegistrationFieldTemplate
|
||||
{
|
||||
$old = $template->toArray();
|
||||
|
||||
if (isset($data['label']) && $data['label'] !== $template->label) {
|
||||
$data['slug'] = $this->generateUniqueSlug($template->organisation, $data['label'], $template->id);
|
||||
}
|
||||
|
||||
$template->update($data);
|
||||
|
||||
$activityLogger = activity('registration_templates')
|
||||
->performedOn($template)
|
||||
->withProperties(['old' => $old, 'attributes' => $data]);
|
||||
|
||||
if (auth()->user()) {
|
||||
$activityLogger->causedBy(auth()->user());
|
||||
}
|
||||
|
||||
$activityLogger->log('registration_template.updated');
|
||||
|
||||
return $template->fresh();
|
||||
}
|
||||
|
||||
public function deleteTemplate(RegistrationFieldTemplate $template): void
|
||||
{
|
||||
if ($template->is_system) {
|
||||
$template->update(['is_active' => false]);
|
||||
|
||||
$activityLogger = activity('registration_templates')
|
||||
->performedOn($template);
|
||||
|
||||
if (auth()->user()) {
|
||||
$activityLogger->causedBy(auth()->user());
|
||||
}
|
||||
|
||||
$activityLogger->log('registration_template.deactivated');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$activityLogger = activity('registration_templates')
|
||||
->withProperties(['deleted_template' => $template->toArray()]);
|
||||
|
||||
if (auth()->user()) {
|
||||
$activityLogger->causedBy(auth()->user());
|
||||
}
|
||||
|
||||
$activityLogger->log('registration_template.deleted');
|
||||
|
||||
$template->delete();
|
||||
}
|
||||
|
||||
public function createFieldFromTemplate(Event $event, RegistrationFieldTemplate $template): RegistrationFormField
|
||||
{
|
||||
$slug = $this->generateUniqueFieldSlug($event, $template->label);
|
||||
|
||||
$maxOrder = RegistrationFormField::where('event_id', $event->id)->max('sort_order') ?? -1;
|
||||
|
||||
$field = RegistrationFormField::create([
|
||||
'event_id' => $event->id,
|
||||
'label' => $template->label,
|
||||
'slug' => $slug,
|
||||
'field_type' => $template->field_type,
|
||||
'options' => $template->options,
|
||||
'tag_categories' => $template->tag_categories,
|
||||
'is_required' => $template->is_required,
|
||||
'is_portal_visible' => $template->is_portal_visible,
|
||||
'is_admin_only' => $template->is_admin_only,
|
||||
'is_filterable' => $template->is_filterable,
|
||||
'help_text' => $template->help_text,
|
||||
'sort_order' => $maxOrder + 1,
|
||||
'display_width' => $template->display_width,
|
||||
]);
|
||||
|
||||
$activityLogger = activity('registration_fields')
|
||||
->performedOn($field)
|
||||
->withProperties(['from_template_id' => $template->id]);
|
||||
|
||||
if (auth()->user()) {
|
||||
$activityLogger->causedBy(auth()->user());
|
||||
}
|
||||
|
||||
$activityLogger->log('registration_field.created_from_template');
|
||||
|
||||
return $field;
|
||||
}
|
||||
|
||||
/**
|
||||
* Seed system templates for a newly created organisation.
|
||||
*/
|
||||
public static function seedSystemTemplates(Organisation $organisation): void
|
||||
{
|
||||
$templates = [
|
||||
['label' => 'Persoonlijke voorkeuren', 'field_type' => 'heading', 'help_text' => 'Vertel ons wat we over jou moeten weten', 'display_width' => 'full', 'sort_order' => 1],
|
||||
['label' => 'Shirtmaat', 'field_type' => 'select', 'options' => ['XS', 'S', 'M', 'L', 'XL', 'XXL', 'XXXL'], 'is_filterable' => true, 'display_width' => 'half', 'sort_order' => 2],
|
||||
['label' => 'Dieetwensen', 'field_type' => 'multiselect', 'options' => ['Vegetarisch', 'Veganistisch', 'Halal', 'Glutenvrij', 'Lactosevrij', 'Geen pinda\'s', 'Geen noten'], 'is_filterable' => true, 'display_width' => 'half', 'sort_order' => 3],
|
||||
['label' => 'Vergoeding', 'field_type' => 'heading', 'help_text' => 'Kies hoe je wilt worden bedankt voor je inzet', 'display_width' => 'full', 'sort_order' => 4],
|
||||
['label' => 'Vergoedingstype', 'field_type' => 'radio', 'options' => [
|
||||
['label' => 'Pro Deo', 'description' => 'Je werkt als vrijwilliger zonder financiële vergoeding'],
|
||||
['label' => 'Entreeticket', 'description' => 'Je ontvangt een gratis festivalticket als dank voor je inzet'],
|
||||
['label' => 'Vrijwilligersvergoeding', 'description' => 'Je ontvangt een vergoeding conform de vrijwilligersregeling'],
|
||||
], 'is_required' => true, 'display_width' => 'full', 'sort_order' => 5],
|
||||
['label' => 'Noodcontact', 'field_type' => 'heading', 'help_text' => 'Wie kunnen we bereiken bij calamiteiten?', 'display_width' => 'full', 'sort_order' => 6],
|
||||
['label' => 'Noodcontact naam', 'field_type' => 'text', 'display_width' => 'half', 'sort_order' => 7],
|
||||
['label' => 'Noodcontact telefoon', 'field_type' => 'text', 'display_width' => 'half', 'sort_order' => 8],
|
||||
['label' => 'Ervaring & vaardigheden', 'field_type' => 'heading', 'help_text' => 'Welke diploma\'s en skills heb je?', 'display_width' => 'full', 'sort_order' => 9],
|
||||
['label' => 'EHBO / BHV diploma', 'field_type' => 'boolean', 'is_filterable' => true, 'display_width' => 'half', 'sort_order' => 10],
|
||||
['label' => 'Rijbewijs', 'field_type' => 'boolean', 'is_filterable' => true, 'display_width' => 'half', 'sort_order' => 11],
|
||||
['label' => 'Eerder vrijwilliger geweest', 'field_type' => 'boolean', 'is_filterable' => true, 'display_width' => 'half', 'sort_order' => 12],
|
||||
['label' => 'Certificaten & vaardigheden', 'field_type' => 'tag_picker', 'tag_categories' => null, 'is_filterable' => true, 'display_width' => 'full', 'sort_order' => 13],
|
||||
['label' => 'Aanvullende informatie', 'field_type' => 'heading', 'display_width' => 'full', 'sort_order' => 14],
|
||||
['label' => 'Toestemming gegevensverwerking', 'field_type' => 'boolean', 'is_required' => true, 'help_text' => 'Ik geef toestemming voor de verwerking van mijn persoonsgegevens ten behoeve van de organisatie van dit evenement, conform de Algemene Verordening Gegevensbescherming (AVG).', 'display_width' => 'full', 'sort_order' => 15],
|
||||
['label' => 'Opmerkingen', 'field_type' => 'textarea', 'display_width' => 'full', 'sort_order' => 16],
|
||||
];
|
||||
|
||||
foreach ($templates as $data) {
|
||||
$organisation->registrationFieldTemplates()->create([
|
||||
...$data,
|
||||
'slug' => Str::slug($data['label']),
|
||||
'is_system' => true,
|
||||
'is_active' => true,
|
||||
'is_required' => $data['is_required'] ?? false,
|
||||
'is_filterable' => $data['is_filterable'] ?? false,
|
||||
'is_portal_visible' => true,
|
||||
'is_admin_only' => false,
|
||||
'display_width' => $data['display_width'] ?? 'full',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
private function generateUniqueSlug(Organisation $organisation, string $label, ?string $excludeId = null): string
|
||||
{
|
||||
$base = Str::slug($label);
|
||||
$slug = $base;
|
||||
$counter = 1;
|
||||
|
||||
while (true) {
|
||||
$query = RegistrationFieldTemplate::where('organisation_id', $organisation->id)
|
||||
->where('slug', $slug);
|
||||
|
||||
if ($excludeId) {
|
||||
$query->where('id', '!=', $excludeId);
|
||||
}
|
||||
|
||||
if (!$query->exists()) {
|
||||
return $slug;
|
||||
}
|
||||
|
||||
$counter++;
|
||||
$slug = "{$base}-{$counter}";
|
||||
}
|
||||
}
|
||||
|
||||
private function generateUniqueFieldSlug(Event $event, string $label): string
|
||||
{
|
||||
$base = Str::slug($label);
|
||||
$slug = $base;
|
||||
$counter = 1;
|
||||
|
||||
while (RegistrationFormField::where('event_id', $event->id)->where('slug', $slug)->exists()) {
|
||||
$counter++;
|
||||
$slug = "{$base}-{$counter}";
|
||||
}
|
||||
|
||||
return $slug;
|
||||
}
|
||||
}
|
||||
@@ -1,225 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Enums\FieldDisplayWidth;
|
||||
use App\Enums\RegistrationFieldType;
|
||||
use App\Models\Event;
|
||||
use App\Models\Person;
|
||||
use App\Models\PersonFieldValue;
|
||||
use App\Models\RegistrationFormField;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
final class RegistrationFormFieldService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly TagSyncService $tagSyncService,
|
||||
) {}
|
||||
|
||||
public function listForEvent(Event $event): Collection
|
||||
{
|
||||
return RegistrationFormField::where('event_id', $event->id)
|
||||
->ordered()
|
||||
->get();
|
||||
}
|
||||
|
||||
public function createField(Event $event, array $data): RegistrationFormField
|
||||
{
|
||||
$data['slug'] = $this->generateUniqueSlug($event, $data['label']);
|
||||
|
||||
if (!isset($data['display_width'])) {
|
||||
$fieldType = RegistrationFieldType::from($data['field_type']);
|
||||
$data['display_width'] = FieldDisplayWidth::defaultForFieldType($fieldType)->value;
|
||||
}
|
||||
|
||||
$field = RegistrationFormField::create([
|
||||
'event_id' => $event->id,
|
||||
...$data,
|
||||
]);
|
||||
|
||||
$activityLogger = activity('registration_fields')
|
||||
->performedOn($field)
|
||||
->withProperties(['attributes' => $data]);
|
||||
|
||||
if (auth()->user()) {
|
||||
$activityLogger->causedBy(auth()->user());
|
||||
}
|
||||
|
||||
$activityLogger->log('registration_field.created');
|
||||
|
||||
return $field;
|
||||
}
|
||||
|
||||
public function updateField(RegistrationFormField $field, array $data): RegistrationFormField
|
||||
{
|
||||
$old = $field->toArray();
|
||||
|
||||
if (isset($data['label']) && $data['label'] !== $field->label) {
|
||||
$data['slug'] = $this->generateUniqueSlug($field->event, $data['label'], $field->id);
|
||||
}
|
||||
|
||||
$field->update($data);
|
||||
|
||||
$activityLogger = activity('registration_fields')
|
||||
->performedOn($field)
|
||||
->withProperties(['old' => $old, 'attributes' => $data]);
|
||||
|
||||
if (auth()->user()) {
|
||||
$activityLogger->causedBy(auth()->user());
|
||||
}
|
||||
|
||||
$activityLogger->log('registration_field.updated');
|
||||
|
||||
return $field->fresh();
|
||||
}
|
||||
|
||||
public function deleteField(RegistrationFormField $field): void
|
||||
{
|
||||
$activityLogger = activity('registration_fields')
|
||||
->withProperties(['deleted_field' => $field->toArray()]);
|
||||
|
||||
if (auth()->user()) {
|
||||
$activityLogger->causedBy(auth()->user());
|
||||
}
|
||||
|
||||
$activityLogger->log('registration_field.deleted');
|
||||
|
||||
$field->delete();
|
||||
}
|
||||
|
||||
public function reorderFields(Event $event, array $orderedIds): void
|
||||
{
|
||||
DB::transaction(function () use ($event, $orderedIds): void {
|
||||
foreach ($orderedIds as $index => $id) {
|
||||
RegistrationFormField::where('id', $id)
|
||||
->where('event_id', $event->id)
|
||||
->update(['sort_order' => $index]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function upsertPersonValues(Person $person, array $values): void
|
||||
{
|
||||
$fields = RegistrationFormField::where('event_id', $person->event_id)
|
||||
->get()
|
||||
->keyBy('slug');
|
||||
|
||||
DB::transaction(function () use ($person, $values, $fields): void {
|
||||
foreach ($values as $slug => $rawValue) {
|
||||
$field = $fields->get($slug);
|
||||
if ($field === null || $field->field_type->isStructural()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$data = ['person_id' => $person->id, 'registration_form_field_id' => $field->id];
|
||||
|
||||
if ($field->isMultiValue()) {
|
||||
$data['value'] = null;
|
||||
$data['selected_options'] = is_array($rawValue) ? $rawValue : [$rawValue];
|
||||
} else {
|
||||
$data['value'] = $rawValue === null ? null : (string) $rawValue;
|
||||
$data['selected_options'] = null;
|
||||
}
|
||||
|
||||
PersonFieldValue::updateOrCreate(
|
||||
['person_id' => $person->id, 'registration_form_field_id' => $field->id],
|
||||
$data,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
$activityLogger = activity('registration_values')
|
||||
->performedOn($person)
|
||||
->withProperties(['slugs' => array_keys($values)]);
|
||||
|
||||
if (auth()->user()) {
|
||||
$activityLogger->causedBy(auth()->user());
|
||||
}
|
||||
|
||||
$activityLogger->log('person.field_values.upserted');
|
||||
|
||||
$this->tagSyncService->syncFromRegistration($person);
|
||||
}
|
||||
|
||||
public function getPersonValues(Person $person): Collection
|
||||
{
|
||||
return PersonFieldValue::where('person_id', $person->id)
|
||||
->with('registrationFormField')
|
||||
->get();
|
||||
}
|
||||
|
||||
public function importFromEvent(Event $targetEvent, Event $sourceEvent): Collection
|
||||
{
|
||||
$sourceFields = RegistrationFormField::where('event_id', $sourceEvent->id)
|
||||
->ordered()
|
||||
->get();
|
||||
|
||||
$maxOrder = RegistrationFormField::where('event_id', $targetEvent->id)->max('sort_order') ?? -1;
|
||||
|
||||
$created = collect();
|
||||
|
||||
foreach ($sourceFields as $sourceField) {
|
||||
$slug = $this->generateUniqueSlug($targetEvent, $sourceField->label);
|
||||
|
||||
$field = RegistrationFormField::create([
|
||||
'event_id' => $targetEvent->id,
|
||||
'label' => $sourceField->label,
|
||||
'slug' => $slug,
|
||||
'field_type' => $sourceField->field_type,
|
||||
'options' => $sourceField->options,
|
||||
'tag_categories' => $sourceField->tag_categories,
|
||||
'is_required' => $sourceField->is_required,
|
||||
'is_portal_visible' => $sourceField->is_portal_visible,
|
||||
'is_admin_only' => $sourceField->is_admin_only,
|
||||
'is_filterable' => $sourceField->is_filterable,
|
||||
'help_text' => $sourceField->help_text,
|
||||
'sort_order' => ++$maxOrder,
|
||||
'display_width' => $sourceField->display_width,
|
||||
]);
|
||||
|
||||
$created->push($field);
|
||||
}
|
||||
|
||||
$activityLogger = activity('registration_fields')
|
||||
->withProperties([
|
||||
'source_event_id' => $sourceEvent->id,
|
||||
'target_event_id' => $targetEvent->id,
|
||||
'fields_copied' => $created->count(),
|
||||
]);
|
||||
|
||||
if (auth()->user()) {
|
||||
$activityLogger->causedBy(auth()->user());
|
||||
}
|
||||
|
||||
$activityLogger->log('registration_field.imported_from_event');
|
||||
|
||||
return $created;
|
||||
}
|
||||
|
||||
private function generateUniqueSlug(Event $event, string $label, ?string $excludeId = null): string
|
||||
{
|
||||
$base = Str::slug($label);
|
||||
$slug = $base;
|
||||
$counter = 1;
|
||||
|
||||
while (true) {
|
||||
$query = RegistrationFormField::where('event_id', $event->id)
|
||||
->where('slug', $slug);
|
||||
|
||||
if ($excludeId) {
|
||||
$query->where('id', '!=', $excludeId);
|
||||
}
|
||||
|
||||
if (!$query->exists()) {
|
||||
return $slug;
|
||||
}
|
||||
|
||||
$counter++;
|
||||
$slug = "{$base}-{$counter}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Enums\RegistrationFieldType;
|
||||
use App\Models\Person;
|
||||
use App\Models\PersonFieldValue;
|
||||
use App\Models\RegistrationFormField;
|
||||
use App\Models\UserOrganisationTag;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* Syncs tag_picker registration field values to user_organisation_tags.
|
||||
*
|
||||
* Called from:
|
||||
* - PersonController::approve() — when a person is approved
|
||||
* - PersonIdentityService::confirmMatch() — when user_id is linked via identity matching
|
||||
* - PersonIdentityService::manualLink() — when user_id is linked manually
|
||||
*/
|
||||
final class TagSyncService
|
||||
{
|
||||
public function syncFromRegistration(Person $person): void
|
||||
{
|
||||
if ($person->user_id === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$organisationId = $person->event?->organisation_id;
|
||||
if ($organisationId === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$tagPickerFields = RegistrationFormField::where('event_id', $person->event_id)
|
||||
->where('field_type', RegistrationFieldType::TAG_PICKER)
|
||||
->pluck('id');
|
||||
|
||||
if ($tagPickerFields->isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$selectedTagIds = PersonFieldValue::where('person_id', $person->id)
|
||||
->whereIn('registration_form_field_id', $tagPickerFields)
|
||||
->whereNotNull('selected_options')
|
||||
->get()
|
||||
->flatMap(fn (PersonFieldValue $v) => $v->selected_options ?? [])
|
||||
->unique()
|
||||
->values()
|
||||
->all();
|
||||
|
||||
DB::transaction(function () use ($person, $organisationId, $selectedTagIds): void {
|
||||
$existingSelfReported = UserOrganisationTag::where('user_id', $person->user_id)
|
||||
->where('organisation_id', $organisationId)
|
||||
->where('source', 'self_reported')
|
||||
->pluck('person_tag_id')
|
||||
->all();
|
||||
|
||||
$toAdd = array_diff($selectedTagIds, $existingSelfReported);
|
||||
$toRemove = array_diff($existingSelfReported, $selectedTagIds);
|
||||
|
||||
if (!empty($toRemove)) {
|
||||
UserOrganisationTag::where('user_id', $person->user_id)
|
||||
->where('organisation_id', $organisationId)
|
||||
->where('source', 'self_reported')
|
||||
->whereIn('person_tag_id', $toRemove)
|
||||
->delete();
|
||||
}
|
||||
|
||||
foreach ($toAdd as $tagId) {
|
||||
UserOrganisationTag::create([
|
||||
'user_id' => $person->user_id,
|
||||
'organisation_id' => $organisationId,
|
||||
'person_tag_id' => $tagId,
|
||||
'source' => 'self_reported',
|
||||
'assigned_at' => now(),
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
$activityLogger = activity('tag_sync')
|
||||
->performedOn($person)
|
||||
->withProperties([
|
||||
'synced_tag_ids' => $selectedTagIds,
|
||||
'organisation_id' => $organisationId,
|
||||
]);
|
||||
|
||||
if (auth()->user()) {
|
||||
$activityLogger->causedBy(auth()->user());
|
||||
}
|
||||
|
||||
$activityLogger->log('person.tags.synced_from_registration');
|
||||
}
|
||||
}
|
||||
@@ -1,204 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Enums\PersonStatus;
|
||||
use App\Mail\RegistrationConfirmationMail;
|
||||
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\Mail;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
final class VolunteerRegistrationService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly RegistrationFormFieldService $registrationFormFieldService,
|
||||
private readonly PersonSectionPreferenceService $personSectionPreferenceService,
|
||||
private readonly TagSyncService $tagSyncService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @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 = strtolower($user?->email ?? $validated['email']);
|
||||
|
||||
$this->checkDuplicateRegistration($festivalEvent, $email);
|
||||
|
||||
$volunteerCrowdType = $this->resolveVolunteerCrowdType($event);
|
||||
|
||||
$person = DB::transaction(function () use ($festivalEvent, $validated, $user, $email, $volunteerCrowdType): Person {
|
||||
$person = Person::updateOrCreate(
|
||||
[
|
||||
'event_id' => $festivalEvent->id,
|
||||
'email' => $email,
|
||||
],
|
||||
[
|
||||
'crowd_type_id' => $volunteerCrowdType->id,
|
||||
'first_name' => $validated['first_name'] ?? $user->first_name,
|
||||
'last_name' => $validated['last_name'] ?? $user->last_name,
|
||||
'phone' => $validated['phone'] ?? null,
|
||||
'date_of_birth' => $validated['date_of_birth'] ?? null,
|
||||
'status' => PersonStatus::PENDING,
|
||||
'registration_source' => 'self',
|
||||
'custom_fields' => [
|
||||
'tshirt_size' => $validated['tshirt_size'] ?? null,
|
||||
'first_aid' => $validated['first_aid'] ?? false,
|
||||
'allergies' => $validated['allergies'] ?? null,
|
||||
'driving_licence' => $validated['driving_licence'] ?? false,
|
||||
'motivation' => $validated['motivation'] ?? null,
|
||||
'motivation_other' => $validated['motivation_other'] ?? null,
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
// Link to authenticated user directly (they already have an account)
|
||||
if ($user) {
|
||||
$person->user_id = $user->id;
|
||||
$person->save();
|
||||
}
|
||||
|
||||
$this->syncAvailabilities($person, $festivalEvent, $validated['availabilities'] ?? []);
|
||||
|
||||
if (!empty($validated['field_values'])) {
|
||||
$this->registrationFormFieldService->upsertPersonValues(
|
||||
$person,
|
||||
$validated['field_values']
|
||||
);
|
||||
}
|
||||
|
||||
if (!empty($validated['section_preferences'])) {
|
||||
$this->personSectionPreferenceService->replacePreferences(
|
||||
$person,
|
||||
$validated['section_preferences']
|
||||
);
|
||||
}
|
||||
|
||||
// Trigger tag sync if user_id is known
|
||||
if ($person->user_id) {
|
||||
$this->tagSyncService->syncFromRegistration($person);
|
||||
}
|
||||
|
||||
$source = $user ? '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) {
|
||||
$activityLogger->causedBy($user);
|
||||
}
|
||||
|
||||
$activityLogger->log('person.registered');
|
||||
|
||||
return $person;
|
||||
});
|
||||
|
||||
// Send confirmation email (queued, outside transaction)
|
||||
Mail::to($person->email)->queue(new RegistrationConfirmationMail($person, $festivalEvent));
|
||||
|
||||
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->value) {
|
||||
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(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user