[export] fix acpw socialWork agent ScopeFilter query + unit test (partial)

This commit is contained in:
2023-09-27 16:13:11 +02:00
parent bc69f83c37
commit aeb0d5eab8
5 changed files with 65 additions and 36 deletions

View File

@@ -120,7 +120,7 @@ class UserJobFilter implements FilterInterface
];
}
public function getTitle():string
public function getTitle(): string
{
return 'export.filter.course.by_user_job.Filter by user job';
}

View File

@@ -121,7 +121,7 @@ class UserScopeFilter implements FilterInterface
];
}
public function getTitle():string
public function getTitle(): string
{
return 'export.filter.course.by_user_scope.Filter by user scope';
}

View File

@@ -12,19 +12,28 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Export\Filter\SocialWorkFilters;
use Chill\MainBundle\Entity\Scope;
use Chill\MainBundle\Entity\User\UserScopeHistory;
use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Form\Type\PickRollingDateType;
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\Andx;
use Doctrine\ORM\Query\Expr;
use Doctrine\ORM\QueryBuilder;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use function in_array;
class ScopeFilter implements FilterInterface
{
public function __construct(protected TranslatorInterface $translator, private readonly TranslatableStringHelper $translatableStringHelper) {}
private const PREFIX = 'acp_work_action_filter_user_scope';
public function __construct(
private readonly RollingDateConverter $rollingDateConverter,
protected TranslatorInterface $translator,
private readonly TranslatableStringHelper $translatableStringHelper
) {}
public function addRole(): ?string
{
@@ -33,21 +42,32 @@ class ScopeFilter implements FilterInterface
public function alterQuery(QueryBuilder $qb, $data)
{
if (!in_array('acpwuser', $qb->getAllAliases(), true)) {
$qb->join('acpw.referrers', 'acpwuser');
}
$p = self::PREFIX;
$where = $qb->getDQLPart('where');
$clause = $qb->expr()->in('acpwuser.mainScope', ':scope');
if ($where instanceof Andx) {
$where->add($clause);
} else {
$where = $qb->expr()->andX($clause);
}
$qb->add('where', $where);
$qb->setParameter('scope', $data['scope']);
$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")
)
)
)
->andWhere(
$qb->expr()->in("{$p}_history.scope", ":{$p}_scope")
)
->setParameters([
["{$p}_scope", $data["scope"]],
["{$p}_at", $this->rollingDateConverter->convert($data['scope_at'])]
]);
}
public function applyOn()
@@ -57,21 +77,27 @@ class ScopeFilter implements FilterInterface
public function buildForm(FormBuilderInterface $builder)
{
$builder->add('scope', EntityType::class, [
'class' => Scope::class,
'choice_label' => fn (Scope $s) => $this->translatableStringHelper->localize(
$s->getName()
),
'multiple' => true,
'expanded' => true,
]);
$builder
->add('scope', EntityType::class, [
'class' => Scope::class,
'choice_label' => fn (Scope $s) => $this->translatableStringHelper->localize(
$s->getName()
),
'multiple' => true,
'expanded' => true,
])
->add('scope_at', PickRollingDateType::class, [
'label' => 'export.filter.work.by_user_scope.Calc date',
'required' => true,
])
;
}
public function getFormDefaultData(): array
{
return [];
return ['scope_at' => new RollingDate(RollingDate::T_TODAY)];
}
public function describeAction($data, $format = 'string')
public function describeAction($data, $format = 'string'): array
{
$scopes = [];
@@ -81,13 +107,13 @@ class ScopeFilter implements FilterInterface
);
}
return ['Filtered by treating agent scope: only %scopes%', [
return ['export.filter.work.by_user_scope.Filtered by treating agent scope: only %scopes%', [
'%scopes%' => implode(', ', $scopes),
]];
}
public function getTitle()
public function getTitle(): string
{
return 'Filter by treating agent scope';
return 'export.filter.work.by_user_scope.Filter by treating agent scope';
}
}