Files
crewli/api/app/Models/Person.php
bert.hausmans 85815ccb16 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>
2026-04-17 12:35:41 +02:00

157 lines
3.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Models;
use App\Enums\IdentityMatchStatus;
use App\Models\Scopes\OrganisationScope;
use Illuminate\Database\Eloquent\Builder;
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\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
{
use HasFactory;
use HasUlids;
use SoftDeletes;
/** @var string Used by OrganisationScope to determine filtering strategy */
public string $organisationScopeColumn = 'event_id';
protected static function booted(): void
{
static::addGlobalScope(new OrganisationScope());
}
protected $table = 'persons';
protected $fillable = [
'event_id',
'crowd_type_id',
'company_id',
'first_name',
'last_name',
'date_of_birth',
'email',
'phone',
'status',
'registration_source',
'is_blacklisted',
'admin_notes',
'remarks',
'custom_fields',
];
public function getFullNameAttribute(): string
{
return trim("{$this->first_name} {$this->last_name}");
}
public function getNameAttribute(): string
{
return $this->full_name;
}
protected function casts(): array
{
return [
'date_of_birth' => 'date',
'is_blacklisted' => 'boolean',
'custom_fields' => 'array',
];
}
public function event(): BelongsTo
{
return $this->belongsTo(Event::class);
}
public function crowdType(): BelongsTo
{
return $this->belongsTo(CrowdType::class);
}
public function company(): BelongsTo
{
return $this->belongsTo(Company::class);
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function crowdLists(): BelongsToMany
{
return $this->belongsToMany(CrowdList::class, 'crowd_list_persons')
->withPivot('added_at', 'added_by_user_id');
}
public function shiftAssignments(): HasMany
{
return $this->hasMany(ShiftAssignment::class);
}
public function volunteerAvailabilities(): HasMany
{
return $this->hasMany(VolunteerAvailability::class);
}
public function fieldValues(): HasMany
{
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);
}
public function identityMatches(): HasMany
{
return $this->hasMany(PersonIdentityMatch::class);
}
public function pendingIdentityMatch(): HasOne
{
return $this->hasOne(PersonIdentityMatch::class)
->where('status', IdentityMatchStatus::PENDING)
->latest();
}
public function confirmedIdentityMatch(): HasOne
{
return $this->hasOne(PersonIdentityMatch::class)
->where('status', IdentityMatchStatus::CONFIRMED)
->latest();
}
public function scopeApproved(Builder $query): Builder
{
return $query->where('status', 'approved');
}
public function scopePending(Builder $query): Builder
{
return $query->where('status', 'pending');
}
public function scopeForCrowdType(Builder $query, string $type): Builder
{
return $query->whereHas('crowdType', fn (Builder $q) => $q->where('system_type', $type));
}
}