From 2feb137ac2eea12e686f4cd76bb184d54ba17e12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 13 Jan 2026 15:32:07 +0000 Subject: [PATCH] Fix issue with goal/result deactivation date handling and improve formatting --- .../unreleased/Fixed-20260112-153337.yaml | 6 +++ .../SocialWorkEvaluationApiController.php | 2 +- .../SocialWorkGoalApiController.php | 7 ++-- .../SocialWorkResultApiController.php | 10 +++-- .../Repository/SocialWork/GoalRepository.php | 23 +++++++---- .../SocialWork/ResultRepository.php | 38 +++++++++++++------ .../views/SocialWork/Goal/index.html.twig | 2 +- .../views/SocialWork/Result/index.html.twig | 2 +- 8 files changed, 61 insertions(+), 29 deletions(-) create mode 100644 .changes/unreleased/Fixed-20260112-153337.yaml diff --git a/.changes/unreleased/Fixed-20260112-153337.yaml b/.changes/unreleased/Fixed-20260112-153337.yaml new file mode 100644 index 000000000..2e947f438 --- /dev/null +++ b/.changes/unreleased/Fixed-20260112-153337.yaml @@ -0,0 +1,6 @@ +kind: Fixed +body: Fix desactivation date for Goals and results +time: 2026-01-12T15:33:37.95108325+01:00 +custom: + Issue: "489" + SchemaChange: No schema change diff --git a/src/Bundle/ChillPersonBundle/Controller/SocialWorkEvaluationApiController.php b/src/Bundle/ChillPersonBundle/Controller/SocialWorkEvaluationApiController.php index 27a7acbf5..8c138d44a 100644 --- a/src/Bundle/ChillPersonBundle/Controller/SocialWorkEvaluationApiController.php +++ b/src/Bundle/ChillPersonBundle/Controller/SocialWorkEvaluationApiController.php @@ -38,7 +38,7 @@ class SocialWorkEvaluationApiController extends AbstractController $pagination->getCurrentPageFirstItemNumber(), $pagination->getItemsPerPage() ); - $collection = new Collection($evaluations, $pagination); + $collection = new Collection(array_values($evaluations), $pagination); return $this->json($collection, Response::HTTP_OK, [], ['groups' => ['read']]); } diff --git a/src/Bundle/ChillPersonBundle/Controller/SocialWorkGoalApiController.php b/src/Bundle/ChillPersonBundle/Controller/SocialWorkGoalApiController.php index 2a7f39712..b0da74c7f 100644 --- a/src/Bundle/ChillPersonBundle/Controller/SocialWorkGoalApiController.php +++ b/src/Bundle/ChillPersonBundle/Controller/SocialWorkGoalApiController.php @@ -25,14 +25,15 @@ class SocialWorkGoalApiController extends ApiController public function listBySocialAction(Request $request, SocialAction $action): Response { - $totalItems = $this->goalRepository->countBySocialActionWithDescendants($action); - $paginator = $this->getPaginatorFactory()->create($totalItems); + $totalItems = $this->goalRepository->countBySocialActionWithDescendants($action, true); + $paginator = $this->paginator->create($totalItems); $entities = $this->goalRepository->findBySocialActionWithDescendants( $action, ['id' => 'ASC'], $paginator->getItemsPerPage(), - $paginator->getCurrentPageFirstItemNumber() + $paginator->getCurrentPageFirstItemNumber(), + onlyActive: true ); $model = new Collection($entities, $paginator); diff --git a/src/Bundle/ChillPersonBundle/Controller/SocialWorkResultApiController.php b/src/Bundle/ChillPersonBundle/Controller/SocialWorkResultApiController.php index 9b97d8129..c758475be 100644 --- a/src/Bundle/ChillPersonBundle/Controller/SocialWorkResultApiController.php +++ b/src/Bundle/ChillPersonBundle/Controller/SocialWorkResultApiController.php @@ -25,14 +25,15 @@ class SocialWorkResultApiController extends ApiController public function listByGoal(Request $request, Goal $goal): Response { - $totalItems = $this->resultRepository->countByGoal($goal); + $totalItems = $this->resultRepository->countByGoal($goal, true); $paginator = $this->getPaginatorFactory()->create($totalItems); $entities = $this->resultRepository->findByGoal( $goal, ['id' => 'ASC'], $paginator->getItemsPerPage(), - $paginator->getCurrentPageFirstItemNumber() + $paginator->getCurrentPageFirstItemNumber(), + onlyActive: true, ); $model = new Collection($entities, $paginator); @@ -42,14 +43,15 @@ class SocialWorkResultApiController extends ApiController public function listBySocialAction(Request $request, SocialAction $action): Response { - $totalItems = $this->resultRepository->countBySocialActionWithDescendants($action); + $totalItems = $this->resultRepository->countBySocialActionWithDescendants($action, true); $paginator = $this->getPaginatorFactory()->create($totalItems); $entities = $this->resultRepository->findBySocialActionWithDescendants( $action, ['id' => 'ASC'], $paginator->getItemsPerPage(), - $paginator->getCurrentPageFirstItemNumber() + $paginator->getCurrentPageFirstItemNumber(), + onlyActive: true ); $model = new Collection($entities, $paginator); diff --git a/src/Bundle/ChillPersonBundle/Repository/SocialWork/GoalRepository.php b/src/Bundle/ChillPersonBundle/Repository/SocialWork/GoalRepository.php index ac25445f4..13cfed9a7 100644 --- a/src/Bundle/ChillPersonBundle/Repository/SocialWork/GoalRepository.php +++ b/src/Bundle/ChillPersonBundle/Repository/SocialWork/GoalRepository.php @@ -20,19 +20,23 @@ use Doctrine\ORM\NoResultException; use Doctrine\ORM\QueryBuilder; use Doctrine\Persistence\ObjectRepository; use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\Clock\ClockInterface; final readonly class GoalRepository implements ObjectRepository { private EntityRepository $repository; - public function __construct(private EntityManagerInterface $entityManager, private RequestStack $requestStack) - { + public function __construct( + private EntityManagerInterface $entityManager, + private ClockInterface $clock, + private RequestStack $requestStack, + ) { $this->repository = $entityManager->getRepository(Goal::class); } - public function countBySocialActionWithDescendants(SocialAction $action): int + public function countBySocialActionWithDescendants(SocialAction $action, bool $onlyActive = false): int { - $qb = $this->buildQueryBySocialActionWithDescendants($action); + $qb = $this->buildQueryBySocialActionWithDescendants($action, $onlyActive); $qb->select('COUNT(g)'); return $qb @@ -67,9 +71,9 @@ final readonly class GoalRepository implements ObjectRepository /** * @return Goal[] */ - public function findBySocialActionWithDescendants(SocialAction $action, array $orderBy = [], ?int $limit = null, ?int $offset = null): array + public function findBySocialActionWithDescendants(SocialAction $action, array $orderBy = [], ?int $limit = null, ?int $offset = null, bool $onlyActive = false): array { - $qb = $this->buildQueryBySocialActionWithDescendants($action); + $qb = $this->buildQueryBySocialActionWithDescendants($action, $onlyActive); $qb->select('g'); $qb->andWhere( @@ -200,7 +204,7 @@ final readonly class GoalRepository implements ObjectRepository } } - private function buildQueryBySocialActionWithDescendants(SocialAction $action): QueryBuilder + private function buildQueryBySocialActionWithDescendants(SocialAction $action, bool $onlyActive): QueryBuilder { $actions = $action->getDescendantsWithThis(); @@ -215,6 +219,11 @@ final readonly class GoalRepository implements ObjectRepository } $qb->where($orx); + if ($onlyActive) { + $qb->andWhere('g.desactivationDate > :now OR g.desactivationDate IS NULL') + ->setParameter('now', $this->clock->now()); + } + return $qb; } } diff --git a/src/Bundle/ChillPersonBundle/Repository/SocialWork/ResultRepository.php b/src/Bundle/ChillPersonBundle/Repository/SocialWork/ResultRepository.php index 9e80ec491..4f0be3977 100644 --- a/src/Bundle/ChillPersonBundle/Repository/SocialWork/ResultRepository.php +++ b/src/Bundle/ChillPersonBundle/Repository/SocialWork/ResultRepository.php @@ -21,19 +21,23 @@ use Doctrine\ORM\NoResultException; use Doctrine\ORM\QueryBuilder; use Doctrine\Persistence\ObjectRepository; use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\Clock\ClockInterface; final readonly class ResultRepository implements ObjectRepository { private EntityRepository $repository; - public function __construct(private EntityManagerInterface $entityManager, private RequestStack $requestStack) - { + public function __construct( + private EntityManagerInterface $entityManager, + private ClockInterface $clock, + private RequestStack $requestStack, + ) { $this->repository = $entityManager->getRepository(Result::class); } - public function countByGoal(Goal $goal): int + public function countByGoal(Goal $goal, bool $onlyActive = false): int { - $qb = $this->buildQueryByGoal($goal); + $qb = $this->buildQueryByGoal($goal, $onlyActive); $qb->select('COUNT(r)'); return $qb @@ -41,9 +45,9 @@ final readonly class ResultRepository implements ObjectRepository ->getSingleScalarResult(); } - public function countBySocialActionWithDescendants(SocialAction $action): int + public function countBySocialActionWithDescendants(SocialAction $action, bool $onlyActive = false): int { - $qb = $this->buildQueryBySocialActionWithDescendants($action); + $qb = $this->buildQueryBySocialActionWithDescendants($action, $onlyActive); $qb->select('COUNT(r)'); return $qb @@ -78,9 +82,9 @@ final readonly class ResultRepository implements ObjectRepository /** * @return array */ - public function findByGoal(Goal $goal, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array + public function findByGoal(Goal $goal, ?array $orderBy = null, ?int $limit = null, ?int $offset = null, bool $onlyActive = false): array { - $qb = $this->buildQueryByGoal($goal); + $qb = $this->buildQueryByGoal($goal, $onlyActive); if (null !== $orderBy) { foreach ($orderBy as $sort => $order) { @@ -99,9 +103,9 @@ final readonly class ResultRepository implements ObjectRepository /** * @return Result[] */ - public function findBySocialActionWithDescendants(SocialAction $action, array $orderBy = [], ?int $limit = null, ?int $offset = null): array + public function findBySocialActionWithDescendants(SocialAction $action, array $orderBy = [], ?int $limit = null, ?int $offset = null, bool $onlyActive = false): array { - $qb = $this->buildQueryBySocialActionWithDescendants($action); + $qb = $this->buildQueryBySocialActionWithDescendants($action, $onlyActive); $qb->select('r'); foreach ($orderBy as $sort => $order) { @@ -222,17 +226,22 @@ final readonly class ResultRepository implements ObjectRepository } } - private function buildQueryByGoal(Goal $goal): QueryBuilder + private function buildQueryByGoal(Goal $goal, bool $onlyActive): QueryBuilder { $qb = $this->repository->createQueryBuilder('r'); $qb->where(':goal MEMBER OF r.goals') ->setParameter('goal', $goal); + if ($onlyActive) { + $qb->andWhere('r.desactivationDate > :now OR r.desactivationDate IS NULL') + ->setParameter('now', $this->clock->now()); + } + return $qb; } - private function buildQueryBySocialActionWithDescendants(SocialAction $action): QueryBuilder + private function buildQueryBySocialActionWithDescendants(SocialAction $action, bool $onlyActive = false): QueryBuilder { $actions = $action->getDescendantsWithThis(); @@ -247,6 +256,11 @@ final readonly class ResultRepository implements ObjectRepository } $qb->where($orx); + if ($onlyActive) { + $qb->andWhere('r.desactivationDate > :now OR r.desactivationDate IS NULL') + ->setParameter('now', $this->clock->now()); + } + return $qb; } } diff --git a/src/Bundle/ChillPersonBundle/Resources/views/SocialWork/Goal/index.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/SocialWork/Goal/index.html.twig index eac9d4bfc..f6f9a84c5 100644 --- a/src/Bundle/ChillPersonBundle/Resources/views/SocialWork/Goal/index.html.twig +++ b/src/Bundle/ChillPersonBundle/Resources/views/SocialWork/Goal/index.html.twig @@ -25,7 +25,7 @@ {% if entity.desactivationDate is not null %} - {{ entity.desactivationDate|date('Y-m-d') }} + {{ entity.desactivationDate|format_date('medium') }} {% endif %} diff --git a/src/Bundle/ChillPersonBundle/Resources/views/SocialWork/Result/index.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/SocialWork/Result/index.html.twig index 85286ab5b..cd4f24b5a 100644 --- a/src/Bundle/ChillPersonBundle/Resources/views/SocialWork/Result/index.html.twig +++ b/src/Bundle/ChillPersonBundle/Resources/views/SocialWork/Result/index.html.twig @@ -19,7 +19,7 @@ {{ entity.title|localize_translatable_string }} {% if entity.desactivationDate is not null %} - {{ entity.desactivationDate|date('Y-m-d') }} + {{ entity.desactivationDate|format_date('medium') }} {% endif %}