Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -766,13 +766,6 @@ services:
public: true
class: AppBundle\Service\Routing\OsrmWithFallback

AppBundle\Api\Filter\DeliveryOrderFilter:
autowire: false
autoconfigure: false
public: true
parent: 'api_platform.doctrine.orm.date_filter'
arguments:
$properties: []

coopcycle.web_success_handler:
class: AppBundle\EventListener\AuthenticationWebSuccessHandler
Expand Down Expand Up @@ -1631,11 +1624,6 @@ services:

AppBundle\Utils\Defaults: ~

AppBundle\Api\Filter\AssignedFilter:
arguments:
$properties: { assigned: ~ }
$security: '@security.helper'
tags: [ 'api_platform.filter' ]

AppBundle\Fixtures\DatabasePurger: ~

Expand Down
56 changes: 21 additions & 35 deletions src/Api/Filter/AssignedFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,54 @@

namespace AppBundle\Api\Filter;

use ApiPlatform\Doctrine\Orm\Filter\AbstractFilter;
use ApiPlatform\Doctrine\Orm\Filter\FilterInterface;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ParameterNotFound;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
use Psr\Log\LoggerInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;

final class AssignedFilter extends AbstractFilter
final class AssignedFilter implements FilterInterface
{
private $security;

public function __construct(
ManagerRegistry $managerRegistry,
Security $security,
?LoggerInterface $logger = null,
?array $properties = null,
?NameConverterInterface $nameConverter = null)
{
$this->security = $security;

parent::__construct($managerRegistry, $logger, $properties, $nameConverter);
private readonly Security $security
) {
}

protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
{
// otherwise filter is applied to order and page as well
if (!$this->isPropertyEnabled($property, $resourceClass)) {
$parameter = $context['parameter'] ?? null;
$value = $parameter?->getValue();

// The parameter may not be present
if ($value instanceof ParameterNotFound || null === $value) {
return;
}

$user = $this->security->getUser();

if (!$user) {
return;
}

// Only admin/dispatcher can filter by assignment status
if ($user->hasRole('ROLE_ADMIN') || $user->hasRole('ROLE_DISPATCHER')) {
$isAssigned = filter_var($value, FILTER_VALIDATE_BOOLEAN);
$alias = $queryBuilder->getRootAliases()[0];

if (false === $isAssigned) {
$queryBuilder
->andWhere(sprintf('o.%s IS NULL', 'assignedTo'));
->andWhere(sprintf('%s.%s IS NULL', $alias, 'assignedTo'));
} else {
$queryBuilder
->andWhere(sprintf('o.%s IS NOT NULL', 'assignedTo'));
->andWhere(sprintf('%s.%s IS NOT NULL', $alias, 'assignedTo'));
}
}
}

public function getDescription(string $resourceClass): array
{
if (!$this->properties) {
return [];
}

$description = [];
foreach ($this->properties as $property => $strategy) {
$description[$property] = [
'property' => $property,
'type' => 'string',
'required' => false,
];
}

return $description;
// For BC, this function is not useful anymore when documentation occurs on the Parameter
return [];
}
}
40 changes: 17 additions & 23 deletions src/Api/Filter/DateFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,38 @@

namespace AppBundle\Api\Filter;

use ApiPlatform\Doctrine\Orm\Filter\AbstractFilter;
use ApiPlatform\Doctrine\Orm\Filter\FilterInterface;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ParameterNotFound;
use Doctrine\ORM\QueryBuilder;

final class DateFilter extends AbstractFilter
final class DateFilter implements FilterInterface
{
protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
{
// otherwise filter is applied to order and page as well
if (
!$this->isPropertyEnabled($property, $resourceClass) ||
!$this->isPropertyMapped($property, $resourceClass)
) {
$parameter = $context['parameter'] ?? null;
$value = $parameter?->getValue();

// The parameter may not be present
if ($value instanceof ParameterNotFound || null === $value) {
return;
}

// Get the property from the parameter
$property = $parameter->getProperty() ?? $parameter->getKey() ?? 'date';

$alias = $queryBuilder->getRootAliases()[0];
$parameterName = $queryNameGenerator->generateParameterName($property);

$queryBuilder
->andWhere(sprintf('DATE(o.%s) = :%s', $property, $parameterName))
->andWhere(sprintf('DATE(%s.%s) = :%s', $alias, $property, $parameterName))
->setParameter($parameterName, $value);
}

public function getDescription(string $resourceClass): array
{
if (!$this->properties) {
return [];
}

$description = [];
foreach ($this->properties as $property => $strategy) {
$description[$property] = [
'property' => $property,
'type' => 'string',
'required' => false,
];
}

return $description;
// For BC, this function is not useful anymore when documentation occurs on the Parameter
return [];
}
}
32 changes: 13 additions & 19 deletions src/Api/Filter/DeliveryOrderFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,31 @@

use AppBundle\Entity\Delivery;
use AppBundle\Entity\Task;
use ApiPlatform\Doctrine\Orm\Filter\AbstractFilter;
use ApiPlatform\Doctrine\Orm\Filter\FilterInterface;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ParameterNotFound;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\Query\Expr\Join;

class DeliveryOrderFilter extends AbstractFilter
class DeliveryOrderFilter implements FilterInterface
{
protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
{
if (Delivery::class !== $resourceClass) {
return;
}

if (!in_array('dropoff.before', array_keys($context['filters'][$property]))) {
$parameter = $context['parameter'] ?? null;
$value = $parameter?->getValue();

// The parameter may not be present
if ($value instanceof ParameterNotFound || null === $value) {
return;
}

$direction = $context['filters'][$property]['dropoff.before'];
// Value should be the direction (asc/desc)
$direction = $value;

$rootAlias = $queryBuilder->getRootAliases()[0];

Expand Down Expand Up @@ -97,19 +103,7 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB

public function getDescription(string $resourceClass): array
{
if (!$this->properties) {
return [];
}

$description = [];
foreach ($this->properties as $property => $strategy) {
$description[$property] = [
'property' => $property,
'type' => 'string',
'required' => false,
];
}

return $description;
// For BC, this function is not useful anymore when documentation occurs on the Parameter
return [];
}
}
26 changes: 15 additions & 11 deletions src/Api/Filter/DeliveryTaskDateFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@

namespace AppBundle\Api\Filter;

use ApiPlatform\Doctrine\Orm\Filter\AbstractFilter;
use ApiPlatform\Doctrine\Orm\Filter\FilterInterface;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ParameterNotFound;
use Doctrine\ORM\QueryBuilder;

class DeliveryTaskDateFilter extends AbstractFilter
class DeliveryTaskDateFilter implements FilterInterface
{
protected function filterProperty(
string $property,
$value,
QueryBuilder $queryBuilder,
QueryNameGeneratorInterface $queryNameGenerator,
string $resourceClass,
?Operation $operation = null,
array $context = []
): void {
public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
{
$parameter = $context['parameter'] ?? null;
$value = $parameter?->getValue();

// The parameter may not be present
if ($value instanceof ParameterNotFound || null === $value) {
return;
}

$property = $parameter->getKey();

if (!in_array($property, ['pickup.before', 'pickup.after', 'dropoff.before', 'dropoff.after'])) {
return;
}
Expand Down
36 changes: 15 additions & 21 deletions src/Api/Filter/OrderDateFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
namespace AppBundle\Api\Filter;

use ApiPlatform\Doctrine\Common\Filter\DateFilterInterface;
use ApiPlatform\Doctrine\Orm\Filter\AbstractFilter;
use ApiPlatform\Doctrine\Orm\Filter\FilterInterface;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ParameterNotFound;
use Doctrine\ORM\QueryBuilder;
use Sylius\Component\Order\Model\OrderInterface;

final class OrderDateFilter extends AbstractFilter
final class OrderDateFilter implements FilterInterface
{
protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
{
// Do not use isPropertyMapped(), because this is a "virtual" property
if (!$this->isPropertyEnabled($property, $resourceClass)) {
$parameter = $context['parameter'] ?? null;
$value = $parameter?->getValue();

// The parameter may not be present
if ($value instanceof ParameterNotFound || null === $value) {
return;
}

Expand All @@ -30,29 +34,19 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
$rangeEnd = $dateTime;
}

$alias = $queryBuilder->getRootAliases()[0];

$queryBuilder
->andWhere('OVERLAPS(o.shippingTimeRange, CAST(:range AS tsrange)) = TRUE')
->andWhere(sprintf('OVERLAPS(%s.shippingTimeRange, CAST(:range AS tsrange)) = TRUE', $alias))
// FIXME Move this to another filter?
->andWhere('o.state != :state_cart')
->andWhere(sprintf('%s.state != :state_cart', $alias))
->setParameter('range', sprintf('[%s, %s]', $rangeStart->format('Y-m-d 00:00:00'), $rangeEnd->format('Y-m-d 23:59:59')))
->setParameter('state_cart', OrderInterface::STATE_CART);
}

public function getDescription(string $resourceClass): array
{
if (!$this->properties) {
return [];
}

$description = [];
foreach ($this->properties as $property => $strategy) {
$description[$property] = [
'property' => $property,
'type' => 'string',
'required' => false,
];
}

return $description;
// For BC, this function is not useful anymore when documentation occurs on the Parameter
return [];
}
}
Loading
Loading