Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 73b5b92

Browse files
authored
Merge pull request #3870 from magento-engcom/graphql-develop-prs
[EngCom] Public Pull Requests - GraphQL
2 parents c427332 + 0b6099a commit 73b5b92

File tree

19 files changed

+957
-61
lines changed

19 files changed

+957
-61
lines changed

app/code/Magento/CustomerGraphQl/Model/Customer/CheckCustomerPassword.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99

1010
use Magento\Customer\Model\AuthenticationInterface;
1111
use Magento\Framework\Exception\InvalidEmailOrPasswordException;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\Exception\NoSuchEntityException;
14+
use Magento\Framework\Exception\State\UserLockedException;
1215
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
16+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
17+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1318

1419
/**
1520
* Check customer password
@@ -36,15 +41,21 @@ public function __construct(
3641
* @param string $password
3742
* @param int $customerId
3843
* @throws GraphQlAuthenticationException
44+
* @throws GraphQlInputException
45+
* @throws GraphQlNoSuchEntityException
3946
*/
4047
public function execute(string $password, int $customerId)
4148
{
4249
try {
4350
$this->authentication->authenticate($customerId, $password);
4451
} catch (InvalidEmailOrPasswordException $e) {
45-
throw new GraphQlAuthenticationException(
46-
__('The password doesn\'t match this account. Verify the password and try again.')
47-
);
52+
throw new GraphQlAuthenticationException(__($e->getMessage()), $e);
53+
} catch (UserLockedException $e) {
54+
throw new GraphQlAuthenticationException(__($e->getMessage()), $e);
55+
} catch (NoSuchEntityException $e) {
56+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
57+
} catch (LocalizedException $e) {
58+
throw new GraphQlInputException(__($e->getMessage()), $e);
4859
}
4960
}
5061
}

app/code/Magento/CustomerGraphQl/Model/Customer/UpdateCustomerData.php

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99

1010
use Magento\Customer\Api\CustomerRepositoryInterface;
1111
use Magento\Framework\Exception\AlreadyExistsException;
12+
use Magento\Framework\Exception\LocalizedException;
1213
use Magento\Framework\GraphQl\Exception\GraphQlAlreadyExistsException;
14+
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
1315
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14-
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1516
use Magento\Store\Model\StoreManagerInterface;
17+
use Magento\Customer\Api\Data\CustomerInterface;
18+
use Magento\Framework\Api\DataObjectHelper;
1619

1720
/**
1821
* Update customer data
@@ -34,19 +37,35 @@ class UpdateCustomerData
3437
*/
3538
private $checkCustomerPassword;
3639

40+
/**
41+
* @var DataObjectHelper
42+
*/
43+
private $dataObjectHelper;
44+
45+
/**
46+
* @var array
47+
*/
48+
private $restrictedKeys;
49+
3750
/**
3851
* @param CustomerRepositoryInterface $customerRepository
3952
* @param StoreManagerInterface $storeManager
4053
* @param CheckCustomerPassword $checkCustomerPassword
54+
* @param DataObjectHelper $dataObjectHelper
55+
* @param array $restrictedKeys
4156
*/
4257
public function __construct(
4358
CustomerRepositoryInterface $customerRepository,
4459
StoreManagerInterface $storeManager,
45-
CheckCustomerPassword $checkCustomerPassword
60+
CheckCustomerPassword $checkCustomerPassword,
61+
DataObjectHelper $dataObjectHelper,
62+
array $restrictedKeys = []
4663
) {
4764
$this->customerRepository = $customerRepository;
4865
$this->storeManager = $storeManager;
4966
$this->checkCustomerPassword = $checkCustomerPassword;
67+
$this->dataObjectHelper = $dataObjectHelper;
68+
$this->restrictedKeys = $restrictedKeys;
5069
}
5170

