Skip to content

Commit 6347a7f

Browse files
committed
Store Credit GraphQl
1 parent 81f2969 commit 6347a7f

File tree

12 files changed

+785
-0
lines changed

12 files changed

+785
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* Mageplaza
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Mageplaza.com license that is
8+
* available through the world-wide-web at this URL:
9+
* https://www.mageplaza.com/LICENSE.txt
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 Mageplaza
17+
* @package Mageplaza_StoreCreditGraphQl
18+
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
19+
* @license https://www.mageplaza.com/LICENSE.txt
20+
*/
21+
22+
declare(strict_types=1);
23+
24+
namespace Mageplaza\StoreCreditGraphQl\Model\Resolver;
25+
26+
use Magento\Framework\GraphQl\Config\Element\Field;
27+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
28+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
29+
use Magento\Framework\GraphQl\Query\ResolverInterface;
30+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
31+
use Mageplaza\StoreCredit\Helper\Data;
32+
33+
/**
34+
* Class AbstractStoreCredit
35+
* @package Mageplaza\StoreCreditGraphQl\Model\Resolver
36+
*/
37+
abstract class AbstractStoreCredit implements ResolverInterface
38+
{
39+
/**
40+
* @var Data
41+
*/
42+
protected $helperData;
43+
44+
/**
45+
* AbstractReward constructor.
46+
*
47+
* @param Data $helperData
48+
*/
49+
public function __construct(Data $helperData)
50+
{
51+
$this->helperData = $helperData;
52+
}
53+
54+
/**
55+
* @inheritdoc
56+
*/
57+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
58+
{
59+
if (!$this->helperData->isEnabled()) {
60+
throw new GraphQlNoSuchEntityException(__('Reward points is disabled.'));
61+
}
62+
63+
if ($this->helperData->versionCompare('2.3.3')) {
64+
if ($context->getExtensionAttributes()->getIsCustomer() === false) {
65+
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
66+
}
67+
}
68+
}
69+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/**
3+
* Mageplaza
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Mageplaza.com license that is
8+
* available through the world-wide-web at this URL:
9+
* https://www.mageplaza.com/LICENSE.txt
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 Mageplaza
17+
* @package Mageplaza_StoreCreditGraphQl
18+
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
19+
* @license https://www.mageplaza.com/LICENSE.txt
20+
*/
21+
22+
declare(strict_types=1);
23+
24+
namespace Mageplaza\StoreCreditGraphQl\Model\Resolver\Customer;
25+
26+
use Magento\Framework\GraphQl\Config\Element\Field;
27+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
28+
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
29+
use Mageplaza\StoreCredit\Model\Api\SpendingManagement;
30+
use Magento\Checkout\Model\TotalsInformation;
31+
use Mageplaza\StoreCredit\Helper\Data;
32+
use Mageplaza\StoreCreditGraphQl\Model\Resolver\AbstractStoreCredit;
33+
34+
/**
35+
* Class Spending
36+
* @package Mageplaza\StoreCreditGraphQl\Model\Resolver\Customer
37+
*/
38+
class Spending extends AbstractStoreCredit
39+
{
40+
/**
41+
* @var SpendingManagement
42+
*/
43+
protected $spendingManagement;
44+
45+
/**
46+
* @var GetCartForUser
47+
*/
48+
private $getCartForUser;
49+
50+
/**
51+
* @var TotalsInformation
52+
*/
53+
protected $totalInformation;
54+
55+
/**
56+
* SpendingPoint constructor.
57+
*
58+
* @param SpendingManagement $spendingManagement
59+
* @param GetCartForUser $getCartForUser
60+
* @param TotalsInformation $totalInformation
61+
* @param Data $helperData
62+
*/
63+
public function __construct(
64+
SpendingManagement $spendingManagement,
65+
GetCartForUser $getCartForUser,
66+
TotalsInformation $totalInformation,
67+
Data $helperData
68+
) {
69+
$this->spendingManagement = $spendingManagement;
70+
$this->getCartForUser = $getCartForUser;
71+
$this->totalInformation = $totalInformation;
72+
parent::__construct($helperData);
73+
}
74+
75+
/**
76+
* @inheritdoc
77+
*/
78+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
79+
{
80+
parent::resolve($field, $context, $info, $value, $args);
81+
82+
if ($this->helperData->versionCompare('2.3.3')) {
83+
$store = $context->getExtensionAttributes()->getStore();
84+
$quote = $this->getCartForUser->execute($args['cart_id'], $context->getUserId(), (int) $store->getId());
85+
} else {
86+
$quote = $this->getCartForUser->execute($args['cart_id'], $context->getUserId());
87+
}
88+
89+
$totals = $this->spendingManagement->spend(
90+
$quote->getId(),
91+
$args['amount']
92+
);
93+
94+
return $totals->getTotalSegments();
95+
}
96+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Mageplaza
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Mageplaza.com license that is
8+
* available through the world-wide-web at this URL:
9+
* https://www.mageplaza.com/LICENSE.txt
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 Mageplaza
17+
* @package Mageplaza_StoreCreditGraphQl
18+
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
19+
* @license https://www.mageplaza.com/LICENSE.txt
20+
*/
21+
22+
23+
declare(strict_types=1);
24+
25+
namespace Mageplaza\StoreCreditGraphQl\Model\Resolver\FilterArgument;
26+
27+
use Magento\Framework\GraphQl\Config\Element\Field;
28+
use Magento\Framework\GraphQl\ConfigInterface;
29+
use Magento\Framework\GraphQl\Query\Resolver\Argument\FieldEntityAttributesInterface;
30+
31+
/**
32+
* Class StoreCreditTransaction
33+
* @package Mageplaza\StoreCreditGraphQl\Model\Resolver\FilterArgument
34+
*/
35+
class StoreCreditTransaction implements FieldEntityAttributesInterface
36+
{
37+
/**
38+
* @var string
39+
*/
40+
protected $type = 'MpStoreCreditCustomerTransaction';
41+
42+
/**
43+
* @var ConfigInterface
44+
*/
45+
private $config;
46+
47+
/**
48+
* FilterArgument constructor.
49+
*
50+
* @param ConfigInterface $config
51+
*/
52+
public function __construct(ConfigInterface $config)
53+
{
54+
$this->config = $config;
55+
}
56+
57+
/**
58+
* @return array
59+
*/
60+
public function getEntityAttributes(): array
61+
{
62+
$fields = [];
63+
64+
/** @var Field $field */
65+
foreach ($this->config->getConfigElement($this->type)->getFields() as $field) {
66+
$fields[$field->getName()] = '';
67+
}
68+
69+
return array_keys($fields);
70+
}
71+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* Mageplaza
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Mageplaza.com license that is
8+
* available through the world-wide-web at this URL:
9+
* https://www.mageplaza.com/LICENSE.txt
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 Mageplaza
17+
* @package Mageplaza_StoreCreditGraphQl
18+
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
19+
* @license https://www.mageplaza.com/LICENSE.txt
20+
*/
21+
22+
declare(strict_types=1);
23+
24+
namespace Mageplaza\StoreCreditGraphQl\Model\Resolver\Product;
25+
26+
use Magento\Catalog\Api\Data\ProductInterface;
27+
use Magento\Framework\Exception\LocalizedException;
28+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
29+
use Magento\Framework\GraphQl\Config\Element\Field;
30+
use Magento\Framework\GraphQl\Query\ResolverInterface;
31+
use Magento\Catalog\Api\ProductRepositoryInterface;
32+
33+
/**
34+
* Class ProductAttribute
35+
* @package Mageplaza\StoreCreditGraphQl\Model\Resolver\Product
36+
*/
37+
class ProductAttribute implements ResolverInterface
38+
{
39+
/**
40+
* @var ProductRepositoryInterface
41+
*/
42+
private $productRepository;
43+
44+
/**
45+
* ProductAttribute constructor.
46+
*
47+
* @param ProductRepositoryInterface $productRepository
48+
*/
49+
public function __construct(ProductRepositoryInterface $productRepository)
50+
{
51+
$this->productRepository = $productRepository;
52+
}
53+
54+
/**
55+
* @inheritdoc
56+
*/
57+
public function resolve(
58+
Field $field,
59+
$context,
60+
ResolveInfo $info,
61+
array $value = null,
62+
array $args = null
63+
): array {
64+
if (!isset($value['model'])) {
65+
throw new LocalizedException(__('"model" value should be specified'));
66+
}
67+
68+
/** @var ProductInterface $product */
69+
$product = $this->productRepository->getById($value['model']->getId());
70+
$storeCreditAttributes = ['min_credit', 'max_credit', 'credit_rate', 'credit_amount', 'allow_credit_range'];
71+
$attributesData = [];
72+
73+
foreach ($storeCreditAttributes as $attributeCode) {
74+
$attributesData[] = [
75+
'attribute_code' => $attributeCode,
76+
'value' => $product->getData($attributeCode)
77+
];
78+
}
79+
80+
return $attributesData;
81+
}
82+
}

0 commit comments

Comments
 (0)