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

#1029 Add Postcode as required depending of the country Mutation createCustomerAddress #1031

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Customer\Api\Data\AddressInterface;
use Magento\Customer\Api\Data\AddressInterfaceFactory;
use Magento\Directory\Helper\Data as DirectoryData;
use Magento\Framework\Api\DataObjectHelper;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\Api\DataObjectHelper;

/**
* Create customer address
Expand All @@ -38,23 +39,30 @@ class CreateCustomerAddress
* @var DataObjectHelper
*/
private $dataObjectHelper;
/**
* @var DirectoryData
*/
private $directoryData;

/**
* @param GetAllowedAddressAttributes $getAllowedAddressAttributes
* @param AddressInterfaceFactory $addressFactory
* @param AddressRepositoryInterface $addressRepository
* @param DataObjectHelper $dataObjectHelper
* @param DirectoryData $directoryData
*/
public function __construct(
GetAllowedAddressAttributes $getAllowedAddressAttributes,
AddressInterfaceFactory $addressFactory,
AddressRepositoryInterface $addressRepository,
DataObjectHelper $dataObjectHelper
DataObjectHelper $dataObjectHelper,
DirectoryData $directoryData
) {
$this->getAllowedAddressAttributes = $getAllowedAddressAttributes;
$this->addressFactory = $addressFactory;
$this->addressRepository = $addressRepository;
$this->dataObjectHelper = $dataObjectHelper;
$this->directoryData = $directoryData;
}

/**
Expand Down Expand Up @@ -102,6 +110,13 @@ public function validateData(array $addressData): void
$attributes = $this->getAllowedAddressAttributes->execute();
$errorInput = [];

//Add error for empty postcode with country with no optional ZIP
if (!$this->directoryData->isZipCodeOptional($addressData['country_id'])
&& (!isset($addressData['postcode']) || empty($addressData['postcode']))
) {
$errorInput[] = 'postcode';
}

foreach ($attributes as $attributeName => $attributeInfo) {
if ($attributeInfo->getIsRequired()
&& (!isset($addressData[$attributeName]) || empty($addressData[$attributeName]))
Expand Down
4 changes: 2 additions & 2 deletions app/code/Magento/CustomerGraphQl/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
"type": "magento2-module",
"require": {
"php": "~7.1.3||~7.2.0||~7.3.0",
"magento/module-customer": "*",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I deleted because is duplicated

"magento/module-authorization": "*",
"magento/module-customer": "*",
"magento/module-eav": "*",
"magento/module-graph-ql": "*",
"magento/module-newsletter": "*",
"magento/module-integration": "*",
"magento/module-store": "*",
"magento/framework": "*"
"magento/framework": "*",
"magento/module-directory": "*"
},
"license": [
"OSL-3.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,67 @@ public function testCreateCustomerAddressWithRedundantStreetLine()
$this->graphQlMutation($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password));
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer_without_addresses.php
* @magentoConfigFixture default_store general/country/optional_zip_countries UA
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function testCreateCustomerAddressWithOptionalZipCode()
{
$newAddress = [
'country_code' => 'UA',
'street' => ['Line 1 Street', 'Line 2'],
'company' => 'Company name',
'telephone' => '123456789',
'fax' => '123123123',
'city' => 'City Name',
'firstname' => 'Adam',
'lastname' => 'Phillis',
'middlename' => 'A',
'prefix' => 'Mr.',
'suffix' => 'Jr.',
'vat_id' => '1',
'default_shipping' => true,
'default_billing' => false
];

$mutation
= <<<MUTATION
mutation {
createCustomerAddress(input: {
country_code: {$newAddress['country_code']}
street: ["{$newAddress['street'][0]}","{$newAddress['street'][1]}"]
company: "{$newAddress['company']}"
telephone: "{$newAddress['telephone']}"
fax: "{$newAddress['fax']}"
city: "{$newAddress['city']}"
firstname: "{$newAddress['firstname']}"
lastname: "{$newAddress['lastname']}"
middlename: "{$newAddress['middlename']}"
prefix: "{$newAddress['prefix']}"
suffix: "{$newAddress['suffix']}"
vat_id: "{$newAddress['vat_id']}"
default_shipping: true
default_billing: false
}) {
id
}
}
MUTATION;

$userName = 'customer@example.com';
$password = 'password';

$response = $this->graphQlMutation(
$mutation,
[],
'',
$this->getCustomerAuthHeaders($userName, $password)
);
$this->assertNotEmpty($response['createCustomerAddress']['id']);
}

/**
* Create new address with invalid input
*
Expand Down