Skip to content

Commit

Permalink
Merge branch 'graphql-update-message-in-quote' into graphql-256-cart-…
Browse files Browse the repository at this point in the history
…item-coverage
  • Loading branch information
Usik2203 committed May 22, 2020
2 parents 81737c0 + 035c157 commit 8ed5018
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 58 deletions.
18 changes: 18 additions & 0 deletions app/code/Magento/GiftMessageGraphQl/etc/graphql/di.xml
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>
15 changes: 15 additions & 0 deletions app/code/Magento/GiftMessageGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# 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")
allow_items : String @doc(description: "Allow Gift Messages for order items")
}

type Cart {
gift_message: GiftMessage @resolver (class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\Cart\\GiftMessage") @doc(description: "The entered gift message for the cart")
}
Expand All @@ -26,3 +31,13 @@ type GiftMessage {
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 {
to: String! @doc(description: "Recepient name")
from: String! @doc(description: "Sender name")
message: String! @doc(description: "Gift message text")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?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
*/
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.'));
}

$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'));
}
}
}
70 changes: 13 additions & 57 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,53 +14,43 @@
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Api\CartItemRepositoryInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\Quote;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
use Magento\QuoteGraphQl\Model\Cart\UpdateCartItem;
use Magento\QuoteGraphQl\Model\CartItem\DataProvider\UpdateCartItems as UpdateCartItemsProvider;

/**
* @inheritdoc
*/
class UpdateCartItems implements ResolverInterface
{
/**
* @var UpdateCartItem
*/
private $updateCartItem;

/**
* @var GetCartForUser
*/
private $getCartForUser;

/**
* @var CartItemRepositoryInterface
* @var CartRepositoryInterface
*/
private $cartItemRepository;
private $cartRepository;

/**
* @var CartRepositoryInterface
* @var UpdateCartItemsProvider
*/
private $cartRepository;
private $updateCartItems;

/**
* @param GetCartForUser $getCartForUser
* @param CartItemRepositoryInterface $cartItemRepository
* @param UpdateCartItem $updateCartItem
* @param GetCartForUser $getCartForUser
* @param CartRepositoryInterface $cartRepository
* @param UpdateCartItemsProvider $updateCartItems
*/
public function __construct(
GetCartForUser $getCartForUser,
CartItemRepositoryInterface $cartItemRepository,
UpdateCartItem $updateCartItem,
CartRepositoryInterface $cartRepository
CartRepositoryInterface $cartRepository,
UpdateCartItemsProvider $updateCartItems
) {
$this->getCartForUser = $getCartForUser;
$this->cartItemRepository = $cartItemRepository;
$this->updateCartItem = $updateCartItem;
$this->cartRepository = $cartRepository;
$this->updateCartItems = $updateCartItems;
}

/**
Expand All @@ -71,20 +61,21 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
if (empty($args['input']['cart_id'])) {
throw new GraphQlInputException(__('Required parameter "cart_id" is missing.'));
}

$maskedCartId = $args['input']['cart_id'];

if (empty($args['input']['cart_items'])
|| !is_array($args['input']['cart_items'])
) {
throw new GraphQlInputException(__('Required parameter "cart_items" is missing.'));
}
$cartItems = $args['input']['cart_items'];

$cartItems = $args['input']['cart_items'];
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);

try {
$this->processCartItems($cart, $cartItems);
$this->updateCartItems->processCartItems($cart, $cartItems);
$this->cartRepository->save($cart);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
Expand All @@ -98,39 +89,4 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
],
];
}

/**
* Process cart items
*
* @param Quote $cart
* @param array $items
* @throws GraphQlInputException
* @throws LocalizedException
*/
private 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);
}
}
}
}
3 changes: 2 additions & 1 deletion app/code/Magento/QuoteGraphQl/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"magento/module-customer-graph-ql": "*",
"magento/module-sales": "*",
"magento/module-directory": "*",
"magento/module-graph-ql": "*"
"magento/module-graph-ql": "*",
"magento/module-gift-message": "*"
},
"suggest": {
"magento/module-graph-ql-cache": "*"
Expand Down

0 comments on commit 8ed5018

Please sign in to comment.