Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacker committed Feb 8, 2022
2 parents db51637 + 0779d72 commit 8f1b315
Show file tree
Hide file tree
Showing 9 changed files with 485 additions and 27 deletions.
3 changes: 2 additions & 1 deletion Model/Resolver/AbstractGetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ abstract class AbstractGetList implements ResolverInterface

/**
* AbstractGetList constructor.
*
* @param SearchCriteriaBuilder $searchCriteriaBuilder
*/
public function __construct(
Expand Down Expand Up @@ -100,7 +101,7 @@ abstract protected function getSearchResult($customer, $searchCriteria);
* @return array
* @throws GraphQlInputException
*/
private function getPageInfo($searchResult, $args)
protected function getPageInfo($searchResult, $args)
{
$totalPages = ceil($searchResult->getTotalCount() / $args['pageSize']);
$currentPage = $args['currentPage'];
Expand Down
90 changes: 90 additions & 0 deletions Model/Resolver/Orders/MpReward.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_RewardPointsGraphQl
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/

declare(strict_types=1);

namespace Mageplaza\RewardPointsGraphQl\Model\Resolver\Orders;

use Exception;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\OrderFactory;
use Mageplaza\RewardPoints\Helper\Data;

/**
* Class MpReward
* @package Mageplaza\RewardPointsGraphQl\Model\Resolver\Orders
*/
class MpReward implements ResolverInterface
{
/**
* @var Data
*/
protected $helperData;

/**
* @var OrderFactory
*/
protected $orderFactory;

/**
* MpReward constructor.
*
* @param Data $helperData
* @param OrderFactory $orderFactory
*/
public function __construct(
Data $helperData,
OrderFactory $orderFactory
) {
$this->helperData = $helperData;
$this->orderFactory = $orderFactory;
}

/**
* @inheritDoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
if (!$this->helperData->isEnabled()) {
return null;
}

try {
if (isset($value['increment_id'])) {
$order = $this->orderFactory->create()->loadByIncrementId($value['increment_id']);

return [
'earn' => $order->getData('mp_reward_earn'),
'spent' => $order->getData('mp_reward_spent'),
'discount' => abs($order->getData('mp_reward_discount'))
];
}

return null;
} catch (Exception $e) {
return null;
}
}
}
23 changes: 22 additions & 1 deletion Model/Resolver/RewardCustomer/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Mageplaza\RewardPoints\Helper\Data;
use Mageplaza\RewardPoints\Model\AccountFactory as RewardCustomerFactory;
use Magento\Framework\Encryption\EncryptorInterface;

/**
* Class Account
Expand All @@ -46,17 +47,26 @@ class Account implements ResolverInterface
*/
protected $helperData;

/**
* @var EncryptorInterface
*/
protected $encryptor;

/**
* Account constructor.
*
* @param RewardCustomerFactory $rewardCustomerFactory
* @param Data $helperData
* @param EncryptorInterface $encryptor
*/
public function __construct(
RewardCustomerFactory $rewardCustomerFactory,
Data $helperData
Data $helperData,
EncryptorInterface $encryptor
) {
$this->helperData = $helperData;
$this->rewardCustomerFactory = $rewardCustomerFactory;
$this->encryptor = $encryptor;
}

/**
Expand Down Expand Up @@ -84,6 +94,7 @@ public function resolve(
$data = $rewardCustomer->toArray();
$data['point_spent'] = $rewardCustomer->getPointSpent();
$data['point_earned'] = $rewardCustomer->getPointEarned();
$data['refer_code'] = $this->encrypt($customer->getId());

$pointHelper = $this->helperData->getPointHelper();
if ($this->helperData->getMaxPointPerCustomer()) {
Expand All @@ -105,4 +116,14 @@ public function resolve(

return $data;
}

/**
* @param string|int $data
*
* @return string
*/
public function encrypt($data)
{
return base64_encode($this->encryptor->encrypt((string) $data));
}
}
132 changes: 132 additions & 0 deletions Model/Resolver/RewardCustomer/TransactionsByOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the Mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_RewardPointsGraphQl
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/

declare(strict_types=1);

namespace Mageplaza\RewardPointsGraphQl\Model\Resolver\RewardCustomer;

use Exception;
use Magento\Framework\Api\Search\SearchCriteriaInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\Resolver\Argument\SearchCriteria\Builder as SearchCriteriaBuilder;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Sales\Model\Order;
use Mageplaza\RewardPointsGraphQl\Model\Resolver\AbstractGetList;
use Mageplaza\RewardPoints\Model\TransactionRepository;

/**
* Class TransactionsByOrder
* @package Mageplaza\RewardPointsGraphQl\Model\Resolver\RewardCustomer
*/
class TransactionsByOrder extends AbstractGetList
{
/**
* @var string
*/
protected $fieldName = 'mp_reward_transactions';

/**
* @var TransactionRepository
*/
protected $transactionRepository;

/**
* @var Order
*/
protected $order;

/**
* TransactionsByOrder constructor.
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param TransactionRepository $transactionRepository
* @param Order $order
*/
public function __construct(
SearchCriteriaBuilder $searchCriteriaBuilder,
TransactionRepository $transactionRepository,
Order $order
) {
$this->transactionRepository = $transactionRepository;
$this->order = $order;

parent::__construct($searchCriteriaBuilder);
}

/**
* @param Field $field
* @param ContextInterface $context
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
* @return array
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
try {
/** @var Order $order */
$order = isset($value['model']) ? $value['model'] : null;
if (!$order) {
$order = $this->order->loadByIncrementId($value['increment_id']);
}

if (isset($args['currentPage']) && $args['currentPage'] < 1) {
throw new GraphQlInputException(__('currentPage value must be greater than 0.'));
}

if (isset($args['pageSize']) && $args['pageSize'] < 1) {
throw new GraphQlInputException(__('pageSize value must be greater than 0.'));
}

$searchCriteria = $this->searchCriteriaBuilder->build($this->fieldName, $args);
$searchCriteria->setCurrentPage($args['currentPage']);
$searchCriteria->setPageSize($args['pageSize']);
$searchResult = $this->getSearchResult($order->getId(), $searchCriteria);

return [
'total_count' => $searchResult->getTotalCount(),
'items' => $searchResult->getItems(),
'page_info' => $this->getPageInfo($searchResult, $args)
];
} catch (Exception $e) {
return [];
}
}

/**
* @param int $orderId
* @param SearchCriteriaInterface $searchCriteria
*
* @return mixed
* @throws LocalizedException
*/
public function getSearchResult($orderId, $searchCriteria)
{
$result = $this->transactionRepository->getListByOrderId($searchCriteria, $orderId);
foreach ($result->getItems() as $item) {
$item->setComment($item->getTitle());
}

return $result;
}
}
Loading

0 comments on commit 8f1b315

Please sign in to comment.