refs #1281. Created filter OU added all clients
testing/ogcore-api/pipeline/head This commit looks good
Details
testing/ogcore-api/pipeline/head This commit looks good
Details
parent
fb381b9627
commit
825387d881
|
@ -15,4 +15,40 @@ class ClientRepository extends AbstractRepository
|
|||
{
|
||||
parent::__construct($registry, Client::class);
|
||||
}
|
||||
|
||||
public function findClientsByOrganizationalUnitAndDescendants(int $organizationalUnitId): array
|
||||
{
|
||||
$query = $this->getEntityManager()->createQuery(
|
||||
'SELECT o.path
|
||||
FROM App\Entity\OrganizationalUnit o
|
||||
WHERE o.id = :id'
|
||||
)->setParameter('id', $organizationalUnitId);
|
||||
|
||||
$result = $query->getOneOrNullResult();
|
||||
|
||||
if (!$result) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$path = $result['path'] . '/%';
|
||||
|
||||
$query = $this->getEntityManager()->createQuery(
|
||||
'SELECT o.id
|
||||
FROM App\Entity\OrganizationalUnit o
|
||||
WHERE o.id = :id OR o.path LIKE :path'
|
||||
)
|
||||
->setParameter('id', $organizationalUnitId)
|
||||
->setParameter('path', $path);
|
||||
|
||||
$unitIds = array_column($query->getArrayResult(), 'id');
|
||||
|
||||
$query = $this->getEntityManager()->createQuery(
|
||||
'SELECT c
|
||||
FROM App\Entity\Client c
|
||||
WHERE c.organizationalUnit IN (:unitIds)'
|
||||
)
|
||||
->setParameter('unitIds', $unitIds);
|
||||
|
||||
return $query->getResult();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,11 +11,13 @@ use ApiPlatform\State\Pagination\TraversablePaginator;
|
|||
use ApiPlatform\State\ProviderInterface;
|
||||
use App\Dto\Input\ClientInput;
|
||||
use App\Dto\Output\ClientOutput;
|
||||
use App\Repository\ClientRepository;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
readonly class ClientProvider implements ProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private ClientRepository $clientRepository,
|
||||
private ProviderInterface $collectionProvider,
|
||||
private ProviderInterface $itemProvider
|
||||
)
|
||||
|
@ -35,18 +37,25 @@ readonly class ClientProvider implements ProviderInterface
|
|||
}
|
||||
}
|
||||
|
||||
private function provideCollection(Operation $operation, array $uriVariables = [], array $context = []): object
|
||||
public function provideCollection(Operation $operation, array $uriVariables = [], array $context = []): object
|
||||
{
|
||||
$paginator = $this->collectionProvider->provide($operation, $uriVariables, $context);
|
||||
$organizationalUnitId = $context['filters']['organizationalUnit.id'] ?? null;
|
||||
|
||||
$items = new \ArrayObject();
|
||||
foreach ($paginator->getIterator() as $item){
|
||||
$items[] = new ClientOutput($item);
|
||||
if ($organizationalUnitId) {
|
||||
$clients = $this->clientRepository->findClientsByOrganizationalUnitAndDescendants((int) $organizationalUnitId);
|
||||
|
||||
$items = new \ArrayObject();
|
||||
foreach ($clients as $client) {
|
||||
$items[] = new ClientOutput($client);
|
||||
}
|
||||
|
||||
return new TraversablePaginator($items, 1, count($clients), count($clients));
|
||||
}
|
||||
|
||||
return new TraversablePaginator($items, $paginator->getCurrentPage(), $paginator->getItemsPerPage(), $paginator->getTotalItems());
|
||||
return $this->collectionProvider->provide($operation, $uriVariables, $context);
|
||||
}
|
||||
|
||||
|
||||
public function provideItem(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
|
||||
{
|
||||
$item = $this->itemProvider->provide($operation, $uriVariables, $context);
|
||||
|
|
Loading…
Reference in New Issue