Files
crewli/api/app/Http/Controllers/Api/V1/PersonIdentityMatchController.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

190 lines
6.8 KiB
PHP

<?php
declare(strict_types=1);
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
{
public function __construct(
private readonly PersonIdentityService $identityService,
) {}
public function index(Request $request, Organisation $organisation): AnonymousResourceCollection
{
Gate::authorize('viewAny', [PersonIdentityMatch::class, $organisation]);
$eventIds = $organisation->events()->pluck('id');
$matches = PersonIdentityMatch::pending()
->whereHas('person', fn ($q) => $q->whereIn('event_id', $eventIds))
->with(['person.crowdType', 'person.event', 'matchedUser'])
->orderBy('created_at', 'desc')
->paginate(25);
return PersonIdentityMatchResource::collection($matches);
}
public function showForPerson(Organisation $organisation, Person $person): PersonIdentityMatchResource
{
Gate::authorize('view', [$person, $person->event]);
$match = $person->pendingIdentityMatch()
->with('matchedUser')
->firstOrFail();
return new PersonIdentityMatchResource($match);
}
public function confirm(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->confirmMatch($personIdentityMatch, $request->user());
} catch (\DomainException $e) {
return $this->error($e->getMessage(), 422);
}
$personIdentityMatch->refresh()->load(['person.crowdType', 'person.event', 'matchedUser', 'confirmedBy', 'resolvedBy']);
return $this->success(new PersonIdentityMatchResource($personIdentityMatch));
}
public function dismiss(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('dismiss', $personIdentityMatch);
try {
$this->identityService->dismissMatch($personIdentityMatch, $request->user());
} catch (\DomainException $e) {
return $this->error($e->getMessage(), 422);
}
$personIdentityMatch->refresh()->load(['person.crowdType', 'person.event', 'matchedUser', 'resolvedBy']);
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]);
$orgEventIds = $organisation->events()->pluck('id');
$matches = PersonIdentityMatch::whereIn('id', $request->validated('match_ids'))
->whereHas('person', fn ($q) => $q->whereIn('event_id', $orgEventIds))
->with('person')
->get()
->keyBy('id');
$confirmed = 0;
$errors = [];
foreach ($request->validated('match_ids') as $matchId) {
$match = $matches->get($matchId);
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;
}
try {
$this->identityService->confirmMatch($match, $request->user());
$confirmed++;
} catch (\DomainException $e) {
$errors[] = ['match_id' => $matchId, 'error' => $e->getMessage()];
}
}
return response()->json([
'confirmed' => $confirmed,
'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'])));
}
}