Skip to content

Commit 5238059

Browse files
committed
added seller group query
1 parent 98acb30 commit 5238059

File tree

6 files changed

+374
-0
lines changed

6 files changed

+374
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Landofcoder
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Landofcoder.com license that is
8+
* available through the world-wide-web at this URL:
9+
* https://landofcoder.com/terms
10+
*
11+
* DISCLAIMER
12+
*
13+
* Do not edit or add to this file if you wish to upgrade this extension to newer
14+
* version in the future.
15+
*
16+
* @category Landofcoder
17+
* @package Lof_MarketplaceGraphQl
18+
* @copyright Copyright (c) 2021 Landofcoder (https://www.landofcoder.com/)
19+
* @license https://landofcoder.com/terms
20+
*/
21+
22+
declare(strict_types=1);
23+
24+
namespace Lof\MarketplaceGraphQl\Model\Resolver;
25+
26+
use Magento\Framework\GraphQl\Config\Element\Field;
27+
use Magento\Framework\GraphQl\ConfigInterface;
28+
use Magento\Framework\GraphQl\Query\Resolver\Argument\FieldEntityAttributesInterface;
29+
30+
/**
31+
* Class FilterArgumentSellerGroup
32+
* @package Lof\MarketplaceGraphQl\Model\Resolver
33+
*/
34+
class FilterArgumentSellerGroup implements FieldEntityAttributesInterface
35+
{
36+
/** @var ConfigInterface */
37+
private $config;
38+
39+
/**
40+
* FilterArgumentSellerGroup constructor.
41+
* @param ConfigInterface $config
42+
*/
43+
public function __construct(ConfigInterface $config)
44+
{
45+
$this->config = $config;
46+
}
47+
48+
/**
49+
* @return array
50+
*/
51+
public function getEntityAttributes(): array
52+
{
53+
$fields = [];
54+
/** @var Field $field */
55+
foreach ($this->config->getConfigElement('SellerGroupFilterInput')->getFields() as $field) {
56+
$fields[$field->getName()] = [
57+
'type' => 'String',
58+
'fieldName' => $field->getName(),
59+
];
60+
}
61+
return $fields;
62+
}
63+
}

Model/Resolver/SellerGroups.php

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php
2+
/**
3+
* Landofcoder
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Landofcoder.com license that is
8+
* available through the world-wide-web at this URL:
9+
* https://landofcoder.com/terms
10+
*
11+
* DISCLAIMER
12+
*
13+
* Do not edit or add to this file if you wish to upgrade this extension to newer
14+
* version in the future.
15+
*
16+
* @category Landofcoder
17+
* @package Lof_MarketplaceGraphQl
18+
* @copyright Copyright (c) 2021 Landofcoder (https://www.landofcoder.com/)
19+
* @license https://landofcoder.com/terms
20+
*/
21+
22+
namespace Lof\MarketplaceGraphQl\Model\Resolver;
23+
24+
use Lof\MarketPlace\Api\SellerProductsRepositoryInterface;
25+
use Lof\MarketPlace\Api\SellersFrontendRepositoryInterface;
26+
use Lof\MarketPlace\Api\SellerGroupRepositoryInterface;
27+
use Magento\Catalog\Api\ProductRepositoryInterface;
28+
use Magento\Framework\GraphQl\Config\Element\Field;
29+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
30+
use Magento\Framework\GraphQl\Query\Resolver\Argument\SearchCriteria\Builder as SearchCriteriaBuilder;
31+
use Magento\Framework\GraphQl\Query\ResolverInterface;
32+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
33+
use Magento\Framework\Exception\InputException;
34+
use Magento\Framework\App\Config\ScopeConfigInterface;
35+
use Magento\Search\Model\Query;
36+
use Magento\Store\Api\Data\StoreInterface;
37+
use Magento\Store\Model\ScopeInterface;
38+
use Magento\Framework\GraphQl\Query\Resolver\Argument\SearchCriteria\ArgumentApplier\Filter;
39+
40+
/**
41+
* Class SellerGroups
42+
*
43+
* @package Lof\MarketplaceGraphQl\Model\Resolver
44+
*/
45+
class SellerGroups extends AbstractSellerQuery implements ResolverInterface
46+
{
47+
48+
/**
49+
* @var SellerGroupRepositoryInterface
50+
*/
51+
private $sellerGroup;
52+
53+
/**
54+
* @var string
55+
*/
56+
private const SPECIAL_CHARACTERS = '-+~/\\<>\'":*$#@()!,.?`=%&^';
57+
58+
/**
59+
* @var ScopeConfigInterface
60+
*/
61+
private $scopeConfig;
62+
63+
/**
64+
* Sellers constructor.
65+
* @param SearchCriteriaBuilder $searchCriteriaBuilder
66+
* @param SellersFrontendRepositoryInterface $seller
67+
* @param SellerProductsRepositoryInterface $productSeller
68+
* @param ProductRepositoryInterface $productRepository
69+
* @param SellerGroupRepositoryInterface $sellerGroup
70+
* @param ScopeConfigInterface $scopeConfig
71+
*/
72+
public function __construct(
73+
SearchCriteriaBuilder $searchCriteriaBuilder,
74+
SellersFrontendRepositoryInterface $seller,
75+
SellerProductsRepositoryInterface $productSeller,
76+
ProductRepositoryInterface $productRepository,
77+
SellerGroupRepositoryInterface $sellerGroup,
78+
ScopeConfigInterface $scopeConfig
79+
) {
80+
$this->sellerGroup = $sellerGroup;
81+
$this->scopeConfig = $scopeConfig;
82+
parent::__construct($searchCriteriaBuilder, $seller, $productSeller, $productRepository);
83+
}
84+
85+
/**
86+
* @inheritdoc
87+
*/
88+
public function resolve(
89+
Field $field,
90+
$context,
91+
ResolveInfo $info,
92+
array $value = null,
93+
array $args = null
94+
) {
95+
if ($args['currentPage'] < 1) {
96+
throw new GraphQlInputException(__('currentPage value must be greater than 0.'));
97+
}
98+
if ($args['pageSize'] < 1) {
99+
throw new GraphQlInputException(__('pageSize value must be greater than 0.'));
100+
}
101+
$store = $context->getExtensionAttributes()->getStore();
102+
$args[Filter::ARGUMENT_NAME] = $this->formatMatchFilters($args['filter'], $store);
103+
$searchCriteria = $this->searchCriteriaBuilder->build('lof_marketplace_seller_group', $args);
104+
$searchCriteria->setCurrentPage($args['currentPage']);
105+
$searchCriteria->setPageSize($args['pageSize']);
106+
107+
$searchResult = $this->sellerGroup->getList($searchCriteria);
108+
$totalPages = $args['pageSize'] ? ((int)ceil($searchResult->getTotalCount() / $args['pageSize'])) : 0;
109+
$resultItems = $searchResult->getItems();
110+
$items = [];
111+
if($resultItems){
112+
foreach($resultItems as $_item){
113+
$items[] = $_item->__toArray();
114+
}
115+
}
116+
return [
117+
'total_count' => $searchResult->getTotalCount(),
118+
'items' => $items,
119+
'page_info' => [
120+
'page_size' => $args['pageSize'],
121+
'current_page' => $args['currentPage'],
122+
'total_pages' => $totalPages
123+
]
124+
];
125+
}
126+
127+
/**
128+
* Format match filter to behave like fuzzy match
129+
*
130+
* @param array $filter
131+
* @param StoreInterface $store
132+
* @return array
133+
* @throws InputException
134+
*/
135+
private function formatMatchFilters(array $filters, StoreInterface $store): array
136+
{
137+
$minQueryLength = $this->scopeConfig->getValue(
138+
Query::XML_PATH_MIN_QUERY_LENGTH,
139+
ScopeInterface::SCOPE_STORE,
140+
$store
141+
);
142+
foreach ($filters as $filter => $condition) {
143+
$conditionType = current(array_keys($condition));
144+
$tmpminQueryLength = $minQueryLength;
145+
if ($conditionType === 'match') {
146+
$searchValue = trim(str_replace(self::SPECIAL_CHARACTERS, '', $condition[$conditionType]));
147+
$matchLength = strlen($searchValue);
148+
if ($matchLength < $tmpminQueryLength) {
149+
throw new InputException(__('Invalid match filter. Minimum length is %1.', $tmpminQueryLength));
150+
}
151+
unset($filters[$filter]['match']);
152+
$filters[$filter]['like'] = '%' . $searchValue . '%';
153+
}
154+
}
155+
return $filters;
156+
}
157+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Landofcoder
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Landofcoder.com license that is
8+
* available through the world-wide-web at this URL:
9+
* https://landofcoder.com/terms
10+
*
11+
* DISCLAIMER
12+
*
13+
* Do not edit or add to this file if you wish to upgrade this extension to newer
14+
* version in the future.
15+
*
16+
* @category Landofcoder
17+
* @package Lof_MarketplaceGraphQl
18+
* @copyright Copyright (c) 2021 Landofcoder (https://www.landofcoder.com/)
19+
* @license https://landofcoder.com/terms
20+
*/
21+
22+
namespace Lof\MarketplaceGraphQl\Model\Resolver\SellerGroups;
23+
24+
25+
use Magento\Framework\GraphQl\Query\Resolver\IdentityInterface;
26+
27+
class Identity implements IdentityInterface
28+
{
29+
30+
private $cacheTag = \Magento\Framework\App\Config::CACHE_TAG;
31+
32+
/**
33+
* @param array $resolvedData
34+
* @return string[]
35+
*/
36+
public function getIdentities(array $resolvedData)
37+
{
38+
$ids = empty($resolvedData['group_id']) ?
39+
[] : [$this->cacheTag, sprintf('%s_%s', $this->cacheTag, $resolvedData['group_id'])];
40+
41+
return $ids;
42+
}
43+
}
44+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Landofcoder
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Landofcoder.com license that is
8+
* available through the world-wide-web at this URL:
9+
* https://landofcoder.com/terms
10+
*
11+
* DISCLAIMER
12+
*
13+
* Do not edit or add to this file if you wish to upgrade this extension to newer
14+
* version in the future.
15+
*
16+
* @category Landofcoder
17+
* @package Lof_MarketplaceGraphQl
18+
* @copyright Copyright (c) 2021 Landofcoder (https://www.landofcoder.com/)
19+
* @license https://landofcoder.com/terms
20+
*/
21+
22+
namespace Lof\MarketplaceGraphQl\Model\Resolver\SellerGroups;
23+
24+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
25+
use Magento\Framework\GraphQl\Config\Element\Field;
26+
use Magento\Framework\GraphQl\Query\ResolverInterface;
27+
28+
/**
29+
* Retrieves the sort fields data
30+
*/
31+
class SortFields implements ResolverInterface
32+
{
33+
/**
34+
* @inheritdoc
35+
*/
36+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
37+
{
38+
$sortFieldsOptions = [
39+
['label' => "position", 'value' => "position"],
40+
['label' => "name", 'value' => "name"],
41+
['label' => "group_id", 'value' => "group_id"],
42+
['label' => "status", 'value' => "status"],
43+
['label' => "shown_in_sidebar", 'value' => "shown_in_sidebar"]
44+
];
45+
46+
$data = [
47+
'default' => "position",
48+
'options' => $sortFieldsOptions,
49+
];
50+
51+
return $data;
52+
}
53+
}

etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<arguments>
2525
<argument name="attributesInstances" xsi:type="array">
2626
<item name="lof_marketplace_seller" xsi:type="object">\Lof\MarketplaceGraphQl\Model\Resolver\FilterArgumentSeller</item>
27+
<item name="lof_marketplace_seller_group" xsi:type="object">\Lof\MarketplaceGraphQl\Model\Resolver\FilterArgumentSellerGroup</item>
2728
</argument>
2829
</arguments>
2930
</type>

0 commit comments

Comments
 (0)