Skip to content

createCustomer validation requirements #28888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CustomerGraphQl\Model\Resolver;

use Magento\CustomerGraphQl\Model\Customer\ExtractCustomerData;
use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
use Magento\CustomerGraphQl\Model\Customer\UpdateCustomerAccount;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\GraphQl\Model\Query\ContextInterface;

/**
* Customer email update, used for GraphQL request processing
*/
class UpdateCustomerEmail implements ResolverInterface
{
/**
* @var GetCustomer
*/
private $getCustomer;

/**
* @var UpdateCustomerAccount
*/
private $updateCustomerAccount;

/**
* @var ExtractCustomerData
*/
private $extractCustomerData;

/**
* @param GetCustomer $getCustomer
* @param UpdateCustomerAccount $updateCustomerAccount
* @param ExtractCustomerData $extractCustomerData
*/
public function __construct(
GetCustomer $getCustomer,
UpdateCustomerAccount $updateCustomerAccount,
ExtractCustomerData $extractCustomerData
) {
$this->getCustomer = $getCustomer;
$this->updateCustomerAccount = $updateCustomerAccount;
$this->extractCustomerData = $extractCustomerData;
}

/**
* Resolve customer email update mutation
*
* @param \Magento\Framework\GraphQl\Config\Element\Field $field
* @param \Magento\Framework\GraphQl\Query\Resolver\ContextInterface $context
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
* @return array|Value
* @throws \Exception
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
/** @var ContextInterface $context */
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}

$customer = $this->getCustomer->execute($context);
$this->updateCustomerAccount->execute(
$customer,
[
'email' => $args['email'] ?? null,
'password' => $args['password'] ?? null
],
$context->getExtensionAttributes()->getStore()
);

$data = $this->extractCustomerData->execute($customer);

return ['customer' => $data];
}
}
33 changes: 32 additions & 1 deletion app/code/Magento/CustomerGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ type Mutation {
generateCustomerToken(email: String!, password: String!): CustomerToken @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\GenerateCustomerToken") @doc(description:"Retrieve the customer token")
changeCustomerPassword(currentPassword: String!, newPassword: String!): Customer @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\ChangePassword") @doc(description:"Changes the password for the logged-in customer")
createCustomer (input: CustomerInput!): CustomerOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\CreateCustomer") @doc(description:"Create customer account")
updateCustomer (input: CustomerInput!): CustomerOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\UpdateCustomer") @doc(description:"Update the customer's personal information")
createCustomerV2 (input: CustomerCreateInput!): CustomerOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\CreateCustomer") @doc(description:"Create customer account")
updateCustomer (input: CustomerInput!): CustomerOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\UpdateCustomer") @doc(description:"Deprecated. Use UpdateCustomerV2 instead.")
updateCustomerV2 (input: CustomerUpdateInput!): CustomerOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\UpdateCustomer") @doc(description:"Update the customer's personal information")
revokeCustomerToken: RevokeCustomerTokenOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\RevokeCustomerToken") @doc(description:"Revoke the customer token")
createCustomerAddress(input: CustomerAddressInput!): CustomerAddress @resolver(class: "Magento\\CustomerGraphQl\\Model\\Resolver\\CreateCustomerAddress") @doc(description: "Create customer address")
updateCustomerAddress(id: Int!, input: CustomerAddressInput): CustomerAddress @resolver(class: "Magento\\CustomerGraphQl\\Model\\Resolver\\UpdateCustomerAddress") @doc(description: "Update customer address")
deleteCustomerAddress(id: Int!): Boolean @resolver(class: "Magento\\CustomerGraphQl\\Model\\Resolver\\DeleteCustomerAddress") @doc(description: "Delete customer address")
requestPasswordResetEmail(email: String!): Boolean @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\RequestPasswordResetEmail") @doc(description: "Request an email with a reset password token for the registered customer identified by the specified email.")
resetPassword(email: String!, resetPasswordToken: String!, newPassword: String!): Boolean @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\ResetPassword") @doc(description: "Reset a customer's password using the reset password token that the customer received in an email after requesting it using requestPasswordResetEmail.")
updateCustomerEmail(email: String!, password: String!): CustomerOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\UpdateCustomerEmail") @doc(description: "")
}

input CustomerAddressInput {
Expand Down Expand Up @@ -78,6 +81,34 @@ input CustomerInput {
is_subscribed: Boolean @doc(description: "Indicates whether the customer is subscribed to the company's newsletter")
}

input CustomerCreateInput {
prefix: String @doc(description: "An honorific, such as Dr., Mr., or Mrs.")
firstname: String! @doc(description: "The customer's first name")
middlename: String @doc(description: "The customer's middle name")
lastname: String! @doc(description: "The customer's family name")
suffix: String @doc(description: "A value such as Sr., Jr., or III")
email: String! @doc(description: "The customer's email address. Required for customer creation")
dob: String @doc(description: "Deprecated: Use `date_of_birth` instead")
date_of_birth: String @doc(description: "The customer's date of birth")
taxvat: String @doc(description: "The customer's Tax/VAT number (for corporate customers)")
gender: Int @doc(description: "The customer's gender (Male - 1, Female - 2)")
password: String @doc(description: "The customer's password")
is_subscribed: Boolean @doc(description: "Indicates whether the customer is subscribed to the company's newsletter")
}

input CustomerUpdateInput {
date_of_birth: String @doc(description: "The customer's date of birth")
dob: String @doc(description: "Deprecated: Use `date_of_birth` instead")
firstname: String @doc(description: "The customer's first name")
gender: Int @doc(description: "The customer's gender (Male - 1, Female - 2)")
is_subscribed: Boolean @doc(description: "Indicates whether the customer is subscribed to the company's newsletter")
lastname: String @doc(description: "The customer's family name")
middlename: String @doc(description: "The customer's middle name")
prefix: String @doc(description: "An honorific, such as Dr., Mr., or Mrs.")
suffix: String @doc(description: "A value such as Sr., Jr., or III")
taxvat: String @doc(description: "The customer's Tax/VAT number (for corporate customers)")
}

type CustomerOutput {
customer: Customer!
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function testCreateCustomerIfInputDataIsEmpty()
mutation {
createCustomer(
input: {

}
) {
customer {
Expand Down Expand Up @@ -339,7 +339,9 @@ public function testCreateCustomerSubscribed()
public function testCreateCustomerIfCustomerWithProvidedEmailAlreadyExists()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('A customer with the same email address already exists in an associated website.');
$this->expectExceptionMessage(
'A customer with the same email address already exists in an associated website.'
);

$existedEmail = '[email protected]';
$password = 'test123#';
Expand Down
Loading