From 09dc09c6052cdf27131086d63695ff3cb14bd962 Mon Sep 17 00:00:00 2001 From: Lyzun Oleksandr Date: Tue, 21 Aug 2018 19:44:43 +0200 Subject: [PATCH] Prototype of GraphQL mutations for customer update --- .../Model/Resolver/CustomerUpdate.php | 150 ++++++++++++++++++ .../CustomerGraphQl/etc/schema.graphqls | 11 ++ 2 files changed, 161 insertions(+) create mode 100644 app/code/Magento/CustomerGraphQl/Model/Resolver/CustomerUpdate.php diff --git a/app/code/Magento/CustomerGraphQl/Model/Resolver/CustomerUpdate.php b/app/code/Magento/CustomerGraphQl/Model/Resolver/CustomerUpdate.php new file mode 100644 index 0000000000000..96fe852f700cf --- /dev/null +++ b/app/code/Magento/CustomerGraphQl/Model/Resolver/CustomerUpdate.php @@ -0,0 +1,150 @@ +customerResolver = $customerResolver; + $this->valueFactory = $valueFactory; + $this->customerRepository = $customerRepository; + $this->subscriberFactory = $subscriberFactory; + $this->storeResolver = $storeResolver; + $this->encryptor = $encryptor; + $this->customerRegistry = $customerRegistry; + } + + /** + * {@inheritdoc} + */ + public function resolve( + Field $field, + $context, + ResolveInfo $info, + array $value = null, + array $args = null + ) : Value { + + /** @var ContextInterface $context */ + if ((!$context->getUserId()) || $context->getUserType() == UserContextInterface::USER_TYPE_GUEST) { + throw new GraphQlAuthorizationException( + __( + 'Current customer does not have access to the resource "%1"', + [\Magento\Customer\Model\Customer::ENTITY] + ) + ); + } + + $customer = $this->customerRepository->getById($context->getUserId()); + + if (isset($args['email']) && $customer->getEmail() !== $args['email']) { + $customerSecure = $this->customerRegistry->retrieveSecureData($context->getUserId()); + $hash = $customerSecure->getPasswordHash(); + if (!$this->encryptor->validateHash($args['password'], $hash)) { + throw new GraphQlAuthorizationException(__('Invalid login or password.')); + } + $customer->setEmail($args['email']); + } + + if (isset($args['firstname'])) { + $customer->setFirstname($args['firstname']); + } + if (isset($args['lastname'])){ + $customer->setLastname($args['lastname']); + } + + $customer->setStoreId($this->storeResolver->getCurrentStoreId()); + $this->customerRepository->save($customer); + + if (isset($args['is_subscribed'])) { + + $checkSubscriber = $this->subscriberFactory->create()->loadByCustomerId($context->getUserId()); + + if ($args['is_subscribed'] === true && !$checkSubscriber->isSubscribed()) { + $this->subscriberFactory->create()->subscribeCustomerById($context->getUserId()); + } elseif ($args['is_subscribed'] === false && $checkSubscriber->isSubscribed()) { + $this->subscriberFactory->create()->unsubscribeCustomerById($context->getUserId()); + } + } + + + $data = $args; + $result = function () use ($data) { + return !empty($data) ? $data : []; + }; + + return $this->valueFactory->create($result); + + + } + +} diff --git a/app/code/Magento/CustomerGraphQl/etc/schema.graphqls b/app/code/Magento/CustomerGraphQl/etc/schema.graphqls index 687826cff001d..f74b4f82f77f8 100644 --- a/app/code/Magento/CustomerGraphQl/etc/schema.graphqls +++ b/app/code/Magento/CustomerGraphQl/etc/schema.graphqls @@ -51,3 +51,14 @@ type CustomerAddressRegion @doc(description: "CustomerAddressRegion defines the region_id: Int @doc(description: "Uniquely identifies the region") } +type Mutation { + customerUpdate (firstname: String, lastname: String, email: String, password: String, is_subscribed: Boolean): customerItem @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\CustomerUpdate") @doc(description:"Update customer personal information") +} + +type customerItem { + firstname: String + lastname: String + email: String + password: String + is_subscribed: Boolean +}