Add notification to user groups on workflow transition

Implemented NotificationToUserGroupsOnTransition to send group emails upon workflow completion. Also updated NotificationOnTransition to prevent double notifications and created a unit test for the new functionality.
This commit is contained in:
2024-10-18 19:25:03 +02:00
parent 29fa086fde
commit 91a4b45607
4 changed files with 247 additions and 2 deletions

View File

@@ -42,8 +42,8 @@ class NotificationOnTransition implements EventSubscriberInterface
/**
* Send a notification to:.
*
* * the dests of the new step;
* * the users which subscribed to workflow, on each step, or on final
* * the dests of the new step, or the members of a user group if the user group has no email;
* * the users which subscribed to workflow, on each step, or on final;
*
* **Warning** take care that this method must be executed **after** the dest users are added to
* the step (@see{EntityWorkflowStep::addDestUser}). Currently, this is done during
@@ -74,6 +74,11 @@ class NotificationOnTransition implements EventSubscriberInterface
// the users within groups
$entityWorkflow->getCurrentStep()->getDestUserGroups()->reduce(
function (array $accumulator, UserGroup $userGroup) {
if ($userGroup->hasEmail()) {
// this prevent users to be notified twice if they will already be notiied by the group
return $accumulator;
}
foreach ($userGroup->getUsers() as $user) {
$accumulator[] = $user;
}

View File

@@ -0,0 +1,90 @@
<?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\MainBundle\Workflow\EventSubscriber;
use Chill\MainBundle\Entity\Notification;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Workflow\Helper\MetadataExtractor;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Workflow\Event\Event;
use Symfony\Component\Workflow\Registry;
final readonly class NotificationToUserGroupsOnTransition implements EventSubscriberInterface
{
public function __construct(
private \Twig\Environment $engine,
private MetadataExtractor $metadataExtractor,
private Registry $registry,
private MailerInterface $mailer,
) {}
public static function getSubscribedEvents(): array
{
return [
'workflow.completed' => ['onCompletedSendNotification', 2048],
];
}
/**
* Send a notification to:.
*
* * the dests of the new step;
* * the users which subscribed to workflow, on each step, or on final
*
* **Warning** take care that this method must be executed **after** the dest users are added to
* the step (@see{EntityWorkflowStep::addDestUser}). Currently, this is done during
*
* @see{EntityWorkflowTransitionEventSubscriber::addDests}.
*/
public function onCompletedSendNotification(Event $event): void
{
if (!$event->getSubject() instanceof EntityWorkflow) {
return;
}
/** @var EntityWorkflow $entityWorkflow */
$entityWorkflow = $event->getSubject();
$place = $this->metadataExtractor->buildArrayPresentationForPlace($entityWorkflow);
$workflow = $this->metadataExtractor->buildArrayPresentationForWorkflow(
$this->registry->get($entityWorkflow, $entityWorkflow->getWorkflowName())
);
// send to groups
foreach ($entityWorkflow->getCurrentStep()->getDestUserGroups() as $userGroup) {
if (!$userGroup->hasEmail()) {
continue;
}
$context = [
'entity_workflow' => $entityWorkflow,
'user_group' => $userGroup,
'place' => $place,
'workflow' => $workflow,
'is_dest' => true,
];
$email = new TemplatedEmail();
$email
->htmlTemplate('@ChillMain/Workflow/workflow_notification_on_transition_completed_content_to_user_group.fr.txt.twig')
->context($context)
->subject(
$this->engine->render('@ChillMain/Workflow/workflow_notification_on_transition_completed_title.fr.txt.twig', $context)
)
->to($userGroup->getEmail());
$this->mailer->send($email);
}
}
}