Add PersonIdentifierManagerInterface to PersonACLAwareRepository and enhance search logic for identifiers

- Inject `PersonIdentifierManagerInterface` into `PersonACLAwareRepository` for improved identifier handling.
- Update search queries to include logic for filtering and matching `PersonIdentifier` values.
- Modify test cases to support the new dependency and ensure proper coverage.
This commit is contained in:
2025-09-24 00:01:28 +02:00
parent 34af53130b
commit 0fd76d3fa8
2 changed files with 48 additions and 8 deletions

View File

@@ -17,6 +17,8 @@ use Chill\MainBundle\Search\ParsingException;
use Chill\MainBundle\Search\SearchApiQuery;
use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\PersonIdentifier\PersonIdentifierManagerInterface;
use Chill\PersonBundle\PersonIdentifier\PersonIdentifierWorker;
use Chill\PersonBundle\Security\Authorization\PersonVoter;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\NonUniqueResultException;
@@ -27,7 +29,13 @@ use Symfony\Component\Security\Core\Security;
final readonly class PersonACLAwareRepository implements PersonACLAwareRepositoryInterface
{
public function __construct(private Security $security, private EntityManagerInterface $em, private CountryRepository $countryRepository, private AuthorizationHelperInterface $authorizationHelper) {}
public function __construct(
private Security $security,
private EntityManagerInterface $em,
private CountryRepository $countryRepository,
private AuthorizationHelperInterface $authorizationHelper,
private PersonIdentifierManagerInterface $personIdentifierManager,
) {}
public function buildAuthorizedQuery(
?string $default = null,
@@ -107,6 +115,15 @@ final readonly class PersonACLAwareRepository implements PersonACLAwareRepositor
$query
->setFromClause('chill_person_person AS person');
$idDefinitionWorkers = array_map(
fn (PersonIdentifierWorker $worker) => $worker->getDefinition()->getId(),
array_filter(
$this->personIdentifierManager->getWorkers(),
fn (PersonIdentifierWorker $worker) => $worker->getDefinition()->isSearchable()
)
);
$idDefinitionWorkerQuestionMarks = implode(', ', array_fill(0, count($idDefinitionWorkers), '?'));
$pertinence = [];
$pertinenceArgs = [];
$andWhereSearchClause = [];
@@ -124,10 +141,25 @@ final readonly class PersonACLAwareRepository implements PersonACLAwareRepositor
'(starts_with(LOWER(UNACCENT(lastname)), UNACCENT(LOWER(?))))::int';
\array_push($pertinenceArgs, $str, $str, $str, $str);
$andWhereSearchClause[] =
'(LOWER(UNACCENT(?)) <<% person.fullnamecanonical OR '.
"person.fullnamecanonical LIKE '%' || LOWER(UNACCENT(?)) || '%' )";
\array_push($andWhereSearchClauseArgs, $str, $str);
$q = [
'LOWER(UNACCENT(?)) <<% person.fullnamecanonical',
"person.fullnamecanonical LIKE '%' || LOWER(UNACCENT(?)) || '%' ",
];
$qArguments = [$str, $str];
if (count($idDefinitionWorkers) > 0) {
$q[] = $mq = "EXISTS (
SELECT 1 FROM chill_person_identifier AS identifier
WHERE identifier.canonical LIKE LOWER(UNACCENT(?)) || '%' AND identifier.definition_id IN ({$idDefinitionWorkerQuestionMarks})
AND person.id = identifier.person_id
)";
$pertinence[] = "({$mq})::int * 1_000_000";
$qArguments = [...$qArguments, $str, ...$idDefinitionWorkers];
$pertinenceArgs = [...$pertinenceArgs, $str, ...$idDefinitionWorkers];
}
$andWhereSearchClause[] = '('.implode(' OR ', $q).')';
$andWhereSearchClauseArgs = [...$andWhereSearchClauseArgs, ...$qArguments];
}
$query->andWhereClause(

View File

@@ -18,6 +18,7 @@ use Chill\MainBundle\Repository\CountryRepository;
use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\PersonPhone;
use Chill\PersonBundle\PersonIdentifier\PersonIdentifierManagerInterface;
use Chill\PersonBundle\Repository\PersonACLAwareRepository;
use Chill\PersonBundle\Security\Authorization\PersonVoter;
use Doctrine\ORM\EntityManagerInterface;
@@ -42,6 +43,8 @@ final class PersonACLAwareRepositoryTest extends KernelTestCase
private EntityManagerInterface $entityManager;
private PersonIdentifierManagerInterface $personIdentifierManager;
protected function setUp(): void
{
self::bootKernel();
@@ -49,6 +52,8 @@ final class PersonACLAwareRepositoryTest extends KernelTestCase
$this->entityManager = self::getContainer()->get(EntityManagerInterface::class);
$this->countryRepository = self::getContainer()->get(CountryRepository::class);
$this->centerRepository = self::getContainer()->get(CenterRepositoryInterface::class);
$this->personIdentifierManager = self::getContainer()->get(PersonIdentifierManagerInterface::class);
}
public function testCountByCriteria()
@@ -66,7 +71,8 @@ final class PersonACLAwareRepositoryTest extends KernelTestCase
$security->reveal(),
$this->entityManager,
$this->countryRepository,
$authorizationHelper->reveal()
$authorizationHelper->reveal(),
$this->personIdentifierManager,
);
$number = $repository->countBySearchCriteria('diallo');
@@ -89,7 +95,8 @@ final class PersonACLAwareRepositoryTest extends KernelTestCase
$security->reveal(),
$this->entityManager,
$this->countryRepository,
$authorizationHelper->reveal()
$authorizationHelper->reveal(),
$this->personIdentifierManager,
);
$results = $repository->findBySearchCriteria(0, 5, false, 'diallo');
@@ -120,7 +127,8 @@ final class PersonACLAwareRepositoryTest extends KernelTestCase
$security->reveal(),
$this->entityManager,
$this->countryRepository,
$authorizationHelper->reveal()
$authorizationHelper->reveal(),
$this->personIdentifierManager,
);
$actual = $repository->findByPhone($phoneNumber, 0, 10);