diff --git a/.changes/unreleased/Fixed-20230621-135912.yaml b/.changes/unreleased/Fixed-20230621-135912.yaml new file mode 100644 index 000000000..676d1c21b --- /dev/null +++ b/.changes/unreleased/Fixed-20230621-135912.yaml @@ -0,0 +1,5 @@ +kind: Fixed +body: 'Api: filter social action to keep only the currently activated' +time: 2023-06-21T13:59:12.57760217+02:00 +custom: + Issue: "" diff --git a/src/Bundle/ChillPersonBundle/Controller/SocialWorkSocialActionApiController.php b/src/Bundle/ChillPersonBundle/Controller/SocialWorkSocialActionApiController.php index 4fe877a72..ae70d55f9 100644 --- a/src/Bundle/ChillPersonBundle/Controller/SocialWorkSocialActionApiController.php +++ b/src/Bundle/ChillPersonBundle/Controller/SocialWorkSocialActionApiController.php @@ -16,21 +16,19 @@ use Chill\MainBundle\Pagination\PaginatorFactory; use Chill\MainBundle\Serializer\Model\Collection; use Chill\PersonBundle\Entity\SocialWork\SocialAction; use Chill\PersonBundle\Repository\SocialWork\SocialIssueRepository; +use Symfony\Component\Clock\ClockInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use function count; -class SocialWorkSocialActionApiController extends ApiController +final class SocialWorkSocialActionApiController extends ApiController { - private PaginatorFactory $paginator; - - private SocialIssueRepository $socialIssueRepository; - - public function __construct(SocialIssueRepository $socialIssueRepository, PaginatorFactory $paginator) - { - $this->socialIssueRepository = $socialIssueRepository; - $this->paginator = $paginator; + public function __construct( + private readonly SocialIssueRepository $socialIssueRepository, + private readonly PaginatorFactory $paginator, + private readonly ClockInterface $clock, + ) { } public function listBySocialIssueApi($id, Request $request) @@ -42,7 +40,10 @@ class SocialWorkSocialActionApiController extends ApiController throw $this->createNotFoundException('socialIssue not found'); } - $socialActions = $socialIssue->getRecursiveSocialActions()->toArray(); + $socialActions = SocialAction::filterRemoveDeactivatedActions( + $socialIssue->getRecursiveSocialActions()->toArray(), + \DateTime::createFromImmutable($this->clock->now()) + ); usort($socialActions, static fn (SocialAction $sa, SocialAction $sb) => $sa->getOrdering() <=> $sb->getOrdering()); diff --git a/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialAction.php b/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialAction.php index a8c68c8ce..b5b0f9101 100644 --- a/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialAction.php +++ b/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialAction.php @@ -15,6 +15,7 @@ use DateInterval; use DateTimeInterface; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; +use Doctrine\Common\Collections\ReadableCollection; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Serializer\Annotation as Serializer; @@ -295,6 +296,19 @@ class SocialAction return 0 < $this->getChildren()->count(); } + public function isDesactivated(\DateTime $atDate): bool + { + if (null !== $this->desactivationDate && $this->desactivationDate < $atDate) { + return true; + } + + if ($this->hasParent()) { + return $this->parent->isDesactivated($atDate); + } + + return false; + } + public function hasParent(): bool { return $this->getParent() instanceof self; @@ -401,4 +415,14 @@ class SocialAction return $this; } + + public static function filterRemoveDeactivatedActions(ReadableCollection|array $actions, \DateTime $comparisonDate): ReadableCollection|array + { + $filterFn = fn (SocialAction $socialAction) => !$socialAction->isDesactivated($comparisonDate); + + return match ($actions instanceof ReadableCollection) { + true => $actions->filter($filterFn), + false => array_filter($actions, $filterFn) + }; + } } diff --git a/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialIssue.php b/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialIssue.php index 675a1a923..f12627ae3 100644 --- a/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialIssue.php +++ b/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialIssue.php @@ -253,7 +253,7 @@ class SocialIssue } /** - * @return Collection|SocialAction[] All the descendant social actions of all + * @return Collection All the descendant social actions of all * the descendants of the entity */ public function getRecursiveSocialActions(): Collection @@ -272,7 +272,7 @@ class SocialIssue } /** - * @return Collection|SocialAction[] + * @return Collection */ public function getSocialActions(): Collection { diff --git a/src/Bundle/ChillPersonBundle/Resources/views/SocialWork/SocialAction/index.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/SocialWork/SocialAction/index.html.twig index e78216f04..9e6c6e5aa 100644 --- a/src/Bundle/ChillPersonBundle/Resources/views/SocialWork/SocialAction/index.html.twig +++ b/src/Bundle/ChillPersonBundle/Resources/views/SocialWork/SocialAction/index.html.twig @@ -20,7 +20,7 @@ {{ entity.ordering }} {% if entity.desactivationDate is not null %} - {{ entity.desactivationDate|date('Y-m-d') }} + {{ entity.desactivationDate|format_date('medium') }} {% endif %}