diff --git a/Model/Resolver/AbstractGetList.php b/Model/Resolver/AbstractGetList.php index c6aa662..e1f597f 100755 --- a/Model/Resolver/AbstractGetList.php +++ b/Model/Resolver/AbstractGetList.php @@ -49,6 +49,7 @@ abstract class AbstractGetList implements ResolverInterface /** * AbstractGetList constructor. + * * @param SearchCriteriaBuilder $searchCriteriaBuilder */ public function __construct( @@ -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']; diff --git a/Model/Resolver/Orders/MpReward.php b/Model/Resolver/Orders/MpReward.php new file mode 100644 index 0000000..e5a64c2 --- /dev/null +++ b/Model/Resolver/Orders/MpReward.php @@ -0,0 +1,90 @@ +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; + } + } +} diff --git a/Model/Resolver/RewardCustomer/Account.php b/Model/Resolver/RewardCustomer/Account.php index e1e0771..97fd7e8 100755 --- a/Model/Resolver/RewardCustomer/Account.php +++ b/Model/Resolver/RewardCustomer/Account.php @@ -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 @@ -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; } /** @@ -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()) { @@ -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)); + } } diff --git a/Model/Resolver/RewardCustomer/TransactionsByOrder.php b/Model/Resolver/RewardCustomer/TransactionsByOrder.php new file mode 100644 index 0000000..2b7086d --- /dev/null +++ b/Model/Resolver/RewardCustomer/TransactionsByOrder.php @@ -0,0 +1,132 @@ +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; + } +} diff --git a/Plugin/Model/Resolver/CreateCustomer.php b/Plugin/Model/Resolver/CreateCustomer.php new file mode 100644 index 0000000..a12e4e1 --- /dev/null +++ b/Plugin/Model/Resolver/CreateCustomer.php @@ -0,0 +1,104 @@ +ultimateData = $ultimateData; + $this->customerRepository = $customerRepository; + } + + /** + * @param CreateCustomerAbstract $subject + * @param Field $field + * @param ContextInterface $context + * @param ResolveInfo $info + * @param array|null $value + * @param array|null $args + * + * @return array + * @throws InputException + * @throws CookieSizeLimitReachedException + * @throws FailureToSendException + */ + public function beforeResolve( + CreateCustomerAbstract $subject, + Field $field, + $context, + ResolveInfo $info, + array $value = null, + array $args = null + ) { + + if (isset($args['input']['mp_refer'])) { + try { + $referCodeOrEmail = trim($args['input']['mp_refer']); + $referCode = $this->ultimateData->getCryptHelper()->checkReferCodeOrEmail($referCodeOrEmail); + } catch (Exception $e) { + $referCode = false; + } + + if ($referCode) { + $this->ultimateData->getCookieHelper()->set($referCode); + } else { + $this->ultimateData->getCookieHelper()->deleteMpRefererKeyFromCookie(); + } + } + + return [$field, $context, $info, $value, $args]; + } +} diff --git a/Plugin/Model/Resolver/OrderTotal.php b/Plugin/Model/Resolver/OrderTotal.php new file mode 100644 index 0000000..94070ba --- /dev/null +++ b/Plugin/Model/Resolver/OrderTotal.php @@ -0,0 +1,79 @@ +helperData = $helperData; + } + + /** + * @param SalesGraphQlOrderTotal $subject + * @param array $result + * + * @return mixed + * @throws LocalizedException + */ + public function afterResolve(SalesGraphQlOrderTotal $subject, $result) + { + if (!$this->helperData->isEnabled()) { + return $result; + } + + if (!(($result['model'] ?? null) instanceof OrderInterface)) { + throw new LocalizedException(__('"model" value should be specified')); + } + + /** @var Order $order */ + $order = $result['model']; + $result['mp_reward_points'] = [ + 'earn' => $order->getData('mp_reward_earn'), + 'spent' => $order->getData('mp_reward_spent'), + 'discount' => abs($order->getData('mp_reward_discount')) + ]; + + return $result; + } +} diff --git a/composer.json b/composer.json index 89d31d5..3418261 100755 --- a/composer.json +++ b/composer.json @@ -1,23 +1,23 @@ -{ - "name": "mageplaza/module-reward-points-graphql", - "description": "Magento 2 Reward Points Graphql Extension", - "type": "magento2-module", - "version": "1.1.0", - "license": "proprietary", - "authors": [ - { - "name": "Mageplaza", - "email": "support@mageplaza.com", - "homepage": "https://www.mageplaza.com", - "role": "Technical Support" - } - ], - "autoload": { - "files": [ - "registration.php" - ], - "psr-4": { - "Mageplaza\\RewardPointsGraphQl\\": "" - } - } -} \ No newline at end of file +{ + "name": "mageplaza/module-reward-points-graphql", + "description": "Magento 2 Reward Points Graphql Extension", + "type": "magento2-module", + "version": "1.2.0", + "license": "proprietary", + "authors": [ + { + "name": "Mageplaza", + "email": "support@mageplaza.com", + "homepage": "https://www.mageplaza.com", + "role": "Technical Support" + } + ], + "autoload": { + "files": [ + "registration.php" + ], + "psr-4": { + "Mageplaza\\RewardPointsGraphQl\\": "" + } + } +} diff --git a/etc/di.xml b/etc/di.xml index 9aa03d7..d83aadc 100755 --- a/etc/di.xml +++ b/etc/di.xml @@ -33,4 +33,10 @@ + + + + + + diff --git a/etc/schema.graphqls b/etc/schema.graphqls index 82ac4f6..0ef1868 100755 --- a/etc/schema.graphqls +++ b/etc/schema.graphqls @@ -8,7 +8,7 @@ type Query { } ## ======================================== Customer =================================================================== -type Customer { + type Customer { mp_reward:MpRewardCustomersOutput @doc(description: "Reward customer") @resolver(class: "Mageplaza\\RewardPointsGraphQl\\Model\\Resolver\\RewardCustomer\\Account") } @@ -107,6 +107,7 @@ type MpRewardCustomersOutput { notification_expire : String @doc(description: "Notification of expire") balance_limitation : String @doc(description: "Balance limitation") earn_point_expire: String @doc(description: "Earn point expire") + refer_code: String @doc(description: "Refer code") current_exchange_rates: RewardExchangeRate @doc(description: "Current exchange rates") @resolver(class: "Mageplaza\\RewardPointsGraphQl\\Model\\Resolver\\RewardCustomer\\Rate") transactions( filter: MpRewardTransactionsFilterInput @doc(description: "Identifies which reward customer transaction attributes to search for and return.") @@ -273,4 +274,28 @@ type MpRewardInvitations { invited_discount: Float @doc(description: "Invited discount") store_id: String @doc(description: "Store id") created_at: String @doc(description: "Created at") -} \ No newline at end of file +} + +## ========================================= Refer Code ============================================================== +input CustomerInput { + mp_refer: String @doc(description: "Refer Code") +} + +type CustomerOrder { + mp_reward_points: MpRewardPointsOutput @doc(description: "GetMageplaza Reward Points Information(compatible with 2.3.x).") @resolver(class: "Mageplaza\\RewardPointsGraphQl\\Model\\Resolver\\Orders\\MpReward") + transactions( + filter: MpRewardTransactionsFilterInput @doc(description: "Identifies which reward customer transaction attributes to search for and return.") + pageSize: Int = 5 @doc(description: "How many items should be shown on the page?") + currentPage: Int = 1 @doc(description: "Allow page number to start from page 1.") + ):MpRewardTransactionsListOutput @resolver(class: "Mageplaza\\RewardPointsGraphQl\\Model\\Resolver\\RewardCustomer\\TransactionsByOrder") @doc(description: "The query searches for reward customer.") +} + +type OrderTotal { + mp_reward_points: MpRewardPointsOutput @doc(description: "Mageplaza Reward Points Information.") +} + +type MpRewardPointsOutput { + earn: Int @doc(description: "Points earned.") + spent: Int @doc(description: "Points spent.") + discount: Float @doc(description: "Discount for reward.") +}