From 2d8b960d9e59730f3874787231b8a7731726018c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 24 Apr 2024 18:48:00 +0200 Subject: [PATCH] Re-open the same ticket if a ticket already exists with the same externalRef, instead of creating a new one --- .../src/Controller/CreateTicketController.php | 12 +++- .../src/Repository/TicketRepository.php | 56 +++++++++++++++++++ .../Repository/TicketRepositoryInterface.php | 23 ++++++++ .../src/config/services.yaml | 7 ++- 4 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 src/Bundle/ChillTicketBundle/src/Repository/TicketRepository.php create mode 100644 src/Bundle/ChillTicketBundle/src/Repository/TicketRepositoryInterface.php diff --git a/src/Bundle/ChillTicketBundle/src/Controller/CreateTicketController.php b/src/Bundle/ChillTicketBundle/src/Controller/CreateTicketController.php index 8a6d8dd32..a3bdf31c8 100644 --- a/src/Bundle/ChillTicketBundle/src/Controller/CreateTicketController.php +++ b/src/Bundle/ChillTicketBundle/src/Controller/CreateTicketController.php @@ -15,6 +15,7 @@ use Chill\TicketBundle\Action\Ticket\AssociateByPhonenumberCommand; use Chill\TicketBundle\Action\Ticket\Handler\AssociateByPhonenumberCommandHandler; use Chill\TicketBundle\Action\Ticket\CreateTicketCommand; use Chill\TicketBundle\Action\Ticket\Handler\CreateTicketCommandHandler; +use Chill\TicketBundle\Repository\TicketRepositoryInterface; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; @@ -31,7 +32,8 @@ final readonly class CreateTicketController private AssociateByPhonenumberCommandHandler $associateByPhonenumberCommandHandler, private Security $security, private UrlGeneratorInterface $urlGenerator, - private EntityManagerInterface $entityManager + private EntityManagerInterface $entityManager, + private TicketRepositoryInterface $ticketRepository, ) {} #[Route('{_locale}/ticket/ticket/create')] @@ -41,6 +43,14 @@ final readonly class CreateTicketController throw new AccessDeniedHttpException('Only users are allowed to create tickets.'); } + if ('' !== $extId = $request->query->get('extId', '')) { + if (null !== $ticket = $this->ticketRepository->findOneByExternalRef($extId)) { + return new RedirectResponse( + $this->urlGenerator->generate('chill_ticket_ticket_edit', ['id' => $ticket->getId()]) + ); + } + } + $createCommand = new CreateTicketCommand($request->query->get('extId', '')); $ticket = $this->createTicketCommandHandler->__invoke($createCommand); diff --git a/src/Bundle/ChillTicketBundle/src/Repository/TicketRepository.php b/src/Bundle/ChillTicketBundle/src/Repository/TicketRepository.php new file mode 100644 index 000000000..d4301585a --- /dev/null +++ b/src/Bundle/ChillTicketBundle/src/Repository/TicketRepository.php @@ -0,0 +1,56 @@ +repository = $objectManager->getRepository($this->getClassName()); + } + + public function find($id): ?Ticket + { + return $this->repository->find($id); + } + + public function findAll(): array + { + return $this->repository->findAll(); + } + + public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array + { + return $this->repository->findBy($criteria, $orderBy, $limit, $offset); + } + + public function findOneBy(array $criteria): ?Ticket + { + return $this->repository->findOneBy($criteria); + } + + public function getClassName() + { + return Ticket::class; + } + + public function findOneByExternalRef(string $extId): ?Ticket + { + return $this->repository->findOneBy(['externalRef' => $extId]); + } +} diff --git a/src/Bundle/ChillTicketBundle/src/Repository/TicketRepositoryInterface.php b/src/Bundle/ChillTicketBundle/src/Repository/TicketRepositoryInterface.php new file mode 100644 index 000000000..0b2cb4d66 --- /dev/null +++ b/src/Bundle/ChillTicketBundle/src/Repository/TicketRepositoryInterface.php @@ -0,0 +1,23 @@ + + */ +interface TicketRepositoryInterface extends ObjectRepository +{ + public function findOneByExternalRef(string $extId): ?Ticket; +} diff --git a/src/Bundle/ChillTicketBundle/src/config/services.yaml b/src/Bundle/ChillTicketBundle/src/config/services.yaml index 006fa5cb6..4b662ecdc 100644 --- a/src/Bundle/ChillTicketBundle/src/config/services.yaml +++ b/src/Bundle/ChillTicketBundle/src/config/services.yaml @@ -3,13 +3,16 @@ services: autoconfigure: true autowire: true + Chill\TicketBundle\Action\Ticket\Handler\: + resource: '../Action/Ticket/Handler/' + Chill\TicketBundle\Controller\: resource: '../Controller/' tags: - controller.service_arguments - Chill\TicketBundle\Action\Ticket\Handler\: - resource: '../Action/Ticket/Handler/' + Chill\TicketBundle\Repository\: + resource: '../Repository/' Chill\TicketBundle\Serializer\: resource: '../Serializer/'