diff --git a/src/Entity/Assignment.php b/src/Entity/Assignment.php index f52aa36..a676362 100644 --- a/src/Entity/Assignment.php +++ b/src/Entity/Assignment.php @@ -350,6 +350,19 @@ class Assignment implements BlameableEntityInterface, TimestampableEntityInterfa return $this->applications; } + public function getApplicationByTeamer(Teamer $teamer): ?Application + { + $applications = $this->applications->filter(function (Application $application) use ($teamer) { + return $application->getTeamer() === $teamer; + }); + + if (0 === count($applications)) { + return null; + } + + return $applications->first(); + } + public function getValidApplications(): Collection { return $this->applications->filter(function (Application $application) { diff --git a/src/Repository/AssignmentRepository.php b/src/Repository/AssignmentRepository.php index 30c1c9e..746d5da 100644 --- a/src/Repository/AssignmentRepository.php +++ b/src/Repository/AssignmentRepository.php @@ -50,6 +50,33 @@ class AssignmentRepository extends ServiceEntityRepository public function getListQueryForTeamer(Teamer $teamer, AssignmentFilterDto $filterDto): Query { + // Find ids of assignments with available slots first + $qb = $this->createQueryBuilder('assignment'); + + $availableAssignments = $qb + ->select('assignment.id') + ->leftJoin('assignment.dispositions', 'disposition') + ->innerJoin('assignment.destination', 'destination') + ->where($qb->expr()->andX( + $qb->expr()->orX( + $qb->expr()->gt('destination.dateFrom', ':now'), + $qb->expr()->andX( + $qb->expr()->isNotNull('assignment.dateFrom'), + $qb->expr()->gt('assignment.dateFrom', ':now') + ) + ), + $qb->expr()->isNull('assignment.deletedAt') + )) + ->groupBy('assignment') + ->having($qb->expr()->gt('assignment.availableDispositions', $qb->expr()->count('disposition.id'))) + ->setParameter('now', new \DateTimeImmutable()) + ->getQuery() + ->getArrayResult() + ; + + // Extract ids of available assignments for the following query + $availableAssignmentsIds = array_column($availableAssignments, 'id'); + $qb = $this->createQueryBuilder('assignment'); $qb @@ -64,7 +91,8 @@ class AssignmentRepository extends ServiceEntityRepository $qb->expr()->gt('assignment.dateFrom', ':now') ) ), - $qb->expr()->isNull('assignment.deletedAt') + $qb->expr()->isNull('assignment.deletedAt'), + $qb->expr()->in('assignment.id', $availableAssignmentsIds) )) ->leftJoin('assignment.applications', 'application', Join::WITH, $qb->expr()->eq('application.teamer', ':teamer')) ->leftJoin('assignment.dispositions', 'disposition', Join::WITH, $qb->expr()->eq('disposition.teamer', ':teamer')) diff --git a/templates/teamer/assignment/index.html.twig b/templates/teamer/assignment/index.html.twig index 0c29bd3..bd05dff 100644 --- a/templates/teamer/assignment/index.html.twig +++ b/templates/teamer/assignment/index.html.twig @@ -59,7 +59,7 @@
{% if assignment.applications|length %} - {% set application = assignment.applications[0] %} + {% set application = assignment.applicationByTeamer(teamer) %} {% if application.status == constant('App\\Entity\\Application::STATUS_REJECTED') %}