[Feature] add active property to evaluation

This commit is contained in:
Julien Fastré 2022-10-13 15:22:48 +02:00
parent 1ba9f0365f
commit 5a94ce49f1
7 changed files with 201 additions and 4 deletions

View File

@ -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()
);

View File

@ -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;

View File

@ -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,
]);
}

View File

@ -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

View File

@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\PersonBundle\Repository\SocialWork;
use Chill\PersonBundle\Entity\SocialWork\Evaluation;
use Doctrine\Persistence\ObjectRepository;
interface EvaluationRepositoryInterface extends ObjectRepository
{
public function find($id, ?int $lockMode = null, ?int $lockVersion = null): ?Evaluation;
/**
* @return array<int, Evaluation>
*/
public function findAll(): array;
/**
* @return array<int, Evaluation>
*/
public function findAllActive(): array;
/**
* @param mixed|null $limit
* @param mixed|null $offset
*
* @return array<int, Evaluation>
*/
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;
}

View File

@ -0,0 +1,86 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\PersonBundle\Tests\Controller;
use Chill\MainBundle\Test\PrepareClientTrait;
use Chill\PersonBundle\Entity\SocialWork\Evaluation;
use Chill\PersonBundle\Entity\SocialWork\SocialAction;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* @internal
* @coversNothing
*/
final class SocialWorkEvaluationApiControllerTest extends WebTestCase
{
use PrepareClientTrait;
private EntityManagerInterface $em;
private ?Evaluation $evaluationToReset = null;
protected function tearDown(): void
{
if (null === $this->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);
}
}

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\Migrations\Person;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20221013131221 extends AbstractMigration
{
public function down(Schema $schema): void
{
$this->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');
}
}