This repository has been archived by the owner on Dec 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Merge Pull Request #163 from magento/graphql-ce:mutations-coupons - Merged commits: 1. ced2301 2. 6abc7e5 3. b6eaa1f 4. 1a077ac 5. 9cb459e 6. ba6dd12 7. 02c2bc0 8. 7f1148f 9. b6365e1 10. 3007b3a 11. 2a0a450 12. 462601c 13. 2f4767d 14. 343e463 15. 46b40b4 16. fc12cb7
- Loading branch information
Showing
7 changed files
with
559 additions
and
16 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
app/code/Magento/QuoteGraphQl/Model/Authorization/IsCartMutationAllowedForCurrentUser.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
app/code/Magento/QuoteGraphQl/Model/Resolver/Coupon/ApplyCouponToCart.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
app/code/Magento/QuoteGraphQl/Model/Resolver/Coupon/RemoveCouponFromCart.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.