Skip to content

Commit

Permalink
Add tax rates filtering in API
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubtobiasz committed Feb 16, 2023
1 parent 57f94db commit fc4abf8
Show file tree
Hide file tree
Showing 7 changed files with 337 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ Feature: Filtering tax rates by end date
And the store has "3 weeks tax rate" tax rate of 25% for "Clothes" within the "US" zone with dates between "2022-12-24" and "2023-01-15"
And I am logged in as an administrator

@ui @no-api
@ui @api
Scenario: Filtering tax rates from end date
When I browse tax rates
And I filter tax rates by end date from "2022-12-26"
Then I should see the tax rate "2023 tax rate" in the list
And I should see the tax rate "2022 tax rate" in the list
And I should see the tax rate "3 weeks tax rate" in the list

@ui @no-api
@ui @api
Scenario: Filtering tax rates up to end date
When I browse tax rates
And I filter tax rates by end date up to "2022-12-31"
Then I should not see a tax rate with name "2023 tax rate"
And I should not see a tax rate with name "3 weeks tax rate"
But I should see the tax rate "2022 tax rate" in the list

@ui @no-api
@ui @api
Scenario: Filtering tax rates in a end date range
When I browse tax rates
And I filter tax rates by end date from "2023-01-02" up to "2023-01-31"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ Feature: Filtering tax rates by start date
And the store has "3 weeks tax rate" tax rate of 25% for "Clothes" within the "US" zone with dates between "2022-12-24" and "2023-01-15"
And I am logged in as an administrator

@ui @no-api
@ui @api
Scenario: Filtering tax rates from start date
When I browse tax rates
And I filter tax rates by start date from "2022-12-26"
Then I should not see a tax rate with name "2022 tax rate"
And I should not see a tax rate with name "3 weeks tax rate"
But I should see the tax rate "2023 tax rate" in the list

@ui @no-api
@ui @api
Scenario: Filtering catalog promotions up to start date
When I browse tax rates
And I filter tax rates by start date up to "2022-12-22"
Then I should not see a tax rate with name "2023 tax rate"
And I should not see a tax rate with name "3 weeks tax rate"
But I should see the tax rate "2022 tax rate" in the list

@ui @no-api
@ui @api
Scenario: Filtering catalog promotions in a start date range
When I browse tax rates
And I filter tax rates by start date from "2022-08-20" up to "2022-12-26"
Expand Down
68 changes: 68 additions & 0 deletions src/Sylius/Behat/Context/Api/Admin/ManagingTaxRateContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,17 @@ public function iShouldBeNotifiedThatCategoryHasToBeSelected(): void
);
}

/**
* @Then I should not see a tax rate with name :name
*/
public function iShouldNotSeeATaxRateWithName(string $name): void
{
Assert::false(
$this->responseChecker->hasItemWithValue($this->client->getLastResponse(), 'name', $name),
sprintf('Tax rate with name %s exists', $name)
);
}

/**
* @When /^I want to modify (this tax rate)$/
* @When I want to modify a tax rate :taxRate
Expand Down Expand Up @@ -388,6 +399,63 @@ public function iRemoveItsName(): void
$this->client->addRequestData('name', '');
}

/**
* @When I filter tax rates by start date from :startDate
*/
public function iFilterTaxRatesByStartDateFrom($startDate): void
{
$this->client->addFilter('startDate[from]', $startDate);
$this->client->filter();
}

/**
* @When I filter tax rates by start date up to :startDate
*/
public function iFilterTaxRatesByStartDateUpTo($startDate): void
{
$this->client->addFilter('startDate[to]', $startDate);
$this->client->filter();
}

/**
* @When I filter tax rates by start date from :startDate up to :endDate
*/
public function iFilterTaxRatesByStartDateFromUpTo($startDate, $endDate): void
{
$this->client->addFilter('startDate[from]', $startDate);
$this->client->addFilter('startDate[to]', $endDate);
$this->client->filter();
}

/**
* @When I filter tax rates by end date from :endDate
*/
public function iFilterTaxRatesByEndDateFrom($endDate): void
{
$this->client->addFilter('endDate[from]', $endDate);
$this->client->filter();
}

/**
* @When I filter tax rates by end date up to :endDate
*/
public function iFilterTaxRatesByEndDateUpTo($endDate): void
{
$this->client->addFilter('endDate[to]', $endDate);
$this->client->filter();
}

