[export] fix acpw socialWork agent ScopeAggregator query + unit test

This commit is contained in:
2023-09-27 14:14:08 +02:00
parent 7c25ca8dd4
commit 0953faedc4
3 changed files with 53 additions and 13 deletions

View File

@@ -11,17 +11,28 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Export\Aggregator\SocialWorkAggregators;
use Chill\MainBundle\Entity\User\UserScopeHistory;
use Chill\MainBundle\Export\AggregatorInterface;
use Chill\MainBundle\Form\Type\PickRollingDateType;
use Chill\MainBundle\Repository\ScopeRepository;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use Chill\MainBundle\Service\RollingDate\RollingDateConverter;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\PersonBundle\Export\Declarations;
use Doctrine\ORM\Query\Expr;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface;
use function in_array;
final readonly class ScopeAggregator implements AggregatorInterface
{
public function __construct(private ScopeRepository $scopeRepository, private TranslatableStringHelper $translatableStringHelper) {}
private const PREFIX = 'acp_work_action_agg_user_scope';
public function __construct(
private RollingDateConverter $rollingDateConverter,
private ScopeRepository $scopeRepository,
private TranslatableStringHelper $translatableStringHelper
) {}
public function addRole(): ?string
{
@@ -30,12 +41,31 @@ final readonly class ScopeAggregator implements AggregatorInterface
public function alterQuery(QueryBuilder $qb, $data)
{
if (!in_array('acpwuser', $qb->getAllAliases(), true)) {
$qb->leftJoin('acpw.referrers', 'acpwuser');
}
$p = self::PREFIX;
$qb->addSelect('IDENTITY(acpwuser.mainScope) as scope_aggregator');
$qb->addGroupBy('scope_aggregator');
$qb
->leftJoin("acpw.referrers", "{$p}_user")
->leftJoin(
UserScopeHistory::class,
"{$p}_history",
Expr\Join::WITH,
$qb->expr()->eq("{$p}_history.user", "{$p}_user")
)
->andWhere(
$qb->expr()->andX(
$qb->expr()->lte("{$p}_history.startDate", ":{$p}_at"),
$qb->expr()->orX(
$qb->expr()->isNull("{$p}_history.endDate"),
$qb->expr()->gt("{$p}_history.endDate", ":{$p}_at")
)
)
)
->addSelect("IDENTITY({$p}_history.scope) as {$p}_select")
->setParameter(
"{$p}_at",
$this->rollingDateConverter->convert($data['scope_at'])
)
->addGroupBy("{$p}_select");
}
public function applyOn(): string
@@ -45,11 +75,14 @@ final readonly class ScopeAggregator implements AggregatorInterface
public function buildForm(FormBuilderInterface $builder)
{
// no form
$builder->add('scope_at', PickRollingDateType::class, [
'label' => 'export.acpw.agent_scope.Calc date',
'required' => true,
]);
}
public function getFormDefaultData(): array
{
return [];
return ['scope_at' => new RollingDate(RollingDate::T_TODAY)];
}
public function getLabels($key, array $values, $data)
@@ -63,7 +96,9 @@ final readonly class ScopeAggregator implements AggregatorInterface
return '';
}
$s = $this->scopeRepository->find($value);
if (null === $s = $this->scopeRepository->find($value)) {
return '';
}
return $this->translatableStringHelper->localize(
$s->getName()
@@ -73,11 +108,11 @@ final readonly class ScopeAggregator implements AggregatorInterface
public function getQueryKeys($data): array
{
return ['scope_aggregator'];
return [self::PREFIX . '_select'];
}
public function getTitle(): string
{
return 'Group by treating agent scope';
return 'export.acpw.agent_scope.Group by treating agent scope';
}
}