diff --git a/src/Bundle/ChillPersonBundle/Controller/PersonApiController.php b/src/Bundle/ChillPersonBundle/Controller/PersonApiController.php index 6e77ae69e..5be598426 100644 --- a/src/Bundle/ChillPersonBundle/Controller/PersonApiController.php +++ b/src/Bundle/ChillPersonBundle/Controller/PersonApiController.php @@ -22,6 +22,7 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; +use function in_array; class PersonApiController extends ApiController { @@ -79,6 +80,8 @@ class PersonApiController extends ApiController { $this->denyAccessUnlessGranted(PersonVoter::SEE, $person); + $seenAddressIds = []; + // collect addresses from location in courses $addresses = $person ->getAccompanyingPeriodParticipations() @@ -91,13 +94,30 @@ class PersonApiController extends ApiController static function (AccompanyingPeriodParticipation $accompanyingPeriodParticipation): ?Address { return $accompanyingPeriodParticipation->getAccompanyingPeriod()->getAddressLocation(); } + ) + ->filter( + // We remove potential null addresses. + static fn (?Address $address): bool => null !== $address + ) + ->filter( + static function (Address $address) use (&$seenAddressIds): bool { + $id = $address->getId(); + + if (in_array($id, $seenAddressIds, true)) { + return false; + } + + $seenAddressIds[] = $id; + + return true; + } ); // remove the actual address $actual = $person->getCurrentHouseholdAddress(); if (null !== $actual) { - $addresses = $addresses->filter(static fn (?Address $address): bool => $address !== $actual); + $addresses = $addresses->filter(static fn (Address $address): bool => $address !== $actual); } return $this->json(