Skip to content

Commit c00c433

Browse files
committed
MC-33948: [GraphQL] updateCustomerAddress returns wrong data
- added combined branch with all code, test changes
1 parent 5843140 commit c00c433

File tree

12 files changed

+1866
-223
lines changed

12 files changed

+1866
-223
lines changed

app/code/Magento/CustomerGraphQl/Model/Customer/Address/CreateCustomerAddress.php

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
use Magento\Customer\Api\Data\AddressInterface;
1212
use Magento\Customer\Api\Data\AddressInterfaceFactory;
1313
use Magento\Directory\Helper\Data as DirectoryData;
14-
use Magento\Framework\Api\DataObjectHelper;
14+
use Magento\Directory\Model\ResourceModel\Region\CollectionFactory as RegionCollectionFactory;
1515
use Magento\Framework\Exception\LocalizedException;
1616
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1717

1818
/**
19-
* Create customer address
19+
* Create customer and validate address
2020
*/
2121
class CreateCustomerAddress
2222
{
@@ -35,34 +35,51 @@ class CreateCustomerAddress
3535
*/
3636
private $addressRepository;
3737

38-
/**
39-
* @var DataObjectHelper
40-
*/
41-
private $dataObjectHelper;
4238
/**
4339
* @var DirectoryData
4440
*/
4541
private $directoryData;
4642

43+
/**
44+
* @var RegionCollectionFactory
45+
*/
46+
private $regionCollectionFactory;
47+
48+
/**
49+
* @var ValidateAddress
50+
*/
51+
private $addressValidator;
52+
53+
/**
54+
* @var PopulateCustomerAddressFromInput
55+
*/
56+
private $populateCustomerAddressFromInput;
57+
4758
/**
4859
* @param GetAllowedAddressAttributes $getAllowedAddressAttributes
4960
* @param AddressInterfaceFactory $addressFactory
5061
* @param AddressRepositoryInterface $addressRepository
51-
* @param DataObjectHelper $dataObjectHelper
5262
* @param DirectoryData $directoryData
63+
* @param RegionCollectionFactory $regionCollectionFactory
64+
* @param ValidateAddress $addressValidator
65+
* @param PopulateCustomerAddressFromInput $populateCustomerAddressFromInput
5366
*/
5467
public function __construct(
5568
GetAllowedAddressAttributes $getAllowedAddressAttributes,
5669
AddressInterfaceFactory $addressFactory,
5770
AddressRepositoryInterface $addressRepository,
58-
DataObjectHelper $dataObjectHelper,
59-
DirectoryData $directoryData
71+
DirectoryData $directoryData,
72+
RegionCollectionFactory $regionCollectionFactory,
73+
ValidateAddress $addressValidator,
74+
PopulateCustomerAddressFromInput $populateCustomerAddressFromInput
6075
) {
6176
$this->getAllowedAddressAttributes = $getAllowedAddressAttributes;
6277
$this->addressFactory = $addressFactory;
6378
$this->addressRepository = $addressRepository;
64-
$this->dataObjectHelper = $dataObjectHelper;
6579
$this->directoryData = $directoryData;
80+
$this->regionCollectionFactory = $regionCollectionFactory;
81+
$this->addressValidator = $addressValidator;
82+
$this->populateCustomerAddressFromInput =$populateCustomerAddressFromInput;
6683
}
6784

6885
/**
@@ -79,15 +96,13 @@ public function execute(int $customerId, array $data): AddressInterface
7996
if (isset($data['country_code'])) {
8097
$data['country_id'] = $data['country_code'];
8198
}
99+
82100
$this->validateData($data);
83101

84102
/** @var AddressInterface $address */
85103
$address = $this->addressFactory->create();
86-
$this->dataObjectHelper->populateWithArray($address, $data, AddressInterface::class);
87-
88-
if (isset($data['region']['region_id'])) {
89-
$address->setRegionId($address->getRegion()->getRegionId());
90-
}
104+
$this->populateCustomerAddressFromInput->execute($address, $data);
105+
$this->addressValidator->execute($address);
91106
$address->setCustomerId($customerId);
92107

93108
try {
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CustomerGraphQl\Model\Customer\Address;
9+
10+
use Magento\Customer\Api\Data\AddressInterface;
11+
use Magento\Customer\Api\Data\AddressInterfaceFactory;
12+
use Magento\Customer\Api\Data\RegionInterface;
13+
use Magento\Customer\Api\Data\RegionInterfaceFactory;
14+
use Magento\Directory\Helper\Data as DirectoryData;
15+
use Magento\Directory\Model\ResourceModel\Region\CollectionFactory as RegionCollectionFactory;
16+
use Magento\Framework\Api\DataObjectHelper;
17+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
18+
19+
/**
20+
* Populate customer address from the input
21+
*/
22+
class PopulateCustomerAddressFromInput
23+
{
24+
/**
25+
* @var AddressInterfaceFactory
26+
*/
27+
private $addressFactory;
28+
29+
/**
30+
* @var RegionInterfaceFactory
31+
*/
32+
private $regionFactory;
33+
34+
/**
35+
* @var DirectoryData
36+
*/
37+
private $directoryData;
38+
39+
/**
40+
* @var RegionCollectionFactory
41+
*/
42+
private $regionCollectionFactory;
43+
44+
/**
45+
* @var DataObjectHelper
46+
*/
47+
private $dataObjectHelper;
48+
49+
/**
50+
* @param AddressInterfaceFactory $addressFactory
51+
* @param RegionInterfaceFactory $regionFactory
52+
* @param DirectoryData $directoryData
53+
* @param RegionCollectionFactory $regionCollectionFactory
54+
* @param DataObjectHelper $dataObjectHelper
55+
*/
56+
public function __construct(
57+
AddressInterfaceFactory $addressFactory,
58+
RegionInterfaceFactory $regionFactory,
59+
DirectoryData $directoryData,
60+
RegionCollectionFactory $regionCollectionFactory,
61+
DataObjectHelper $dataObjectHelper
62+
) {
63+
$this->addressFactory = $addressFactory;
64+
$this->regionFactory = $regionFactory;
65+
$this->directoryData = $directoryData;
66+
$this->regionCollectionFactory = $regionCollectionFactory;
67+
$this->dataObjectHelper = $dataObjectHelper;
68+
}
69+
70+
/**
71+
* Populate the customer address and region data from input
72+
*
73+
* @param AddressInterface $address
74+
* @param array $addressData
75+
* @return AddressInterface
76+
* @throws GraphQlInputException
77+
*/
78+
public function execute(AddressInterface $address, array $addressData): AddressInterface
79+
{
80+
$this->dataObjectHelper->populateWithArray($address, $addressData, AddressInterface::class);
81+
82+
return $this->setRegionData($address, $addressData);
83+
}
84+
85+
/**
86+
* Set the address region data
87+
*
88+
* @param AddressInterface $address
89+
* @param array $addressData
90+
* @return AddressInterface
91+
* @throws GraphQlInputException
92+
*/
93+
private function setRegionData(AddressInterface $address, array $addressData):AddressInterface
94+
{
95+
if (!empty($addressData['region']['region_id'])) {
96+
if (array_key_exists('country_code', $addressData)) {
97+
$regionCollection = $this->regionCollectionFactory
98+
->create()
99+
->addCountryFilter($addressData['country_code']);
100+
if (!empty($addressData['region']['region_code'])) {
101+
$regionCollection->addRegionCodeFilter($addressData['region']['region_code']);
102+
}
103+
104+
$regionResult = $regionCollection->getItemById($addressData['region']['region_id']);
105+
/** @var RegionInterface $region */
106+
$region = $this->regionFactory->create();
107+
if ($regionResult != null) {
108+
$region->setRegionId($regionResult->getRegionId());
109+
$region->setRegionCode($regionResult->getCode());
110+
$region->setRegion($regionResult->getDefaultName());
111+
$address->setRegion($region);
112+
} else {
113+
throw new GraphQlInputException(
114+
__('The region_id does not match the selected country or region')
115+
);
116+
}
117+
}
118+
}
119+
return $address;
120+
}
121+
}

app/code/Magento/CustomerGraphQl/Model/Customer/Address/UpdateCustomerAddress.php

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99

1010
use Magento\Customer\Api\AddressRepositoryInterface;
1111
use Magento\Customer\Api\Data\AddressInterface;
12+
use Magento\Directory\Helper\Data as DirectoryData;
13+
use Magento\Directory\Model\ResourceModel\Region\CollectionFactory as RegionCollectionFactory;
14+
use Magento\Framework\Api\DataObjectHelper;
1215
use Magento\Framework\Exception\LocalizedException;
1316
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14-
use Magento\Framework\Api\DataObjectHelper;
1517

1618
/**
17-
* Update customer address
19+
* Update customer address on the existing customer address
1820
*/
1921
class UpdateCustomerAddress
2022
{
@@ -38,21 +40,53 @@ class UpdateCustomerAddress
3840
*/
3941
private $restrictedKeys;
4042

43+
/**
44+
* @var DirectoryData
45+
*/
46+
private $directoryData;
47+
48+
/**
49+
* @var RegionCollectionFactory
50+
*/
51+
private $regionCollectionFactory;
52+
53+
/**
54+
* @var ValidateAddress
55+
*/
56+
private $addressValidator;
57+
58+
/**
59+
* @var PopulateCustomerAddressFromInput
60+
*/
61+
private $populateCustomerAddressFromInput;
62+
4163
/**
4264
* @param GetAllowedAddressAttributes $getAllowedAddressAttributes
4365
* @param AddressRepositoryInterface $addressRepository
4466
* @param DataObjectHelper $dataObjectHelper
67+
* @param DirectoryData $directoryData
68+
* @param RegionCollectionFactory $regionCollectionFactory
69+
* @param ValidateAddress $addressValidator
70+
* @param PopulateCustomerAddressFromInput $populateCustomerAddressFromInput
4571
* @param array $restrictedKeys
4672
*/
4773
public function __construct(
4874
GetAllowedAddressAttributes $getAllowedAddressAttributes,
4975
AddressRepositoryInterface $addressRepository,
5076
DataObjectHelper $dataObjectHelper,
77+
DirectoryData $directoryData,
78+
RegionCollectionFactory $regionCollectionFactory,
79+
ValidateAddress $addressValidator,
80+
PopulateCustomerAddressFromInput $populateCustomerAddressFromInput,
5181
array $restrictedKeys = []
5282
) {
5383
$this->getAllowedAddressAttributes = $getAllowedAddressAttributes;
5484
$this->addressRepository = $addressRepository;
5585
$this->dataObjectHelper = $dataObjectHelper;
86+
$this->directoryData = $directoryData;
87+
$this->regionCollectionFactory = $regionCollectionFactory;
88+
$this->addressValidator = $addressValidator;
89+
$this->populateCustomerAddressFromInput = $populateCustomerAddressFromInput;
5690
$this->restrictedKeys = $restrictedKeys;
5791
}
5892

@@ -74,10 +108,15 @@ public function execute(AddressInterface $address, array $data): void
74108
$filteredData = array_diff_key($data, array_flip($this->restrictedKeys));
75109
$this->dataObjectHelper->populateWithArray($address, $filteredData, AddressInterface::class);
76110

77-
if (isset($data['region']['region_id'])) {
111+
if (!empty($data['region']['region_id'])) {
78112
$address->setRegionId($address->getRegion()->getRegionId());
113+
} else {
114+
$data['region']['region_id'] = null;
79115
}
80116

117+
$this->populateCustomerAddressFromInput->execute($address, $filteredData);
118+
$this->addressValidator->execute($address);
119+
81120
try {
82121
$this->addressRepository->save($address);
83122
} catch (LocalizedException $e) {

0 commit comments

Comments
 (0)