Skip to content

Commit

Permalink
Merge pull request #11 from dmitrijs-voronovs/my-orders
Browse files Browse the repository at this point in the history
Added support for graphql-orders.
  • Loading branch information
alfredsgenkins authored Oct 14, 2019
2 parents d16efd2 + 33f30cf commit 7eb599f
Show file tree
Hide file tree
Showing 5 changed files with 499 additions and 1 deletion.
116 changes: 116 additions & 0 deletions src/Model/Customer/CheckCustomerAccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
/**
* ScandiPWA - Progressive Web App for Magento
*
* Copyright © Scandiweb, Inc. All rights reserved.
* See LICENSE for license details.
*
* @license OSL-3.0 (Open Software License ("OSL") v. 3.0)
* @package scandipwa/quote-graphql
* @link https://github.com/scandipwa/quote-graphql
*/

declare(strict_types=1);

namespace ScandiPWA\QuoteGraphQl\Model\Customer;

use Magento\Authorization\Model\UserContextInterface;
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\AuthenticationInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;

/**
* Check customer account
*/
class CheckCustomerAccount
{
/**
* @var AuthenticationInterface
*/
private $authentication;

/**
* @var CustomerRepositoryInterface
*/
private $customerRepository;

/**
* @var AccountManagementInterface
*/
private $accountManagement;

/**
* @param AuthenticationInterface $authentication
* @param CustomerRepositoryInterface $customerRepository
* @param AccountManagementInterface $accountManagement
*/
public function __construct(
AuthenticationInterface $authentication,
CustomerRepositoryInterface $customerRepository,
AccountManagementInterface $accountManagement
) {
$this->authentication = $authentication;
$this->customerRepository = $customerRepository;
$this->accountManagement = $accountManagement;
}

/**
* Check customer account
*
* @param int|null $customerId
* @param int|null $customerType
* @return void
* @throws GraphQlAuthorizationException
* @throws GraphQlNoSuchEntityException
* @throws GraphQlAuthenticationException
*/
public function execute(?int $customerId, ?int $customerType): bool
{
if ($this->isCustomerGuest($customerId, $customerType)) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
return false;
}

try {
$this->customerRepository->getById($customerId);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(
__('Customer with id "%customer_id" does not exist.', ['customer_id' => $customerId]),
$e
);
return false;
}

if ($this->authentication->isLocked($customerId)) {
throw new GraphQlAuthenticationException(__('The account is locked.'));
return false;
}

$confirmationStatus = $this->accountManagement->getConfirmationStatus($customerId);
if ($confirmationStatus === AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED) {
throw new GraphQlAuthenticationException(__("This account isn't confirmed. Verify and try again."));
return false;
}

return true;
}

/**
* Checking if current customer is guest
*
* @param int|null $customerId
* @param int|null $customerType
* @return bool
*/
private function isCustomerGuest(?int $customerId, ?int $customerType): bool
{
if ($customerId === null || $customerType === null) {
return true;
}
return $customerId == 0 || $customerType == UserContextInterface::USER_TYPE_GUEST;
}
}
121 changes: 121 additions & 0 deletions src/Model/Resolver/ExpandedOrderResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php
/**
* ScandiPWA - Progressive Web App for Magento
*
* Copyright © Scandiweb, Inc. All rights reserved.
* See LICENSE for license details.
*
* @license OSL-3.0 (Open Software License ("OSL") v. 3.0)
* @package scandipwa/quote-graphql
* @link https://github.com/scandipwa/quote-graphql
*/

declare(strict_types=1);

namespace ScandiPWA\QuoteGraphQl\Model\Resolver;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactoryInterface;
use ScandiPWA\QuoteGraphQl\Model\Customer\CheckCustomerAccount;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;

use Magento\Sales\Model\OrderRepository;

