Skip to content

Commit bfd4fc1

Browse files
committed
GraphQL-57: Refactor address update and data provider
1 parent 6436bcb commit bfd4fc1

File tree

4 files changed

+45
-107
lines changed

4 files changed

+45
-107
lines changed

app/code/Magento/CustomerGraphQl/Model/Resolver/Address/AddressDataProvider.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
namespace Magento\CustomerGraphQl\Model\Resolver\Address;
99

1010
use Magento\Customer\Api\Data\AddressInterface;
11+
use Magento\Customer\Api\Data\CustomerInterface;
12+
use Magento\Framework\Api\CustomAttributesDataInterface;
1113
use Magento\Customer\Api\AddressRepositoryInterface;
14+
use Magento\Customer\Model\ResourceModel\Customer as CustomerResourceModel;
15+
use Magento\Customer\Model\CustomerFactory;
1216
use Magento\Framework\Webapi\ServiceOutputProcessor;
1317
use Magento\Framework\Serialize\SerializerInterface;
1418

@@ -27,16 +31,32 @@ class AddressDataProvider
2731
*/
2832
private $jsonSerializer;
2933

34+
/**
35+
* @var CustomerResourceModel
36+
*/
37+
private $customerResourceModel;
38+
39+
/**
40+
* @var CustomerFactory
41+
*/
42+
private $customerFactory;
43+
3044
/**
3145
* @param ServiceOutputProcessor $serviceOutputProcessor
3246
* @param SerializerInterface $jsonSerializer
47+
* @param CustomerResourceModel $customerResourceModel
48+
* @param CustomerFactory $customerFactory
3349
*/
3450
public function __construct(
3551
ServiceOutputProcessor $serviceOutputProcessor,
36-
SerializerInterface $jsonSerializer
52+
SerializerInterface $jsonSerializer,
53+
CustomerResourceModel $customerResourceModel,
54+
CustomerFactory $customerFactory
3755
) {
3856
$this->serviceOutputProcessor = $serviceOutputProcessor;
3957
$this->jsonSerializer = $jsonSerializer;
58+
$this->customerResourceModel = $customerResourceModel;
59+
$this->customerFactory = $customerFactory;
4060
}
4161

4262
/**
@@ -52,12 +72,19 @@ public function processCustomerAddress(AddressInterface $addressObject) : array
5272
AddressRepositoryInterface::class,
5373
'getById'
5474
);
55-
if (isset($address['extension_attributes'])) {
56-
$address = array_merge($address, $address['extension_attributes']);
75+
$customerModel = $this->customerFactory->create();
76+
$this->customerResourceModel->load($customerModel, $addressObject->getCustomerId());
77+
$address[CustomerInterface::DEFAULT_BILLING] =
78+
($addressObject->getId() == $customerModel->getDefaultBillingAddress()->getId()) ? true : false;
79+
$address[CustomerInterface::DEFAULT_SHIPPING] =
80+
($addressObject->getId() == $customerModel->getDefaultShippingAddress()->getId()) ? true : false;
81+
82+
if (isset($address[CustomAttributesDataInterface::EXTENSION_ATTRIBUTES_KEY])) {
83+
$address = array_merge($address, $address[CustomAttributesDataInterface::EXTENSION_ATTRIBUTES_KEY]);
5784
}
5885
$customAttributes = [];
59-
if (isset($address['custom_attributes'])) {
60-
foreach ($address['custom_attributes'] as $attribute) {
86+
if (isset($address[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES])) {
87+
foreach ($address[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES] as $attribute) {
6188
$isArray = false;
6289
if (is_array($attribute['value'])) {
6390
$isArray = true;

app/code/Magento/CustomerGraphQl/Model/Resolver/AddressUpdate.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Magento\Authorization\Model\UserContextInterface;
1111
use Magento\Customer\Api\AddressRepositoryInterface;
1212
use Magento\Customer\Api\AddressMetadataManagementInterface;
13-
use Magento\Customer\Api\Data\AddressInterfaceFactory;
1413
use Magento\Customer\Api\Data\AddressInterface;
1514
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1615
use Magento\Framework\GraphQl\Config\Element\Field;
@@ -32,11 +31,6 @@ class AddressUpdate implements ResolverInterface
3231
*/
3332
private $addressRepositoryInterface;
3433

35-
/**
36-
* @var AddressInterfaceFactory
37-
*/
38-
private $addressInterfaceFactory;
39-
4034
/**
4135
* @var AddressDataProvider
4236
*/
@@ -49,18 +43,15 @@ class AddressUpdate implements ResolverInterface
4943

