Add ticket filtering "byTicketId"

This commit is contained in:
2025-09-15 11:11:40 +02:00
parent e12ad563a3
commit 8e2e676e3d
7 changed files with 196 additions and 1 deletions

View File

@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\PersonBundle\Controller;
final readonly class PersonIdentifierApiController {}

View File

@@ -60,6 +60,18 @@ paths:
- ticket - ticket
summary: List of tickets summary: List of tickets
parameters: parameters:
- name: byTicketId
in: query
description: >
The id of the ticket.
When the id of the ticket is set, the other parameters are ignored.
required: false
style: form
explode: false
schema:
type: integer
minimum: 0
- name: byPerson - name: byPerson
in: query in: query
description: the id of the person description: the id of the person

View File

@@ -59,6 +59,14 @@ final readonly class TicketListApiController
$params = []; $params = [];
if ($request->query->has('byTicketId')) {
$params['byTicketId'] = $request->query->getInt('byTicketId', -1);
if (-1 === $params['byTicketId']) {
throw new BadRequestHttpException("The parameter 'byTicketId' is not an integer.");
}
}
if ($request->query->has('byPerson')) { if ($request->query->has('byPerson')) {
$personIds = explode(',', $request->query->get('byPerson')); $personIds = explode(',', $request->query->get('byPerson'));
foreach ($personIds as $id) { foreach ($personIds as $id) {

View File

@@ -53,6 +53,13 @@ final readonly class TicketACLAwareRepository implements TicketACLAwareRepositor
// counter for all the loops // counter for all the loops
$i = 0; $i = 0;
if (array_key_exists('byTicketId', $params)) {
$qb->andWhere($qb->expr()->in('t.id', ':byTicketId'));
$qb->setParameter('byTicketId', $params['byTicketId']);
return $qb;
}
if (array_key_exists('byPerson', $params)) { if (array_key_exists('byPerson', $params)) {
$or = $qb->expr()->orX(); $or = $qb->expr()->orX();

View File

@@ -22,7 +22,7 @@ use Chill\TicketBundle\Entity\Ticket;
/** /**
* Repository to find tickets, taking care of permissions. * Repository to find tickets, taking care of permissions.
* *
* @phpstan-type TicketACLAwareRepositoryParam array{byPerson?: list<Person>, byCurrentState?: list<StateEnum>, byCurrentStateEmergency?: list<EmergencyStatusEnum>, byMotives?: list<Motive>, byCreatedBefore?: \DateTimeImmutable, byCreatedAfter?: \DateTimeImmutable, byAddressee?: list<User>, byAddresseeGroup?: list<UserGroup>, byCreator?: list<User>} * @phpstan-type TicketACLAwareRepositoryParam array{byPerson?: list<Person>, byCurrentState?: list<StateEnum>, byCurrentStateEmergency?: list<EmergencyStatusEnum>, byMotives?: list<Motive>, byCreatedBefore?: \DateTimeImmutable, byCreatedAfter?: \DateTimeImmutable, byAddressee?: list<User>, byAddresseeGroup?: list<UserGroup>, byCreator?: list<User>, byTicketId?: int}
*/ */
interface TicketACLAwareRepositoryInterface interface TicketACLAwareRepositoryInterface
{ {

View File

@@ -0,0 +1,147 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\TicketBundle\Tests\Controller;
use Chill\MainBundle\Pagination\PaginatorFactoryInterface;
use Chill\MainBundle\Pagination\PaginatorInterface;
use Chill\MainBundle\Repository\UserGroupRepositoryInterface;
use Chill\MainBundle\Repository\UserRepositoryInterface;
use Chill\MainBundle\Serializer\Model\Collection;
use Chill\PersonBundle\Repository\PersonRepository;
use Chill\TicketBundle\Controller\TicketListApiController;
use Chill\TicketBundle\Entity\Ticket;
use Chill\TicketBundle\Repository\MotiveRepository;
use Chill\TicketBundle\Repository\TicketACLAwareRepositoryInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Clock\MockClock;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\SerializerInterface;
/**
* @internal
*
* @covers \Chill\TicketBundle\Controller\TicketListApiController
*/
final class TicketListApiControllerByTicketIdTest extends TestCase
{
use ProphecyTrait;
public function testListTicketWithByTicketIdFilter(): void
{
// Mock dependencies
$security = $this->prophesize(Security::class);
$security->isGranted('ROLE_USER')->willReturn(true);
$ticketRepository = $this->prophesize(TicketACLAwareRepositoryInterface::class);
$tickets = [new Ticket(), new Ticket()];
$ticketRepository->countTickets(
Argument::that(fn ($params) => isset($params['byTicketId']) && 5 === $params['byTicketId'])
)
->shouldBeCalled()
->willReturn(2);
$ticketRepository->findTickets(
Argument::that(fn ($params) => isset($params['byTicketId']) && 5 === $params['byTicketId']),
0,
10
)->shouldBeCalled()->willReturn($tickets);
$paginator = $this->prophesize(PaginatorInterface::class);
$paginator->getCurrentPageFirstItemNumber()->willReturn(0);
$paginator->getItemsPerPage()->willReturn(10);
$paginatorFactory = $this->prophesize(PaginatorFactoryInterface::class);
$paginatorFactory->create(2)->willReturn($paginator->reveal());
$serializer = $this->prophesize(SerializerInterface::class);
$serializer->serialize(
Argument::that(fn (Collection $collection) => $collection->getItems() === $tickets),
'json',
['groups' => 'read:simple']
)->willReturn('{"items":[{},{}],"pagination":{}}');
$personRepository = $this->prophesize(PersonRepository::class);
$motiveRepository = $this->prophesize(MotiveRepository::class);
// Needed for controller constructor but not used here
$userRepository = $this->prophesize(UserRepositoryInterface::class);
$userGroupRepository = $this->prophesize(UserGroupRepositoryInterface::class);
// Create controller
$controller = new TicketListApiController(
$security->reveal(),
$ticketRepository->reveal(),
$paginatorFactory->reveal(),
$serializer->reveal(),
$personRepository->reveal(),
$motiveRepository->reveal(),
new MockClock(),
new ParameterBag(['chill_ticket' => ['ticket' => ['response_time_exceeded_delay' => 'PT12H']]]),
$userRepository->reveal(),
$userGroupRepository->reveal()
);
// Create request with byTicketId filter
$request = new Request(
query: ['byTicketId' => '5']
);
// Call controller method
$response = $controller->listTicket($request);
// Assert response
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertEquals('{"items":[{},{}],"pagination":{}}', $response->getContent());
}
public function testListTicketWithInvalidByTicketIdThrows(): void
{
$this->expectException(BadRequestHttpException::class);
// Mock dependencies
$security = $this->prophesize(Security::class);
$security->isGranted('ROLE_USER')->willReturn(true);
$ticketRepository = $this->prophesize(TicketACLAwareRepositoryInterface::class);
$paginatorFactory = $this->prophesize(PaginatorFactoryInterface::class);
$serializer = $this->prophesize(SerializerInterface::class);
$personRepository = $this->prophesize(PersonRepository::class);
$motiveRepository = $this->prophesize(MotiveRepository::class);
$userRepository = $this->prophesize(UserRepositoryInterface::class);
$userGroupRepository = $this->prophesize(UserGroupRepositoryInterface::class);
$controller = new TicketListApiController(
$security->reveal(),
$ticketRepository->reveal(),
$paginatorFactory->reveal(),
$serializer->reveal(),
$personRepository->reveal(),
$motiveRepository->reveal(),
new MockClock(),
new ParameterBag(['chill_ticket' => ['ticket' => ['response_time_exceeded_delay' => 'PT12H']]]),
$userRepository->reveal(),
$userGroupRepository->reveal()
);
// Use -1 to trigger the controller's validation error
$request = new Request(query: ['byTicketId' => '-1']);
// This should throw BadRequestHttpException
$controller->listTicket($request);
}
}

View File

@@ -185,4 +185,11 @@ class TicketACLAwareRepositoryTest extends KernelTestCase
self::assertIsArray($actual); self::assertIsArray($actual);
} }
public function testFindByTicketid(): void
{
$actual = $this->repository->findTickets(['byTicketId' => 1]);
self::assertIsArray($actual);
}
} }