Send a notification to all User which are members of UserGroups, when a workflow is sent to them

This commit is contained in:
Julien Fastré 2024-09-26 17:29:27 +02:00
parent 86ec6f82da
commit 87599425df
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
2 changed files with 26 additions and 21 deletions

View File

@ -13,6 +13,7 @@ namespace Chill\MainBundle\Tests\Workflow\EventSubscriber;
use Chill\MainBundle\Entity\Notification;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserGroup;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Entity\Workflow\EntityWorkflowStep;
use Chill\MainBundle\Workflow\EventSubscriber\NotificationOnTransition;
@ -20,8 +21,6 @@ use Chill\MainBundle\Workflow\Helper\MetadataExtractor;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Call\Call;
use Prophecy\Exception\Prediction\FailedPredictionException;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Workflow\Event\Event;
@ -57,29 +56,23 @@ final class NotificationOnTransitionTest extends TestCase
$id->setValue($entityWorkflow, 1);
$step = new EntityWorkflowStep();
$userGroup = (new UserGroup())->addUser($userInGroup = new User())->addUser($dest);
$entityWorkflow->addStep($step);
$step->addDestUser($dest)
$step
->addDestUser($dest)
->addDestUserGroup($userGroup)
->setCurrentStep('to_state');
$em = $this->prophesize(EntityManagerInterface::class);
$em->persist(Argument::type(Notification::class))->should(
static function ($args) use ($dest) {
/** @var Call[] $args */
if (1 !== \count($args)) {
throw new FailedPredictionException('no notification sent');
}
$notification = $args[0]->getArguments()[0];
if (!$notification instanceof Notification) {
throw new FailedPredictionException('persist is not a notification');
}
if (!$notification->getAddressees()->contains($dest)) {
throw new FailedPredictionException('the dest is not notified');
}
}
);
// we check that both notification has been persisted once,
// eliminating doublons
$em->persist(Argument::that(
fn ($notificationCandidate) => $notificationCandidate instanceof Notification && $notificationCandidate->getAddressees()->contains($dest)
))->shouldBeCalledOnce();
$em->persist(Argument::that(
fn ($notificationCandidate) => $notificationCandidate instanceof Notification && $notificationCandidate->getAddressees()->contains($userInGroup)
))->shouldBeCalledOnce();
$engine = $this->prophesize(\Twig\Environment::class);
$engine->render(Argument::type('string'), Argument::type('array'))

View File

@ -13,6 +13,7 @@ namespace Chill\MainBundle\Workflow\EventSubscriber;
use Chill\MainBundle\Entity\Notification;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserGroup;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Workflow\Helper\MetadataExtractor;
use Doctrine\ORM\EntityManagerInterface;
@ -69,7 +70,18 @@ class NotificationOnTransition implements EventSubscriberInterface
// the dests for the current step
$entityWorkflow->getCurrentStep()->getDestUser()->toArray(),
// the cc users for the current step
$entityWorkflow->getCurrentStep()->getCcUser()->toArray()
$entityWorkflow->getCurrentStep()->getCcUser()->toArray(),
// the users within groups
$entityWorkflow->getCurrentStep()->getDestUserGroups()->reduce(
function (array $accumulator, UserGroup $userGroup) {
foreach ($userGroup->getUsers() as $user) {
$accumulator[] = $user;
}
return $accumulator;
},
[]
),
) as $dest) {
$dests[spl_object_hash($dest)] = $dest;
}