5044
/**
5145
* @param AddressRepositoryInterface $addressRepositoryInterface
52-
* @param AddressInterfaceFactory $addressInterfaceFactory
5346
* @param AddressDataProvider $addressDataProvider
5447
* @param AddressConfigProvider $addressConfigProvider
5548
*/
5649
public function __construct(
5750
AddressRepositoryInterface $addressRepositoryInterface,
58-
AddressInterfaceFactory $addressInterfaceFactory,
5951
AddressDataProvider $addressDataProvider,
6052
AddressConfigProvider $addressConfigProvider
6153
) {
6254
$this->addressRepositoryInterface = $addressRepositoryInterface;
63-
$this->addressInterfaceFactory = $addressInterfaceFactory;
6455
$this->addressDataProvider = $addressDataProvider;
6556
$this->addressConfigProvider = $addressConfigProvider;
6657
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CustomerAddressCreateTest.php

Lines changed: 11 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function testAddCustomerAddressWithValidCredentials()
4545
'prefix' => 'Mr.',
4646
'suffix' => 'Jr.',
4747
'vat_id' => '1',
48-
'default_shipping' => false,
48+
'default_shipping' => true,
4949
'default_billing' => false
5050
];
5151
$defaultShippingText = $newAddress['default_shipping'] ? "true": "false";
@@ -172,56 +172,20 @@ public function testAddCustomerAddressWithoutCredentials()
172172
*/
173173
public function testAddCustomerAddressWithMissingAttributeWithValidCredentials()
174174
{
175-
$newAddress = [
176-
'region' => [
177-
'region' => 'Alaska',
178-
'region_id' => 4,
179-
'region_code' => 'AK'
180-
],
181-
'region_id' => 4,
182-
'country_id' => 'US',
183-
'street' => ['Line 1 Street', 'Line 2'],
184-
'company' => 'Company name',
185-
'telephone' => '123456789',
186-
'fax' => '123123123',
187-
'postcode' => '7777',
188-
'city' => 'City Name',
189-
'firstname' => '', //empty firstname
190-
'lastname' => 'Phillis',
191-
'middlename' => 'A',
192-
'prefix' => 'Mr.',
193-
'suffix' => 'Jr.',
194-
'vat_id' => '1',
195-
'default_shipping' => false,
196-
'default_billing' => false
197-
];
198-
$defaultShippingText = $newAddress['default_shipping'] ? "true": "false";
199-
$defaultBillingText = $newAddress['default_billing'] ? "true": "false";
200175
$mutation
201176
= <<<MUTATION
202177
mutation {
203178
customerAddressCreate(input: {
204-
region: {
205-
region: "{$newAddress['region']['region']}"
206-
region_id: {$newAddress['region']['region_id']}
207-
region_code: "{$newAddress['region']['region_code']}"
208-
}
209-
region_id: {$newAddress['region_id']}
210-
country_id: {$newAddress['country_id']}
211-
street: ["{$newAddress['street'][0]}","{$newAddress['street'][1]}"]
212-
company: "{$newAddress['company']}"
213-
telephone: "{$newAddress['telephone']}"
214-
fax: "{$newAddress['fax']}"
215-
postcode: "{$newAddress['postcode']}"
216-
city: "{$newAddress['city']}"
217-
firstname: "{$newAddress['firstname']}"
218-
lastname: "{$newAddress['lastname']}"
219-
middlename: "{$newAddress['middlename']}"
220-
prefix: "{$newAddress['prefix']}"
221-
suffix: "{$newAddress['suffix']}"
222-
vat_id: "{$newAddress['vat_id']}"
223-
default_shipping: {$defaultShippingText}
224-
default_billing: {$defaultBillingText}
179+
region_id: 4
180+
country_id: US
181+
street: ["Line 1 Street","Line 2"]
182+
company: "Company name"
183+
telephone: "123456789"
184+
fax: "123123123"
185+
postcode: "7777"
186+
city: "City Name"
187+
firstname: ""
188+
lastname: "Phillis"
225189
}) {
226190
id
227191
customer_id

dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CustomerAddressUpdateTest.php

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -181,31 +181,6 @@ public function testUpdateCustomerAddressWithMissingAttributeWithValidCredential
181181
{
182182
$userName = 'customer@example.com';
183183
$password = 'password';
184-
$updateAddress = [
185-
'region' => [
186-
'region' => 'Alaska',
187-
'region_id' => 4,
188-
'region_code' => 'AK'
189-
],
190-
'region_id' => 4,
191-
'country_id' => 'US',
192-
'street' => ['Line 1 Street', 'Line 2'],
193-
'company' => 'Company Name',
194-
'telephone' => '123456789',
195-
'fax' => '123123123',
196-
'postcode' => '7777',
197-
'city' => 'City Name',
198-
'firstname' => '', //empty name
199-
'lastname' => 'Phillis',
200-
'middlename' => 'A',
201-
'prefix' => 'Mr.',
202-
'suffix' => 'Jr.',
203-
'vat_id' => '1',
204-
'default_shipping' => true,
205-
'default_billing' => true
206-
];
207-
$defaultShippingText = $updateAddress['default_shipping'] ? "true": "false";
208-
$defaultBillingText = $updateAddress['default_billing'] ? "true": "false";
209184
/** @var CustomerRepositoryInterface $customerRepository */
210185
$customerRepository = ObjectManager::getInstance()->get(CustomerRepositoryInterface::class);
211186
$customer = $customerRepository->get($userName);
@@ -218,27 +193,8 @@ public function testUpdateCustomerAddressWithMissingAttributeWithValidCredential
218193
= <<<MUTATION
219194
mutation {
220195
customerAddressUpdate(id: {$addressId}, input: {
221-
region: {
222-
region: "{$updateAddress['region']['region']}"
223-
region_id: {$updateAddress['region']['region_id']}
224-
region_code: "{$updateAddress['region']['region_code']}"
225-
}
226-
region_id: {$updateAddress['region_id']}
227-
country_id: {$updateAddress['country_id']}
228-
street: ["{$updateAddress['street'][0]}","{$updateAddress['street'][1]}"]
229-
company: "{$updateAddress['company']}"
230-
telephone: "{$updateAddress['telephone']}"
231-
fax: "{$updateAddress['fax']}"
232-
postcode: "{$updateAddress['postcode']}"
233-
city: "{$updateAddress['city']}"
234-
firstname: "{$updateAddress['firstname']}"
235-
lastname: "{$updateAddress['lastname']}"
236-
middlename: "{$updateAddress['middlename']}"
237-
prefix: "{$updateAddress['prefix']}"
238-
suffix: "{$updateAddress['suffix']}"
239-
vat_id: "{$updateAddress['vat_id']}"
240-
default_shipping: {$defaultShippingText}
241-
default_billing: {$defaultBillingText}
196+
firstname: ""
197+
lastname: "Phillis"
242198
}) {
243199
id
244200
customer_id

0 commit comments

Comments
 (0)