[export] use a rolling date on age aggregator (Person)

This query allow to detects the saved export which won't work any more:

```sql
select
s.id, user_id, description, title, u.label
from chill_main_saved_export s join users u on u.id = s.user_id
WHERE
options->'export'->'export'->'aggregators'->'person_age_aggregator'->'enabled' is not null;
```
This commit is contained in:
Julien Fastré 2023-06-29 23:15:15 +02:00
parent c019fffbe7
commit b7df62d4f5
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
2 changed files with 16 additions and 20 deletions

View File

@ -0,0 +1,5 @@
kind: Fixed
body: '[export] set rolling date on person age aggregator'
time: 2023-06-29T23:15:03.20841309+02:00
custom:
Issue: ""

View File

@ -13,20 +13,18 @@ namespace Chill\PersonBundle\Export\Aggregator\PersonAggregators;
use Chill\MainBundle\Export\AggregatorInterface; use Chill\MainBundle\Export\AggregatorInterface;
use Chill\MainBundle\Export\ExportElementValidatedInterface; use Chill\MainBundle\Export\ExportElementValidatedInterface;
use DateTime; use Chill\MainBundle\Form\Type\PickRollingDateType;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use Chill\MainBundle\Service\RollingDate\RollingDateConverterInterface;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Context\ExecutionContextInterface; use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
final class AgeAggregator implements AggregatorInterface, ExportElementValidatedInterface final readonly class AgeAggregator implements AggregatorInterface, ExportElementValidatedInterface
{ {
private TranslatorInterface $translator; public function __construct(
private RollingDateConverterInterface $rollingDateConverter,
public function __construct(TranslatorInterface $translator) ) {
{
$this->translator = $translator;
} }
public function addRole(): ?string public function addRole(): ?string
@ -37,7 +35,7 @@ final class AgeAggregator implements AggregatorInterface, ExportElementValidated
public function alterQuery(QueryBuilder $qb, $data) public function alterQuery(QueryBuilder $qb, $data)
{ {
$qb->addSelect('DATE_DIFF(:date_age_calculation, person.birthdate)/365 as person_age'); $qb->addSelect('DATE_DIFF(:date_age_calculation, person.birthdate)/365 as person_age');
$qb->setParameter('date_age_calculation', $data['date_age_calculation']); $qb->setParameter('date_age_calculation', $this->rollingDateConverter->convert($data['date_age_calculation']));
$qb->addGroupBy('person_age'); $qb->addGroupBy('person_age');
} }
@ -48,16 +46,13 @@ final class AgeAggregator implements AggregatorInterface, ExportElementValidated
public function buildForm(FormBuilderInterface $builder) public function buildForm(FormBuilderInterface $builder)
{ {
$builder->add('date_age_calculation', DateType::class, [ $builder->add('date_age_calculation', PickRollingDateType::class, [
'label' => 'Calculate age in relation to this date', 'label' => 'Calculate age in relation to this date',
'attr' => ['class' => 'datepicker'],
'widget' => 'single_text',
'format' => 'dd-MM-yyyy',
]); ]);
} }
public function getFormDefaultData(): array public function getFormDefaultData(): array
{ {
return ['date_age_calculation' => new DateTime()]; return ['date_age_calculation' => new RollingDate(RollingDate::T_TODAY)];
} }
public function getLabels($key, array $values, $data) public function getLabels($key, array $values, $data)
@ -67,11 +62,7 @@ final class AgeAggregator implements AggregatorInterface, ExportElementValidated
return 'Age'; return 'Age';
} }
if (null === $value) { return $value ?? '';
return $this->translator->trans('without data');
}
return $value;
}; };
} }