Skip to content

Commit

Permalink
Merge pull request #4009 from magento-engcom/graphql-develop-prs
Browse files Browse the repository at this point in the history
[EngCom] Public Pull Requests - GraphQL
  • Loading branch information
naydav authored Apr 5, 2019
2 parents bb46a2f + 29a039d commit 7002ebc
Show file tree
Hide file tree
Showing 42 changed files with 988 additions and 209 deletions.
25 changes: 20 additions & 5 deletions app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Customer\Api\Data\AddressInterface as CustomerAddress;
use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddress;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Quote\Model\Quote\Address as QuoteAddress;
use Magento\Quote\Model\Quote\AddressFactory as BaseQuoteAddressFactory;

Expand All @@ -23,13 +25,21 @@ class QuoteAddressFactory
*/
private $quoteAddressFactory;

/**
* @var GetCustomerAddress
*/
private $getCustomerAddress;

/**
* @param BaseQuoteAddressFactory $quoteAddressFactory
* @param GetCustomerAddress $getCustomerAddress
*/
public function __construct(
BaseQuoteAddressFactory $quoteAddressFactory
BaseQuoteAddressFactory $quoteAddressFactory,
GetCustomerAddress $getCustomerAddress
) {
$this->quoteAddressFactory = $quoteAddressFactory;
$this->getCustomerAddress = $getCustomerAddress;
}

/**
Expand All @@ -48,14 +58,19 @@ public function createBasedOnInputData(array $addressInput): QuoteAddress
}

/**
* Create QuoteAddress based on CustomerAddress
* Create Quote Address based on Customer Address
*
* @param CustomerAddress $customerAddress
* @param int $customerAddressId
* @param int $customerId
* @return QuoteAddress
* @throws GraphQlInputException
* @throws GraphQlAuthorizationException
* @throws GraphQlNoSuchEntityException
*/
public function createBasedOnCustomerAddress(CustomerAddress $customerAddress): QuoteAddress
public function createBasedOnCustomerAddress(int $customerAddressId, int $customerId): QuoteAddress
{
$customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, $customerId);

$quoteAddress = $this->quoteAddressFactory->create();
try {
$quoteAddress->importCustomerAddressData($customerAddress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddress;
use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
Expand All @@ -29,11 +30,6 @@ class SetBillingAddressOnCart
*/
private $getCustomer;

/**
* @var GetCustomerAddress
*/
private $getCustomerAddress;

/**
* @var AssignBillingAddressToCart
*/
Expand All @@ -42,18 +38,15 @@ class SetBillingAddressOnCart
/**
* @param QuoteAddressFactory $quoteAddressFactory
* @param GetCustomer $getCustomer
* @param GetCustomerAddress $getCustomerAddress
* @param AssignBillingAddressToCart $assignBillingAddressToCart
*/
public function __construct(
QuoteAddressFactory $quoteAddressFactory,
GetCustomer $getCustomer,
GetCustomerAddress $getCustomerAddress,
AssignBillingAddressToCart $assignBillingAddressToCart
) {
$this->quoteAddressFactory = $quoteAddressFactory;
$this->getCustomer = $getCustomer;
$this->getCustomerAddress = $getCustomerAddress;
$this->assignBillingAddressToCart = $assignBillingAddressToCart;
}

Expand All @@ -65,6 +58,8 @@ public function __construct(
* @param array $billingAddressInput
* @return void
* @throws GraphQlInputException
* @throws GraphQlAuthenticationException
* @throws GraphQlAuthorizationException
* @throws GraphQlNoSuchEntityException
*/
public function execute(ContextInterface $context, CartInterface $cart, array $billingAddressInput): void
Expand Down Expand Up @@ -97,8 +92,10 @@ public function execute(ContextInterface $context, CartInterface $cart, array $b
$billingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);
} else {
$customer = $this->getCustomer->execute($context);
$customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, (int)$customer->getId());
$billingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress($customerAddress);
$billingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress(
(int)$customerAddressId,
(int)$customer->getId()
);
}

$this->assignBillingAddressToCart->execute($cart, $billingAddress, $useForShipping);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddress;
use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
Expand All @@ -28,11 +27,6 @@ class SetShippingAddressesOnCart implements SetShippingAddressesOnCartInterface
*/
private $getCustomer;

/**
* @var GetCustomerAddress
*/
private $getCustomerAddress;

/**
* @var AssignShippingAddressToCart
*/
Expand All @@ -41,18 +35,15 @@ class SetShippingAddressesOnCart implements SetShippingAddressesOnCartInterface
/**
* @param QuoteAddressFactory $quoteAddressFactory
* @param GetCustomer $getCustomer
* @param GetCustomerAddress $getCustomerAddress
* @param AssignShippingAddressToCart $assignShippingAddressToCart
*/
public function __construct(
QuoteAddressFactory $quoteAddressFactory,
GetCustomer $getCustomer,
GetCustomerAddress $getCustomerAddress,
AssignShippingAddressToCart $assignShippingAddressToCart
) {
$this->quoteAddressFactory = $quoteAddressFactory;
$this->getCustomer = $getCustomer;
$this->getCustomerAddress = $getCustomerAddress;
$this->assignShippingAddressToCart = $assignShippingAddressToCart;
}

Expand Down Expand Up @@ -86,8 +77,10 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s
$shippingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);
} else {
$customer = $this->getCustomer->execute($context);
$customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, (int)$customer->getId());
$shippingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress($customerAddress);
$shippingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress(
(int)$customerAddressId,
(int)$customer->getId()
);
}

