Add support for handling user groups in workflow counters and list workflows in "my workflows" controller

- rewrite queries in repositories;
- fix cache key cleaning for members of users when a workflow is transitionned
This commit is contained in:
Julien Fastré 2024-09-26 17:44:31 +02:00
parent 87599425df
commit 0c1d9ee4be
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
5 changed files with 76 additions and 1 deletions

View File

@ -12,6 +12,7 @@ declare(strict_types=1);
namespace Chill\MainBundle\Repository\Workflow;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserGroup;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Entity\Workflow\EntityWorkflowStep;
use Doctrine\ORM\EntityManagerInterface;
@ -264,6 +265,7 @@ class EntityWorkflowRepository implements ObjectRepository
$qb->expr()->orX(
$qb->expr()->isMemberOf(':user', 'step.destUser'),
$qb->expr()->isMemberOf(':user', 'step.destUserByAccessKey'),
$qb->expr()->exists(sprintf('SELECT 1 FROM %s ug WHERE ug MEMBER OF step.destUserGroups AND :user MEMBER OF ug.users', UserGroup::class))
),
$qb->expr()->isNull('step.transitionAfter'),
$qb->expr()->eq('step.isFinal', "'FALSE'")

View File

@ -12,6 +12,7 @@ declare(strict_types=1);
namespace Chill\MainBundle\Repository\Workflow;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserGroup;
use Chill\MainBundle\Entity\Workflow\EntityWorkflowStep;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
@ -65,7 +66,11 @@ readonly class EntityWorkflowStepRepository implements ObjectRepository
$qb->where(
$qb->expr()->andX(
$qb->expr()->isMemberOf(':user', 'e.destUser'),
$qb->expr()->orX(
$qb->expr()->isMemberOf(':user', 'e.destUser'),
$qb->expr()->isMemberOf(':user', 'e.destUserByAccessKey'),
$qb->expr()->exists(sprintf('SELECT 1 FROM %s ug WHERE ug MEMBER OF e.destUserGroups AND :user MEMBER OF ug.users', UserGroup::class))
),
$qb->expr()->isNull('e.transitionAt'),
$qb->expr()->eq('e.isFinal', ':bool'),
)

View File

@ -11,6 +11,7 @@ declare(strict_types=1);
namespace ChillMainBundle\Tests\Repository;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
@ -43,4 +44,14 @@ class EntityWorkflowRepositoryTest extends KernelTestCase
self::assertIsArray($actual, 'check that the query is successful');
}
public function testCountQueryByDest(): void
{
$repository = new EntityWorkflowRepository($this->em);
$user = $this->em->createQuery(sprintf('SELECT u FROM %s u', User::class))
->setMaxResults(1)->getSingleResult();
$actual = $repository->countByDest($user);
self::assertIsInt($actual);
}
}

View File

@ -0,0 +1,44 @@
<?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 ChillMainBundle\Tests\Repository;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Repository\Workflow\EntityWorkflowStepRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* @internal
*
* @coversNothing
*/
class EntityWorkflowStepRepositoryTest extends KernelTestCase
{
private EntityManagerInterface $entityManager;
protected function setUp(): void
{
self::bootKernel();
$this->entityManager = self::getContainer()->get(EntityManagerInterface::class);
}
public function testCountUnreadByUser(): void
{
$repository = new EntityWorkflowStepRepository($this->entityManager);
$user = $this->entityManager->createQuery(sprintf('SELECT u FROM %s u', User::class))
->setMaxResults(1)->getSingleResult();
$actual = $repository->countUnreadByUser($user);
self::assertIsInt($actual);
}
}

View File

@ -12,6 +12,7 @@ declare(strict_types=1);
namespace Chill\MainBundle\Workflow\Counter;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserGroup;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Repository\Workflow\EntityWorkflowStepRepository;
use Chill\MainBundle\Templating\UI\NotificationCounterInterface;
@ -82,6 +83,18 @@ final readonly class WorkflowByUserCounter implements NotificationCounterInterfa
foreach ($step->getDestUser() as $user) {
$keys[] = self::generateCacheKeyWorkflowByUser($user);
}
foreach ($step->getDestUserGroups()->reduce(
function (array $accumulator, UserGroup $userGroup) {
foreach ($userGroup->getUsers() as $user) {
$accumulator[] = $user;
}
return $accumulator;
},
[]
) as $user) {
$keys[] = self::generateCacheKeyWorkflowByUser($user);
}
if ([] !== $keys) {
$this->cacheItemPool->deleteItems($keys);