Adds UserObserver::created() that firstOrCreate's a user_profiles row for every User. Registered in AppServiceProvider alongside PersonObserver. Covers DevSeeder (3 scattered User::create sites: DatabaseSeeder super admin, DevSeeder org staff, DevSeeder volunteer users) and all future creation paths (invite/register/import) with zero per-caller boilerplate. New FormBuilderDevSeeder seeder class holds canonical 16-field registration template (borrowed from the legacy RegistrationFieldTemplateService list so test data stays recognisable). Produces per-org: - 16 form_templates (system, schema_snapshot per ARCH §4.6.1) - 1 FormSchema per event (event_registration, owner=event, draft_single mode, is_published mirrors event.status lifecycle) - 16 FormFields per schema - 1 FormSubmission per person whose status ∈ applied/approved/no_show (same rule as MigrateLegacyFormsData), with 6 realistic FormValues each DevSeeder::run() now wraps the whole seed body in ActivityLog::suppressed(...) so the ~80 field creates + ~277 submission lifecycle triggers don't flood activity_log. Also removes the legacy RegistrationFieldTemplateService::seedSystemTemplates call — the 16 system templates now land directly in form_templates. Post-seed totals (dev DB): 5 form_schemas, 80 form_fields, 277 form_submissions, 1662 form_values, 16 form_templates, 270 user_profiles (1:1 with users). forms:verify-data-integrity on freshly seeded DB: exit 0. php artisan test: 910/910. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
232 lines
12 KiB
PHP
232 lines
12 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use App\Enums\FormBuilder\FormFieldType;
|
|
use App\Enums\FormBuilder\FormPurpose;
|
|
use App\Enums\FormBuilder\FormSchemaSnapshotMode;
|
|
use App\Enums\FormBuilder\FormSubmissionMode;
|
|
use App\Enums\FormBuilder\FormSubmissionStatus;
|
|
use App\Enums\FormBuilder\FormValueStorageHint;
|
|
use App\Models\Event;
|
|
use App\Models\FormBuilder\FormField;
|
|
use App\Models\FormBuilder\FormSchema;
|
|
use App\Models\FormBuilder\FormSubmission;
|
|
use App\Models\FormBuilder\FormTemplate;
|
|
use App\Models\FormBuilder\FormValue;
|
|
use App\Models\Organisation;
|
|
use App\Models\Person;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* Seeds the form-builder layer for DevSeeder. Replaces the legacy
|
|
* RegistrationFieldTemplateService::seedSystemTemplates call — produces 16
|
|
* system form_templates per org, one FormSchema per event, canonical 16
|
|
* fields per schema, and a realistic set of FormSubmissions for
|
|
* approved/applied/no_show persons (matching the migration command rule).
|
|
*
|
|
* Callers MUST wrap this in ActivityLog::suppressed() — every field create
|
|
* would otherwise fire a logFieldChange("created") entry.
|
|
*/
|
|
final class FormBuilderDevSeeder
|
|
{
|
|
/**
|
|
* 16 canonical registration fields mirroring the legacy system templates.
|
|
* Used by both seedSystemTemplates() and seedEventSchema() so the shape
|
|
* stays aligned.
|
|
*
|
|
* @return list<array<string, mixed>>
|
|
*/
|
|
public static function canonicalFields(): array
|
|
{
|
|
return [
|
|
['type' => FormFieldType::HEADING, 'slug' => 'persoonlijke-voorkeuren', 'label' => 'Persoonlijke voorkeuren', 'help_text' => 'Vertel ons wat we over jou moeten weten', 'display_width' => 'full'],
|
|
['type' => FormFieldType::SELECT, 'slug' => 'shirtmaat', 'label' => 'Shirtmaat', 'options' => ['XS', 'S', 'M', 'L', 'XL', 'XXL', 'XXXL'], 'is_filterable' => true, 'display_width' => 'half'],
|
|
['type' => FormFieldType::MULTISELECT, 'slug' => 'dieetwensen', 'label' => 'Dieetwensen', 'options' => ['Vegetarisch', 'Veganistisch', 'Halal', 'Glutenvrij', 'Lactosevrij', "Geen pinda's", 'Geen noten'], 'is_filterable' => true, 'display_width' => 'half'],
|
|
['type' => FormFieldType::HEADING, 'slug' => 'vergoeding', 'label' => 'Vergoeding', 'help_text' => 'Kies hoe je wilt worden bedankt voor je inzet', 'display_width' => 'full'],
|
|
['type' => FormFieldType::RADIO, 'slug' => 'vergoedingstype', 'label' => 'Vergoedingstype', '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'],
|
|
['type' => FormFieldType::HEADING, 'slug' => 'noodcontact', 'label' => 'Noodcontact', 'help_text' => 'Wie kunnen we bereiken bij calamiteiten?', 'display_width' => 'full'],
|
|
['type' => FormFieldType::TEXT, 'slug' => 'noodcontact-naam', 'label' => 'Noodcontact naam', 'is_pii' => true, 'display_width' => 'half'],
|
|
['type' => FormFieldType::TEXT, 'slug' => 'noodcontact-telefoon', 'label' => 'Noodcontact telefoon', 'is_pii' => true, 'display_width' => 'half'],
|
|
['type' => FormFieldType::HEADING, 'slug' => 'ervaring-vaardigheden', 'label' => 'Ervaring & vaardigheden', 'help_text' => "Welke diploma's en skills heb je?", 'display_width' => 'full'],
|
|
['type' => FormFieldType::BOOLEAN, 'slug' => 'ehbo-bhv-diploma', 'label' => 'EHBO / BHV diploma', 'is_filterable' => true, 'display_width' => 'half'],
|
|
['type' => FormFieldType::BOOLEAN, 'slug' => 'rijbewijs', 'label' => 'Rijbewijs', 'is_filterable' => true, 'display_width' => 'half'],
|
|
['type' => FormFieldType::BOOLEAN, 'slug' => 'eerder-vrijwilliger-geweest', 'label' => 'Eerder vrijwilliger geweest', 'is_filterable' => true, 'display_width' => 'half'],
|
|
['type' => FormFieldType::TAG_PICKER, 'slug' => 'certificaten-vaardigheden', 'label' => 'Certificaten & vaardigheden', 'is_filterable' => true, 'display_width' => 'full'],
|
|
['type' => FormFieldType::HEADING, 'slug' => 'aanvullende-informatie', 'label' => 'Aanvullende informatie', 'display_width' => 'full'],
|
|
['type' => FormFieldType::BOOLEAN, 'slug' => 'toestemming-gegevensverwerking', 'label' => 'Toestemming gegevensverwerking', '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).', 'is_required' => true, 'display_width' => 'full'],
|
|
['type' => FormFieldType::TEXTAREA, 'slug' => 'opmerkingen', 'label' => 'Opmerkingen', 'display_width' => 'full'],
|
|
];
|
|
}
|
|
|
|
public static function seedSystemTemplates(Organisation $org): int
|
|
{
|
|
$count = 0;
|
|
foreach (self::canonicalFields() as $sortOrder => $field) {
|
|
$snapshot = [
|
|
'schema_version' => 1,
|
|
'snapshot_created_at' => now()->toIso8601String(),
|
|
'schema' => [
|
|
'name' => $field['label'].' (template)',
|
|
'slug' => $field['slug'],
|
|
'purpose' => FormPurpose::EVENT_REGISTRATION->value,
|
|
'description' => null,
|
|
'locale' => 'nl',
|
|
'freeze_on_submit' => false,
|
|
'section_level_submit' => false,
|
|
'settings' => [],
|
|
],
|
|
'sections' => [],
|
|
'fields' => [[
|
|
'id' => (string) Str::ulid(),
|
|
'slug' => $field['slug'],
|
|
'field_type' => $field['type']->value,
|
|
'label' => $field['label'],
|
|
'help_text' => $field['help_text'] ?? null,
|
|
'section_slug' => null,
|
|
'options' => $field['options'] ?? null,
|
|
'validation_rules' => null,
|
|
'is_required' => $field['is_required'] ?? false,
|
|
'is_filterable' => $field['is_filterable'] ?? false,
|
|
'is_pii' => $field['is_pii'] ?? false,
|
|
'binding' => null,
|
|
'conditional_logic' => null,
|
|
'translations' => null,
|
|
'value_storage_hint' => $field['type']->recommendedValueStorageHint()->value,
|
|
'sort_order' => $sortOrder + 1,
|
|
]],
|
|
];
|
|
|
|
FormTemplate::create([
|
|
'organisation_id' => $org->id,
|
|
'name' => $field['label'],
|
|
'slug' => $field['slug'],
|
|
'purpose' => FormPurpose::EVENT_REGISTRATION,
|
|
'description' => null,
|
|
'schema_snapshot' => $snapshot,
|
|
'is_active' => true,
|
|
])->forceFill(['is_system' => true])->save();
|
|
$count++;
|
|
}
|
|
|
|
return $count;
|
|
}
|
|
|
|
/**
|
|
* Create one FormSchema (event_registration) for this event with the
|
|
* canonical 16-field set.
|
|
*/
|
|
public static function seedEventSchema(Event $event): FormSchema
|
|
{
|
|
$schema = FormSchema::create([
|
|
'organisation_id' => $event->organisation_id,
|
|
'owner_type' => 'event',
|
|
'owner_id' => $event->id,
|
|
'name' => $event->name.' — registratie',
|
|
'slug' => Str::slug($event->slug.'-registratie'),
|
|
'purpose' => FormPurpose::EVENT_REGISTRATION,
|
|
'description' => "Registratieformulier voor {$event->name}.",
|
|
'is_published' => in_array(
|
|
$event->status,
|
|
['registration_open', 'buildup', 'showday', 'teardown', 'closed'],
|
|
true
|
|
),
|
|
'submission_mode' => FormSubmissionMode::DRAFT_SINGLE,
|
|
'locale' => 'nl',
|
|
'snapshot_mode' => FormSchemaSnapshotMode::NEVER,
|
|
'freeze_on_submit' => false,
|
|
'section_level_submit' => false,
|
|
'auto_save_enabled' => false,
|
|
]);
|
|
|
|
foreach (self::canonicalFields() as $sortOrder => $field) {
|
|
FormField::create([
|
|
'form_schema_id' => $schema->id,
|
|
'field_type' => $field['type']->value,
|
|
'slug' => $field['slug'],
|
|
'label' => $field['label'],
|
|
'help_text' => $field['help_text'] ?? null,
|
|
'options' => $field['options'] ?? null,
|
|
'is_required' => $field['is_required'] ?? false,
|
|
'is_filterable' => $field['is_filterable'] ?? false,
|
|
'is_portal_visible' => true,
|
|
'is_admin_only' => false,
|
|
'is_pii' => $field['is_pii'] ?? false,
|
|
'display_width' => $field['display_width'] ?? 'full',
|
|
'value_storage_hint' => $field['type']->recommendedValueStorageHint(),
|
|
'sort_order' => $sortOrder + 1,
|
|
]);
|
|
}
|
|
|
|
return $schema;
|
|
}
|
|
|
|
/**
|
|
* For each person with status ∈ applied/approved/no_show on this event,
|
|
* create one FormSubmission with a realistic handful of FormValues.
|
|
* Status follows the same rule as MigrateLegacyFormsData.
|
|
*/
|
|
public static function seedSubmissionsForEvent(Event $event, FormSchema $schema): int
|
|
{
|
|
$fields = $schema->fields()->get()->keyBy('slug');
|
|
$persons = Person::where('event_id', $event->id)
|
|
->whereIn('status', ['applied', 'approved', 'no_show'])
|
|
->get();
|
|
|
|
$count = 0;
|
|
foreach ($persons as $person) {
|
|
$isSubmittedStatus = in_array($person->status, ['applied', 'approved', 'no_show'], true);
|
|
$status = $isSubmittedStatus ? FormSubmissionStatus::SUBMITTED : FormSubmissionStatus::DRAFT;
|
|
|
|
$submission = FormSubmission::create([
|
|
'form_schema_id' => $schema->id,
|
|
'subject_type' => 'person',
|
|
'subject_id' => $person->id,
|
|
'submitted_by_user_id' => $person->user_id,
|
|
'status' => $status,
|
|
'is_test' => false,
|
|
'submitted_in_locale' => 'nl',
|
|
'submitted_at' => $status === FormSubmissionStatus::SUBMITTED ? now()->subDays(rand(1, 30)) : null,
|
|
'schema_version_at_submit' => $status === FormSubmissionStatus::SUBMITTED ? 1 : null,
|
|
]);
|
|
|
|
$seed = crc32((string) $person->id);
|
|
$sizes = ['XS', 'S', 'M', 'L', 'XL', 'XXL'];
|
|
self::createValueIfField($fields, 'shirtmaat', ['value' => $sizes[$seed % 6]], $submission);
|
|
self::createValueIfField($fields, 'dieetwensen', ($seed % 3 === 0) ? ['Vegetarisch'] : [], $submission);
|
|
self::createValueIfField($fields, 'rijbewijs', ['value' => ($seed % 2) === 0], $submission);
|
|
self::createValueIfField($fields, 'ehbo-bhv-diploma', ['value' => ($seed % 4) === 0], $submission);
|
|
self::createValueIfField($fields, 'noodcontact-naam', ['value' => 'Partner / Familielid'], $submission);
|
|
self::createValueIfField($fields, 'toestemming-gegevensverwerking', ['value' => true], $submission);
|
|
|
|
$count++;
|
|
}
|
|
|
|
return $count;
|
|
}
|
|
|
|
/**
|
|
* @param \Illuminate\Support\Collection<string, FormField> $fields
|
|
* @param mixed $value
|
|
*/
|
|
private static function createValueIfField($fields, string $slug, $value, FormSubmission $submission): void
|
|
{
|
|
$field = $fields->get($slug);
|
|
if ($field === null) {
|
|
return;
|
|
}
|
|
|
|
FormValue::create([
|
|
'form_submission_id' => $submission->id,
|
|
'form_field_id' => $field->id,
|
|
'value' => $value,
|
|
]);
|
|
}
|
|
}
|