Improve assignee handling and task assignment logic

- Added `hasAssigneeChanged` method for detecting assignee change
- Updated `onTaskAssigned` logic to trigger only on assignee change
This commit is contained in:
2025-08-25 19:38:15 +02:00
parent bc5f8d56fe
commit 6d9834de71
3 changed files with 25 additions and 9 deletions

View File

@@ -13,7 +13,6 @@ namespace Chill\TaskBundle\Controller;
use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Pagination\PaginatorFactory; use Chill\MainBundle\Pagination\PaginatorFactory;
use Chill\MainBundle\Security\Resolver\CenterResolverDispatcherInterface;
use Chill\MainBundle\Serializer\Model\Collection; use Chill\MainBundle\Serializer\Model\Collection;
use Chill\MainBundle\Serializer\Model\Counter; use Chill\MainBundle\Serializer\Model\Counter;
use Chill\MainBundle\Templating\Listing\FilterOrderHelper; use Chill\MainBundle\Templating\Listing\FilterOrderHelper;
@@ -49,7 +48,6 @@ use Symfony\Contracts\Translation\TranslatorInterface;
final class SingleTaskController extends AbstractController final class SingleTaskController extends AbstractController
{ {
public function __construct( public function __construct(
private readonly CenterResolverDispatcherInterface $centerResolverDispatcher,
private readonly PaginatorFactory $paginatorFactory, private readonly PaginatorFactory $paginatorFactory,
private readonly SingleTaskAclAwareRepositoryInterface $singleTaskAclAwareRepository, private readonly SingleTaskAclAwareRepositoryInterface $singleTaskAclAwareRepository,
private readonly TranslatorInterface $translator, private readonly TranslatorInterface $translator,
@@ -170,6 +168,9 @@ final class SingleTaskController extends AbstractController
->setForm($this->setCreateForm($task, TaskVoter::UPDATE)); ->setForm($this->setCreateForm($task, TaskVoter::UPDATE));
$this->eventDispatcher->dispatch($event, UIEvent::EDIT_FORM); $this->eventDispatcher->dispatch($event, UIEvent::EDIT_FORM);
// To keep track of specific assignee change
$initialAssignee = $task->getAssignee();
$form = $event->getForm(); $form = $event->getForm();
$form->handleRequest($request); $form->handleRequest($request);
@@ -179,6 +180,13 @@ final class SingleTaskController extends AbstractController
$em = $this->managerRegistry->getManager(); $em = $this->managerRegistry->getManager();
$em->persist($task); $em->persist($task);
if (null !== $task->getAssignee()) {
$this->eventDispatcher->dispatch(
new AssignTaskEvent($task, $initialAssignee),
AssignTaskEvent::PERSIST
);
}
$em->flush(); $em->flush();
$this->addFlash('success', $this->translator $this->addFlash('success', $this->translator
@@ -528,7 +536,7 @@ final class SingleTaskController extends AbstractController
if (null !== $task->getAssignee()) { if (null !== $task->getAssignee()) {
$this->eventDispatcher->dispatch( $this->eventDispatcher->dispatch(
new AssignTaskEvent($task, $user), new AssignTaskEvent($task),
AssignTaskEvent::PERSIST AssignTaskEvent::PERSIST
); );
} }

View File

@@ -20,7 +20,7 @@ class AssignTaskEvent extends Event
final public const PERSIST = 'chill_task.assign_task'; final public const PERSIST = 'chill_task.assign_task';
public function __construct( public function __construct(
private readonly SingleTask $task, private readonly SingleTask $task,
private readonly User $assignedUser, private readonly User $initialAssignee,
) {} ) {}
public function getTask(): SingleTask public function getTask(): SingleTask
@@ -28,8 +28,13 @@ class AssignTaskEvent extends Event
return $this->task; return $this->task;
} }
public function getAssignedUser(): User public function getInitialAssignee(): User
{ {
return $this->assignedUser; return $this->initialAssignee;
}
public function hasAssigneeChanged(): bool
{
return $this->initialAssignee !== $this->task->getAssignee();
} }
} }

View File

@@ -33,13 +33,16 @@ readonly class TaskAssignEventSubscriber implements EventSubscriberInterface
/** /**
* Send a notification when a user is assigned to a task. * Send a notification when a user is assigned to a task.
* * Only triggers when the assignee actually changes.
* The notification will be sent to the newly assigned user
*/ */
public function onTaskAssigned(AssignTaskEvent $event): void public function onTaskAssigned(AssignTaskEvent $event): void
{ {
if (!$event->hasAssigneeChanged()) {
return;
}
$task = $event->getTask(); $task = $event->getTask();
$assignedUser = $event->getAssignedUser(); $assignedUser = $task->getAssignee();
$title = $task->getTitle(); $title = $task->getTitle();