feat(forms): add Eloquent models, observer, events, activity-log helpers
Phase 4 of S1.
Models (app/Models/FormBuilder/): FormSchema, FormSchemaSection, FormField,
FormSubmission, FormValue, FormValueOption, FormTemplate, FormFieldLibrary,
FormSchemaWebhook, FormWebhookDelivery, FormSubmissionSectionStatus,
FormSubmissionDelegation. Plus UserProfile at app/Models/ (user-universal).
OrganisationScope applied on: FormSchema, FormTemplate, FormFieldLibrary.
FormSchemaWebhook documents inherited-scope discipline (OrganisationScope's
strategies — organisation_id/event_id/festival_section_id — don't cover
form_schema_id; direct queries would leak across orgs, so must go via
$schema->webhooks()).
User::profile()/getOrCreateProfile(), Event::formSchemas() (morphMany),
Person::formSubmissions() (morphMany).
Morph map enforced in AppServiceProvider with 28 keys covering every model
that appears as activitylog subject/causer. Also updated
OrganisationDashboardService (and its test) to query activitylog via
getMorphClass() instead of FQCN.
Activity log strategy: nuanced explicit calls (logSchemaChange on FormSchema,
logFieldChange on FormField) — no LogsActivity trait. Suppression for bulk
fixtures via App\Support\ActivityLog::suppressed(fn() => ...) which flips
config('activitylog.enabled') around a callback. Both our explicit calls
and spatie's trait on Organisation respect the flag via ActivityLogger::log().
FormValueObserver (app/Observers/FormBuilder/) populates value_indexed/
value_number/value_date/value_bool on save per field.value_storage_hint,
rebuilds form_value_options pivot on multi-value filterable fields, cleans
up on delete. Memoised field cache avoids N+1. Registered in AppServiceProvider.
9 lightweight event classes (app/Events/FormBuilder/) as SerializesModels
containers — submission lifecycle signatures lock in for S2 services, no
listeners yet.
Factories for all models with Dutch fake data (fake('nl_NL')). FormSchema
factory uses defaultSubmissionMode(); FormField factory uses
recommendedValueStorageHint().
Tests: 9 new observer tests (all pass); full suite 910/910 (up from 901).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
76
api/database/factories/FormBuilder/FormFieldFactory.php
Normal file
76
api/database/factories/FormBuilder/FormFieldFactory.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Database\Factories\FormBuilder;
|
||||
|
||||
use App\Enums\FormBuilder\FormFieldDisplayWidth;
|
||||
use App\Enums\FormBuilder\FormFieldType;
|
||||
use App\Models\FormBuilder\FormField;
|
||||
use App\Models\FormBuilder\FormSchema;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/** @extends Factory<FormField> */
|
||||
final class FormFieldFactory extends Factory
|
||||
{
|
||||
protected $model = FormField::class;
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function definition(): array
|
||||
{
|
||||
$fieldType = fake()->randomElement([
|
||||
FormFieldType::TEXT,
|
||||
FormFieldType::TEXTAREA,
|
||||
FormFieldType::EMAIL,
|
||||
FormFieldType::NUMBER,
|
||||
FormFieldType::BOOLEAN,
|
||||
FormFieldType::SELECT,
|
||||
]);
|
||||
$label = fake('nl_NL')->randomElement([
|
||||
'Voornaam', 'Achternaam', 'E-mail', 'Telefoon', 'Opmerkingen',
|
||||
'Shirtmaat', 'Allergieën', 'Motivatie', 'Geboortedatum',
|
||||
]);
|
||||
|
||||
return [
|
||||
'form_schema_id' => FormSchema::factory(),
|
||||
'form_schema_section_id' => null,
|
||||
'library_field_id' => null,
|
||||
'field_type' => $fieldType->value,
|
||||
'slug' => Str::slug($label).'-'.Str::lower(Str::random(4)),
|
||||
'label' => $label,
|
||||
'help_text' => fake()->boolean(30) ? fake('nl_NL')->sentence() : null,
|
||||
'options' => $fieldType === FormFieldType::SELECT
|
||||
? ['Optie A', 'Optie B', 'Optie C']
|
||||
: null,
|
||||
'validation_rules' => null,
|
||||
'is_required' => fake()->boolean(40),
|
||||
'is_filterable' => false,
|
||||
'is_portal_visible' => true,
|
||||
'is_admin_only' => false,
|
||||
'is_unique' => false,
|
||||
'is_pii' => false,
|
||||
'display_width' => FormFieldDisplayWidth::FULL,
|
||||
'binding' => null,
|
||||
'conditional_logic' => null,
|
||||
'role_restrictions' => null,
|
||||
'translations' => null,
|
||||
'value_storage_hint' => $fieldType->recommendedValueStorageHint(),
|
||||
'review_required' => false,
|
||||
'sort_order' => 0,
|
||||
];
|
||||
}
|
||||
|
||||
public function ofType(FormFieldType $type): static
|
||||
{
|
||||
return $this->state(fn () => [
|
||||
'field_type' => $type->value,
|
||||
'value_storage_hint' => $type->recommendedValueStorageHint(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function filterable(): static
|
||||
{
|
||||
return $this->state(fn () => ['is_filterable' => true]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Database\Factories\FormBuilder;
|
||||
|
||||
use App\Enums\FormBuilder\FormFieldType;
|
||||
use App\Models\FormBuilder\FormFieldLibrary;
|
||||
use App\Models\Organisation;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/** @extends Factory<FormFieldLibrary> */
|
||||
final class FormFieldLibraryFactory extends Factory
|
||||
{
|
||||
protected $model = FormFieldLibrary::class;
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function definition(): array
|
||||
{
|
||||
$name = fake('nl_NL')->randomElement([
|
||||
'Shirtmaat (standaard)', 'Dieet (standaard)',
|
||||
'Noodcontact (standaard)', 'Motivatie (standaard)',
|
||||
]);
|
||||
|
||||
return [
|
||||
'organisation_id' => Organisation::factory(),
|
||||
'name' => $name,
|
||||
'slug' => Str::slug($name).'-'.Str::lower(Str::random(4)),
|
||||
'field_type' => FormFieldType::TEXT->value,
|
||||
'label' => fake('nl_NL')->words(2, true),
|
||||
'help_text' => null,
|
||||
'options' => null,
|
||||
'validation_rules' => null,
|
||||
'default_is_required' => false,
|
||||
'default_is_filterable' => false,
|
||||
'default_binding' => null,
|
||||
'translations' => null,
|
||||
'description' => fake('nl_NL')->sentence(),
|
||||
'is_active' => true,
|
||||
];
|
||||
}
|
||||
|
||||
public function system(): static
|
||||
{
|
||||
return $this->state(fn () => ['is_system' => true]);
|
||||
}
|
||||
}
|
||||
72
api/database/factories/FormBuilder/FormSchemaFactory.php
Normal file
72
api/database/factories/FormBuilder/FormSchemaFactory.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Database\Factories\FormBuilder;
|
||||
|
||||
use App\Enums\FormBuilder\FormPurpose;
|
||||
use App\Enums\FormBuilder\FormSchemaSnapshotMode;
|
||||
use App\Enums\FormBuilder\FormSubmissionMode;
|
||||
use App\Models\FormBuilder\FormSchema;
|
||||
use App\Models\Organisation;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/** @extends Factory<FormSchema> */
|
||||
final class FormSchemaFactory extends Factory
|
||||
{
|
||||
protected $model = FormSchema::class;
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function definition(): array
|
||||
{
|
||||
$purpose = fake()->randomElement([
|
||||
FormPurpose::EVENT_REGISTRATION,
|
||||
FormPurpose::FEEDBACK,
|
||||
FormPurpose::INCIDENT_REPORT,
|
||||
FormPurpose::USER_PROFILE,
|
||||
]);
|
||||
$name = 'Formulier '.fake('nl_NL')->words(2, true);
|
||||
|
||||
return [
|
||||
'organisation_id' => Organisation::factory(),
|
||||
'owner_type' => null,
|
||||
'owner_id' => null,
|
||||
'name' => $name,
|
||||
'slug' => Str::slug($name).'-'.Str::lower(Str::random(4)),
|
||||
'purpose' => $purpose,
|
||||
'custom_purpose_slug' => null,
|
||||
'description' => fake('nl_NL')->sentence(),
|
||||
'is_published' => false,
|
||||
'submission_mode' => $purpose->defaultSubmissionMode(),
|
||||
'locale' => 'nl',
|
||||
'snapshot_mode' => FormSchemaSnapshotMode::NEVER,
|
||||
'freeze_on_submit' => false,
|
||||
'section_level_submit' => false,
|
||||
'auto_save_enabled' => false,
|
||||
];
|
||||
}
|
||||
|
||||
public function custom(string $slug): static
|
||||
{
|
||||
return $this->state(fn () => [
|
||||
'purpose' => FormPurpose::CUSTOM,
|
||||
'submission_mode' => FormSubmissionMode::SINGLE,
|
||||
'custom_purpose_slug' => $slug,
|
||||
]);
|
||||
}
|
||||
|
||||
public function published(): static
|
||||
{
|
||||
return $this->state(fn () => ['is_published' => true]);
|
||||
}
|
||||
|
||||
public function forPurpose(FormPurpose $purpose): static
|
||||
{
|
||||
return $this->state(fn () => [
|
||||
'purpose' => $purpose,
|
||||
'submission_mode' => $purpose->defaultSubmissionMode(),
|
||||
'custom_purpose_slug' => $purpose === FormPurpose::CUSTOM ? 'custom-'.Str::lower(Str::random(6)) : null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Database\Factories\FormBuilder;
|
||||
|
||||
use App\Models\FormBuilder\FormSchema;
|
||||
use App\Models\FormBuilder\FormSchemaSection;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/** @extends Factory<FormSchemaSection> */
|
||||
final class FormSchemaSectionFactory extends Factory
|
||||
{
|
||||
protected $model = FormSchemaSection::class;
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function definition(): array
|
||||
{
|
||||
$name = fake('nl_NL')->randomElement([
|
||||
'Algemene gegevens', 'Contactgegevens', 'Technische rider',
|
||||
'Productie', 'Catering', 'Transport', 'Accreditatie',
|
||||
]);
|
||||
|
||||
return [
|
||||
'form_schema_id' => FormSchema::factory(),
|
||||
'slug' => Str::slug($name).'-'.Str::lower(Str::random(4)),
|
||||
'name' => $name,
|
||||
'description' => fake('nl_NL')->sentence(),
|
||||
'sort_order' => 0,
|
||||
'submit_independent' => true,
|
||||
'required_for_schema_submit' => true,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Database\Factories\FormBuilder;
|
||||
|
||||
use App\Models\FormBuilder\FormSchema;
|
||||
use App\Models\FormBuilder\FormSchemaWebhook;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/** @extends Factory<FormSchemaWebhook> */
|
||||
final class FormSchemaWebhookFactory extends Factory
|
||||
{
|
||||
protected $model = FormSchemaWebhook::class;
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'form_schema_id' => FormSchema::factory(),
|
||||
'name' => 'Webhook '.fake()->words(2, true),
|
||||
'trigger_event' => 'submission_submitted',
|
||||
'url' => 'https://webhook.example.com/'.Str::lower(Str::random(12)),
|
||||
'secret' => Str::random(32),
|
||||
'is_active' => true,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Database\Factories\FormBuilder;
|
||||
|
||||
use App\Models\FormBuilder\FormSubmission;
|
||||
use App\Models\FormBuilder\FormSubmissionDelegation;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/** @extends Factory<FormSubmissionDelegation> */
|
||||
final class FormSubmissionDelegationFactory extends Factory
|
||||
{
|
||||
protected $model = FormSubmissionDelegation::class;
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'form_submission_id' => FormSubmission::factory(),
|
||||
'delegated_to_user_id' => User::factory(),
|
||||
'delegated_by_user_id' => User::factory(),
|
||||
'granted_at' => now(),
|
||||
'message' => fake('nl_NL')->sentence(),
|
||||
];
|
||||
}
|
||||
}
|
||||
43
api/database/factories/FormBuilder/FormSubmissionFactory.php
Normal file
43
api/database/factories/FormBuilder/FormSubmissionFactory.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Database\Factories\FormBuilder;
|
||||
|
||||
use App\Enums\FormBuilder\FormSubmissionStatus;
|
||||
use App\Models\FormBuilder\FormSchema;
|
||||
use App\Models\FormBuilder\FormSubmission;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/** @extends Factory<FormSubmission> */
|
||||
final class FormSubmissionFactory extends Factory
|
||||
{
|
||||
protected $model = FormSubmission::class;
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'form_schema_id' => FormSchema::factory(),
|
||||
'subject_type' => null,
|
||||
'subject_id' => null,
|
||||
'submitted_by_user_id' => null,
|
||||
'status' => FormSubmissionStatus::DRAFT,
|
||||
'is_test' => false,
|
||||
'submitted_in_locale' => 'nl',
|
||||
];
|
||||
}
|
||||
|
||||
public function submitted(): static
|
||||
{
|
||||
return $this->state(fn () => [
|
||||
'status' => FormSubmissionStatus::SUBMITTED,
|
||||
'submitted_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function test(): static
|
||||
{
|
||||
return $this->state(fn () => ['is_test' => true]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Database\Factories\FormBuilder;
|
||||
|
||||
use App\Models\FormBuilder\FormSchemaSection;
|
||||
use App\Models\FormBuilder\FormSubmission;
|
||||
use App\Models\FormBuilder\FormSubmissionSectionStatus;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/** @extends Factory<FormSubmissionSectionStatus> */
|
||||
final class FormSubmissionSectionStatusFactory extends Factory
|
||||
{
|
||||
protected $model = FormSubmissionSectionStatus::class;
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'form_submission_id' => FormSubmission::factory(),
|
||||
'form_schema_section_id' => FormSchemaSection::factory(),
|
||||
'status' => 'draft',
|
||||
];
|
||||
}
|
||||
}
|
||||
52
api/database/factories/FormBuilder/FormTemplateFactory.php
Normal file
52
api/database/factories/FormBuilder/FormTemplateFactory.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Database\Factories\FormBuilder;
|
||||
|
||||
use App\Enums\FormBuilder\FormPurpose;
|
||||
use App\Models\FormBuilder\FormTemplate;
|
||||
use App\Models\Organisation;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/** @extends Factory<FormTemplate> */
|
||||
final class FormTemplateFactory extends Factory
|
||||
{
|
||||
protected $model = FormTemplate::class;
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function definition(): array
|
||||
{
|
||||
$name = 'Template '.fake('nl_NL')->words(2, true);
|
||||
|
||||
return [
|
||||
'organisation_id' => Organisation::factory(),
|
||||
'name' => $name,
|
||||
'slug' => Str::slug($name).'-'.Str::lower(Str::random(4)),
|
||||
'purpose' => FormPurpose::EVENT_REGISTRATION,
|
||||
'description' => fake('nl_NL')->sentence(),
|
||||
'schema_snapshot' => [
|
||||
'schema_version' => 1,
|
||||
'snapshot_created_at' => now()->toIso8601String(),
|
||||
'schema' => [
|
||||
'name' => $name,
|
||||
'slug' => Str::slug($name),
|
||||
'purpose' => FormPurpose::EVENT_REGISTRATION->value,
|
||||
'locale' => 'nl',
|
||||
'freeze_on_submit' => false,
|
||||
'section_level_submit' => false,
|
||||
'settings' => [],
|
||||
],
|
||||
'sections' => [],
|
||||
'fields' => [],
|
||||
],
|
||||
'is_active' => true,
|
||||
];
|
||||
}
|
||||
|
||||
public function system(): static
|
||||
{
|
||||
return $this->state(fn () => ['is_system' => true]);
|
||||
}
|
||||
}
|
||||
27
api/database/factories/FormBuilder/FormValueFactory.php
Normal file
27
api/database/factories/FormBuilder/FormValueFactory.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Database\Factories\FormBuilder;
|
||||
|
||||
use App\Models\FormBuilder\FormField;
|
||||
use App\Models\FormBuilder\FormSubmission;
|
||||
use App\Models\FormBuilder\FormValue;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/** @extends Factory<FormValue> */
|
||||
final class FormValueFactory extends Factory
|
||||
{
|
||||
protected $model = FormValue::class;
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'form_submission_id' => FormSubmission::factory(),
|
||||
'form_field_id' => FormField::factory(),
|
||||
'value' => ['value' => fake('nl_NL')->word()],
|
||||
'value_anonymised' => false,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Database\Factories\FormBuilder;
|
||||
|
||||
use App\Models\FormBuilder\FormField;
|
||||
use App\Models\FormBuilder\FormSubmission;
|
||||
use App\Models\FormBuilder\FormValue;
|
||||
use App\Models\FormBuilder\FormValueOption;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/** @extends Factory<FormValueOption> */
|
||||
final class FormValueOptionFactory extends Factory
|
||||
{
|
||||
protected $model = FormValueOption::class;
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'form_value_id' => FormValue::factory(),
|
||||
'form_field_id' => FormField::factory(),
|
||||
'form_submission_id' => FormSubmission::factory(),
|
||||
'option_value' => fake()->word(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Database\Factories\FormBuilder;
|
||||
|
||||
use App\Enums\FormBuilder\FormWebhookDeliveryStatus;
|
||||
use App\Models\FormBuilder\FormSchemaWebhook;
|
||||
use App\Models\FormBuilder\FormSubmission;
|
||||
use App\Models\FormBuilder\FormWebhookDelivery;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/** @extends Factory<FormWebhookDelivery> */
|
||||
final class FormWebhookDeliveryFactory extends Factory
|
||||
{
|
||||
protected $model = FormWebhookDelivery::class;
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'form_schema_webhook_id' => FormSchemaWebhook::factory(),
|
||||
'form_submission_id' => FormSubmission::factory(),
|
||||
'trigger_event' => 'submission_submitted',
|
||||
'status' => FormWebhookDeliveryStatus::PENDING,
|
||||
'attempts' => 0,
|
||||
'payload_snapshot' => ['event' => 'submission_submitted'],
|
||||
];
|
||||
}
|
||||
}
|
||||
33
api/database/factories/UserProfileFactory.php
Normal file
33
api/database/factories/UserProfileFactory.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\UserProfile;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/** @extends Factory<UserProfile> */
|
||||
final class UserProfileFactory extends Factory
|
||||
{
|
||||
/** @return array<string, mixed> */
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'user_id' => User::factory(),
|
||||
'bio' => fake('nl_NL')->sentence(10),
|
||||
'photo_url' => null,
|
||||
'emergency_contact_name' => fake('nl_NL')->name(),
|
||||
'emergency_contact_phone' => fake('nl_NL')->phoneNumber(),
|
||||
'reliability_score' => fake()->randomFloat(2, 3.00, 5.00),
|
||||
'is_ambassador' => false,
|
||||
'settings' => null,
|
||||
];
|
||||
}
|
||||
|
||||
public function ambassador(): static
|
||||
{
|
||||
return $this->state(fn () => ['is_ambassador' => true]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user