feat: complete confirmed dispositions after assignment has ended

This commit is contained in:
Björn Fromme
2024-03-14 12:18:08 +01:00
parent 2c7747fc98
commit 217996d535
4 changed files with 82 additions and 4 deletions
+6 -1
View File
@@ -2,6 +2,7 @@
namespace App\Command;
use App\Service\Cron\DispositionStatusService;
use App\Service\Cron\FeedbackReminderService;
use App\Service\Cron\FlushLogsService;
use App\Service\Cron\UploadReminderService;
@@ -20,7 +21,8 @@ class CronCommand extends Command
public function __construct(
private readonly FlushLogsService $flushLogsService,
private readonly UploadReminderService $uploadReminderService,
private readonly FeedbackReminderService $feedbackReminderService
private readonly FeedbackReminderService $feedbackReminderService,
private readonly DispositionStatusService $dispositionStatusService
) {
parent::__construct();
}
@@ -45,6 +47,9 @@ class CronCommand extends Command
$message = $this->feedbackReminderService->sendFeedbackReminders();
$io->info($message);
$message = $this->dispositionStatusService->setEndedStatus();
$io->info($message);
return Command::SUCCESS;
}
}
+29
View File
@@ -208,4 +208,33 @@ class DispositionRepository extends ServiceEntityRepository
->getResult()
;
}
/**
* @return Disposition[]|array
*/
public function findEndedDispositions(): array
{
$qb = $this->createQueryBuilder('disposition');
return $qb
->innerJoin('disposition.assignment', 'assignment')
->innerJoin('assignment.destination', 'destination')
->where($qb->expr()->andX(
$qb->expr()->neq('disposition.status', ':status'),
$qb->expr()->orX(
$qb->expr()->andX(
$qb->expr()->isNotNull('assignment.dateTo'),
$qb->expr()->lte('assignment.dateTo', ':dateTo')
),
$qb->expr()->lte('destination.dateTo', ':dateTo')
)
))
->setParameters([
'status' => Disposition::STATUS_COMPLETED,
'dateTo' => new \DateTimeImmutable(),
])
->getQuery()
->getResult()
;
}
}
+3 -3
View File
@@ -46,7 +46,7 @@ class DispositionVoter extends Voter
$disposition = $subject;
return match ($attribute) {
static::VIEW, static::EDIT, static::CONTRACT, static::INVOICE => $this->assertAdministrativeAccess() || $this->assertTeamerAccess($token, $disposition),
static::VIEW, static::EDIT, static::CONTRACT, static::INVOICE => $this->assertAdministrativeAccess() || $this->assertTeamerAccess($disposition),
static::DELETE => $this->assertAdminAccess(),
static::FEEDBACK => $this->assertHouseManagerAccess($token, $disposition),
default => false,
@@ -85,12 +85,12 @@ class DispositionVoter extends Voter
&& $destination->getDateTo() < new \DateTimeImmutable();
}
private function assertTeamerAccess(TokenInterface $token, Disposition $disposition): bool
private function assertTeamerAccess(Disposition $disposition): bool
{
if (false === $this->security->isGranted('ROLE_TEAMER')) {
return false;
}
return $token->getUser()->getTeamer() === $disposition->getTeamer();
return $this->security->getUser()->getTeamer() === $disposition->getTeamer();
}
}
@@ -0,0 +1,44 @@
<?php
namespace App\Service\Cron;
use App\Entity\Disposition;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Workflow\WorkflowInterface;
class DispositionStatusService
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly WorkflowInterface $dispositionStateMachine,
private readonly LoggerInterface $logger
) {
}
public function setEndedStatus(): string
{
$endedDispositions = $this
->entityManager
->getRepository(Disposition::class)->findEndedDispositions()
;
if (0 === $count = count($endedDispositions)) {
return 'No ended dispositions to update';
}
foreach ($endedDispositions as $disposition) {
if ($this->dispositionStateMachine->can($disposition, 'complete')) {
$this->dispositionStateMachine->apply($disposition, 'complete');
}
}
$this->entityManager->flush();
$message = 'Ended '.$count.' dispositions';
$this->logger->info($message);
return $message;
}
}