/**
* @When I filter tax rates by end date from :startDate up to :endDate
*/
public function iFilterTaxRatesByEndDateFromUpTo($startDate, $endDate): void
{
$this->client->addFilter('endDate[from]', $startDate);
$this->client->addFilter('endDate[to]', $endDate);
$this->client->filter();
}


/**
* @Then /^(this tax rate) should still be named "([^"]+)"$/
* @Then /^(this tax rate) name should be "([^"]*)"$/
Expand Down
81 changes: 81 additions & 0 deletions src/Sylius/Bundle/ApiBundle/Filter/Doctrine/DateRangeFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ApiBundle\Filter\Doctrine;

use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\AbstractContextAwareFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use Doctrine\ORM\QueryBuilder;

/** @experimental */
final class DateRangeFilter extends AbstractContextAwareFilter
{
private const FROM = 'from';

private const TO = 'to';

protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null)
{
if (
!\is_array($value) ||
!$this->isPropertyEnabled($property, $resourceClass) ||
!$this->isPropertyMapped($property, $resourceClass)
) {
return;
}

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

if ($this->isPropertyNested($property, $resourceClass)) {
[$alias, $field] = $this->addJoinsForNestedProperty($property, $alias, $queryBuilder, $queryNameGenerator, $resourceClass);
}

if (isset($value[self::FROM])) {
$queryBuilder
->andWhere(sprintf('%s.%s >= :%s', $alias, $field, $parameterName = $queryNameGenerator->generateParameterName($property)))
->setParameter($parameterName, $value[self::FROM])
;
}

if (isset($value[self::TO])) {
$queryBuilder
->andWhere(sprintf('%s.%s <= :%s', $alias, $field, $parameterName = $queryNameGenerator->generateParameterName($property)))
->setParameter($parameterName, $value[self::TO])
;
}
}

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

$description = [];
foreach ($this->properties as $property => $strategy) {
$description[sprintf('%s[from]', $property)] = [
'property' => $property,
'type' => \DateTimeInterface::class,
'required' => false,
];
$description[sprintf('%s[to]', $property)] = [
'property' => $property,
'type' => \DateTimeInterface::class,
'required' => false,
];
}

return $description;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,18 @@
<attribute name="method">DELETE</attribute>
</itemOperation>
</itemOperations>

<collectionOperations>
<collectionOperation name="admin_get">
<attribute name="method">GET</attribute>
<attribute name="filters">
<attribute>sylius.api.tax_rate.date_range_filter</attribute>
</attribute>
</collectionOperation>

<collectionOperation name="admin_post">
<attribute name="method">POST</attribute>
</collectionOperation>
</collectionOperations>
</resource>
</resources>
29 changes: 29 additions & 0 deletions src/Sylius/Bundle/ApiBundle/Resources/config/services/filters.xml
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,39 @@
<tag name="api_platform.filter" />
</service>

<service id="sylius.api.tax_rates_start_date_filter" parent="api_platform.doctrine.orm.date_filter" public="true">
<argument type="collection">
<argument key="startDate">exclude_null</argument>
</argument>
<tag name="api_platform.filter" />
</service>

<service id="sylius.api.tax_rates_end_date_filter" parent="api_platform.doctrine.orm.date_filter" public="true">
<argument type="collection">
<argument key="endDate">exclude_null</argument>
</argument>
<tag name="api_platform.filter" />
</service>

<service id="Sylius\Bundle\ApiBundle\Filter\Doctrine\CatalogPromotionChannelFilter" public="true">
<argument type="service" id="api_platform.iri_converter" />
<argument type="service" id="doctrine" />
<tag name="api_platform.filter" />
</service>

<service id="Sylius\Bundle\ApiBundle\Filter\Doctrine\DateRangeFilter" abstract="true" public="true">
<argument type="service" id="doctrine" />
<argument>null</argument>
<argument type="service" id="logger" on-invalid="ignore" />
<argument key="$nameConverter" type="service" id="api_platform.name_converter" on-invalid="ignore" />
</service>

<service id="sylius.api.tax_rate.date_range_filter" parent="Sylius\Bundle\ApiBundle\Filter\Doctrine\DateRangeFilter" public="true">
<argument type="collection">
<argument key="startDate"/>
<argument key="endDate"/>
</argument>
<tag name="api_platform.filter" />
</service>
</services>
</container>
Loading

0 comments on commit fc4abf8

Please sign in to comment.