5271
/**
@@ -55,21 +74,16 @@ public function __construct(
5574
* @param int $customerId
5675
* @param array $data
5776
* @return void
58-
* @throws GraphQlNoSuchEntityException
59-
* @throws GraphQlInputException
6077
* @throws GraphQlAlreadyExistsException
78+
* @throws GraphQlInputException
79+
* @throws GraphQlAuthenticationException
6180
*/
6281
public function execute(int $customerId, array $data): void
6382
{
6483
$customer = $this->customerRepository->getById($customerId);
6584

66-
if (isset($data['firstname'])) {
67-
$customer->setFirstname($data['firstname']);
68-
}
69-
70-
if (isset($data['lastname'])) {
71-
$customer->setLastname($data['lastname']);
72-
}
85+
$filteredData = array_diff_key($data, array_flip($this->restrictedKeys));
86+
$this->dataObjectHelper->populateWithArray($customer, $filteredData, CustomerInterface::class);
7387

7488
if (isset($data['email']) && $customer->getEmail() !== $data['email']) {
7589
if (!isset($data['password']) || empty($data['password'])) {
@@ -89,6 +103,8 @@ public function execute(int $customerId, array $data): void
89103
__('A customer with the same email address already exists in an associated website.'),
90104
$e
91105
);
106+
} catch (LocalizedException $e) {
107+
throw new GraphQlInputException(__($e->getMessage()), $e);
92108
}
93109
}
94110
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<type name="Magento\CustomerGraphQl\Model\Customer\UpdateCustomerData">
10+
<arguments>
11+
<argument name="restrictedKeys" xsi:type="array">
12+
<item name="email" xsi:type="const">Magento\Customer\Api\Data\CustomerInterface::EMAIL</item>
13+
</argument>
14+
</arguments>
15+
</type>
16+
</config>

app/code/Magento/CustomerGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ type Customer @doc(description: "Customer defines the customer name and address
9292
id: Int @doc(description: "The ID assigned to the customer")
9393
is_subscribed: Boolean @doc(description: "Indicates whether the customer is subscribed to the company's newsletter") @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\IsSubscribed")
9494
addresses: [CustomerAddress] @doc(description: "An array containing the customer's shipping and billing addresses")
95+
gender: Int @doc(description: "The customer's gender(Male - 1, Female - 2)")
9596
}
9697

