mirror of
https://github.com/beestat/app.git
synced 2025-05-24 02:14:03 -04:00
Fixed address sometimes not being set correctly
This commit is contained in:
parent
6f6514ddaa
commit
3bfb9f9ef5
@ -37,10 +37,11 @@ class address extends cora\crud {
|
|||||||
* @return array The address row.
|
* @return array The address row.
|
||||||
*/
|
*/
|
||||||
public function search($address, $country) {
|
public function search($address, $country) {
|
||||||
|
$skip_lookup = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If any of these fields are missing, set normalized to an empty array
|
* If any of these fields are missing, set normalized to null and skip the
|
||||||
* and skip the SmartyStreets lookup. Also, line_1 must have a number and
|
* SmartyStreets lookup. Also, line_1 must have a number and text.
|
||||||
* text.
|
|
||||||
*/
|
*/
|
||||||
foreach(['line_1', 'locality', 'administrative_area', 'postal_code'] as $key) {
|
foreach(['line_1', 'locality', 'administrative_area', 'postal_code'] as $key) {
|
||||||
if(
|
if(
|
||||||
@ -48,20 +49,23 @@ class address extends cora\crud {
|
|||||||
trim($address[$key]) === '' ||
|
trim($address[$key]) === '' ||
|
||||||
$address[$key] === null
|
$address[$key] === null
|
||||||
) {
|
) {
|
||||||
$normalized = [];
|
$skip_lookup = true;
|
||||||
|
$normalized = null;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Don't even bother sending to Smarty if there's no number in address line 1.
|
||||||
if(
|
if(
|
||||||
isset($address['line_1']) === true &&
|
isset($address['line_1']) === true &&
|
||||||
preg_match('/\d+ [0-9]*[a-z]+/i', trim(preg_replace('/[^a-z0-9 ]/i', ' ', $address['line_1']))) !== 1
|
preg_match('/\d+ [0-9]*[a-z]+/i', trim(preg_replace('/[^a-z0-9 ]/i', ' ', $address['line_1']))) !== 1
|
||||||
) {
|
) {
|
||||||
$normalized = [];
|
$skip_lookup = true;
|
||||||
|
$normalized = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If normalized wasn't overrridden, check with SmartyStreets.
|
// If normalized wasn't overrridden, check with SmartyStreets.
|
||||||
if(isset($normalized) === false) {
|
if($skip_lookup === false) {
|
||||||
$address_string = $address['line_1'] . ', ' . $address['locality'] . ', ' . $address['administrative_area'] . ', ' . $address['postal_code'];
|
$address_string = $address['line_1'] . ', ' . $address['locality'] . ', ' . $address['administrative_area'] . ', ' . $address['postal_code'];
|
||||||
$normalized = $this->api(
|
$normalized = $this->api(
|
||||||
'smarty_streets',
|
'smarty_streets',
|
||||||
@ -85,9 +89,31 @@ class address extends cora\crud {
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
/**
|
||||||
|
* There was an issue at some point that caused addresses to be inserted
|
||||||
|
* with null/[] as the normalized column even though Smarty returned
|
||||||
|
* actual valid data for the address. I *think* this may have been
|
||||||
|
* caused by addresses that were originally created when a Smarty
|
||||||
|
* subscription was down. Once an address exists, it was then never
|
||||||
|
* updated, so while a successful API call was sent to Smarty, the
|
||||||
|
* actual address was never updated.
|
||||||
|
*
|
||||||
|
* This fixes that by updating the address row with the current Smarty
|
||||||
|
* response. Sometimes Smarty cannot find an address and to fix that I
|
||||||
|
* will manually set the normalized column despite it normally being
|
||||||
|
* null. This won't override that because it will never set normalized
|
||||||
|
* to null.
|
||||||
|
*/
|
||||||
|
if ($normalized !== null) {
|
||||||
|
return $this->update([
|
||||||
|
'address_id' => $existing_address['address_id'],
|
||||||
|
'normalized' => $normalized
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
return $existing_address;
|
return $existing_address;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a key from the normalized address to see whether or not it's
|
* Generate a key from the normalized address to see whether or not it's
|
||||||
|
@ -145,7 +145,7 @@ abstract class crud extends api {
|
|||||||
* @param int $id The id of the item to update.
|
* @param int $id The id of the item to update.
|
||||||
* @param array $attributes An array of attributes to set for this item.
|
* @param array $attributes An array of attributes to set for this item.
|
||||||
*
|
*
|
||||||
* @return int The number of affected rows.
|
* @return int The updated row.
|
||||||
*/
|
*/
|
||||||
public function update($attributes) {
|
public function update($attributes) {
|
||||||
// Get the item first to see if it exists. The get call will throw an
|
// Get the item first to see if it exists. The get call will throw an
|
||||||
|
Loading…
x
Reference in New Issue
Block a user