$this->assignShippingAddressToCart->execute($cart, $shippingAddress);
Expand Down
19 changes: 17 additions & 2 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/AppliedCoupon.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,27 @@
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Api\CouponManagementInterface;

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

/**
* @param CouponManagementInterface $couponManagement
*/
public function __construct(
CouponManagementInterface $couponManagement
) {
$this->couponManagement = $couponManagement;
}

/**
* @inheritdoc
*/
Expand All @@ -26,9 +41,9 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
throw new LocalizedException(__('"model" value should be specified'));
}
$cart = $value['model'];
$cartId = $cart->getId();

$appliedCoupon = $cart->getCouponCode();

$appliedCoupon = $this->couponManagement->get($cartId);
return $appliedCoupon ? ['code' => $appliedCoupon] : null;
}
}
83 changes: 83 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/PlaceOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Resolver;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Config\Element\Field;
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\CartManagementInterface;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
use Magento\Sales\Api\OrderRepositoryInterface;

/**
* @inheritdoc
*/
class PlaceOrder implements ResolverInterface
{
/**
* @var CartManagementInterface
*/
private $cartManagement;

/**
* @var GetCartForUser
*/
private $getCartForUser;

/**
* @var OrderRepositoryInterface
*/
private $orderRepository;

/**
* @param GetCartForUser $getCartForUser
* @param CartManagementInterface $cartManagement
* @param OrderRepositoryInterface $orderRepository
*/
public function __construct(
GetCartForUser $getCartForUser,
CartManagementInterface $cartManagement,
OrderRepositoryInterface $orderRepository
) {
$this->getCartForUser = $getCartForUser;
$this->cartManagement = $cartManagement;
$this->orderRepository = $orderRepository;
}

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

$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());

try {
$orderId = $this->cartManagement->placeOrder($cart->getId());
$order = $this->orderRepository->get($orderId);

return [
'order' => [
'order_id' => $order->getIncrementId(),
],
];
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__('Unable to place order: %message', ['message' => $e->getMessage()]), $e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
try {
$this->couponManagement->remove($cartId);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
$message = $e->getMessage();
if (preg_match('/The "\d+" Cart doesn\'t contain products/', $message)) {
$message = 'Cart does not contain products';
}
throw new GraphQlNoSuchEntityException(__($message), $e);
} catch (CouldNotDeleteException $e) {
throw new LocalizedException(__($e->getMessage()), $e);
}
Expand Down
3 changes: 2 additions & 1 deletion app/code/Magento/QuoteGraphQl/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"magento/module-catalog": "*",
"magento/module-store": "*",
"magento/module-customer": "*",
"magento/module-customer-graph-ql": "*"
"magento/module-customer-graph-ql": "*",
"magento/module-sales": "*"
},
"suggest": {
"magento/module-graph-ql": "*"
Expand Down
13 changes: 13 additions & 0 deletions app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Mutation {
setBillingAddressOnCart(input: SetBillingAddressOnCartInput): SetBillingAddressOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetBillingAddressOnCart")
setShippingMethodsOnCart(input: SetShippingMethodsOnCartInput): SetShippingMethodsOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetShippingMethodsOnCart")
setPaymentMethodOnCart(input: SetPaymentMethodOnCartInput): SetPaymentMethodOnCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\SetPaymentMethodOnCart")
placeOrder(input: PlaceOrderInput): PlaceOrderOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\PlaceOrder")
}

input AddSimpleProductsToCartInput {
Expand Down Expand Up @@ -114,6 +115,10 @@ input ShippingMethodInput {
method_code: String!
}

input PlaceOrderInput {
cart_id: String!
}

input SetPaymentMethodOnCartInput {
cart_id: String!
payment_method: PaymentMethodInput!
Expand Down Expand Up @@ -144,6 +149,10 @@ type ApplyCouponToCartOutput {
cart: Cart!
}

type PlaceOrderOutput {
order: Order!
}

type Cart {
items: [CartItemInterface] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartItems")
applied_coupon: AppliedCoupon @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\AppliedCoupon")
Expand Down Expand Up @@ -285,3 +294,7 @@ type CartItemSelectedOptionValuePrice {
units: String!
type: PriceTypeEnum!
}

type Order {
order_id: String
}
Loading

0 comments on commit 7002ebc

Please sign in to comment.