9798
type CustomerAddress @doc(description: "CustomerAddress contains detailed information about a customer's billing and shipping addresses"){

app/code/Magento/QuoteGraphQl/Model/Resolver/ApplyCouponToCart.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ public function __construct(
5050
*/
5151
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
5252
{
53-
if (!isset($args['input']['cart_id'])) {
53+
if (!isset($args['input']['cart_id']) || empty($args['input']['cart_id'])) {
5454
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
5555
}
5656
$maskedCartId = $args['input']['cart_id'];
5757

58-
if (!isset($args['input']['coupon_code'])) {
58+
if (!isset($args['input']['coupon_code']) || empty($args['input']['coupon_code'])) {
5959
throw new GraphQlInputException(__('Required parameter "coupon_code" is missing'));
6060
}
6161
$couponCode = $args['input']['coupon_code'];

app/code/Magento/QuoteGraphQl/Model/Resolver/Cart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function __construct(
3737
*/
3838
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
3939
{
40-
if (!isset($args['cart_id'])) {
40+
if (!isset($args['cart_id']) || empty($args['cart_id'])) {
4141
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
4242
}
4343
$maskedCartId = $args['cart_id'];

app/code/Magento/QuoteGraphQl/Model/Resolver/RemoveCouponFromCart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function __construct(
5050
*/
5151
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
5252
{
53-
if (!isset($args['input']['cart_id'])) {
53+
if (!isset($args['input']['cart_id']) || empty($args['input']['cart_id'])) {
5454
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
5555
}
5656
$maskedCartId = $args['input']['cart_id'];

app/code/Magento/QuoteGraphQl/Model/Resolver/RemoveItemFromCart.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1515
use Magento\Framework\GraphQl\Query\ResolverInterface;
1616
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17-
use Magento\Quote\Api\GuestCartItemRepositoryInterface;
17+
use Magento\Quote\Api\CartItemRepositoryInterface;
1818
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
1919

2020
/**
@@ -23,46 +23,46 @@
2323
class RemoveItemFromCart implements ResolverInterface
2424
{
2525
/**
26-
* @var GuestCartItemRepositoryInterface
26+
* @var GetCartForUser
2727
*/
28-
private $guestCartItemRepository;
28+
private $getCartForUser;
2929

3030
/**
31-
* @var GetCartForUser
31+
* @var CartItemRepositoryInterface
3232
*/
33-
private $getCartForUser;
33+
private $cartItemRepository;
3434

3535
/**
36-
* @param GuestCartItemRepositoryInterface $guestCartItemRepository
3736
* @param GetCartForUser $getCartForUser
37+
* @param CartItemRepositoryInterface $cartItemRepository
3838
*/
3939
public function __construct(
40-
GuestCartItemRepositoryInterface $guestCartItemRepository,
41-
GetCartForUser $getCartForUser
40+
GetCartForUser $getCartForUser,
41+
CartItemRepositoryInterface $cartItemRepository
4242
) {
43-
$this->guestCartItemRepository = $guestCartItemRepository;
4443
$this->getCartForUser = $getCartForUser;
44+
$this->cartItemRepository = $cartItemRepository;
4545
}
4646

4747
/**
4848
* @inheritdoc
4949
*/
5050
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
5151
{
52-
if (!isset($args['input']['cart_id'])) {
53-
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
52+
if (!isset($args['input']['cart_id']) || empty($args['input']['cart_id'])) {
53+
throw new GraphQlInputException(__('Required parameter "cart_id" is missing.'));
5454
}
5555
$maskedCartId = $args['input']['cart_id'];
5656

57-
if (!isset($args['input']['cart_item_id'])) {
58-
throw new GraphQlInputException(__('Required parameter "cart_item_id" is missing'));
57+
if (!isset($args['input']['cart_item_id']) || empty($args['input']['cart_item_id'])) {
58+
throw new GraphQlInputException(__('Required parameter "cart_item_id" is missing.'));
5959
}
6060
$itemId = $args['input']['cart_item_id'];
6161

6262
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
6363

6464
try {
65-
$this->guestCartItemRepository->deleteById($maskedCartId, $itemId);
65+
$this->cartItemRepository->deleteById((int)$cart->getId(), $itemId);
6666
} catch (NoSuchEntityException $e) {
6767
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
6868
} catch (LocalizedException $e) {
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Resolver;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
15+
use Magento\Framework\GraphQl\Query\ResolverInterface;
16+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17+
use Magento\Quote\Api\CartItemRepositoryInterface;
18+
use Magento\Quote\Model\Quote;
19+
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
20+
21+
/**
22+
* @inheritdoc
23+
*/
24+
class UpdateCartItems implements ResolverInterface
25+
{
26+
/**
27+
* @var GetCartForUser
28+
*/
29+
private $getCartForUser;
30+
31+
/**
32+
* @var CartItemRepositoryInterface
33+
*/
34+
private $cartItemRepository;
35+
36+
/**
37+
* @param GetCartForUser $getCartForUser
38+
* @param CartItemRepositoryInterface $cartItemRepository
39+
*/
40+
public function __construct(
41+
GetCartForUser $getCartForUser,
42+
CartItemRepositoryInterface $cartItemRepository
43+
) {
44+
$this->getCartForUser = $getCartForUser;
45+
$this->cartItemRepository = $cartItemRepository;
46+
}
47+
48+
/**
49+
* @inheritdoc
50+
*/
51+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
52+
{
53+
if (!isset($args['input']['cart_id']) || empty($args['input']['cart_id'])) {
54+
throw new GraphQlInputException(__('Required parameter "cart_id" is missing.'));
55+
}
56+
$maskedCartId = $args['input']['cart_id'];
57+
58+
if (!isset($args['input']['cart_items']) || empty($args['input']['cart_items'])
59+
|| !is_array($args['input']['cart_items'])
60+
) {
61+
throw new GraphQlInputException(__('Required parameter "cart_items" is missing.'));
62+
}
63+
$cartItems = $args['input']['cart_items'];
64+
65+
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
66+
67+
try {
68+
$this->processCartItems($cart, $cartItems);
69+
} catch (NoSuchEntityException $e) {
70+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
71+
} catch (LocalizedException $e) {
72+
throw new GraphQlInputException(__($e->getMessage()), $e);
73+
}
74+
75+
return [
76+
'cart' => [
77+
'model' => $cart,
78+
],
79+
];
80+
}
81+
82+
/**
83+
* Process cart items
84+
*
85+
* @param Quote $cart
86+
* @param array $items
87+
* @throws GraphQlInputException
88+
* @throws LocalizedException
89+
*/
90+
private function processCartItems(Quote $cart, array $items): void
91+
{
92+
foreach ($items as $item) {
93+
if (!isset($item['cart_item_id']) || empty($item['cart_item_id'])) {
94+
throw new GraphQlInputException(__('Required parameter "cart_item_id" for "cart_items" is missing.'));
95+
}
96+
$itemId = $item['cart_item_id'];
97+
98+
if (!isset($item['quantity'])) {
99+
throw new GraphQlInputException(__('Required parameter "quantity" for "cart_items" is missing.'));
100+
}
101+
$qty = (float)$item['quantity'];
102+
103+
$cartItem = $cart->getItemById($itemId);
104+
if ($cartItem === false) {
105+
throw new GraphQlNoSuchEntityException(
106+
__('Could not find cart item with id: %1.', $item['cart_item_id'])
107+
);
108+
}
109+
110+
if ($qty <= 0.0) {
111+
$this->cartItemRepository->deleteById((int)$cart->getId(), $itemId);
112+
} else {
113+
$cartItem->setQty($qty);
114+
$this->cartItemRepository->save($cartItem);
115+
}
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)