/**
* Orders data reslover
*/
class ExpandedOrderResolver implements ResolverInterface
{
/**
* @var CollectionFactoryInterface
*/
private $collectionFactory;

/**
* @var CheckCustomerAccount
*/
private $checkCustomerAccount;

/**
* @var OrderRepository
*/
private $orderRepository;

/**
* @param CollectionFactoryInterface $collectionFactory
* @param CheckCustomerAccount $checkCustomerAccount
* @param OrderRepository $orderRepository
*/
public function __construct(
CollectionFactoryInterface $collectionFactory,
CheckCustomerAccount $checkCustomerAccount,
OrderRepository $orderRepository
) {
$this->collectionFactory = $collectionFactory;
$this->checkCustomerAccount = $checkCustomerAccount;
$this->orderRepository = $orderRepository;
}

/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
$itemsData = [];
$trackNumbers = [];

$customerId = $context->getUserId();
$this->checkCustomerAccount->execute($customerId, $context->getUserType());

$orderId = $args['id'];
$order = $this->orderRepository->get($orderId);

if ($customerId != $order->getCustomerId()) {
throw new GraphQlNoSuchEntityException(__('Customer ID is invalid.'));
}

foreach ($order->getAllVisibleItems() as $item) {
$itemsData[] = $item;
}

$tracksCollection = $order->getTracksCollection();

foreach ($tracksCollection->getItems() as $track) {
$trackNumbers[] = $track->getTrackNumber();
}

$shippingInfo = [
'shipping_amount' => $order->getShippingAmount(),
'shipping_method' => $order->getShippingMethod(),
'shipping_address' => $order->getShippingAddress(),
'shipping_description' => $order->getShippingDescription(),
'tracking_numbers' => $trackNumbers
];

$base_info = [
'id' => $order->getId(),
'increment_id' => $order->getIncrementId(),
'created_at' => $order->getCreatedAt(),
'grand_total' => $order->getGrandTotal(),
'sub_total' => $order->getBaseSubtotalInclTax(),
'status' => $order->getStatus(),
'status_label' => $order->getStatusLabel(),
'total_qty_ordered' => $order->getTotalQtyOrdered(),
];

return [
'base_order_info' => $base_info,
'shipping_info' => $shippingInfo,
'payment_info' => $order->getPayment()->getData(),
'products' => $itemsData
];
}
}
104 changes: 104 additions & 0 deletions src/Model/Resolver/OrderListResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* ScandiPWA - Progressive Web App for Magento
*
* Copyright © Scandiweb, Inc. All rights reserved.
* See LICENSE for license details.
*
* @license OSL-3.0 (Open Software License ("OSL") v. 3.0)
* @package scandipwa/quote-graphql
* @link https://github.com/scandipwa/quote-graphql
*/

declare(strict_types=1);

namespace ScandiPWA\QuoteGraphQl\Model\Resolver;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactoryInterface;
use ScandiPWA\QuoteGraphQl\Model\Customer\CheckCustomerAccount;

/**
* Orders data reslover
*/
class OrderListResolver implements ResolverInterface
{
/**
* @var CollectionFactoryInterface
*/
private $collectionFactory;

/**
* @var CheckCustomerAccount
*/
private $checkCustomerAccount;

/**
* @param CollectionFactoryInterface $collectionFactory
* @param CheckCustomerAccount $checkCustomerAccount
*/
public function __construct(
CollectionFactoryInterface $collectionFactory,
CheckCustomerAccount $checkCustomerAccount
) {
$this->collectionFactory = $collectionFactory;
$this->checkCustomerAccount = $checkCustomerAccount;
}

/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
$items = [];

$customerId = $context->getUserId();

$this->checkCustomerAccount->execute($customerId, $context->getUserType());

$orders = $this->collectionFactory->create($customerId);

foreach ($orders as $order) {
$trackNumbers = [];
$tracksCollection = $order->getTracksCollection();

foreach ($tracksCollection->getItems() as $track) {
$trackNumbers[] = $track->getTrackNumber();
}

$shippingInfo = [
'shipping_amount' => $order->getShippingAmount(),
'shipping_method' => $order->getShippingMethod(),
'shipping_address' => $order->getShippingAddress(),
'shipping_description' => $order->getShippingDescription(),
'tracking_numbers' => $trackNumbers
];

$base_info = [
'id' => $order->getId(),
'increment_id' => $order->getIncrementId(),
'created_at' => $order->getCreatedAt(),
'grand_total' => $order->getGrandTotal(),
'sub_total' => $order->getBaseSubtotalInclTax(),
'status' => $order->getStatus(),
'status_label' => $order->getStatusLabel(),
'total_qty_ordered' => $order->getTotalQtyOrdered(),
];

$items[] = [
'base_order_info' => $base_info,
'shipping_info' => $shippingInfo,
'payment_info' => $order->getPayment()->getData()
];
}

return ['items' => $items];
}
}
Loading

0 comments on commit 7eb599f

Please sign in to comment.