diff --git a/src/Bundle/ChillPersonBundle/Controller/SocialWorkEvaluationApiController.php b/src/Bundle/ChillPersonBundle/Controller/SocialWorkEvaluationApiController.php index 41ca55d0e..bd12ec8c2 100644 --- a/src/Bundle/ChillPersonBundle/Controller/SocialWorkEvaluationApiController.php +++ b/src/Bundle/ChillPersonBundle/Controller/SocialWorkEvaluationApiController.php @@ -13,6 +13,7 @@ namespace Chill\PersonBundle\Controller; use Chill\MainBundle\Pagination\PaginatorFactory; use Chill\MainBundle\Serializer\Model\Collection; +use Chill\PersonBundle\Entity\SocialWork\Evaluation; use Chill\PersonBundle\Entity\SocialWork\SocialAction; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; @@ -39,9 +40,11 @@ class SocialWorkEvaluationApiController extends AbstractController */ public function listEvaluationBySocialAction(SocialAction $action): Response { - $pagination = $this->paginatorFactory->create($action->getEvaluations()->count()); + $evaluations = $action->getEvaluations()->filter(static fn (Evaluation $eval) => $eval->isActive()); - $evaluations = $action->getEvaluations()->slice( + $pagination = $this->paginatorFactory->create($evaluations->count()); + + $evaluations = $evaluations->slice( $pagination->getCurrentPageFirstItemNumber(), $pagination->getItemsPerPage() ); diff --git a/src/Bundle/ChillPersonBundle/Entity/SocialWork/Evaluation.php b/src/Bundle/ChillPersonBundle/Entity/SocialWork/Evaluation.php index 3324a236f..08dc0f425 100644 --- a/src/Bundle/ChillPersonBundle/Entity/SocialWork/Evaluation.php +++ b/src/Bundle/ChillPersonBundle/Entity/SocialWork/Evaluation.php @@ -26,6 +26,11 @@ use Symfony\Component\Serializer\Annotation as Serializer; */ class Evaluation { + /** + * @ORM\Column(type="boolean", nullable=false, options={"default": true}) + */ + private bool $active = true; + /** * @ORM\Column(type="dateinterval", nullable=true, options={"default": null}) * @Serializer\Groups({"read"}) @@ -114,6 +119,11 @@ class Evaluation return $this->url; } + public function isActive(): bool + { + return $this->active; + } + /** * @return $this * @@ -128,6 +138,13 @@ class Evaluation return $this; } + public function setActive(bool $active): Evaluation + { + $this->active = $active; + + return $this; + } + public function setDelay(?DateInterval $delay): self { $this->delay = $delay; diff --git a/src/Bundle/ChillPersonBundle/Form/SocialWork/EvaluationType.php b/src/Bundle/ChillPersonBundle/Form/SocialWork/EvaluationType.php index 685915203..668a00276 100644 --- a/src/Bundle/ChillPersonBundle/Form/SocialWork/EvaluationType.php +++ b/src/Bundle/ChillPersonBundle/Form/SocialWork/EvaluationType.php @@ -16,6 +16,7 @@ use Chill\MainBundle\Form\Type\TranslatableStringFormType; use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\PersonBundle\Entity\SocialWork\Evaluation; use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\UrlType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -52,6 +53,14 @@ class EvaluationType extends AbstractType ->add('notificationDelay', DateIntervalType::class, [ 'label' => 'evaluation.notificationDelay', 'required' => false, + ]) + ->add('active', ChoiceType::class, [ + 'label' => 'active', + 'choices' => [ + 'active' => true, + 'inactive' => false, + ], + 'required' => true, ]); } diff --git a/src/Bundle/ChillPersonBundle/Repository/SocialWork/EvaluationRepository.php b/src/Bundle/ChillPersonBundle/Repository/SocialWork/EvaluationRepository.php index b02fef8ba..925d4598b 100644 --- a/src/Bundle/ChillPersonBundle/Repository/SocialWork/EvaluationRepository.php +++ b/src/Bundle/ChillPersonBundle/Repository/SocialWork/EvaluationRepository.php @@ -14,9 +14,8 @@ namespace Chill\PersonBundle\Repository\SocialWork; use Chill\PersonBundle\Entity\SocialWork\Evaluation; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; -use Doctrine\Persistence\ObjectRepository; -final class EvaluationRepository implements ObjectRepository +final class EvaluationRepository implements EvaluationRepositoryInterface { private EntityRepository $repository; @@ -38,6 +37,11 @@ final class EvaluationRepository implements ObjectRepository return $this->repository->findAll(); } + public function findAllActive(): array + { + return $this->findBy(['active' => true]); + } + /** * @param mixed|null $limit * @param mixed|null $offset diff --git a/src/Bundle/ChillPersonBundle/Repository/SocialWork/EvaluationRepositoryInterface.php b/src/Bundle/ChillPersonBundle/Repository/SocialWork/EvaluationRepositoryInterface.php new file mode 100644 index 000000000..9ca390ff9 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Repository/SocialWork/EvaluationRepositoryInterface.php @@ -0,0 +1,45 @@ + + */ + public function findAll(): array; + + /** + * @return array + */ + public function findAllActive(): array; + + /** + * @param mixed|null $limit + * @param mixed|null $offset + * + * @return array + */ + public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null): array; + + public function findOneBy(array $criteria, ?array $orderBy = null): ?Evaluation; + + /** + * @return class-string + */ + public function getClassName(): string; +} diff --git a/src/Bundle/ChillPersonBundle/Tests/Controller/SocialWorkEvaluationApiControllerTest.php b/src/Bundle/ChillPersonBundle/Tests/Controller/SocialWorkEvaluationApiControllerTest.php new file mode 100644 index 000000000..aa98fdcd3 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Tests/Controller/SocialWorkEvaluationApiControllerTest.php @@ -0,0 +1,86 @@ +evaluationToReset) { + return; + } + + self::bootKernel(); + + $em = self::$container->get(EntityManagerInterface::class); + $evaluation = $em->find(Evaluation::class, $this->evaluationToReset->getId()); + + $evaluation->setActive(true); + $em->flush(); + } + + public function dataGenerateSocialActionWithEvaluations(): iterable + { + self::bootKernel(); + + $this->em = self::$container->get(EntityManagerInterface::class); + + /** @var SocialAction $socialAction */ + $socialAction = $this->em->createQuery( + 'SELECT s FROM ' . SocialAction::class . ' s WHERE SIZE(s.evaluations) >= 2' + ) + ->setMaxResults(1) + ->getSingleResult(); + + // set the first evaluation as inactive and save + $this->evaluationToReset = $socialAction->getEvaluations()->first(); + $this->evaluationToReset->setActive(false); + + $this->em->flush(); + + yield [$socialAction, $this->evaluationToReset]; + } + + /** + * @dataProvider dataGenerateSocialActionWithEvaluations + */ + public function testListEvaluationBySocialAction(SocialAction $action, Evaluation $inactiveEvaluation): void + { + $client = $this->getClientAuthenticated(); + + $client->request('GET', sprintf('/api/1.0/person/social-work/evaluation/by-social-action/%d.json', $action->getId())); + + $this->assertResponseIsSuccessful(); + + $content = json_decode($client->getResponse()->getContent(), true); + + $ids = array_map(static fn (array $item) => $item['id'], $content['results']); + + $this->assertNotContains($inactiveEvaluation->getId(), $ids); + } +} diff --git a/src/Bundle/ChillPersonBundle/migrations/Version20221013131221.php b/src/Bundle/ChillPersonBundle/migrations/Version20221013131221.php new file mode 100644 index 000000000..e8cb0d392 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/migrations/Version20221013131221.php @@ -0,0 +1,33 @@ +addSql('ALTER TABLE chill_person_social_work_evaluation DROP active'); + } + + public function getDescription(): string + { + return 'Add an active column on evaluation'; + } + + public function up(Schema $schema): void + { + $this->addSql('ALTER TABLE chill_person_social_work_evaluation ADD active BOOLEAN DEFAULT true NOT NULL'); + } +}