[export] fix calendar jobAggregator query + unit test

This commit is contained in:
Mathieu Jaumotte 2023-09-26 15:33:18 +02:00
parent c5b153e6ed
commit 28583f4193
3 changed files with 46 additions and 12 deletions

View File

@ -12,17 +12,27 @@ declare(strict_types=1);
namespace Chill\CalendarBundle\Export\Aggregator; namespace Chill\CalendarBundle\Export\Aggregator;
use Chill\CalendarBundle\Export\Declarations; use Chill\CalendarBundle\Export\Declarations;
use Chill\MainBundle\Entity\User\UserJobHistory;
use Chill\MainBundle\Export\AggregatorInterface; use Chill\MainBundle\Export\AggregatorInterface;
use Chill\MainBundle\Form\Type\PickRollingDateType;
use Chill\MainBundle\Repository\UserJobRepository; use Chill\MainBundle\Repository\UserJobRepository;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use Chill\MainBundle\Service\RollingDate\RollingDateConverterInterface;
use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\MainBundle\Templating\TranslatableStringHelper;
use Closure; use Closure;
use Doctrine\ORM\Query\Expr;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use function in_array;
final readonly class JobAggregator implements AggregatorInterface final readonly class JobAggregator implements AggregatorInterface
{ {
public function __construct(private UserJobRepository $jobRepository, private TranslatableStringHelper $translatableStringHelper) {} private const PREFIX = 'cal_agg';
public function __construct(
private RollingDateConverterInterface $rollingDateConverter,
private UserJobRepository $jobRepository,
private TranslatableStringHelper $translatableStringHelper
) {}
public function addRole(): ?string public function addRole(): ?string
{ {
@ -31,12 +41,26 @@ final readonly class JobAggregator implements AggregatorInterface
public function alterQuery(QueryBuilder $qb, $data) public function alterQuery(QueryBuilder $qb, $data)
{ {
if (!in_array('caluser', $qb->getAllAliases(), true)) { $p = self::PREFIX;
$qb->join('cal.mainUser', 'caluser');
}
$qb->addSelect('IDENTITY(caluser.userJob) as job_aggregator'); $qb
$qb->addGroupBy('job_aggregator'); ->leftJoin(
'cal.mainUser',
"{$p}_user"
)
->leftJoin(
UserJobHistory::class,
"{$p}_ujh",
Expr\Join::WITH,
$qb->expr()->eq("{$p}_ujh.user", "{$p}_user")
)
->andWhere("{$p}_ujh.startDate <= :{$p}_at AND ({$p}_ujh.endDate IS NULL OR {$p}_ujh.endDate > :{$p}_at)")
->addSelect("IDENTITY({$p}_ujh.job) AS {$p}_select")
->setParameter(
"{$p}_at",
$this->rollingDateConverter->convert($data['job_at'])
)
->addGroupBy("{$p}_select");
} }
public function applyOn(): string public function applyOn(): string
@ -46,11 +70,14 @@ final readonly class JobAggregator implements AggregatorInterface
public function buildForm(FormBuilderInterface $builder) public function buildForm(FormBuilderInterface $builder)
{ {
// no form $builder->add('job_at', PickRollingDateType::class, [
'label' => 'export.aggregator.calendar.Calc date',
]);
} }
public function getFormDefaultData(): array public function getFormDefaultData(): array
{ {
return []; return ['job_at' => new RollingDate(RollingDate::T_TODAY)];
} }
public function getLabels($key, array $values, $data): Closure public function getLabels($key, array $values, $data): Closure
@ -64,7 +91,9 @@ final readonly class JobAggregator implements AggregatorInterface
return ''; return '';
} }
$j = $this->jobRepository->find($value); if (null === $j = $this->jobRepository->find($value)) {
return '';
}
return $this->translatableStringHelper->localize( return $this->translatableStringHelper->localize(
$j->getLabel() $j->getLabel()
@ -74,7 +103,7 @@ final readonly class JobAggregator implements AggregatorInterface
public function getQueryKeys($data): array public function getQueryKeys($data): array
{ {
return ['job_aggregator']; return [self::PREFIX . '_select'];
} }
public function getTitle(): string public function getTitle(): string

View File

@ -20,6 +20,7 @@ namespace Chill\CalendarBundle\Tests\Export\Aggregator;
use Chill\CalendarBundle\Entity\Calendar; use Chill\CalendarBundle\Entity\Calendar;
use Chill\CalendarBundle\Export\Aggregator\JobAggregator; use Chill\CalendarBundle\Export\Aggregator\JobAggregator;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use Chill\MainBundle\Test\Export\AbstractAggregatorTest; use Chill\MainBundle\Test\Export\AbstractAggregatorTest;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
@ -46,7 +47,9 @@ final class JobAggregatorTest extends AbstractAggregatorTest
public function getFormData(): array public function getFormData(): array
{ {
return [ return [
[], [
'job_at' => new RollingDate(RollingDate::T_FIXED_DATE, \DateTimeImmutable::createFromFormat('Y-m-d', '2020-01-01')),
]
]; ];
} }

View File

@ -117,6 +117,8 @@ Group calendars by cancel reason: Grouper les rendez-vous par motif d'annulation
Group calendars by month and year: Grouper les rendez-vous par mois et année Group calendars by month and year: Grouper les rendez-vous par mois et année
Group calendars by urgency: Grouper les rendez-vous par urgent ou non Group calendars by urgency: Grouper les rendez-vous par urgent ou non
export.aggregator.calendar.Calc date: Date de calcul du métier de l'agent
Scope: Service Scope: Service
Job: Métier Job: Métier
Location type: Type de localisation Location type: Type de localisation