Skip to content

Commit 5e5e5f4

Browse files
committed
ACP2E-894: add new argument to filter customer orders based on scope level
1 parent 80ebca4 commit 5e5e5f4

File tree

3 files changed

+76
-8
lines changed

3 files changed

+76
-8
lines changed

app/code/Magento/SalesGraphQl/Model/Resolver/CustomerOrders.php

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@
88
namespace Magento\SalesGraphQl\Model\Resolver;
99

1010
use Magento\Framework\Api\SearchCriteriaBuilder;
11+
use Magento\Framework\App\ObjectManager;
1112
use Magento\Framework\Exception\InputException;
1213
use Magento\Framework\GraphQl\Config\Element\Field;
1314
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1415
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1516
use Magento\Framework\GraphQl\Query\ResolverInterface;
1617
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
18+
use Magento\Sales\Api\Data\OrderSearchResultInterface;
1719
use Magento\Sales\Api\OrderRepositoryInterface;
1820
use Magento\SalesGraphQl\Model\Formatter\Order as OrderFormatter;
1921
use Magento\SalesGraphQl\Model\Resolver\CustomerOrders\Query\OrderFilter;
2022
use Magento\Store\Api\Data\StoreInterface;
23+
use Magento\Store\Model\StoreManagerInterface;
2124

2225
/**
2326
* Orders data resolver
@@ -44,22 +47,30 @@ class CustomerOrders implements ResolverInterface
4447
*/
4548
private $orderFormatter;
4649

50+
/**
51+
* @var StoreManagerInterface|mixed|null
52+
*/
53+
private $storeManager;
54+
4755
/**
4856
* @param OrderRepositoryInterface $orderRepository
4957
* @param SearchCriteriaBuilder $searchCriteriaBuilder
5058
* @param OrderFilter $orderFilter
5159
* @param OrderFormatter $orderFormatter
60+
* @param StoreManagerInterface|null $storeManager
5261
*/
5362
public function __construct(
5463
OrderRepositoryInterface $orderRepository,
5564
SearchCriteriaBuilder $searchCriteriaBuilder,
5665
OrderFilter $orderFilter,
57-
OrderFormatter $orderFormatter
66+
OrderFormatter $orderFormatter,
67+
StoreManagerInterface $storeManager = null
5868
) {
5969
$this->orderRepository = $orderRepository;
6070
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
6171
$this->orderFilter = $orderFilter;
6272
$this->orderFormatter = $orderFormatter;
73+
$this->storeManager = ObjectManager::getInstance()->get(StoreManagerInterface::class) ?? null;
6374
}
6475

