Skip to content
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

[GraphQl] Gift Message coverage for cart item #28072

Merged
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'));
lenaorobei marked this conversation as resolved.
Show resolved Hide resolved
}

$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() ?? ''
];
}
}
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>
27 changes: 27 additions & 0 deletions app/code/Magento/GiftMessageGraphQl/etc/schema.graphqls
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")
}
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
}
}
}
Loading