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 all 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
Expand Up @@ -66,7 +66,7 @@ public function resolve(
array $args = null
) {
if (!isset($value['model'])) {
throw new GraphQlInputException(__('"model" value should be specified'));
throw new GraphQlInputException(__('"model" value must be specified'));
}

$cart = $value['model'];
Expand Down
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 must 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() ?? ''
];
}
}
2 changes: 1 addition & 1 deletion app/code/Magento/GiftMessageGraphQl/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# GiftMessageGraphQl

**GiftMessageGraphQl** provides information about gift messages for cart, cart items, order and order items.
**GiftMessageGraphQl** provides information about gift messages for carts, cart items, orders and order items.
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>
39 changes: 33 additions & 6 deletions app/code/Magento/GiftMessageGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
# Copyright © Magento, Inc. All rights reserved.
# See COPYING.txt for license details.

type StoreConfig {
allow_order : String @doc(description: "The value of the Allow Gift Messages on Order Level option")
allow_items : String @doc(description: "The value of the Allow Gift Messages for Order Items option")
}

type Cart {
gift_message: GiftMessage @resolver (class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\Cart\\GiftMessage") @doc(description: "The entered gift message for the cart")
}

type SalesItemInterface {
gift_message: GiftMessage @doc(description: "The entered gift message for the order item")
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 CustomerOrder {
gift_message: GiftMessage @resolver (class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\Order\\GiftMessage") @doc(description: "The entered gift message for the order")
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 @doc(description: "Contains the text of a gift message, its sender, and recipient") {
to: String! @doc(description: "Recipient name")
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")
}

type GiftMessage {
to: String! @doc(description: "Recepient name")
input GiftMessageInput @doc(description: "Contains the text of a gift message, its sender, and recipient") {
to: String! @doc(description: "Recipient name")
from: String! @doc(description: "Sender name")
message: String! @doc(description: "Gift message text")
}

type SalesItemInterface {
gift_message: GiftMessage @doc(description: "The entered gift message for the order item")
}

type CustomerOrder {
gift_message: GiftMessage @resolver (class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\Order\\GiftMessage") @doc(description: "The entered gift message for the order")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?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\Data\MessageInterfaceFactory;
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;

/**
* @var MessageInterfaceFactory
*/
private $giftMessageFactory;

/**
* @param CartItemRepositoryInterface $cartItemRepository
* @param UpdateCartItem $updateCartItem
* @param ItemRepositoryInterface $itemRepository
* @param GiftMessageHelper $giftMessageHelper
* @param MessageInterfaceFactory $giftMessageFactory
*/
public function __construct(
CartItemRepositoryInterface $cartItemRepository,
UpdateCartItem $updateCartItem,
ItemRepositoryInterface $itemRepository,
GiftMessageHelper $giftMessageHelper,
MessageInterfaceFactory $giftMessageFactory
) {
$this->cartItemRepository = $cartItemRepository;
$this->updateCartItem = $updateCartItem;
$this->itemRepository = $itemRepository;
$this->giftMessageHelper = $giftMessageHelper;
$this->giftMessageFactory = $giftMessageFactory;
}

/**
* 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)) {
/** @var MessageInterface $giftMessage */
$giftMessage = $this->giftMessageFactory->create();
$this->updateGiftMessageForItem($cart, $giftMessage, $item, $itemId);
continue;
}
} catch (LocalizedException $exception) {
throw new GraphQlInputException(__('Gift Message cannot 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 cannot be updated'));
}
}
}
Loading