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>
This commit is contained in:
@@ -36,7 +36,7 @@ final class PersonController extends Controller
|
||||
$this->verifyEventBelongsToOrganisation($organisation, $event);
|
||||
Gate::authorize('viewAny', [Person::class, $event]);
|
||||
|
||||
$query = $event->persons()->with(['crowdType', 'pendingIdentityMatch.matchedUser']);
|
||||
$query = $event->persons()->with(['crowdType', 'pendingIdentityMatch.matchedUser', 'user']);
|
||||
|
||||
if ($request->filled('crowd_type_id')) {
|
||||
$query->where('crowd_type_id', $request->input('crowd_type_id'));
|
||||
@@ -77,7 +77,7 @@ final class PersonController extends Controller
|
||||
$this->verifyEventBelongsToOrganisation($organisation, $event);
|
||||
Gate::authorize('view', [$person, $event]);
|
||||
|
||||
$person->load(['crowdType', 'company', 'user']);
|
||||
$person->load(['crowdType', 'company', 'user', 'pendingIdentityMatch.matchedUser']);
|
||||
|
||||
return $this->success(new PersonResource($person));
|
||||
}
|
||||
@@ -89,7 +89,7 @@ final class PersonController extends Controller
|
||||
|
||||
$person = $event->persons()->create($request->validated());
|
||||
|
||||
$this->identityService->detectMatchForPerson($person);
|
||||
// Identity match detection is handled automatically by PersonObserver
|
||||
|
||||
return $this->created(new PersonResource($person->fresh()->load('crowdType')));
|
||||
}
|
||||
|
||||
@@ -7,14 +7,18 @@ namespace App\Http\Controllers\Api\V1;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Api\V1\BulkConfirmIdentityMatchesRequest;
|
||||
use App\Http\Resources\Api\V1\PersonIdentityMatchResource;
|
||||
use App\Http\Resources\Api\V1\PersonResource;
|
||||
use App\Models\Event;
|
||||
use App\Models\Organisation;
|
||||
use App\Models\Person;
|
||||
use App\Models\PersonIdentityMatch;
|
||||
use App\Models\User;
|
||||
use App\Services\PersonIdentityService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
final class PersonIdentityMatchController extends Controller
|
||||
{
|
||||
@@ -63,7 +67,7 @@ final class PersonIdentityMatchController extends Controller
|
||||
return $this->error($e->getMessage(), 422);
|
||||
}
|
||||
|
||||
$personIdentityMatch->refresh()->load(['person.crowdType', 'person.event', 'matchedUser', 'resolvedBy']);
|
||||
$personIdentityMatch->refresh()->load(['person.crowdType', 'person.event', 'matchedUser', 'confirmedBy', 'resolvedBy']);
|
||||
|
||||
return $this->success(new PersonIdentityMatchResource($personIdentityMatch));
|
||||
}
|
||||
@@ -88,6 +92,26 @@ final class PersonIdentityMatchController extends Controller
|
||||
return $this->success(new PersonIdentityMatchResource($personIdentityMatch));
|
||||
}
|
||||
|
||||
public function revert(Request $request, Organisation $organisation, PersonIdentityMatch $personIdentityMatch): JsonResponse
|
||||
{
|
||||
// Verify match belongs to this organisation
|
||||
if ($personIdentityMatch->person->event->organisation_id !== $organisation->id) {
|
||||
return $this->notFound('Match not found.');
|
||||
}
|
||||
|
||||
Gate::authorize('confirm', $personIdentityMatch);
|
||||
|
||||
try {
|
||||
$this->identityService->revertMatch($personIdentityMatch, $request->user());
|
||||
} catch (\DomainException $e) {
|
||||
return $this->error($e->getMessage(), 422);
|
||||
}
|
||||
|
||||
$personIdentityMatch->refresh()->load(['person.crowdType', 'person.event', 'matchedUser', 'revertedBy']);
|
||||
|
||||
return $this->success(new PersonIdentityMatchResource($personIdentityMatch));
|
||||
}
|
||||
|
||||
public function bulkConfirm(BulkConfirmIdentityMatchesRequest $request, Organisation $organisation): JsonResponse
|
||||
{
|
||||
Gate::authorize('bulkConfirm', [PersonIdentityMatch::class, $organisation]);
|
||||
@@ -107,12 +131,14 @@ final class PersonIdentityMatchController extends Controller
|
||||
|
||||
if ($match === null) {
|
||||
$errors[] = ['match_id' => $matchId, 'error' => 'Match not found.'];
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$response = Gate::inspect('update', [$match->person, $match->person->event]);
|
||||
if ($response->denied()) {
|
||||
$errors[] = ['match_id' => $matchId, 'error' => 'Unauthorized.'];
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -129,4 +155,35 @@ final class PersonIdentityMatchController extends Controller
|
||||
'errors' => $errors,
|
||||
]);
|
||||
}
|
||||
|
||||
public function manualLink(Request $request, Organisation $organisation, Event $event, Person $person): JsonResponse
|
||||
{
|
||||
Gate::authorize('update', [$person, $event]);
|
||||
|
||||
$validated = $request->validate([
|
||||
'user_id' => ['required', 'string', 'exists:users,id'],
|
||||
]);
|
||||
|
||||
try {
|
||||
$user = User::findOrFail($validated['user_id']);
|
||||
$match = $this->identityService->manualLink($person, $user, $request->user());
|
||||
} catch (ValidationException $e) {
|
||||
return $this->error($e->getMessage(), 422);
|
||||
}
|
||||
|
||||
return $this->success(new PersonIdentityMatchResource($match->load(['person.crowdType', 'matchedUser'])));
|
||||
}
|
||||
|
||||
public function unlink(Request $request, Organisation $organisation, Event $event, Person $person): JsonResponse
|
||||
{
|
||||
Gate::authorize('update', [$person, $event]);
|
||||
|
||||
try {
|
||||
$person = $this->identityService->unlinkDirect($person, $request->user());
|
||||
} catch (ValidationException $e) {
|
||||
return $this->error($e->getMessage(), 422);
|
||||
}
|
||||
|
||||
return $this->success(new PersonResource($person->load(['crowdType', 'user'])));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ final class MeResource extends JsonResource
|
||||
'first_name' => $this->first_name,
|
||||
'last_name' => $this->last_name,
|
||||
'full_name' => $this->full_name,
|
||||
'date_of_birth' => $this->date_of_birth?->toDateString(),
|
||||
'email' => $this->email,
|
||||
'timezone' => $this->timezone,
|
||||
'locale' => $this->locale,
|
||||
|
||||
@@ -27,18 +27,32 @@ final class PersonIdentityMatchResource extends JsonResource
|
||||
],
|
||||
'matched_user' => [
|
||||
'id' => $this->matchedUser->id,
|
||||
'name' => $this->matchedUser->name,
|
||||
'first_name' => $this->matchedUser->first_name,
|
||||
'last_name' => $this->matchedUser->last_name,
|
||||
'full_name' => $this->matchedUser->full_name,
|
||||
'email' => $this->matchedUser->email,
|
||||
'date_of_birth' => $this->matchedUser->date_of_birth?->toDateString(),
|
||||
],
|
||||
'matched_on' => $this->matched_on->value,
|
||||
'matched_on_label' => $this->matched_on->label(),
|
||||
'confidence' => $this->confidence->value,
|
||||
'confidence_label' => $this->confidence->label(),
|
||||
'status' => $this->status->value,
|
||||
'status_label' => $this->status->label(),
|
||||
'match_details' => $this->match_details,
|
||||
'confirmed_by' => $this->when($this->confirmedBy, fn () => [
|
||||
'id' => $this->confirmedBy->id,
|
||||
'full_name' => $this->confirmedBy->full_name,
|
||||
]),
|
||||
'confirmed_at' => $this->confirmed_at?->toIso8601String(),
|
||||
'dismissed_at' => $this->dismissed_at?->toIso8601String(),
|
||||
'reverted_at' => $this->reverted_at?->toIso8601String(),
|
||||
'resolved_by' => $this->when($this->resolvedBy, fn () => [
|
||||
'id' => $this->resolvedBy->id,
|
||||
'name' => $this->resolvedBy->name,
|
||||
'full_name' => $this->resolvedBy->full_name,
|
||||
]),
|
||||
'resolved_at' => $this->resolved_at?->toISOString(),
|
||||
'created_at' => $this->created_at->toISOString(),
|
||||
'resolved_at' => $this->resolved_at?->toIso8601String(),
|
||||
'created_at' => $this->created_at->toIso8601String(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,12 @@ final class PersonResource extends JsonResource
|
||||
'created_at' => $this->created_at->toIso8601String(),
|
||||
'crowd_type' => new CrowdTypeResource($this->whenLoaded('crowdType')),
|
||||
'company' => new CompanyResource($this->whenLoaded('company')),
|
||||
'has_user_account' => (bool) $this->user_id,
|
||||
'user_account' => $this->when($this->user_id && $this->relationLoaded('user') && $this->user, fn () => [
|
||||
'id' => $this->user->id,
|
||||
'email' => $this->user->email,
|
||||
'full_name' => $this->user->full_name,
|
||||
]),
|
||||
'pending_identity_match' => $this->when(
|
||||
$this->relationLoaded('pendingIdentityMatch') && $this->pendingIdentityMatch,
|
||||
function () {
|
||||
@@ -41,9 +47,13 @@ final class PersonResource extends JsonResource
|
||||
'last_name' => $match->matchedUser->last_name,
|
||||
'full_name' => $match->matchedUser->full_name,
|
||||
'email' => $match->matchedUser->email,
|
||||
'date_of_birth' => $match->matchedUser->date_of_birth?->toDateString(),
|
||||
],
|
||||
'matched_on' => $match->matched_on->value,
|
||||
'matched_on_label' => $match->matched_on->label(),
|
||||
'confidence' => $match->confidence->value,
|
||||
'confidence_label' => $match->confidence->label(),
|
||||
'match_details' => $match->match_details,
|
||||
];
|
||||
}
|
||||
),
|
||||
|
||||
@@ -16,6 +16,7 @@ final class UserResource extends JsonResource
|
||||
'first_name' => $this->first_name,
|
||||
'last_name' => $this->last_name,
|
||||
'full_name' => $this->full_name,
|
||||
'date_of_birth' => $this->date_of_birth?->toDateString(),
|
||||
'email' => $this->email,
|
||||
'roles' => $this->getRoleNames()->values()->all(),
|
||||
'timezone' => $this->timezone,
|
||||
|
||||
Reference in New Issue
Block a user