feat(notification) : add NotificationService and UserRepository::findByRole
This commit is contained in:
@@ -17,4 +17,17 @@ class UserRepository extends ServiceEntityRepository
|
|||||||
{
|
{
|
||||||
parent::__construct($registry, User::class);
|
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()
|
||||||
|
;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
74
src/Service/NotificationService.php
Normal file
74
src/Service/NotificationService.php
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Service;
|
||||||
|
|
||||||
|
use App\Entity\ClientTicket;
|
||||||
|
use App\Entity\Notification;
|
||||||
|
use App\Repository\UserRepository;
|
||||||
|
use DateTimeImmutable;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
|
||||||
|
final readonly class NotificationService
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private EntityManagerInterface $entityManager,
|
||||||
|
private UserRepository $userRepository,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notify all ROLE_ADMIN users that a new ticket was created.
|
||||||
|
*/
|
||||||
|
public function createForTicketCreated(ClientTicket $ticket): void
|
||||||
|
{
|
||||||
|
$admins = $this->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();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user