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:
19
api/app/Events/FormBuilder/FormSubmissionAnonymised.php
Normal file
19
api/app/Events/FormBuilder/FormSubmissionAnonymised.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Events\FormBuilder;
|
||||
|
||||
use App\Models\FormBuilder\FormSubmission;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
final class FormSubmissionAnonymised
|
||||
{
|
||||
use Dispatchable;
|
||||
use SerializesModels;
|
||||
|
||||
public function __construct(
|
||||
public readonly FormSubmission $submission,
|
||||
) {}
|
||||
}
|
||||
19
api/app/Events/FormBuilder/FormSubmissionArchived.php
Normal file
19
api/app/Events/FormBuilder/FormSubmissionArchived.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Events\FormBuilder;
|
||||
|
||||
use App\Models\FormBuilder\FormSubmission;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
final class FormSubmissionArchived
|
||||
{
|
||||
use Dispatchable;
|
||||
use SerializesModels;
|
||||
|
||||
public function __construct(
|
||||
public readonly FormSubmission $submission,
|
||||
) {}
|
||||
}
|
||||
19
api/app/Events/FormBuilder/FormSubmissionCreated.php
Normal file
19
api/app/Events/FormBuilder/FormSubmissionCreated.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Events\FormBuilder;
|
||||
|
||||
use App\Models\FormBuilder\FormSubmission;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
final class FormSubmissionCreated
|
||||
{
|
||||
use Dispatchable;
|
||||
use SerializesModels;
|
||||
|
||||
public function __construct(
|
||||
public readonly FormSubmission $submission,
|
||||
) {}
|
||||
}
|
||||
19
api/app/Events/FormBuilder/FormSubmissionDeleted.php
Normal file
19
api/app/Events/FormBuilder/FormSubmissionDeleted.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Events\FormBuilder;
|
||||
|
||||
use App\Models\FormBuilder\FormSubmission;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
final class FormSubmissionDeleted
|
||||
{
|
||||
use Dispatchable;
|
||||
use SerializesModels;
|
||||
|
||||
public function __construct(
|
||||
public readonly FormSubmission $submission,
|
||||
) {}
|
||||
}
|
||||
19
api/app/Events/FormBuilder/FormSubmissionDraftUpdated.php
Normal file
19
api/app/Events/FormBuilder/FormSubmissionDraftUpdated.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Events\FormBuilder;
|
||||
|
||||
use App\Models\FormBuilder\FormSubmission;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
final class FormSubmissionDraftUpdated
|
||||
{
|
||||
use Dispatchable;
|
||||
use SerializesModels;
|
||||
|
||||
public function __construct(
|
||||
public readonly FormSubmission $submission,
|
||||
) {}
|
||||
}
|
||||
21
api/app/Events/FormBuilder/FormSubmissionReviewed.php
Normal file
21
api/app/Events/FormBuilder/FormSubmissionReviewed.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Events\FormBuilder;
|
||||
|
||||
use App\Models\FormBuilder\FormSubmission;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
final class FormSubmissionReviewed
|
||||
{
|
||||
use Dispatchable;
|
||||
use SerializesModels;
|
||||
|
||||
public function __construct(
|
||||
public readonly FormSubmission $submission,
|
||||
public readonly User $reviewer,
|
||||
) {}
|
||||
}
|
||||
23
api/app/Events/FormBuilder/FormSubmissionSectionReviewed.php
Normal file
23
api/app/Events/FormBuilder/FormSubmissionSectionReviewed.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Events\FormBuilder;
|
||||
|
||||
use App\Models\FormBuilder\FormSubmission;
|
||||
use App\Models\FormBuilder\FormSubmissionSectionStatus;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
final class FormSubmissionSectionReviewed
|
||||
{
|
||||
use Dispatchable;
|
||||
use SerializesModels;
|
||||
|
||||
public function __construct(
|
||||
public readonly FormSubmission $submission,
|
||||
public readonly FormSubmissionSectionStatus $sectionStatus,
|
||||
public readonly User $reviewer,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Events\FormBuilder;
|
||||
|
||||
use App\Models\FormBuilder\FormSubmission;
|
||||
use App\Models\FormBuilder\FormSubmissionSectionStatus;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
final class FormSubmissionSectionSubmitted
|
||||
{
|
||||
use Dispatchable;
|
||||
use SerializesModels;
|
||||
|
||||
public function __construct(
|
||||
public readonly FormSubmission $submission,
|
||||
public readonly FormSubmissionSectionStatus $sectionStatus,
|
||||
) {}
|
||||
}
|
||||
19
api/app/Events/FormBuilder/FormSubmissionSubmitted.php
Normal file
19
api/app/Events/FormBuilder/FormSubmissionSubmitted.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Events\FormBuilder;
|
||||
|
||||
use App\Models\FormBuilder\FormSubmission;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
final class FormSubmissionSubmitted
|
||||
{
|
||||
use Dispatchable;
|
||||
use SerializesModels;
|
||||
|
||||
public function __construct(
|
||||
public readonly FormSubmission $submission,
|
||||
) {}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
@@ -134,6 +135,11 @@ final class Event extends Model
|
||||
return $this->hasMany(CrowdList::class);
|
||||
}
|
||||
|
||||
public function formSchemas(): MorphMany
|
||||
{
|
||||
return $this->morphMany(\App\Models\FormBuilder\FormSchema::class, 'owner');
|
||||
}
|
||||
|
||||
public function parent(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Event::class, 'parent_event_id');
|
||||
|
||||
120
api/app/Models/FormBuilder/FormField.php
Normal file
120
api/app/Models/FormBuilder/FormField.php
Normal file
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models\FormBuilder;
|
||||
|
||||
use App\Enums\FormBuilder\FormFieldDisplayWidth;
|
||||
use App\Enums\FormBuilder\FormValueStorageHint;
|
||||
use Illuminate\Database\Eloquent\Concerns\HasUlids;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
/**
|
||||
* Activity log strategy: explicit calls via logFieldChange() — no LogsActivity
|
||||
* trait. Logged events: create/delete/restore, field_type change, binding
|
||||
* change, is_pii/is_filterable toggle, structural options change.
|
||||
* See ARCH-FORM-BUILDER.md §17.1 and S1 Phase 4b.
|
||||
*
|
||||
* field_type is stored as string (not DB enum) so CustomFieldTypeRegistry
|
||||
* (ARCH §17.2) can extend it at runtime.
|
||||
*/
|
||||
final class FormField extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use HasUlids;
|
||||
use SoftDeletes;
|
||||
|
||||
protected $fillable = [
|
||||
'form_schema_id',
|
||||
'form_schema_section_id',
|
||||
'library_field_id',
|
||||
'field_type',
|
||||
'slug',
|
||||
'label',
|
||||
'help_text',
|
||||
'section',
|
||||
'options',
|
||||
'validation_rules',
|
||||
'is_required',
|
||||
'is_filterable',
|
||||
'is_portal_visible',
|
||||
'is_admin_only',
|
||||
'is_unique',
|
||||
'is_pii',
|
||||
'display_width',
|
||||
'binding',
|
||||
'conditional_logic',
|
||||
'role_restrictions',
|
||||
'translations',
|
||||
'value_storage_hint',
|
||||
'review_required',
|
||||
'sort_order',
|
||||
];
|
||||
|
||||
/** @var array<string, string> */
|
||||
protected $casts = [
|
||||
'options' => 'array',
|
||||
'validation_rules' => 'array',
|
||||
'binding' => 'array',
|
||||
'conditional_logic' => 'array',
|
||||
'role_restrictions' => 'array',
|
||||
'translations' => 'array',
|
||||
'is_required' => 'bool',
|
||||
'is_filterable' => 'bool',
|
||||
'is_portal_visible' => 'bool',
|
||||
'is_admin_only' => 'bool',
|
||||
'is_unique' => 'bool',
|
||||
'is_pii' => 'bool',
|
||||
'review_required' => 'bool',
|
||||
'display_width' => FormFieldDisplayWidth::class,
|
||||
'value_storage_hint' => FormValueStorageHint::class,
|
||||
'sort_order' => 'int',
|
||||
];
|
||||
|
||||
public function schema(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(FormSchema::class, 'form_schema_id');
|
||||
}
|
||||
|
||||
public function section(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(FormSchemaSection::class, 'form_schema_section_id');
|
||||
}
|
||||
|
||||
public function libraryField(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(FormFieldLibrary::class, 'library_field_id');
|
||||
}
|
||||
|
||||
public function values(): HasMany
|
||||
{
|
||||
return $this->hasMany(FormValue::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Nuanced activity log (ARCH §17.1; S1 Phase 4b). Callers choose which
|
||||
* events are worth logging — e.g. created/deleted/restored, field_type
|
||||
* changed (value storage changes), binding changed, is_pii toggled,
|
||||
* is_filterable toggled (triggers backfill), structural options changes.
|
||||
* NOT logged (noise): label/help_text/sort_order/conditional_logic/
|
||||
* translations.
|
||||
*
|
||||
* Bulk-fixture suppression: the activitylog.enabled config key is the
|
||||
* kill-switch. Seeders and one-shot commands wrap themselves in
|
||||
* App\Support\ActivityLog::suppressed(...). activity()->log() becomes
|
||||
* a silent no-op while disabled, so no guard is needed here.
|
||||
*
|
||||
* @param array<string, mixed> $properties
|
||||
*/
|
||||
public function logFieldChange(string $event, array $properties = []): void
|
||||
{
|
||||
activity()
|
||||
->performedOn($this)
|
||||
->withProperties($properties)
|
||||
->log($event);
|
||||
}
|
||||
}
|
||||
68
api/app/Models/FormBuilder/FormFieldLibrary.php
Normal file
68
api/app/Models/FormBuilder/FormFieldLibrary.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models\FormBuilder;
|
||||
|
||||
use App\Models\Organisation;
|
||||
use App\Models\Scopes\OrganisationScope;
|
||||
use Illuminate\Database\Eloquent\Concerns\HasUlids;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
final class FormFieldLibrary extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use HasUlids;
|
||||
|
||||
protected $table = 'form_field_library';
|
||||
|
||||
public string $organisationScopeColumn = 'organisation_id';
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::addGlobalScope(new OrganisationScope());
|
||||
}
|
||||
|
||||
protected $fillable = [
|
||||
'organisation_id',
|
||||
'name',
|
||||
'slug',
|
||||
'field_type',
|
||||
'label',
|
||||
'help_text',
|
||||
'options',
|
||||
'validation_rules',
|
||||
'default_is_required',
|
||||
'default_is_filterable',
|
||||
'default_binding',
|
||||
'translations',
|
||||
'description',
|
||||
'is_active',
|
||||
];
|
||||
|
||||
/** @var array<string, string> */
|
||||
protected $casts = [
|
||||
'options' => 'array',
|
||||
'validation_rules' => 'array',
|
||||
'default_binding' => 'array',
|
||||
'translations' => 'array',
|
||||
'default_is_required' => 'bool',
|
||||
'default_is_filterable' => 'bool',
|
||||
'is_system' => 'bool',
|
||||
'is_active' => 'bool',
|
||||
'usage_count' => 'int',
|
||||
];
|
||||
|
||||
public function organisation(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Organisation::class);
|
||||
}
|
||||
|
||||
public function fields(): HasMany
|
||||
{
|
||||
return $this->hasMany(FormField::class, 'library_field_id');
|
||||
}
|
||||
}
|
||||
150
api/app/Models/FormBuilder/FormSchema.php
Normal file
150
api/app/Models/FormBuilder/FormSchema.php
Normal file
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models\FormBuilder;
|
||||
|
||||
use App\Enums\FormBuilder\FormPurpose;
|
||||
use App\Enums\FormBuilder\FormSchemaSnapshotMode;
|
||||
use App\Enums\FormBuilder\FormSubmissionMode;
|
||||
use App\Models\Organisation;
|
||||
use App\Models\Scopes\OrganisationScope;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Concerns\HasUlids;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
/**
|
||||
* Activity log strategy: explicit calls via logSchemaChange() — no LogsActivity
|
||||
* trait (would produce noise). See ARCH-FORM-BUILDER.md §17.1 and S1 Phase 4b.
|
||||
*/
|
||||
final class FormSchema extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use HasUlids;
|
||||
use SoftDeletes;
|
||||
|
||||
public string $organisationScopeColumn = 'organisation_id';
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::addGlobalScope(new OrganisationScope());
|
||||
}
|
||||
|
||||
protected $fillable = [
|
||||
'organisation_id',
|
||||
'owner_type',
|
||||
'owner_id',
|
||||
'name',
|
||||
'slug',
|
||||
'purpose',
|
||||
'custom_purpose_slug',
|
||||
'description',
|
||||
'is_published',
|
||||
'submission_mode',
|
||||
'public_token',
|
||||
'public_token_previous',
|
||||
'public_token_rotated_at',
|
||||
'submission_deadline',
|
||||
'locale',
|
||||
'settings',
|
||||
'snapshot_mode',
|
||||
'freeze_on_submit',
|
||||
'retention_days',
|
||||
'consent_version',
|
||||
'section_level_submit',
|
||||
'auto_save_enabled',
|
||||
'max_submissions',
|
||||
'created_by_user_id',
|
||||
'last_updated_by_user_id',
|
||||
];
|
||||
|
||||
/** @var array<string, string> */
|
||||
protected $casts = [
|
||||
'purpose' => FormPurpose::class,
|
||||
'submission_mode' => FormSubmissionMode::class,
|
||||
'snapshot_mode' => FormSchemaSnapshotMode::class,
|
||||
'is_published' => 'bool',
|
||||
'freeze_on_submit' => 'bool',
|
||||
'section_level_submit' => 'bool',
|
||||
'auto_save_enabled' => 'bool',
|
||||
'settings' => 'array',
|
||||
'submission_deadline' => 'datetime',
|
||||
'public_token_rotated_at' => 'datetime',
|
||||
'edit_lock_expires_at' => 'datetime',
|
||||
'version' => 'int',
|
||||
'retention_days' => 'int',
|
||||
'max_submissions' => 'int',
|
||||
];
|
||||
|
||||
public function organisation(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Organisation::class);
|
||||
}
|
||||
|
||||
public function owner(): MorphTo
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
|
||||
public function fields(): HasMany
|
||||
{
|
||||
return $this->hasMany(FormField::class);
|
||||
}
|
||||
|
||||
public function sections(): HasMany
|
||||
{
|
||||
return $this->hasMany(FormSchemaSection::class);
|
||||
}
|
||||
|
||||
public function submissions(): HasMany
|
||||
{
|
||||
return $this->hasMany(FormSubmission::class);
|
||||
}
|
||||
|
||||
public function webhooks(): HasMany
|
||||
{
|
||||
return $this->hasMany(FormSchemaWebhook::class);
|
||||
}
|
||||
|
||||
public function createdBy(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'created_by_user_id');
|
||||
}
|
||||
|
||||
public function lastUpdatedBy(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'last_updated_by_user_id');
|
||||
}
|
||||
|
||||
public function editLockUser(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'edit_lock_user_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Nuanced activity log (ARCH §17.1; S1 Phase 4b). Callers choose which
|
||||
* events are worth logging — e.g. created/deleted/restored, published
|
||||
* toggled, purpose changed, freeze_on_submit toggled, retention_days
|
||||
* changed, consent_version changed, public_token rotated, snapshot_mode
|
||||
* changed. NOT logged (noise): name/description/slug, settings, locale.
|
||||
*
|
||||
* Bulk-fixture suppression: the activitylog.enabled config key is the
|
||||
* kill-switch. Seeders and one-shot commands wrap themselves in
|
||||
* App\Support\ActivityLog::suppressed(...). activity()->log() becomes
|
||||
* a silent no-op while disabled, so no guard is needed here.
|
||||
*
|
||||
* @param array<string, mixed> $properties
|
||||
*/
|
||||
public function logSchemaChange(string $event, array $properties = []): void
|
||||
{
|
||||
activity()
|
||||
->performedOn($this)
|
||||
->withProperties($properties)
|
||||
->log($event);
|
||||
}
|
||||
}
|
||||
60
api/app/Models/FormBuilder/FormSchemaSection.php
Normal file
60
api/app/Models/FormBuilder/FormSchemaSection.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models\FormBuilder;
|
||||
|
||||
use Illuminate\Database\Eloquent\Concerns\HasUlids;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
/**
|
||||
* Activity log strategy: log only on create/delete (ARCH §17.1, S1 Phase 4b).
|
||||
*/
|
||||
final class FormSchemaSection extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use HasUlids;
|
||||
use SoftDeletes;
|
||||
|
||||
protected $fillable = [
|
||||
'form_schema_id',
|
||||
'slug',
|
||||
'name',
|
||||
'description',
|
||||
'sort_order',
|
||||
'submit_independent',
|
||||
'depends_on_section_id',
|
||||
'required_for_schema_submit',
|
||||
];
|
||||
|
||||
/** @var array<string, string> */
|
||||
protected $casts = [
|
||||
'submit_independent' => 'bool',
|
||||
'required_for_schema_submit' => 'bool',
|
||||
'sort_order' => 'int',
|
||||
];
|
||||
|
||||
public function schema(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(FormSchema::class, 'form_schema_id');
|
||||
}
|
||||
|
||||
public function dependsOnSection(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(self::class, 'depends_on_section_id');
|
||||
}
|
||||
|
||||
public function fields(): HasMany
|
||||
{
|
||||
return $this->hasMany(FormField::class);
|
||||
}
|
||||
|
||||
public function submissionStatuses(): HasMany
|
||||
{
|
||||
return $this->hasMany(FormSubmissionSectionStatus::class);
|
||||
}
|
||||
}
|
||||
54
api/app/Models/FormBuilder/FormSchemaWebhook.php
Normal file
54
api/app/Models/FormBuilder/FormSchemaWebhook.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models\FormBuilder;
|
||||
|
||||
use Illuminate\Database\Eloquent\Concerns\HasUlids;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
/**
|
||||
* Scope discipline: organisation isolation is enforced via the parent
|
||||
* FormSchema. This model does NOT carry a direct organisation_id column,
|
||||
* and OrganisationScope's column strategies (organisation_id / event_id /
|
||||
* festival_section_id) do not cover form_schema_id — extending the scope
|
||||
* to a new strategy is out of scope for S1.
|
||||
*
|
||||
* NEVER query FormSchemaWebhook::query() without an eager constraint:
|
||||
* always go through $schema->webhooks() or join on form_schema_id. Direct
|
||||
* queries will leak across organisations.
|
||||
*/
|
||||
final class FormSchemaWebhook extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use HasUlids;
|
||||
|
||||
protected $fillable = [
|
||||
'form_schema_id',
|
||||
'name',
|
||||
'trigger_event',
|
||||
'url',
|
||||
'secret',
|
||||
'is_active',
|
||||
];
|
||||
|
||||
/** @var array<string, string> */
|
||||
protected $casts = [
|
||||
'url' => 'encrypted',
|
||||
'secret' => 'encrypted',
|
||||
'is_active' => 'bool',
|
||||
];
|
||||
|
||||
public function schema(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(FormSchema::class, 'form_schema_id');
|
||||
}
|
||||
|
||||
public function deliveries(): HasMany
|
||||
{
|
||||
return $this->hasMany(FormWebhookDelivery::class);
|
||||
}
|
||||
}
|
||||
102
api/app/Models/FormBuilder/FormSubmission.php
Normal file
102
api/app/Models/FormBuilder/FormSubmission.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models\FormBuilder;
|
||||
|
||||
use App\Enums\FormBuilder\FormSubmissionReviewStatus;
|
||||
use App\Enums\FormBuilder\FormSubmissionStatus;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Concerns\HasUlids;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
/**
|
||||
* No direct activity-log hooks on this model: lifecycle events fire from the
|
||||
* FormSubmissionService (arriving in S2) per ARCH §17.1.
|
||||
*/
|
||||
final class FormSubmission extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use HasUlids;
|
||||
use SoftDeletes;
|
||||
|
||||
protected $fillable = [
|
||||
'form_schema_id',
|
||||
'subject_type',
|
||||
'subject_id',
|
||||
'submitted_by_user_id',
|
||||
'public_submitter_name',
|
||||
'public_submitter_email',
|
||||
'public_submitter_ip',
|
||||
'public_submitter_ip_anonymised_at',
|
||||
'status',
|
||||
'review_status',
|
||||
'reviewed_by_user_id',
|
||||
'reviewed_at',
|
||||
'review_notes',
|
||||
'submitted_at',
|
||||
'schema_snapshot',
|
||||
'is_test',
|
||||
'submitted_in_locale',
|
||||
'opened_at',
|
||||
'first_interacted_at',
|
||||
'idempotency_key',
|
||||
];
|
||||
|
||||
/** @var array<string, string> */
|
||||
protected $casts = [
|
||||
'status' => FormSubmissionStatus::class,
|
||||
'review_status' => FormSubmissionReviewStatus::class,
|
||||
'schema_snapshot' => 'array',
|
||||
'is_test' => 'bool',
|
||||
'submitted_at' => 'datetime',
|
||||
'reviewed_at' => 'datetime',
|
||||
'anonymised_at' => 'datetime',
|
||||
'opened_at' => 'datetime',
|
||||
'first_interacted_at' => 'datetime',
|
||||
'public_submitter_ip_anonymised_at' => 'datetime',
|
||||
'schema_version_at_submit' => 'int',
|
||||
'submission_duration_seconds' => 'int',
|
||||
'auto_save_count' => 'int',
|
||||
];
|
||||
|
||||
public function schema(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(FormSchema::class, 'form_schema_id');
|
||||
}
|
||||
|
||||
public function subject(): MorphTo
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
|
||||
public function submittedBy(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'submitted_by_user_id');
|
||||
}
|
||||
|
||||
public function reviewedBy(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'reviewed_by_user_id');
|
||||
}
|
||||
|
||||
public function values(): HasMany
|
||||
{
|
||||
return $this->hasMany(FormValue::class);
|
||||
}
|
||||
|
||||
public function sectionStatuses(): HasMany
|
||||
{
|
||||
return $this->hasMany(FormSubmissionSectionStatus::class);
|
||||
}
|
||||
|
||||
public function delegations(): HasMany
|
||||
{
|
||||
return $this->hasMany(FormSubmissionDelegation::class);
|
||||
}
|
||||
}
|
||||
47
api/app/Models/FormBuilder/FormSubmissionDelegation.php
Normal file
47
api/app/Models/FormBuilder/FormSubmissionDelegation.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models\FormBuilder;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Concerns\HasUlids;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
final class FormSubmissionDelegation extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use HasUlids;
|
||||
|
||||
protected $fillable = [
|
||||
'form_submission_id',
|
||||
'delegated_to_user_id',
|
||||
'delegated_by_user_id',
|
||||
'granted_at',
|
||||
'revoked_at',
|
||||
'message',
|
||||
];
|
||||
|
||||
/** @var array<string, string> */
|
||||
protected $casts = [
|
||||
'granted_at' => 'datetime',
|
||||
'revoked_at' => 'datetime',
|
||||
];
|
||||
|
||||
public function submission(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(FormSubmission::class, 'form_submission_id');
|
||||
}
|
||||
|
||||
public function delegatedTo(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'delegated_to_user_id');
|
||||
}
|
||||
|
||||
public function delegatedBy(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'delegated_by_user_id');
|
||||
}
|
||||
}
|
||||
46
api/app/Models/FormBuilder/FormSubmissionSectionStatus.php
Normal file
46
api/app/Models/FormBuilder/FormSubmissionSectionStatus.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models\FormBuilder;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
final class FormSubmissionSectionStatus extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'form_submission_id',
|
||||
'form_schema_section_id',
|
||||
'status',
|
||||
'submitted_at',
|
||||
'reviewed_by_user_id',
|
||||
'reviewed_at',
|
||||
'review_notes',
|
||||
];
|
||||
|
||||
/** @var array<string, string> */
|
||||
protected $casts = [
|
||||
'submitted_at' => 'datetime',
|
||||
'reviewed_at' => 'datetime',
|
||||
];
|
||||
|
||||
public function submission(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(FormSubmission::class, 'form_submission_id');
|
||||
}
|
||||
|
||||
public function section(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(FormSchemaSection::class, 'form_schema_section_id');
|
||||
}
|
||||
|
||||
public function reviewedBy(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'reviewed_by_user_id');
|
||||
}
|
||||
}
|
||||
49
api/app/Models/FormBuilder/FormTemplate.php
Normal file
49
api/app/Models/FormBuilder/FormTemplate.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models\FormBuilder;
|
||||
|
||||
use App\Enums\FormBuilder\FormPurpose;
|
||||
use App\Models\Organisation;
|
||||
use App\Models\Scopes\OrganisationScope;
|
||||
use Illuminate\Database\Eloquent\Concerns\HasUlids;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
final class FormTemplate extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use HasUlids;
|
||||
|
||||
public string $organisationScopeColumn = 'organisation_id';
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::addGlobalScope(new OrganisationScope());
|
||||
}
|
||||
|
||||
protected $fillable = [
|
||||
'organisation_id',
|
||||
'name',
|
||||
'slug',
|
||||
'purpose',
|
||||
'description',
|
||||
'schema_snapshot',
|
||||
'is_active',
|
||||
];
|
||||
|
||||
/** @var array<string, string> */
|
||||
protected $casts = [
|
||||
'purpose' => FormPurpose::class,
|
||||
'schema_snapshot' => 'array',
|
||||
'is_active' => 'bool',
|
||||
'is_system' => 'bool',
|
||||
];
|
||||
|
||||
public function organisation(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Organisation::class);
|
||||
}
|
||||
}
|
||||
54
api/app/Models/FormBuilder/FormValue.php
Normal file
54
api/app/Models/FormBuilder/FormValue.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models\FormBuilder;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
/**
|
||||
* EAV storage row. Int PK for fast joins (ARCH §4.4). Typed columns are
|
||||
* populated by FormValueObserver based on field.value_storage_hint.
|
||||
*/
|
||||
final class FormValue extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'form_submission_id',
|
||||
'form_field_id',
|
||||
'value',
|
||||
'value_indexed',
|
||||
'value_number',
|
||||
'value_date',
|
||||
'value_bool',
|
||||
'value_anonymised',
|
||||
];
|
||||
|
||||
/** @var array<string, string> */
|
||||
protected $casts = [
|
||||
'value' => 'array',
|
||||
'value_number' => 'decimal:4',
|
||||
'value_date' => 'date',
|
||||
'value_bool' => 'bool',
|
||||
'value_anonymised' => 'bool',
|
||||
];
|
||||
|
||||
public function submission(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(FormSubmission::class, 'form_submission_id');
|
||||
}
|
||||
|
||||
public function field(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(FormField::class, 'form_field_id');
|
||||
}
|
||||
|
||||
public function options(): HasMany
|
||||
{
|
||||
return $this->hasMany(FormValueOption::class);
|
||||
}
|
||||
}
|
||||
43
api/app/Models/FormBuilder/FormValueOption.php
Normal file
43
api/app/Models/FormBuilder/FormValueOption.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models\FormBuilder;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* Filter pivot for multi-value fields (MULTISELECT, CHECKBOX_LIST, TAG_PICKER).
|
||||
* Rows rebuilt by FormValueObserver on each save. No timestamps — ephemeral,
|
||||
* always regenerated from form_values.value.
|
||||
*/
|
||||
final class FormValueOption extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = [
|
||||
'form_value_id',
|
||||
'form_field_id',
|
||||
'form_submission_id',
|
||||
'option_value',
|
||||
];
|
||||
|
||||
public function value(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(FormValue::class, 'form_value_id');
|
||||
}
|
||||
|
||||
public function field(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(FormField::class, 'form_field_id');
|
||||
}
|
||||
|
||||
public function submission(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(FormSubmission::class, 'form_submission_id');
|
||||
}
|
||||
}
|
||||
60
api/app/Models/FormBuilder/FormWebhookDelivery.php
Normal file
60
api/app/Models/FormBuilder/FormWebhookDelivery.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models\FormBuilder;
|
||||
|
||||
use App\Enums\FormBuilder\FormWebhookDeliveryStatus;
|
||||
use Illuminate\Database\Eloquent\Concerns\HasUlids;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* No Eloquent timestamps: this model carries its own lifecycle columns
|
||||
* (last_attempt_at, next_retry_at, delivered_at, failed_permanently_at).
|
||||
*/
|
||||
final class FormWebhookDelivery extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use HasUlids;
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = [
|
||||
'form_schema_webhook_id',
|
||||
'form_submission_id',
|
||||
'trigger_event',
|
||||
'status',
|
||||
'attempts',
|
||||
'last_attempt_at',
|
||||
'response_status',
|
||||
'response_body_excerpt',
|
||||
'next_retry_at',
|
||||
'delivered_at',
|
||||
'failed_permanently_at',
|
||||
'payload_snapshot',
|
||||
];
|
||||
|
||||
/** @var array<string, string> */
|
||||
protected $casts = [
|
||||
'status' => FormWebhookDeliveryStatus::class,
|
||||
'payload_snapshot' => 'array',
|
||||
'last_attempt_at' => 'datetime',
|
||||
'next_retry_at' => 'datetime',
|
||||
'delivered_at' => 'datetime',
|
||||
'failed_permanently_at' => 'datetime',
|
||||
'attempts' => 'int',
|
||||
'response_status' => 'int',
|
||||
];
|
||||
|
||||
public function webhook(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(FormSchemaWebhook::class, 'form_schema_webhook_id');
|
||||
}
|
||||
|
||||
public function submission(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(FormSubmission::class, 'form_submission_id');
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
final class Person extends Model
|
||||
@@ -109,6 +110,11 @@ final class Person extends Model
|
||||
return $this->hasMany(PersonFieldValue::class);
|
||||
}
|
||||
|
||||
public function formSubmissions(): MorphMany
|
||||
{
|
||||
return $this->morphMany(\App\Models\FormBuilder\FormSubmission::class, 'subject');
|
||||
}
|
||||
|
||||
public function sectionPreferences(): HasMany
|
||||
{
|
||||
return $this->hasMany(PersonSectionPreference::class);
|
||||
|
||||
@@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Concerns\HasUlids;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
@@ -87,6 +88,16 @@ final class User extends Authenticatable
|
||||
return $this->hasMany(UserInvitation::class, 'invited_by_user_id');
|
||||
}
|
||||
|
||||
public function profile(): HasOne
|
||||
{
|
||||
return $this->hasOne(UserProfile::class);
|
||||
}
|
||||
|
||||
public function getOrCreateProfile(): UserProfile
|
||||
{
|
||||
return $this->profile()->firstOrCreate([]);
|
||||
}
|
||||
|
||||
public function identityMatches(): HasMany
|
||||
{
|
||||
return $this->hasMany(PersonIdentityMatch::class, 'matched_user_id');
|
||||
|
||||
61
api/app/Models/UserProfile.php
Normal file
61
api/app/Models/UserProfile.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\FormBuilder\FormSubmissionStatus;
|
||||
use App\Models\FormBuilder\FormSubmission;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Concerns\HasUlids;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
final class UserProfile extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use HasUlids;
|
||||
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'bio',
|
||||
'photo_url',
|
||||
'emergency_contact_name',
|
||||
'emergency_contact_phone',
|
||||
'settings',
|
||||
];
|
||||
|
||||
/**
|
||||
* System-managed fields are deliberately NOT fillable: reliability_score,
|
||||
* is_ambassador.
|
||||
*
|
||||
* @var array<string, string>
|
||||
*/
|
||||
protected $casts = [
|
||||
'reliability_score' => 'decimal:2',
|
||||
'is_ambassador' => 'bool',
|
||||
'settings' => 'array',
|
||||
];
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Computed: the most recent submitted_at across submissions whose subject
|
||||
* is this user (non-test only). Null when user has no submissions.
|
||||
*/
|
||||
protected function lastSubmittedAt(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn () => FormSubmission::query()
|
||||
->where('subject_type', 'user')
|
||||
->where('subject_id', $this->user_id)
|
||||
->where('status', FormSubmissionStatus::SUBMITTED)
|
||||
->where('is_test', false)
|
||||
->max('submitted_at'),
|
||||
);
|
||||
}
|
||||
}
|
||||
192
api/app/Observers/FormBuilder/FormValueObserver.php
Normal file
192
api/app/Observers/FormBuilder/FormValueObserver.php
Normal file
@@ -0,0 +1,192 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Observers\FormBuilder;
|
||||
|
||||
use App\Enums\FormBuilder\FormFieldType;
|
||||
use App\Enums\FormBuilder\FormValueStorageHint;
|
||||
use App\Models\FormBuilder\FormField;
|
||||
use App\Models\FormBuilder\FormValue;
|
||||
use App\Models\FormBuilder\FormValueOption;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* Populates typed columns (value_indexed / value_number / value_date /
|
||||
* value_bool) on FormValue upsert, and rebuilds the form_value_options
|
||||
* pivot for multi-value filterable fields. See ARCH §7.2.
|
||||
*
|
||||
* The caller SHOULD eager-load the related FormField; we memoise on the
|
||||
* observer instance to avoid N+1 when saving many values in one batch.
|
||||
*
|
||||
* @var array<string, FormField|null>
|
||||
*/
|
||||
final class FormValueObserver
|
||||
{
|
||||
/** @var array<string, FormField|null> Memoised field lookups for this observer lifetime. */
|
||||
private array $fieldCache = [];
|
||||
|
||||
public function saving(FormValue $value): void
|
||||
{
|
||||
$field = $this->resolveField($value);
|
||||
if ($field === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->resetTypedColumns($value);
|
||||
|
||||
// value stores the canonical payload (always JSON). Typed columns
|
||||
// are derived from it based on the field's storage hint.
|
||||
$raw = $value->value;
|
||||
$scalar = $this->extractScalar($raw);
|
||||
|
||||
match ($field->value_storage_hint) {
|
||||
FormValueStorageHint::STRING => $value->value_indexed = $this->truncateIndexed($scalar),
|
||||
FormValueStorageHint::NUMBER => $value->value_number = is_numeric($scalar) ? (float) $scalar : null,
|
||||
FormValueStorageHint::DATE => $value->value_date = $this->castDate($scalar),
|
||||
FormValueStorageHint::BOOL => $value->value_bool = $scalar === null ? null : (bool) $scalar,
|
||||
FormValueStorageHint::JSON => null,
|
||||
};
|
||||
|
||||
// Single-value filterable fields: ensure value_indexed is populated
|
||||
// regardless of storage hint, so filter queries hit one index.
|
||||
if ($field->is_filterable && ! $this->isMultiValueType($field)) {
|
||||
if ($value->value_indexed === null) {
|
||||
$value->value_indexed = $this->truncateIndexed($scalar);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function saved(FormValue $value): void
|
||||
{
|
||||
$field = $this->resolveField($value);
|
||||
if ($field === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $field->is_filterable) {
|
||||
// Not filterable — ensure no stale pivot rows linger.
|
||||
FormValueOption::where('form_value_id', $value->id)->delete();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $this->isMultiValueType($field)) {
|
||||
return;
|
||||
}
|
||||
|
||||
FormValueOption::where('form_value_id', $value->id)->delete();
|
||||
|
||||
$options = $this->extractOptions($value->value);
|
||||
if ($options === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
$rows = array_map(fn (string $opt): array => [
|
||||
'form_value_id' => $value->id,
|
||||
'form_field_id' => $value->form_field_id,
|
||||
'form_submission_id' => $value->form_submission_id,
|
||||
'option_value' => Str::limit($opt, 255, ''),
|
||||
], $options);
|
||||
|
||||
FormValueOption::insert($rows);
|
||||
}
|
||||
|
||||
public function deleted(FormValue $value): void
|
||||
{
|
||||
// Cascade handles FK delete at DB layer, but pivot rows without
|
||||
// cascade-parent are cheap to clean explicitly.
|
||||
FormValueOption::where('form_value_id', $value->id)->delete();
|
||||
}
|
||||
|
||||
private function resolveField(FormValue $value): ?FormField
|
||||
{
|
||||
if ($value->relationLoaded('field')) {
|
||||
return $value->getRelation('field');
|
||||
}
|
||||
|
||||
$key = (string) $value->form_field_id;
|
||||
if (! array_key_exists($key, $this->fieldCache)) {
|
||||
$this->fieldCache[$key] = FormField::query()->find($value->form_field_id);
|
||||
}
|
||||
|
||||
return $this->fieldCache[$key];
|
||||
}
|
||||
|
||||
private function resetTypedColumns(FormValue $value): void
|
||||
{
|
||||
$value->value_indexed = null;
|
||||
$value->value_number = null;
|
||||
$value->value_date = null;
|
||||
$value->value_bool = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $raw
|
||||
*/
|
||||
private function extractScalar($raw): ?string
|
||||
{
|
||||
if ($raw === null || $raw === []) {
|
||||
return null;
|
||||
}
|
||||
if (is_scalar($raw)) {
|
||||
return (string) $raw;
|
||||
}
|
||||
// Conventional shape: { "value": <scalar> }
|
||||
if (is_array($raw) && array_key_exists('value', $raw) && is_scalar($raw['value'])) {
|
||||
return (string) $raw['value'];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $raw
|
||||
* @return array<int, string>
|
||||
*/
|
||||
private function extractOptions($raw): array
|
||||
{
|
||||
if (is_array($raw)) {
|
||||
// MULTISELECT / CHECKBOX_LIST: list of scalars OR { value: [...] }
|
||||
if (array_is_list($raw)) {
|
||||
return array_values(array_map(fn ($v) => (string) $v, array_filter($raw, 'is_scalar')));
|
||||
}
|
||||
if (array_key_exists('value', $raw) && is_array($raw['value'])) {
|
||||
return array_values(array_map(fn ($v) => (string) $v, array_filter($raw['value'], 'is_scalar')));
|
||||
}
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
private function truncateIndexed(?string $value): ?string
|
||||
{
|
||||
if ($value === null) {
|
||||
return null;
|
||||
}
|
||||
if (mb_strlen($value) > 255) {
|
||||
return mb_substr($value, 0, 255);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
private function castDate(?string $value): ?string
|
||||
{
|
||||
if ($value === null || $value === '') {
|
||||
return null;
|
||||
}
|
||||
$ts = strtotime($value);
|
||||
|
||||
return $ts === false ? null : date('Y-m-d', $ts);
|
||||
}
|
||||
|
||||
private function isMultiValueType(FormField $field): bool
|
||||
{
|
||||
return in_array($field->field_type, [
|
||||
FormFieldType::MULTISELECT->value,
|
||||
FormFieldType::CHECKBOX_LIST->value,
|
||||
FormFieldType::TAG_PICKER->value,
|
||||
], true);
|
||||
}
|
||||
}
|
||||
@@ -4,9 +4,42 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Models\Company;
|
||||
use App\Models\CrowdList;
|
||||
use App\Models\CrowdType;
|
||||
use App\Models\EmailChangeRequest;
|
||||
use App\Models\EmailLog;
|
||||
use App\Models\Event;
|
||||
use App\Models\FestivalSection;
|
||||
use App\Models\ImpersonationSession;
|
||||
use App\Models\Location;
|
||||
use App\Models\MfaBackupCode;
|
||||
use App\Models\MfaEmailCode;
|
||||
use App\Models\Organisation;
|
||||
use App\Models\OrganisationEmailSettings;
|
||||
use App\Models\OrganisationEmailTemplate;
|
||||
use App\Models\Person;
|
||||
use App\Models\PersonFieldValue;
|
||||
use App\Models\PersonIdentityMatch;
|
||||
use App\Models\PersonSectionPreference;
|
||||
use App\Models\PersonTag;
|
||||
use App\Models\RegistrationFieldTemplate;
|
||||
use App\Models\RegistrationFormField;
|
||||
use App\Models\Shift;
|
||||
use App\Models\ShiftAssignment;
|
||||
use App\Models\ShiftWaitlist;
|
||||
use App\Models\TimeSlot;
|
||||
use App\Models\TrustedDevice;
|
||||
use App\Models\User;
|
||||
use App\Models\UserInvitation;
|
||||
use App\Models\UserOrganisationTag;
|
||||
use App\Models\UserProfile;
|
||||
use App\Models\FormBuilder\FormValue;
|
||||
use App\Models\VolunteerAvailability;
|
||||
use App\Observers\FormBuilder\FormValueObserver;
|
||||
use App\Observers\PersonObserver;
|
||||
use Illuminate\Auth\Notifications\ResetPassword;
|
||||
use Illuminate\Database\Eloquent\Relations\Relation;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Spatie\Activitylog\Models\Activity;
|
||||
|
||||
@@ -19,7 +52,52 @@ class AppServiceProvider extends ServiceProvider
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
// Morph map: explicit keys for every class that can end up in a
|
||||
// polymorphic column. Before S1 there were no morphTo/morphMany
|
||||
// relations, but spatie/activitylog stores subject/causer as morph
|
||||
// columns — so every model passed to performedOn()/causedBy() MUST
|
||||
// be registered. Keep form-builder subject_types in sync with
|
||||
// config/form_subjects.php.
|
||||
Relation::enforceMorphMap([
|
||||
// Form-builder subject types
|
||||
'event' => Event::class,
|
||||
'user' => User::class,
|
||||
'user_profile' => UserProfile::class,
|
||||
'person' => Person::class,
|
||||
'company' => Company::class,
|
||||
'organisation' => Organisation::class,
|
||||
// 'artist' added when artist module lands
|
||||
|
||||
// Additional models used as activity-log subjects/causers
|
||||
'crowd_list' => CrowdList::class,
|
||||
'crowd_type' => CrowdType::class,
|
||||
'email_change_request' => EmailChangeRequest::class,
|
||||
'email_log' => EmailLog::class,
|
||||
'festival_section' => FestivalSection::class,
|
||||
'impersonation_session' => ImpersonationSession::class,
|
||||
'location' => Location::class,
|
||||
'mfa_backup_code' => MfaBackupCode::class,
|
||||
'mfa_email_code' => MfaEmailCode::class,
|
||||
'organisation_email_settings' => OrganisationEmailSettings::class,
|
||||
'organisation_email_template' => OrganisationEmailTemplate::class,
|
||||
'person_field_value' => PersonFieldValue::class,
|
||||
'person_identity_match' => PersonIdentityMatch::class,
|
||||
'person_section_preference' => PersonSectionPreference::class,
|
||||
'person_tag' => PersonTag::class,
|
||||
'registration_field_template' => RegistrationFieldTemplate::class,
|
||||
'registration_form_field' => RegistrationFormField::class,
|
||||
'shift' => Shift::class,
|
||||
'shift_assignment' => ShiftAssignment::class,
|
||||
'shift_waitlist' => ShiftWaitlist::class,
|
||||
'time_slot' => TimeSlot::class,
|
||||
'trusted_device' => TrustedDevice::class,
|
||||
'user_invitation' => UserInvitation::class,
|
||||
'user_organisation_tag' => UserOrganisationTag::class,
|
||||
'volunteer_availability' => VolunteerAvailability::class,
|
||||
]);
|
||||
|
||||
Person::observe(PersonObserver::class);
|
||||
FormValue::observe(FormValueObserver::class);
|
||||
|
||||
ResetPassword::createUrlUsing(function ($user, string $token) {
|
||||
return config('crewli.portal_url') . '/wachtwoord-resetten?token=' . $token . '&email=' . urlencode($user->email);
|
||||
|
||||
@@ -112,7 +112,7 @@ final class OrganisationDashboardService
|
||||
private function recentActivity(Organisation $organisation): array
|
||||
{
|
||||
return Activity::query()
|
||||
->where('subject_type', Organisation::class)
|
||||
->where('subject_type', (new Organisation)->getMorphClass())
|
||||
->where('subject_id', $organisation->id)
|
||||
->with('causer')
|
||||
->latest()
|
||||
|
||||
38
api/app/Support/ActivityLog.php
Normal file
38
api/app/Support/ActivityLog.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
/**
|
||||
* Dev-only helper for suppressing activity-log writes during bulk fixture
|
||||
* runs (DevSeeder, data-migration commands). Flips spatie's global
|
||||
* `activitylog.enabled` config key for the duration of the callback and
|
||||
* restores whatever it was before.
|
||||
*
|
||||
* Suppresses BOTH our explicit calls (FormSchema::logSchemaChange etc.)
|
||||
* AND spatie's LogsActivity trait on Organisation — both respect the
|
||||
* `activitylog.enabled` config key via ActivityLogger::log().
|
||||
*
|
||||
* Do NOT use in production request paths. Intended for artisan commands.
|
||||
*/
|
||||
final class ActivityLog
|
||||
{
|
||||
/**
|
||||
* @template T
|
||||
*
|
||||
* @param callable(): T $callback
|
||||
* @return T
|
||||
*/
|
||||
public static function suppressed(callable $callback): mixed
|
||||
{
|
||||
$previous = config('activitylog.enabled');
|
||||
config(['activitylog.enabled' => false]);
|
||||
|
||||
try {
|
||||
return $callback();
|
||||
} finally {
|
||||
config(['activitylog.enabled' => $previous]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user