Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Commit

Permalink
ENGCOM-3007: Mutations coupons #163
Browse files Browse the repository at this point in the history
  • Loading branch information
Valeriy Naida authored Sep 19, 2018
2 parents 43dca41 + ff84a75 commit dc5b402
Show file tree
Hide file tree
Showing 7 changed files with 559 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Authorization;

use Magento\Authorization\Model\UserContextInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Quote\Api\CartRepositoryInterface;

/**
* Service for checking that the shopping cart operations are allowed for current user
*/
class IsCartMutationAllowedForCurrentUser
{
/**
* @var CartRepositoryInterface
*/
private $cartRepository;

/**
* @var UserContextInterface
*/
private $userContext;

/**
* @param UserContextInterface $userContext
* @param CartRepositoryInterface $cartRepository
*/
public function __construct(
UserContextInterface $userContext,
CartRepositoryInterface $cartRepository
) {
$this->userContext = $userContext;
$this->cartRepository = $cartRepository;
}

/**
* Check that the shopping cart operations are allowed for current user
*
* @param int $quoteId
* @return bool
* @throws GraphQlNoSuchEntityException
*/
public function execute(int $quoteId): bool
{
try {
$quote = $this->cartRepository->get($quoteId);
} catch (NoSuchEntityException $exception) {
throw new GraphQlNoSuchEntityException(__($exception->getMessage()));
}

$customerId = $quote->getCustomerId();

/* Guest cart, allow operations */
if (!$customerId) {
return true;
}

/* If the quote belongs to the current customer allow operations */
return $customerId == $this->userContext->getUserId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Resolver\Coupon;

use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
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\Quote\Api\CouponManagementInterface;
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
use Magento\QuoteGraphQl\Model\Authorization\IsCartMutationAllowedForCurrentUser;

/**
* @inheritdoc
*/
class ApplyCouponToCart implements ResolverInterface
{
/**
* @var CouponManagementInterface
*/
private $couponManagement;

/**
* @var MaskedQuoteIdToQuoteIdInterface
*/
private $maskedQuoteIdToQuoteId;

/**
* @var IsCartMutationAllowedForCurrentUser
*/
private $isCartMutationAllowedForCurrentUser;

/**
* @param CouponManagementInterface $couponManagement
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToId
* @param IsCartMutationAllowedForCurrentUser $isCartMutationAllowedForCurrentUser
*/
public function __construct(
CouponManagementInterface $couponManagement,
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToId,
IsCartMutationAllowedForCurrentUser $isCartMutationAllowedForCurrentUser
) {
$this->couponManagement = $couponManagement;
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToId;
$this->isCartMutationAllowedForCurrentUser = $isCartMutationAllowedForCurrentUser;
}

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

if (!isset($args['input']['coupon_code'])) {
throw new GraphQlInputException(__('Required parameter "coupon_code" is missing'));
}
$couponCode = $args['input']['coupon_code'];

try {
$cartId = $this->maskedQuoteIdToQuoteId->execute($maskedCartId);
} catch (NoSuchEntityException $exception) {
throw new GraphQlNoSuchEntityException(
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $maskedCartId])
);
}

if (false === $this->isCartMutationAllowedForCurrentUser->execute($cartId)) {
throw new GraphQlAuthorizationException(
__(
'The current user cannot perform operations on cart "%masked_cart_id"',
['masked_cart_id' => $maskedCartId]
)
);
}

/* Check current cart does not have coupon code applied */
$appliedCouponCode = $this->couponManagement->get($cartId);
if (!empty($appliedCouponCode)) {
throw new GraphQlInputException(
__('A coupon is already applied to the cart. Please remove it to apply another')
);
}

try {
$this->couponManagement->set($cartId, $couponCode);
} catch (NoSuchEntityException $exception) {
throw new GraphQlNoSuchEntityException(__($exception->getMessage()));
} catch (CouldNotSaveException $exception) {
throw new GraphQlInputException(__($exception->getMessage()));
}

$data['cart']['applied_coupon'] = [
'code' => $couponCode,
];
return $data;
}
}
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\QuoteGraphQl\Model\Resolver\Coupon;

use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\NoSuchEntityException;
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\Quote\Api\CouponManagementInterface;
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
use Magento\QuoteGraphQl\Model\Authorization\IsCartMutationAllowedForCurrentUser;

/**
* @inheritdoc
*/
class RemoveCouponFromCart implements ResolverInterface
{
/**
* @var MaskedQuoteIdToQuoteIdInterface
*/
private $maskedQuoteIdToId;

/**
* @var CouponManagementInterface
*/
private $couponManagement;

/**
* @var IsCartMutationAllowedForCurrentUser
*/
private $isCartMutationAllowedForCurrentUser;

/**
* @param CouponManagementInterface $couponManagement
* @param IsCartMutationAllowedForCurrentUser $isCartMutationAllowedForCurrentUser
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToId
*/
public function __construct(
CouponManagementInterface $couponManagement,
IsCartMutationAllowedForCurrentUser $isCartMutationAllowedForCurrentUser,
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToId
) {
$this->couponManagement = $couponManagement;
$this->isCartMutationAllowedForCurrentUser = $isCartMutationAllowedForCurrentUser;
$this->maskedQuoteIdToId = $maskedQuoteIdToId;
}

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

try {
$cartId = $this->maskedQuoteIdToId->execute($maskedCartId);
} catch (NoSuchEntityException $exception) {
throw new GraphQlNoSuchEntityException(
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $maskedCartId])
);
}

if (false === $this->isCartMutationAllowedForCurrentUser->execute($cartId)) {
throw new GraphQlAuthorizationException(
__(
'The current user cannot perform operations on cart "%masked_cart_id"',
['masked_cart_id' => $maskedCartId]
)
);
}

try {
$this->couponManagement->remove($cartId);
} catch (NoSuchEntityException $exception) {
throw new GraphQlNoSuchEntityException(__($exception->getMessage()));
} catch (CouldNotDeleteException $exception) {
throw new GraphQlInputException(__($exception->getMessage()));
}

$data['cart']['applied_coupon'] = [
'code' => '',
];
return $data;
}
}
31 changes: 31 additions & 0 deletions app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,35 @@

type Mutation {
createEmptyCart: String @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\Cart\\CreateEmptyCart") @doc(description:"Creates empty shopping cart for guest or logged in user")
applyCouponToCart(input: ApplyCouponToCartInput): ApplyCouponToCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\Coupon\\ApplyCouponToCart")
removeCouponFromCart(input: RemoveCouponFromCartInput): RemoveCouponFromCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\Coupon\\RemoveCouponFromCart")
}

input ApplyCouponToCartInput {
cart_id: String!
coupon_code: String!
}

type ApplyCouponToCartOutput {
cart: Cart!
}

type Cart {
applied_coupon: AppliedCoupon
}

type CartAddress {
applied_coupon: AppliedCoupon
}

type AppliedCoupon {
code: String!
}

input RemoveCouponFromCartInput {
cart_id: String!
}

type RemoveCouponFromCartOutput {
cart: Cart
}
Loading

0 comments on commit dc5b402

Please sign in to comment.