Files
crewli/api/app/Models/PersonIdentityMatch.php
bert.hausmans eb1a0ac666 feat: complete person identity matching system with fuzzy detection, revert, and manual link
Implements the full identity matching engine: email matching (HIGH confidence),
fuzzy name matching with Levenshtein distance (MEDIUM confidence, upgradable to
HIGH with DOB tiebreaker), manual link/unlink, revert confirmed matches, and
automatic detection via PersonObserver. Includes 33 comprehensive tests, frontend
integration with confirm/dismiss/unlink UI, and match indicators in the persons list.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-14 08:44:24 +02:00

104 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Models;
use App\Enums\IdentityMatchConfidence;
use App\Enums\IdentityMatchMethod;
use App\Enums\IdentityMatchStatus;
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;
final class PersonIdentityMatch extends Model
{
use HasFactory;
use HasUlids;
public const UPDATED_AT = null;
protected $fillable = [
'person_id',
'matched_user_id',
'matched_on',
'confidence',
'status',
'match_details',
'confirmed_by_user_id',
'confirmed_at',
'dismissed_by_user_id',
'dismissed_at',
'reverted_by_user_id',
'reverted_at',
'resolved_by_user_id',
'resolved_at',
];
protected function casts(): array
{
return [
'matched_on' => IdentityMatchMethod::class,
'confidence' => IdentityMatchConfidence::class,
'status' => IdentityMatchStatus::class,
'match_details' => 'array',
'confirmed_at' => 'datetime',
'dismissed_at' => 'datetime',
'reverted_at' => 'datetime',
'resolved_at' => 'datetime',
];
}
public function person(): BelongsTo
{
return $this->belongsTo(Person::class);
}
public function matchedUser(): BelongsTo
{
return $this->belongsTo(User::class, 'matched_user_id');
}
public function resolvedBy(): BelongsTo
{
return $this->belongsTo(User::class, 'resolved_by_user_id');
}
public function confirmedBy(): BelongsTo
{
return $this->belongsTo(User::class, 'confirmed_by_user_id');
}
public function dismissedBy(): BelongsTo
{
return $this->belongsTo(User::class, 'dismissed_by_user_id');
}
public function revertedBy(): BelongsTo
{
return $this->belongsTo(User::class, 'reverted_by_user_id');
}
public function scopePending(Builder $query): Builder
{
return $query->where('status', IdentityMatchStatus::PENDING);
}
public function scopeConfirmed(Builder $query): Builder
{
return $query->where('status', IdentityMatchStatus::CONFIRMED);
}
public function scopeDismissed(Builder $query): Builder
{
return $query->where('status', IdentityMatchStatus::DISMISSED);
}
public function scopeForEvent(Builder $query, string $eventId): Builder
{
return $query->whereHas('person', fn (Builder $q) => $q->where('event_id', $eventId));
}
}