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

This commit is contained in:
Mathieu Jaumotte 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';
}
}

View File

@ -12,6 +12,7 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Tests\Export\Filter\SocialWorkFilters;
use Chill\MainBundle\Entity\Scope;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use Chill\MainBundle\Test\Export\AbstractFilterTest;
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWork;
use Chill\PersonBundle\Export\Filter\SocialWorkFilters\ScopeFilter;
@ -48,7 +49,8 @@ final class ScopeFilterTest extends AbstractFilterTest
return [
[
'scope' => $scopes
'scope' => $scopes,
'scope_at' => new RollingDate(RollingDate::T_FIXED_DATE, \DateTimeImmutable::createFromFormat('Y-m-d', '2020-01-01'))
]
];
}

View File

@ -589,9 +589,6 @@ Filter by current evaluations: Filtrer les évaluations en cours
'Filtered by geographic unit: computed at %date%, only in %units%': 'Filtré par unité géographique: adresse le %date%, seulement les unités %units%'
## social actions filters/aggr
Filter by treating agent scope: Filtrer les actions par service de l'agent traitant
"Filtered by treating agent scope: only %scopes%": "Filtré par service de l'agent traitant: uniquement %scopes%"
Filter by scope: Filtrer par service
Filter by treating agent: Filtrer les actions par agent traitant
@ -1160,6 +1157,10 @@ export:
Filter by treating agent job: Filtrer les actions par métier de l'agent traitant
"Filtered by treating agent job: only %jobs%": "Filtré par métier de l'agent traitant: uniquement %jobs%"
Calc date: Date de calcul du métier de l'agent traitant
by_user_scope:
Filter by treating agent scope: Filtrer les actions par service de l'agent traitant
"Filtered by treating agent scope: only %scopes%": "Filtré par service de l'agent traitant: uniquement %scopes%"
Calc date: Date de calcul du service de l'agent traitant
list:
person_with_acp: