Skip to content

Commit

Permalink
Merge pull request #16 from AleksandrsKondratjevs/main
Browse files Browse the repository at this point in the history
Fix compatibility issues with magento 2.4.6
  • Loading branch information
AleksandrsKondratjevs authored Apr 4, 2023
2 parents 5004e6a + cf59fed commit 50816a3
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 21 deletions.
107 changes: 88 additions & 19 deletions Model/Resolver/CustomerOrders.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@
namespace ScandiPWA\SalesGraphQl\Model\Resolver;

use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\InputException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Sales\Api\Data\OrderSearchResultInterface;
use Magento\SalesGraphQl\Model\Resolver\CustomerOrders as CoreCustomerOrders;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Sales\Api\Data\OrderSearchResultInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\SalesGraphQl\Model\Formatter\Order as OrderFormatter;
use Magento\SalesGraphQl\Model\Resolver\CustomerOrders as CoreCustomerOrders;
use Magento\SalesGraphQl\Model\Resolver\CustomerOrders\Query\OrderFilter;
use Magento\SalesGraphQl\Model\Resolver\CustomerOrders\Query\OrderSort;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Framework\Api\SortOrderBuilder;
use Magento\Framework\Api\SortOrder;
use Magento\Store\Model\StoreManagerInterface;

/**
* Orders data resolver
Expand Down Expand Up @@ -52,45 +53,54 @@ class CustomerOrders extends CoreCustomerOrders
protected $orderFormatter;

/**
* @var SortOrderBuilder
* @var OrderSort
*/
protected $orderSort;

/**
* @var StoreManagerInterface|mixed|null
*/
protected $sortOrderBuilder;
protected $storeManager;

/**
* CustomerOrders constructor.
* @param OrderRepositoryInterface $orderRepository
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param OrderFilter $orderFilter
* @param OrderFormatter $orderFormatter
* @param SortOrderBuilder $sortOrderBuilder
* @param OrderSort $orderSort
* @param StoreManagerInterface|null $storeManager
*/
public function __construct(
OrderRepositoryInterface $orderRepository,
SearchCriteriaBuilder $searchCriteriaBuilder,
OrderFilter $orderFilter,
OrderFormatter $orderFormatter,
SortOrderBuilder $sortOrderBuilder
SortOrderBuilder $sortOrderBuilder,
?StoreManagerInterface $storeManager = null
) {
parent::__construct(
$orderRepository,
$searchCriteriaBuilder,
$orderFilter,
$orderFormatter
$orderFormatter,
$orderSort,
$storeManager
);

$this->orderRepository = $orderRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->orderFilter = $orderFilter;
$this->orderFormatter = $orderFormatter;
$this->sortOrderBuilder = $sortOrderBuilder;
$this->orderSort = $orderSort;
$this->storeManager = $storeManager ?? ObjectManager::getInstance()->get(StoreManagerInterface::class);
}

/**
* @inheritDoc
*/
public function resolve(
Field $field,
$context,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
Expand All @@ -107,12 +117,17 @@ public function resolve(
throw new GraphQlInputException(__('pageSize value must be greater than 0.'));
}

$storeIds = [];
$userId = $context->getUserId();
/** @var StoreInterface $store */
$store = $context->getExtensionAttributes()->getStore();

if (isset($args['scope'])) {
$storeIds = $this->getStoresByScope($args['scope'], $store);
}

try {
$searchResult = $this->getSearchResult($args, (int)$userId, (int)$store->getId());
$searchResult = $this->getSearchResult($args, (int)$userId, (int)$store->getId(), $storeIds);
$maxPages = (int)ceil($searchResult->getTotalCount() / $searchResult->getPageSize());
} catch (InputException $e) {
throw new GraphQlInputException(__($e->getMessage()));
Expand All @@ -126,7 +141,7 @@ public function resolve(

return [
'total_count' => $searchResult->getTotalCount(),
'items' => $ordersArray,
'items' => $ordersArray,
'page_info' => [
'page_size' => $searchResult->getPageSize(),
'current_page' => $searchResult->getCurPage(),
Expand All @@ -144,14 +159,11 @@ public function resolve(
* @return OrderSearchResultInterface
* @throws InputException
*/
private function getSearchResult(array $args, int $userId, int $storeId)
private function getSearchResult(array $args, int $userId, int $storeId, array $storeIds)
{
$filterGroups = $this->orderFilter->createFilterGroups($args, $userId, (int)$storeId);
$filterGroups = $this->orderFilter->createFilterGroups($args, $userId, (int)$storeId, $storeIds);
$this->searchCriteriaBuilder->setFilterGroups($filterGroups);

$sortOrder = $this->sortOrderBuilder->setField('entity_id')->setDirection(SortOrder::SORT_DESC)->create();
$this->searchCriteriaBuilder->setSortOrders([$sortOrder]);

if (isset($args['currentPage'])) {
$this->searchCriteriaBuilder->setCurrentPage($args['currentPage']);
}
Expand All @@ -160,6 +172,63 @@ private function getSearchResult(array $args, int $userId, int $storeId)
$this->searchCriteriaBuilder->setPageSize($args['pageSize']);
}

if (isset($args['sort'])) {
$sortOrders = $this->orderSort->createSortOrders($args);
$this->searchCriteriaBuilder->setSortOrders($sortOrders);
}

return $this->orderRepository->getList($this->searchCriteriaBuilder->create());
}

/**
* Get eligible store to filter by based on scope
*
* @param string $scope
* @param StoreInterface $store
* @return array
*/
private function getStoresByScope(string $scope, StoreInterface $store): array
{
$storeIds = [];
switch ($scope) {
case 'GLOBAL':
$storeIds = $this->getStoresByFilter(null, null);
break;
case 'WEBSITE':
$websiteId = $store->getWebsiteId();
$storeIds = $this->getStoresByFilter((int)$websiteId, null);
break;
case 'STORE':
$storeGroupId = $store->getStoreGroupId();
$storeIds = $this->getStoresByFilter(null, (int)$storeGroupId);
break;
default:
break;
}
return $storeIds;
}

/**
* Filter store ids based on selected scope
*
* @param int|null $websiteId
* @param int|null $storeGroupId
* @return array
*/
private function getStoresByFilter(?int $websiteId, ?int $storeGroupId): array
{
$stores = $this->storeManager->getStores(true, true);
$storeIds = [];
foreach ($stores as $store) {
if (isset($websiteId) && $websiteId === (int)$store->getWebsiteId()
||
isset($storeGroupId) && $storeGroupId === (int)$store->getStoreGroupId()
) {
$storeIds[] = $store->getId();
} elseif (!isset($websiteId) && !isset($storeGroupId)) {
$storeIds[] = $store->getId();
}
}
return $storeIds;
}
}
10 changes: 8 additions & 2 deletions Model/Resolver/CustomerOrders/Query/OrderFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
namespace ScandiPWA\SalesGraphQl\Model\Resolver\CustomerOrders\Query;

use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\Search\FilterGroup;
use Magento\Framework\Api\Search\FilterGroupBuilder;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Api\Search\FilterGroup;
use Magento\Sales\Model\Order\Config;
use Magento\SalesGraphQl\Model\Resolver\CustomerOrders\Query\OrderFilter as CoreOrderFilter;

Expand Down Expand Up @@ -91,7 +91,8 @@ public function __construct(
public function createFilterGroups(
array $args,
int $userId,
int $storeId
int $storeId,
array $storeIds
): array {
$filterGroups = [];

Expand All @@ -104,6 +105,11 @@ public function createFilterGroups(
$this->filterGroupBuilder->setFilters(
[$this->filterBuilder->setField('status')->setValue($this->orderConfig->getVisibleOnFrontStatuses())->setConditionType('in')->create()]
);

$storeIds[] = $storeId;
$this->filterGroupBuilder->setFilters(
[$this->filterBuilder->setField('store_id')->setValue($storeIds)->setConditionType('in')->create()]
);
$filterGroups[] = $this->filterGroupBuilder->create();

if (isset($args['filter'])) {
Expand Down

0 comments on commit 50816a3

Please sign in to comment.