This commit is contained in:
Julien Fastré 2022-01-23 23:22:04 +01:00
parent 7e2fbf93f9
commit eccc75aecf
5 changed files with 65 additions and 55 deletions

View File

@ -18,8 +18,6 @@ use Chill\MainBundle\Serializer\Model\Collection;
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation;
use Chill\PersonBundle\Entity\SocialWork\Evaluation;
use Chill\PersonBundle\Repository\AccompanyingPeriod\AccompanyingPeriodWorkEvaluationRepository;
use DateTimeImmutable;
use DateInterval;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
@ -39,10 +37,10 @@ class AccompanyingPeriodWorkEvaluationApiController
private PaginatorFactory $paginatorFactory;
private SerializerInterface $serializer;
private Security $security;
private SerializerInterface $serializer;
public function __construct(
AccompanyingPeriodWorkEvaluationRepository $accompanyingPeriodWorkEvaluationRepository,
DocGeneratorTemplateRepository $docGeneratorTemplateRepository,
@ -92,8 +90,6 @@ class AccompanyingPeriodWorkEvaluationApiController
/**
* @Route("/api/1.0/person/accompanying-period/work/evaluation/my-near-end")
* @param Request $request
* @return JsonResponse
*/
public function myWorksNearEndDate(Request $request): JsonResponse
{
@ -101,8 +97,11 @@ class AccompanyingPeriodWorkEvaluationApiController
->countNearMaxDateByUser($this->security->getUser());
$paginator = $this->paginatorFactory->create($total);
$works = $this->accompanyingPeriodWorkEvaluationRepository
->findNearMaxDateByUser($this->security->getUser(),
$paginator->getItemsPerPage(), $paginator->getCurrentPageFirstItemNumber());
->findNearMaxDateByUser(
$this->security->getUser(),
$paginator->getItemsPerPage(),
$paginator->getCurrentPageFirstItemNumber()
);
$collection = new Collection($works, $paginator);

View File

@ -1,5 +1,14 @@
<?php
/**
* 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.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Repository\AccompanyingPeriod;
use Chill\MainBundle\Entity\User;
@ -9,7 +18,6 @@ use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ObjectRepository;
use UnexpectedValueException;
class AccompanyingPeriodWorkEvaluationRepository implements ObjectRepository
{
@ -20,6 +28,12 @@ class AccompanyingPeriodWorkEvaluationRepository implements ObjectRepository
$this->repository = $entityManager->getRepository(AccompanyingPeriodWorkEvaluation::class);
}
public function countNearMaxDateByUser(User $user): int
{
return $this->buildQueryNearMaxDateByUser($user)
->select('count(e)')->getQuery()->getSingleScalarResult();
}
public function find($id): ?AccompanyingPeriodWorkEvaluation
{
return $this->repository->find($id);
@ -34,10 +48,9 @@ class AccompanyingPeriodWorkEvaluationRepository implements ObjectRepository
}
/**
* @param array $criteria
* @param array|null $orderBy
* @param int $limit
* @param int $offset
*
* @return array|AccompanyingPeriodWorkEvaluation[]|object[]
*/
public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null): array
@ -45,22 +58,6 @@ class AccompanyingPeriodWorkEvaluationRepository implements ObjectRepository
return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
}
public function findOneBy(array $criteria): ?AccompanyingPeriodWorkEvaluation
{
return $this->repository->findOneBy($criteria);
}
public function getClassName(): string
{
return AccompanyingPeriodWorkEvaluation::class;
}
public function countNearMaxDateByUser(User $user): int
{
return $this->buildQueryNearMaxDateByUser($user)
->select('count(e)')->getQuery()->getSingleScalarResult();
}
public function findNearMaxDateByUser(User $user, int $limit = 20, int $offset = 0): array
{
return $this->buildQueryNearMaxDateByUser($user)
@ -71,6 +68,16 @@ class AccompanyingPeriodWorkEvaluationRepository implements ObjectRepository
->getResult();
}
public function findOneBy(array $criteria): ?AccompanyingPeriodWorkEvaluation
{
return $this->repository->findOneBy($criteria);
}
public function getClassName(): string
{
return AccompanyingPeriodWorkEvaluation::class;
}
private function buildQueryNearMaxDateByUser(User $user): QueryBuilder
{
$qb = $this->repository->createQueryBuilder('e');
@ -87,7 +94,7 @@ class AccompanyingPeriodWorkEvaluationRepository implements ObjectRepository
)
->setParameters([
'user' => $user,
'now' => new \DateTimeImmutable('now')
'now' => new DateTimeImmutable('now'),
]);
return $qb;

View File

@ -29,28 +29,6 @@ final class AccompanyingPeriodWorkRepository implements ObjectRepository
$this->repository = $entityManager->getRepository(AccompanyingPeriodWork::class);
}
private function buildQueryNearEndDateByUser(User $user, DateTimeImmutable $since, DateTimeImmutable $until): QueryBuilder
{
$qb = $this->repository->createQueryBuilder('w');
$qb
->join('w.accompanyingPeriod', 'period')
->where(
$qb->expr()->andX(
$qb->expr()->eq('period.user', ':user'),
$qb->expr()->gte('w.endDate', ':since'),
$qb->expr()->lte('w.startDate', ':until')
)
)
->setParameters([
'user' => $user,
'since' => $since,
'until' => $until,
]);
return $qb;
}
public function countByAccompanyingPeriod(AccompanyingPeriod $period): int
{
return $this->repository->countByAccompanyingPeriod($period);
@ -152,4 +130,26 @@ final class AccompanyingPeriodWorkRepository implements ObjectRepository
return $qb;
}
private function buildQueryNearEndDateByUser(User $user, DateTimeImmutable $since, DateTimeImmutable $until): QueryBuilder
{
$qb = $this->repository->createQueryBuilder('w');
$qb
->join('w.accompanyingPeriod', 'period')
->where(
$qb->expr()->andX(
$qb->expr()->eq('period.user', ':user'),
$qb->expr()->gte('w.endDate', ':since'),
$qb->expr()->lte('w.startDate', ':until')
)
)
->setParameters([
'user' => $user,
'since' => $since,
'until' => $until,
]);
return $qb;
}
}

View File

@ -478,11 +478,15 @@ final class SingleTaskController extends AbstractController
case 'json':
$collection = new Collection($tasks, $paginator);
return $this->json($collection, JsonResponse::HTTP_OK, [],
['groups' => ['read']]);
return $this->json(
$collection,
JsonResponse::HTTP_OK,
[],
['groups' => ['read']]
);
default:
throw new BadRequestException("format not supported: $format");
throw new BadRequestException("format not supported: {$format}");
}
}

View File

@ -29,8 +29,8 @@ use function array_keys;
*
* @ORM\MappedSuperclass
* @Serializer\DiscriminatorMap(typeProperty="type", mapping={
* "single_task": SingleTask::class
* })
* "single_task": SingleTask::class
* })
*/
abstract class AbstractTask implements HasCenterInterface, HasScopeInterface
{