6576
/**
@@ -81,11 +92,15 @@ public function resolve(
8192
if ($args['pageSize'] < 1) {
8293
throw new GraphQlInputException(__('pageSize value must be greater than 0.'));
8394
}
95+
$storeIds = [];
8496
$userId = $context->getUserId();
8597
/** @var StoreInterface $store */
8698
$store = $context->getExtensionAttributes()->getStore();
99+
if (isset($args['scope'])) {
100+
$storeIds = $this->getStoresByScope($args['scope'], $store);
101+
}
87102
try {
88-
$searchResult = $this->getSearchResult($args, (int)$userId, (int)$store->getId());
103+
$searchResult = $this->getSearchResult($args, (int)$userId, (int)$store->getId(), $storeIds);
89104
$maxPages = (int)ceil($searchResult->getTotalCount() / $searchResult->getPageSize());
90105
} catch (InputException $e) {
91106
throw new GraphQlInputException(__($e->getMessage()));
@@ -113,12 +128,13 @@ public function resolve(
113128
* @param array $args
114129
* @param int $userId
115130
* @param int $storeId
116-
* @return \Magento\Sales\Api\Data\OrderSearchResultInterface
131+
* @param array $scope
132+
* @return OrderSearchResultInterface
117133
* @throws InputException
118134
*/
119-
private function getSearchResult(array $args, int $userId, int $storeId)
135+
private function getSearchResult(array $args, int $userId, int $storeId, array $storeIds)
120136
{
121-
$filterGroups = $this->orderFilter->createFilterGroups($args, $userId, (int)$storeId);
137+
$filterGroups = $this->orderFilter->createFilterGroups($args, $userId, (int)$storeId, $storeIds);
122138
$this->searchCriteriaBuilder->setFilterGroups($filterGroups);
123139
if (isset($args['currentPage'])) {
124140
$this->searchCriteriaBuilder->setCurrentPage($args['currentPage']);
@@ -128,4 +144,52 @@ private function getSearchResult(array $args, int $userId, int $storeId)
128144
}
129145
return $this->orderRepository->getList($this->searchCriteriaBuilder->create());
130146
}
147+
148+
/**
149+
* @param string $scope
150+
* @param StoreInterface $store
151+
* @return void
152+
*/
153+
private function getStoresByScope(string $scope, StoreInterface $store)
154+
{
155+
$storeIds = [];
156+
switch ($scope) {
157+
case 'global':
158+
$storeIds = $this->getStoresByFilter(null, null);
159+
break;
160+
case 'website':
161+
$websiteId = $store->getWebsiteId();
162+
$storeIds = $this->getStoresByFilter((int)$websiteId, null);
163+
break;
164+
case 'store':
165+
$storeGroupId = $store->getStoreGroupId();
166+
$storeIds = $this->getStoresByFilter(null, (int)$storeGroupId);
167+
break;
168+
default:
169+
break;
170+
}
171+
return $storeIds;
172+
}
173+
174+
/**
175+
* @param int|null $websiteId
176+
* @param int|null $storeGroupId
177+
* @return array
178+
*/
179+
private function getStoresByFilter(?int $websiteId, ?int $storeGroupId)
180+
{
181+
$stores = $this->storeManager->getStores(true, true);
182+
$storeIds = [];
183+
foreach ($stores as $store) {
184+
if (isset($websiteId) && $websiteId === (int)$store->getWebsiteId()
185+
||
186+
isset($storeGroupId) && $storeGroupId === (int)$store->getStoreGroupId()
187+
) {
188+
$storeIds[] = $store->getId();
189+
} elseif (!isset($websiteId) && !isset($storeGroupId)) {
190+
$storeIds[] = $store->getId();
191+
}
192+
}
193+
return $storeIds;
194+
}
131195
}

app/code/Magento/SalesGraphQl/Model/Resolver/CustomerOrders/Query/OrderFilter.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
namespace Magento\SalesGraphQl\Model\Resolver\CustomerOrders\Query;
99

1010
use Magento\Framework\Api\FilterBuilder;
11+
use Magento\Framework\Api\Search\FilterGroup;
1112
use Magento\Framework\Api\Search\FilterGroupBuilder;
1213
use Magento\Framework\App\Config\ScopeConfigInterface;
1314
use Magento\Framework\Exception\InputException;
14-
use Magento\Framework\Api\Search\FilterGroup;
1515

1616
/**
1717
* Order filter allows to filter collection using 'increment_id' as order number, from the search criteria.
@@ -63,20 +63,23 @@ public function __construct(
6363
* @param int $userId
6464
* @param int $storeId
6565
* @return FilterGroup[]
66+
* @throws InputException
6667
*/
6768
public function createFilterGroups(
6869
array $args,
6970
int $userId,
70-
int $storeId
71+
int $storeId,
72+
array $storeIds
7173
): array {
7274
$filterGroups = [];
7375
$this->filterGroupBuilder->setFilters(
7476
[$this->filterBuilder->setField('customer_id')->setValue($userId)->setConditionType('eq')->create()]
7577
);
7678
$filterGroups[] = $this->filterGroupBuilder->create();
7779

80+
$storeIds[] = $storeId;
7881
$this->filterGroupBuilder->setFilters(
79-
[$this->filterBuilder->setField('store_id')->setValue($storeId)->setConditionType('eq')->create()]
82+
[$this->filterBuilder->setField('store_id')->setValue($storeIds)->setConditionType('in')->create()]
8083
);
8184
$filterGroups[] = $this->filterGroupBuilder->create();
8285

app/code/Magento/SalesGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Customer {
2525
filter: CustomerOrdersFilterInput @doc(description: "Defines the filter to use for searching customer orders."),
2626
currentPage: Int = 1 @doc(description: "Specifies which page of results to return. The default value is 1."),
2727
pageSize: Int = 20 @doc(description: "Specifies the maximum number of results to return at once. The default value is 20."),
28+
scope: String @doc(description: "Specifies the scope of the orders (global, website, store)"),
2829
): CustomerOrders @resolver(class: "Magento\\SalesGraphQl\\Model\\Resolver\\CustomerOrders") @cache(cacheable: false)
2930
}
3031

0 commit comments

Comments
 (0)