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

This commit is contained in:
2023-09-27 15:57:31 +02:00
parent 9b272e9b9e
commit bc69f83c37
3 changed files with 62 additions and 31 deletions

View File

@@ -11,10 +11,15 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Export\Filter\SocialWorkFilters;
use Chill\MainBundle\Entity\User\UserJobHistory;
use Chill\MainBundle\Entity\UserJob;
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;
use Doctrine\ORM\Query\Expr\Andx;
use Doctrine\ORM\QueryBuilder;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
@@ -24,7 +29,13 @@ use function in_array;
class JobFilter implements FilterInterface
{
public function __construct(protected TranslatorInterface $translator, private readonly TranslatableStringHelper $translatableStringHelper) {}
private const PREFIX = 'acp_work_action_filter_user_job';
public function __construct(
private readonly RollingDateConverter $rollingDateConverter,
protected TranslatorInterface $translator,
private readonly TranslatableStringHelper $translatableStringHelper
) {}
public function addRole(): ?string
{
@@ -33,42 +44,59 @@ class JobFilter 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.userJob', ':job');
if ($where instanceof Andx) {
$where->add($clause);
} else {
$where = $qb->expr()->andX($clause);
}
$qb->add('where', $where);
$qb->setParameter('job', $data['job']);
$qb
->leftJoin("acpw.referrers", "{$p}_user")
->leftJoin(
UserJobHistory::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.job", ":{$p}_job")
)
->setParameters([
["{$p}_job", $data["job"]],
["{$p}_at", $this->rollingDateConverter->convert($data['job_at'])]
]);
}
public function applyOn()
public function applyOn(): string
{
return Declarations::SOCIAL_WORK_ACTION_TYPE;
}
public function buildForm(FormBuilderInterface $builder)
{
$builder->add('job', EntityType::class, [
'class' => UserJob::class,
'choice_label' => fn (UserJob $j) => $this->translatableStringHelper->localize(
$j->getLabel()
),
'multiple' => true,
'expanded' => true,
]);
$builder
->add('job', EntityType::class, [
'class' => UserJob::class,
'choice_label' => fn (UserJob $j) => $this->translatableStringHelper->localize(
$j->getLabel()
),
'multiple' => true,
'expanded' => true,
])
->add('job_at', PickRollingDateType::class, [
'label' => 'export.filter.work.by_user_job.Calc date',
'required' => true,
])
;
}
public function getFormDefaultData(): array
{
return [];
return ['job_at' => new RollingDate(RollingDate::T_TODAY)];
}
public function describeAction($data, $format = 'string')
@@ -81,13 +109,13 @@ class JobFilter implements FilterInterface
);
}
return ['Filtered by treating agent job: only %jobs%', [
return ['export.filter.work.by_user_job.Filtered by treating agent job: only %jobs%', [
'%jobs%' => implode(', ', $userjobs),
]];
}
public function getTitle(): string
{
return 'Filter by treating agent job';
return 'export.filter.work.by_user_job.Filter by treating agent job';
}
}