Skip to content

Commit

Permalink
M2CE-30469: [GRAPHQL] Made the attribute destination_cart_id for me…
Browse files Browse the repository at this point in the history
…rgeCarts mutation not required.
  • Loading branch information
andrewbess authored and Andrii Beziazychnyi committed Oct 23, 2020
1 parent 39244bf commit 8521faa
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 20 deletions.
81 changes: 65 additions & 16 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/MergeCarts.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@

namespace Magento\QuoteGraphQl\Model\Resolver;

use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\GraphQl\Model\Query\ContextInterface;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\Cart\CustomerCartResolver;
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;

/**
* Merge Carts Resolver
*
* @SuppressWarnings(PHPMD.LongVariable)
*/
class MergeCarts implements ResolverInterface
{
Expand All @@ -31,44 +37,87 @@ class MergeCarts implements ResolverInterface
*/
private $cartRepository;

/**
* @var CustomerCartResolver
*/
private $customerCartResolver;

/**
* @var QuoteIdToMaskedQuoteIdInterface
*/
private $quoteIdToMaskedQuoteId;

/**
* @param GetCartForUser $getCartForUser
* @param CartRepositoryInterface $cartRepository
* @param CustomerCartResolver $customerCartResolver
* @param QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId
*/
public function __construct(
GetCartForUser $getCartForUser,
CartRepositoryInterface $cartRepository
CartRepositoryInterface $cartRepository,
CustomerCartResolver $customerCartResolver,
QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId
) {
$this->getCartForUser = $getCartForUser;
$this->cartRepository = $cartRepository;
$this->customerCartResolver = $customerCartResolver;
$this->quoteIdToMaskedQuoteId = $quoteIdToMaskedQuoteId;
}

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
if (empty($args['source_cart_id'])) {
throw new GraphQlInputException(__('Required parameter "source_cart_id" is missing'));
}

if (empty($args['destination_cart_id'])) {
throw new GraphQlInputException(__('Required parameter "destination_cart_id" is missing'));
throw new GraphQlInputException(__(
'Required parameter "source_cart_id" is missing'
));
}

/** @var ContextInterface $context */
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
throw new GraphQlAuthorizationException(__(
'The current customer isn\'t authorized.'
));
}
$currentUserId = $context->getUserId();

if (empty($args['destination_cart_id'])) {
try {
$cart = $this->customerCartResolver->resolve($currentUserId);
} catch (CouldNotSaveException $exception) {
throw new GraphQlNoSuchEntityException(__(
'Could not create empty cart for customer',
$exception
));
}
$customerMaskedCartId = $this->quoteIdToMaskedQuoteId->execute(
(int) $cart->getId()
);
}

$guestMaskedCartId = $args['source_cart_id'];
$customerMaskedCartId = $args['destination_cart_id'];
$customerMaskedCartId = $customerMaskedCartId ?? $args['destination_cart_id'];

$currentUserId = $context->getUserId();
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
// passing customerId as null enforces source cart should always be a guestcart
$guestCart = $this->getCartForUser->execute($guestMaskedCartId, null, $storeId);
$customerCart = $this->getCartForUser->execute($customerMaskedCartId, $currentUserId, $storeId);
$guestCart = $this->getCartForUser->execute(
$guestMaskedCartId,
null,
$storeId
);
$customerCart = $this->getCartForUser->execute(
$customerMaskedCartId,
$currentUserId,
$storeId
);
$customerCart->merge($guestCart);
$guestCart->setIsActive(false);
$this->cartRepository->save($customerCart);
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Mutation {
setPaymentMethodOnCart(input: SetPaymentMethodOnCartInput): SetPaymentMethodOnCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\SetPaymentMethodOnCart")
setGuestEmailOnCart(input: SetGuestEmailOnCartInput): SetGuestEmailOnCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\SetGuestEmailOnCart")
setPaymentMethodAndPlaceOrder(input: SetPaymentMethodAndPlaceOrderInput): PlaceOrderOutput @deprecated(reason: "Should use setPaymentMethodOnCart and placeOrder mutations in single request.") @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetPaymentAndPlaceOrder")
mergeCarts(source_cart_id: String!, destination_cart_id: String!): Cart! @doc(description:"Merges the source cart into the destination cart") @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\MergeCarts")
mergeCarts(source_cart_id: String!, destination_cart_id: String): Cart! @doc(description:"Merges the source cart into the destination cart") @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\MergeCarts")
placeOrder(input: PlaceOrderInput): PlaceOrderOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\PlaceOrder")
addProductsToCart(cartId: String!, cartItems: [CartItemInput!]!): AddProductsToCartOutput @doc(description:"Add any type of product to the cart") @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AddProductsToCart")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,6 @@ public function testMergeCartsWithEmptySourceCartId()
*/
public function testMergeCartsWithEmptyDestinationCartId()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Required parameter "destination_cart_id" is missing');

$guestQuote = $this->quoteFactory->create();
$this->quoteResource->load(
$guestQuote,
Expand Down

0 comments on commit 8521faa

Please sign in to comment.