Skip to content

Commit

Permalink
Merge pull request #79 from raivisdejus/get-orders-also-by-email
Browse files Browse the repository at this point in the history
Will select orders by email as well
  • Loading branch information
carinadues authored May 25, 2022
2 parents d0a3bf3 + c6fd2c7 commit 4aa784b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
15 changes: 13 additions & 2 deletions src/Model/Resolver/ExpandedOrderResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use ScandiPWA\QuoteGraphQl\Model\Customer\CheckCustomerAccount;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Sales\Model\OrderRepository;
use Magento\Customer\Api\CustomerRepositoryInterface;

/**
* Orders data resolver
Expand All @@ -42,19 +43,27 @@ class ExpandedOrderResolver implements ResolverInterface
*/
protected $orderRepository;

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

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

/**
Expand All @@ -76,7 +85,9 @@ public function resolve(
$orderId = $args['id'];
$order = $this->orderRepository->get($orderId);

if ($customerId != $order->getCustomerId()) {
$customer = $this->customerRepository->getById($customerId);

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

Expand Down
27 changes: 23 additions & 4 deletions src/Model/Resolver/OrderListResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactoryInterface;
use ScandiPWA\QuoteGraphQl\Model\Customer\CheckCustomerAccount;
use Magento\Customer\Api\CustomerRepositoryInterface;

/**
* Orders data reslover
Expand All @@ -28,23 +29,34 @@ class OrderListResolver implements ResolverInterface
/**
* @var CollectionFactoryInterface
*/
private $collectionFactory;
protected $collectionFactory;

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


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

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

parent::__construct($collectionFactory, $checkCustomerAccount);
}

/**
Expand All @@ -63,7 +75,14 @@ public function resolve(

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

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

$ordersCollection = $this->collectionFactory->create();
$ordersCollection->addFieldToFilter(
['customer_id','customer_email'],
[['eq' => $customerId],['eq'=>$customer->getEmail()]]
);
$orders = $ordersCollection->load();

foreach ($orders as $order) {
$trackNumbers = [];
Expand Down

0 comments on commit 4aa784b

Please sign in to comment.