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

Commit

Permalink
GraphQL-388: updateCustomer mutation doesn't update undefined fields
Browse files Browse the repository at this point in the history
  • Loading branch information
naydav committed Mar 7, 2019
1 parent 234f3ad commit 6690a49
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@

use Magento\Customer\Model\AuthenticationInterface;
use Magento\Framework\Exception\InvalidEmailOrPasswordException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\State\UserLockedException;
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;

/**
* Check customer password
Expand All @@ -36,15 +41,21 @@ public function __construct(
* @param string $password
* @param int $customerId
* @throws GraphQlAuthenticationException
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
*/
public function execute(string $password, int $customerId)
{
try {
$this->authentication->authenticate($customerId, $password);
} catch (InvalidEmailOrPasswordException $e) {
throw new GraphQlAuthenticationException(
__('The password doesn\'t match this account. Verify the password and try again.')
);
throw new GraphQlAuthenticationException(__($e->getMessage()), $e);
} catch (UserLockedException $e) {
throw new GraphQlAuthenticationException(__($e->getMessage()), $e);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()), $e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@

use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlAlreadyExistsException;
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
use Magento\Framework\Api\DataObjectHelper;
use Magento\Framework\Reflection\DataObjectProcessor;

/**
* Update customer data
Expand All @@ -38,21 +39,11 @@ class UpdateCustomerData
*/
private $checkCustomerPassword;

/**
* @var CustomerInterfaceFactory
*/
private $customerFactory;

/**
* @var DataObjectHelper
*/
private $dataObjectHelper;

/**
* @var DataObjectProcessor
*/
private $dataObjectProcessor;

/**
* @var array
*/
Expand All @@ -62,26 +53,20 @@ class UpdateCustomerData
* @param CustomerRepositoryInterface $customerRepository
* @param StoreManagerInterface $storeManager
* @param CheckCustomerPassword $checkCustomerPassword
* @param CustomerInterfaceFactory $customerFactory
* @param DataObjectHelper $dataObjectHelper
* @param DataObjectProcessor $dataObjectProcessor
* @param array $restrictedKeys
*/
public function __construct(
CustomerRepositoryInterface $customerRepository,
StoreManagerInterface $storeManager,
CheckCustomerPassword $checkCustomerPassword,
CustomerInterfaceFactory $customerFactory,
DataObjectHelper $dataObjectHelper,
DataObjectProcessor $dataObjectProcessor,
array $restrictedKeys = []
) {
$this->customerRepository = $customerRepository;
$this->storeManager = $storeManager;
$this->checkCustomerPassword = $checkCustomerPassword;
$this->customerFactory = $customerFactory;
$this->dataObjectHelper = $dataObjectHelper;
$this->dataObjectProcessor = $dataObjectProcessor;
$this->restrictedKeys = $restrictedKeys;
}

Expand All @@ -91,24 +76,23 @@ public function __construct(
* @param int $customerId
* @param array $data
* @return void
* @throws GraphQlNoSuchEntityException
* @throws GraphQlInputException
* @throws GraphQlAlreadyExistsException
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
* @throws GraphQlAuthenticationException
*/
public function execute(int $customerId, array $data): void
{
$customer = $this->customerRepository->getById($customerId);
$newData = array_diff_key($data, array_flip($this->restrictedKeys));

$oldData = $this->dataObjectProcessor->buildOutputDataArray($customer, CustomerInterface::class);
$newData = array_merge($oldData, $newData);
try {
$customer = $this->customerRepository->getById($customerId);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()), $e);
}

$customer = $this->customerFactory->create();
$this->dataObjectHelper->populateWithArray(
$customer,
$newData,
CustomerInterface::class
);
$filteredData = array_diff_key($data, array_flip($this->restrictedKeys));
$this->dataObjectHelper->populateWithArray($customer, $filteredData, CustomerInterface::class);

if (isset($data['email']) && $customer->getEmail() !== $data['email']) {
if (!isset($data['password']) || empty($data['password'])) {
Expand All @@ -128,6 +112,8 @@ public function execute(int $customerId, array $data): void
__('A customer with the same email address already exists in an associated website.'),
$e
);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()), $e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function testUpdateCustomer()
taxvat: "{$newTaxVat}"
email: "{$newEmail}"
password: "{$currentPassword}"
gender: "{$newGender}"
}
) {
customer {
Expand All @@ -96,6 +97,7 @@ public function testUpdateCustomer()
$this->assertEquals($newDobDate->format('Y-m-d'), $response['updateCustomer']['customer']['dob']);
$this->assertEquals($newTaxVat, $response['updateCustomer']['customer']['taxvat']);
$this->assertEquals($newEmail, $response['updateCustomer']['customer']['email']);
$this->assertEquals($newGender, $response['updateCustomer']['customer']['gender']);
}

/**
Expand Down

0 comments on commit 6690a49

Please sign in to comment.