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

Commit

Permalink
Merge pull request #4151 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 May 2, 2019
2 parents 8b3fd9d + 9359cca commit 28757e4
Show file tree
Hide file tree
Showing 31 changed files with 985 additions and 203 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type ConfigurableProductOptionsValues @doc(description: "ConfigurableProductOpti

input AddConfigurableProductsToCartInput {
cart_id: String!
cartItems: [ConfigurableProductCartItemInput!]!
cart_items: [ConfigurableProductCartItemInput!]!
}

type AddConfigurableProductsToCartOutput {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public function resolve(
array $value = null,
array $args = null
) {
if (!isset($args['email'])) {
if (!isset($args['email']) || empty($args['email'])) {
throw new GraphQlInputException(__('Specify the "email" value.'));
}

if (!isset($args['password'])) {
if (!isset($args['password']) || empty($args['password'])) {
throw new GraphQlInputException(__('Specify the "password" value.'));
}

Expand Down
65 changes: 38 additions & 27 deletions app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,13 @@
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\Stdlib\ArrayManager;
use Magento\Quote\Model\Quote;

/**
* Add simple product to cart
*
* TODO: should be replaced for different types resolver
*/
class AddSimpleProductToCart
{
/**
* @var ArrayManager
*/
private $arrayManager;

/**
* @var DataObjectFactory
*/
Expand All @@ -39,16 +31,13 @@ class AddSimpleProductToCart
private $productRepository;

/**
* @param ArrayManager $arrayManager
* @param DataObjectFactory $dataObjectFactory
* @param ProductRepositoryInterface $productRepository
*/
public function __construct(
ArrayManager $arrayManager,
DataObjectFactory $dataObjectFactory,
ProductRepositoryInterface $productRepository
) {
$this->arrayManager = $arrayManager;
$this->dataObjectFactory = $dataObjectFactory;
$this->productRepository = $productRepository;
}
Expand All @@ -67,11 +56,6 @@ public function execute(Quote $cart, array $cartItemData): void
{
$sku = $this->extractSku($cartItemData);
$quantity = $this->extractQuantity($cartItemData);
if ($quantity <= 0) {
throw new GraphQlInputException(
__('Please enter a number greater than 0 in this field.')
);
}
$customizableOptions = $this->extractCustomizableOptions($cartItemData);

try {
Expand Down Expand Up @@ -105,11 +89,10 @@ public function execute(Quote $cart, array $cartItemData): void
*/
private function extractSku(array $cartItemData): string
{
$sku = $this->arrayManager->get('data/sku', $cartItemData);
if (!isset($sku)) {
throw new GraphQlInputException(__('Missing key "sku" in cart item data'));
if (!isset($cartItemData['data']['sku']) || empty($cartItemData['data']['sku'])) {
throw new GraphQlInputException(__('Missed "sku" in cart item data'));
}
return (string)$sku;
return (string)$cartItemData['data']['sku'];
}

/**
Expand All @@ -121,11 +104,17 @@ private function extractSku(array $cartItemData): string
*/
private function extractQuantity(array $cartItemData): float
{
$quantity = $this->arrayManager->get('data/quantity', $cartItemData);
if (!isset($quantity)) {
throw new GraphQlInputException(__('Missing key "quantity" in cart item data'));
if (!isset($cartItemData['data']['quantity'])) {
throw new GraphQlInputException(__('Missed "qty" in cart item data'));
}
$quantity = (float)$cartItemData['data']['quantity'];

if ($quantity <= 0) {
throw new GraphQlInputException(
__('Please enter a number greater than 0 in this field.')
);
}
return (float)$quantity;
return $quantity;
}

/**
Expand All @@ -136,11 +125,17 @@ private function extractQuantity(array $cartItemData): float
*/
private function extractCustomizableOptions(array $cartItemData): array
{
$customizableOptions = $this->arrayManager->get('customizable_options', $cartItemData, []);
if (!isset($cartItemData['customizable_options']) || empty($cartItemData['customizable_options'])) {
return [];
}

$customizableOptionsData = [];
foreach ($customizableOptions as $customizableOption) {
$customizableOptionsData[$customizableOption['id']] = $customizableOption['value'];
foreach ($cartItemData['customizable_options'] as $customizableOption) {
if (isset($customizableOption['value_string'])) {
$customizableOptionsData[$customizableOption['id']] = $this->convertCustomOptionValue(
$customizableOption['value_string']
);
}
}
return $customizableOptionsData;
}
Expand All @@ -161,4 +156,20 @@ private function createBuyRequest(float $quantity, array $customOptions): DataOb
],
]);
}

/**
* Convert custom options vakue
*
* @param string $value
* @return string|array
*/
private function convertCustomOptionValue(string $value)
{
$value = trim($value);
if (substr($value, 0, 1) === "[" &&
substr($value, strlen($value) - 1, 1) === "]") {
return explode(',', substr($value, 1, -1));
}
return $value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
}
$maskedCartId = $args['input']['cart_id'];

if (!isset($args['input']['cartItems']) || empty($args['input']['cartItems'])
|| !is_array($args['input']['cartItems'])
if (!isset($args['input']['cart_items']) || empty($args['input']['cart_items'])
|| !is_array($args['input']['cart_items'])
) {
throw new GraphQlInputException(__('Required parameter "cartItems" is missing'));
throw new GraphQlInputException(__('Required parameter "cart_items" is missing'));
}
$cartItems = $args['input']['cartItems'];
$cartItems = $args['input']['cart_items'];

$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
$this->addProductsToCart->execute($cart, $cartItems);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@

namespace Magento\QuoteGraphQl\Model\Resolver\ShippingAddress;

use Magento\Directory\Model\Currency;
use Magento\Framework\Api\ExtensibleDataObjectConverter;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Api\Data\ShippingMethodInterface;
use Magento\Quote\Model\Cart\ShippingMethodConverter;
use Magento\Store\Model\StoreManagerInterface;

/**
* @inheritdoc
Expand All @@ -30,16 +33,24 @@ class AvailableShippingMethods implements ResolverInterface
*/
private $shippingMethodConverter;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param ExtensibleDataObjectConverter $dataObjectConverter
* @param ShippingMethodConverter $shippingMethodConverter
* @param StoreManagerInterface $storeManager
*/
public function __construct(
ExtensibleDataObjectConverter $dataObjectConverter,
ShippingMethodConverter $shippingMethodConverter
ShippingMethodConverter $shippingMethodConverter,
StoreManagerInterface $storeManager
) {
$this->dataObjectConverter = $dataObjectConverter;
$this->shippingMethodConverter = $shippingMethodConverter;
$this->storeManager = $storeManager;
}

/**
Expand All @@ -65,13 +76,44 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
$shippingRates = $address->getGroupedAllShippingRates();
foreach ($shippingRates as $carrierRates) {
foreach ($carrierRates as $rate) {
$methods[] = $this->dataObjectConverter->toFlatArray(
$methodData = $this->dataObjectConverter->toFlatArray(
$this->shippingMethodConverter->modelToDataObject($rate, $cart->getQuoteCurrencyCode()),
[],
ShippingMethodInterface::class
);
$methods[] = $this->processMoneyTypeData($methodData, $cart->getQuoteCurrencyCode());
}
}
return $methods;
}

/**
* Process money type data
*
* @param array $data
* @param string $quoteCurrencyCode
* @return array
* @throws NoSuchEntityException
*/
private function processMoneyTypeData(array $data, string $quoteCurrencyCode): array
{
if (isset($data['amount'])) {
$data['amount'] = ['value' => $data['amount'], 'currency' => $quoteCurrencyCode];
}

if (isset($data['base_amount'])) {
/** @var Currency $currency */
$currency = $this->storeManager->getStore()->getBaseCurrency();
$data['base_amount'] = ['value' => $data['base_amount'], 'currency' => $currency->getCode()];
}

if (isset($data['price_excl_tax'])) {
$data['price_excl_tax'] = ['value' => $data['price_excl_tax'], 'currency' => $quoteCurrencyCode];
}

if (isset($data['price_incl_tax'])) {
$data['price_incl_tax'] = ['value' => $data['price_incl_tax'], 'currency' => $quoteCurrencyCode];
}
return $data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,34 @@

namespace Magento\QuoteGraphQl\Model\Resolver\ShippingAddress;

use Magento\Directory\Model\Currency;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Model\Quote\Address;
use Magento\Quote\Model\Quote\Address\Rate;
use Magento\Store\Model\StoreManagerInterface;

/**
* @inheritdoc
*/
class SelectedShippingMethod implements ResolverInterface
{
/**
* @var StoreManagerInterface
*/
private $storeManager;

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

/**
* @inheritdoc
*/
Expand All @@ -30,32 +46,44 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
/** @var Address $address */
$address = $value['model'];
$rates = $address->getAllShippingRates();
$carrierTitle = null;
$methodTitle = null;

if (count($rates) > 0) {
list($carrierCode, $methodCode) = explode('_', $address->getShippingMethod(), 2);

/** @var Rate $rate */
$rate = current($rates);
foreach ($rates as $rate) {
if ($rate->getCode() == $address->getShippingMethod()) {
$carrierTitle = $rate->getCarrierTitle();
$methodTitle = $rate->getMethodTitle();
break;
}
}

/** @var Currency $currency */
$currency = $this->storeManager->getStore()->getBaseCurrency();

$data = [
'carrier_code' => $carrierCode,
'method_code' => $methodCode,
'carrier_title' => $rate->getCarrierTitle(),
'method_title' => $rate->getMethodTitle(),
'carrier_title' => $carrierTitle,
'method_title' => $methodTitle,
'amount' => [
'value' => $address->getShippingAmount(),
'currency' => $address->getQuote()->getQuoteCurrencyCode(),
],
'base_amount' => [
'value' => $address->getBaseShippingAmount(),
'currency' => $address->getQuote()->getBaseCurrencyCode(),
'currency' => $currency->getCode(),
],
];
} else {
$data = [
'carrier_code' => null,
'method_code' => null,
'carrier_title' => null,
'method_title' => null,
'carrier_title' => $carrierTitle,
'method_title' => $methodTitle,
'amount' => null,
'base_amount' => null,
];
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 @@ -11,7 +11,8 @@
"magento/module-store": "*",
"magento/module-customer": "*",
"magento/module-customer-graph-ql": "*",
"magento/module-sales": "*"
"magento/module-sales": "*",
"magento/module-directory": "*"
},
"suggest": {
"magento/module-graph-ql": "*",
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/QuoteGraphQl/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<item name="area" xsi:type="string">Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue\Text</item>
<item name="drop_down" xsi:type="string">Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue\Dropdown</item>
<item name="radio" xsi:type="string">Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue\Dropdown</item>
<item name="checkbox" xsi:type="string">Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue\Dropdown</item>
<item name="checkbox" xsi:type="string">Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue\Multiple</item>
<item name="multiple" xsi:type="string">Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue\Multiple</item>
</argument>
</arguments>
Expand Down
Loading

0 comments on commit 28757e4

Please sign in to comment.