From 4094048aba0b8db6e6bb7142124519850913d842 Mon Sep 17 00:00:00 2001 From: matthieu Date: Sun, 15 Mar 2026 19:46:37 +0100 Subject: [PATCH] feat(notification) : add NotificationService and UserRepository::findByRole --- src/Repository/UserRepository.php | 13 +++++ src/Service/NotificationService.php | 74 +++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 src/Service/NotificationService.php diff --git a/src/Repository/UserRepository.php b/src/Repository/UserRepository.php index e85e4bf..d18497d 100644 --- a/src/Repository/UserRepository.php +++ b/src/Repository/UserRepository.php @@ -17,4 +17,17 @@ class UserRepository extends ServiceEntityRepository { parent::__construct($registry, User::class); } + + /** + * @return User[] + */ + public function findByRole(string $role): array + { + return $this->createQueryBuilder('u') + ->where('u.roles LIKE :role') + ->setParameter('role', '%"'.$role.'"%') + ->getQuery() + ->getResult() + ; + } } diff --git a/src/Service/NotificationService.php b/src/Service/NotificationService.php new file mode 100644 index 0000000..d2d9917 --- /dev/null +++ b/src/Service/NotificationService.php @@ -0,0 +1,74 @@ +userRepository->findByRole('ROLE_ADMIN'); + $number = sprintf('CT-%03d', $ticket->getNumber()); + $projectName = $ticket->getProject()?->getName() ?? ''; + + foreach ($admins as $admin) { + $notification = new Notification(); + $notification->setUser($admin); + $notification->setType('ticket_created'); + $notification->setTitle('Nouveau ticket client '.$number); + $notification->setMessage($ticket->getTitle().' — '.$projectName); + $notification->setRelatedTicket($ticket); + $notification->setCreatedAt(new DateTimeImmutable()); + + $this->entityManager->persist($notification); + } + + $this->entityManager->flush(); + } + + /** + * Notify the ticket submitter that the status has changed. + */ + public function createForStatusChange(ClientTicket $ticket): void + { + $submittedBy = $ticket->getSubmittedBy(); + + if (null === $submittedBy) { + return; + } + + $number = sprintf('CT-%03d', $ticket->getNumber()); + $statusLabel = $ticket->getStatus(); + $message = 'Nouveau statut : '.$statusLabel; + + if (null !== $ticket->getStatusComment() && '' !== $ticket->getStatusComment()) { + $message .= ' — '.$ticket->getStatusComment(); + } + + $notification = new Notification(); + $notification->setUser($submittedBy); + $notification->setType('ticket_status_changed'); + $notification->setTitle('Ticket '.$number.' mis à jour'); + $notification->setMessage($message); + $notification->setRelatedTicket($ticket); + $notification->setCreatedAt(new DateTimeImmutable()); + + $this->entityManager->persist($notification); + $this->entityManager->flush(); + } +}