-
Notifications
You must be signed in to change notification settings - Fork 9.4k
[GraphQl] Gift Message coverage for cart item #28072
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
magento-engcom-team
merged 16 commits into
magento:2.4-develop
from
Usik2203:graphql-256-cart-item-coverage
Jun 23, 2020
Merged
Changes from 10 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
034af40
PR 28072 Add coverage for cart items
Usik2203 7e6e023
Added additional checks and test cases
Usik2203 81737c0
Remove redandant line from fixture
Usik2203 120a105
PR 28194 Add ability to update Gift Message for cart item
Usik2203 035c157
move part logic to other file
Usik2203 8ed5018
Merge branch 'graphql-update-message-in-quote' into graphql-256-cart-…
Usik2203 cea930b
changes in test fixture
Usik2203 dabbd3c
Added test and minor fix
Usik2203 f6ce31d
Remove EE type from CA schema
Usik2203 a2f28d8
minor fix
Usik2203 233b695
Add some changes for messages
Usik2203 d127c98
Merge branch '2.4-develop' into graphql-256-cart-item-coverage
Usik2203 d9748d2
Merge branch '2.4-develop' into graphql-256-cart-item-coverage
lenaorobei d994276
minor fix
Usik2203 938e43b
Set gift message if gift message missing for cart item
Usik2203 d339ae8
Merge branch '2.4-develop' into graphql-256-cart-item-coverage
Usik2203 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
app/code/Magento/GiftMessageGraphQl/Model/Resolver/Cart/Item/GiftMessage.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\GiftMessageGraphQl\Model\Resolver\Cart\Item; | ||
|
||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; | ||
use Magento\Framework\GraphQl\Query\Resolver\Value; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\GiftMessage\Api\ItemRepositoryInterface; | ||
use Magento\GiftMessage\Helper\Message as GiftMessageHelper; | ||
|
||
/** | ||
* Class provides ability to get GiftMessage for cart item | ||
*/ | ||
class GiftMessage implements ResolverInterface | ||
{ | ||
/** | ||
* @var ItemRepositoryInterface | ||
*/ | ||
private $itemRepository; | ||
|
||
/** | ||
* @var GiftMessageHelper | ||
*/ | ||
private $giftMessageHelper; | ||
|
||
/** | ||
* @param ItemRepositoryInterface $itemRepository | ||
* @param GiftMessageHelper $giftMessageHelper | ||
*/ | ||
public function __construct( | ||
ItemRepositoryInterface $itemRepository, | ||
GiftMessageHelper $giftMessageHelper | ||
) { | ||
$this->itemRepository = $itemRepository; | ||
$this->giftMessageHelper = $giftMessageHelper; | ||
} | ||
|
||
/** | ||
* Return information about Gift message for item cart | ||
* | ||
* @param Field $field | ||
* @param ContextInterface $context | ||
* @param ResolveInfo $info | ||
* @param array|null $value | ||
* @param array|null $args | ||
* | ||
* @return array|Value|mixed | ||
* @throws GraphQlInputException | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function resolve( | ||
Field $field, | ||
$context, | ||
ResolveInfo $info, | ||
array $value = null, | ||
array $args = null | ||
) { | ||
if (!isset($value['model'])) { | ||
throw new GraphQlInputException(__('"model" value should be specified')); | ||
} | ||
|
||
$quoteItem = $value['model']; | ||
|
||
if (!$this->giftMessageHelper->isMessagesAllowed('items', $quoteItem)) { | ||
return null; | ||
} | ||
|
||
if (!$this->giftMessageHelper->isMessagesAllowed('item', $quoteItem)) { | ||
return null; | ||
} | ||
|
||
try { | ||
$giftItemMessage = $this->itemRepository->get($quoteItem->getQuoteId(), $quoteItem->getItemId()); | ||
} catch (LocalizedException $e) { | ||
throw new GraphQlInputException(__('Can\'t load cart item')); | ||
} | ||
|
||
if (!isset($giftItemMessage)) { | ||
return null; | ||
} | ||
|
||
return [ | ||
'to' => $giftItemMessage->getRecipient() ?? '', | ||
'from' => $giftItemMessage->getSender() ?? '', | ||
'message'=> $giftItemMessage->getMessage() ?? '' | ||
]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<type name="Magento\StoreGraphQl\Model\Resolver\Store\StoreConfigDataProvider"> | ||
<arguments> | ||
<argument name="extendedConfigData" xsi:type="array"> | ||
<item name="allow_order" xsi:type="string">sales/gift_options/allow_order</item> | ||
<item name="allow_items" xsi:type="string">sales/gift_options/allow_items</item> | ||
</argument> | ||
</arguments> | ||
</type> | ||
</config> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,39 @@ | ||
# Copyright © Magento, Inc. All rights reserved. | ||
# See COPYING.txt for license details. | ||
|
||
type StoreConfig { | ||
allow_order : String @doc(description: "Allow Gift Messages on order level") | ||
lenaorobei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
allow_items : String @doc(description: "Allow Gift Messages for order items") | ||
lenaorobei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
type Cart { | ||
gift_message: GiftMessage @resolver (class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\Cart\\GiftMessage") @doc(description: "The entered gift message for the cart") | ||
} | ||
|
||
type SimpleCartItem { | ||
gift_message: GiftMessage @resolver (class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\Cart\\Item\\GiftMessage") @doc(description: "The entered gift message for the cart item") | ||
} | ||
|
||
type ConfigurableCartItem { | ||
gift_message: GiftMessage @resolver (class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\Cart\\Item\\GiftMessage") @doc(description: "The entered gift message for the cart item") | ||
} | ||
|
||
type BundleCartItem { | ||
gift_message: GiftMessage @resolver (class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\Cart\\Item\\GiftMessage") @doc(description: "The entered gift message for the cart item") | ||
} | ||
|
||
type GiftMessage { | ||
lenaorobei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
to: String! @doc(description: "Recepient name") | ||
lenaorobei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
from: String! @doc(description: "Sender name") | ||
message: String! @doc(description: "Gift message text") | ||
} | ||
|
||
input CartItemUpdateInput { | ||
gift_message: GiftMessageInput @doc(description: "Gift message details for the cart item") | ||
} | ||
|
||
input GiftMessageInput { | ||
lenaorobei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
to: String! @doc(description: "Recepient name") | ||
lenaorobei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
from: String! @doc(description: "Sender name") | ||
message: String! @doc(description: "Gift message text") | ||
} |
145 changes: 145 additions & 0 deletions
145
app/code/Magento/QuoteGraphQl/Model/CartItem/DataProvider/UpdateCartItems.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\QuoteGraphQl\Model\CartItem\DataProvider; | ||
|
||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
use Magento\GiftMessage\Api\Data\MessageInterface; | ||
use Magento\GiftMessage\Api\ItemRepositoryInterface; | ||
use Magento\GiftMessage\Helper\Message as GiftMessageHelper; | ||
use Magento\Quote\Api\CartItemRepositoryInterface; | ||
use Magento\Quote\Model\Quote; | ||
use Magento\QuoteGraphQl\Model\Cart\UpdateCartItem; | ||
|
||
/** | ||
* Class contain update cart items methods | ||
*/ | ||
class UpdateCartItems | ||
{ | ||
/** | ||
* @var CartItemRepositoryInterface | ||
*/ | ||
private $cartItemRepository; | ||
|
||
/** | ||
* @var UpdateCartItem | ||
*/ | ||
private $updateCartItem; | ||
|
||
/** | ||
* @var ItemRepositoryInterface | ||
*/ | ||
private $itemRepository; | ||
|
||
/** | ||
* @var GiftMessageHelper | ||
*/ | ||
private $giftMessageHelper; | ||
|
||
/** | ||
* @param CartItemRepositoryInterface $cartItemRepository | ||
* @param UpdateCartItem $updateCartItem | ||
* @param ItemRepositoryInterface $itemRepository | ||
* @param GiftMessageHelper $giftMessageHelper | ||
*/ | ||
public function __construct( | ||
CartItemRepositoryInterface $cartItemRepository, | ||
UpdateCartItem $updateCartItem, | ||
ItemRepositoryInterface $itemRepository, | ||
GiftMessageHelper $giftMessageHelper | ||
) { | ||
$this->cartItemRepository = $cartItemRepository; | ||
$this->updateCartItem = $updateCartItem; | ||
$this->itemRepository = $itemRepository; | ||
$this->giftMessageHelper = $giftMessageHelper; | ||
} | ||
|
||
/** | ||
* Process cart items | ||
* | ||
* @param Quote $cart | ||
* @param array $items | ||
* | ||
* @throws GraphQlInputException | ||
* @throws LocalizedException | ||
* @SuppressWarnings(PHPMD.CyclomaticComplexity) | ||
* @SuppressWarnings(PHPMD.NPathComplexity) | ||
*/ | ||
public function processCartItems(Quote $cart, array $items): void | ||
{ | ||
foreach ($items as $item) { | ||
if (empty($item['cart_item_id'])) { | ||
throw new GraphQlInputException(__('Required parameter "cart_item_id" for "cart_items" is missing.')); | ||
} | ||
|
||
$itemId = (int)$item['cart_item_id']; | ||
$customizableOptions = $item['customizable_options'] ?? []; | ||
$cartItem = $cart->getItemById($itemId); | ||
|
||
if ($cartItem && $cartItem->getParentItemId()) { | ||
throw new GraphQlInputException(__('Child items may not be updated.')); | ||
} | ||
|
||
if (count($customizableOptions) === 0 && !isset($item['quantity'])) { | ||
throw new GraphQlInputException(__('Required parameter "quantity" for "cart_items" is missing.')); | ||
} | ||
|
||
$quantity = (float)$item['quantity']; | ||
|
||
if ($quantity <= 0.0) { | ||
$this->cartItemRepository->deleteById((int)$cart->getId(), $itemId); | ||
} else { | ||
$this->updateCartItem->execute($cart, $itemId, $quantity, $customizableOptions); | ||
} | ||
|
||
if (!empty($item['gift_message'])) { | ||
try { | ||
if (!$this->giftMessageHelper->isMessagesAllowed('items', $cartItem)) { | ||
continue; | ||
} | ||
if (!$this->giftMessageHelper->isMessagesAllowed('item', $cartItem)) { | ||
continue; | ||
} | ||
|
||
/** @var MessageInterface $giftItemMessage */ | ||
$giftItemMessage = $this->itemRepository->get($cart->getEntityId(), $itemId); | ||
|
||
if (empty($giftItemMessage)) { | ||
continue; | ||
} | ||
} catch (LocalizedException $exception) { | ||
throw new GraphQlInputException(__('Gift Message can not be updated.')); | ||
lenaorobei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
$this->updateGiftMessageForItem($cart, $giftItemMessage, $item, $itemId); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Update Gift Message for Quote item | ||
* | ||
* @param Quote $cart | ||
* @param MessageInterface $giftItemMessage | ||
* @param array $item | ||
* @param int $itemId | ||
* | ||
* @throws GraphQlInputException | ||
*/ | ||
private function updateGiftMessageForItem(Quote $cart, MessageInterface $giftItemMessage, array $item, int $itemId) | ||
{ | ||
try { | ||
$giftItemMessage->setRecipient($item['gift_message']['to']); | ||
$giftItemMessage->setSender($item['gift_message']['from']); | ||
$giftItemMessage->setMessage($item['gift_message']['message']); | ||
$this->itemRepository->save($cart->getEntityId(), $giftItemMessage, $itemId); | ||
} catch (LocalizedException $exception) { | ||
throw new GraphQlInputException(__('Gift Message can not be updated')); | ||
lenaorobei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.