|
@@ -6,6 +6,7 @@ use Illuminate\Http\Client\Pool;
|
|
|
use Illuminate\Http\Client\Response;
|
|
use Illuminate\Http\Client\Response;
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
|
+use Illuminate\Support\Facades\Log;
|
|
|
use Throwable;
|
|
use Throwable;
|
|
|
|
|
|
|
|
class ZipCodeCoordinatesService
|
|
class ZipCodeCoordinatesService
|
|
@@ -15,9 +16,22 @@ class ZipCodeCoordinatesService
|
|
|
public function resolve(?float $latitude, ?float $longitude, ?string $zipCode): ?array
|
|
public function resolve(?float $latitude, ?float $longitude, ?string $zipCode): ?array
|
|
|
{
|
|
{
|
|
|
if ($latitude !== null && $longitude !== null) {
|
|
if ($latitude !== null && $longitude !== null) {
|
|
|
|
|
+ Log::debug('ZipCodeCoordinates resolve using address coordinates', [
|
|
|
|
|
+ 'latitude' => $latitude,
|
|
|
|
|
+ 'longitude' => $longitude,
|
|
|
|
|
+ 'zip_code' => $this->normalize($zipCode),
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
return compact('latitude', 'longitude');
|
|
return compact('latitude', 'longitude');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ Log::info('ZipCodeCoordinates fallback to zip code lookup', [
|
|
|
|
|
+ 'zip_code' => $this->normalize($zipCode),
|
|
|
|
|
+ 'has_latitude' => $latitude !== null,
|
|
|
|
|
+ 'has_longitude' => $longitude !== null,
|
|
|
|
|
+ 'reason' => 'missing_address_coordinates',
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
return $this->findByZipCode($zipCode);
|
|
return $this->findByZipCode($zipCode);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -45,17 +59,34 @@ class ZipCodeCoordinatesService
|
|
|
$zipCode = $this->normalize($zipCode);
|
|
$zipCode = $this->normalize($zipCode);
|
|
|
|
|
|
|
|
if ($zipCode === null) {
|
|
if ($zipCode === null) {
|
|
|
|
|
+ Log::info('ZipCodeCoordinates skipped lookup: invalid zip code');
|
|
|
|
|
+
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists($zipCode, $this->resolvedZipCodes)) {
|
|
if (array_key_exists($zipCode, $this->resolvedZipCodes)) {
|
|
|
|
|
+ Log::debug('ZipCodeCoordinates memory cache hit', [
|
|
|
|
|
+ 'zip_code' => $zipCode,
|
|
|
|
|
+ 'found' => $this->resolvedZipCodes[$zipCode] !== null,
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
return $this->resolvedZipCodes[$zipCode];
|
|
return $this->resolvedZipCodes[$zipCode];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if ($this->restoreFromCache($zipCode)) {
|
|
if ($this->restoreFromCache($zipCode)) {
|
|
|
|
|
+ Log::debug('ZipCodeCoordinates persistent cache hit', [
|
|
|
|
|
+ 'zip_code' => $zipCode,
|
|
|
|
|
+ 'found' => $this->resolvedZipCodes[$zipCode] !== null,
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
return $this->resolvedZipCodes[$zipCode];
|
|
return $this->resolvedZipCodes[$zipCode];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ Log::info('ZipCodeCoordinates Brasil API lookup required', [
|
|
|
|
|
+ 'zip_code' => $zipCode,
|
|
|
|
|
+ 'mode' => 'single',
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
$coordinates = $this->requestCoordinates($zipCode);
|
|
$coordinates = $this->requestCoordinates($zipCode);
|
|
|
|
|
|
|
|
$this->remember($zipCode, $coordinates);
|
|
$this->remember($zipCode, $coordinates);
|
|
@@ -82,9 +113,17 @@ class ZipCodeCoordinatesService
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if ($pending === []) {
|
|
if ($pending === []) {
|
|
|
|
|
+ Log::debug('ZipCodeCoordinates preload skipped: no pending zip codes');
|
|
|
|
|
+
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ Log::info('ZipCodeCoordinates Brasil API lookup required', [
|
|
|
|
|
+ 'mode' => 'preload',
|
|
|
|
|
+ 'pending_count' => count($pending),
|
|
|
|
|
+ 'zip_codes_sample' => array_slice(array_values($pending), 0, 20),
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
try {
|
|
try {
|
|
|
$baseUrl = rtrim((string) config('services.brasil_api.base_url'), '/');
|
|
$baseUrl = rtrim((string) config('services.brasil_api.base_url'), '/');
|
|
|
|
|
|
|
@@ -97,7 +136,12 @@ class ZipCodeCoordinatesService
|
|
|
->get("{$baseUrl}/api/cep/v2/{$zipCode}"),
|
|
->get("{$baseUrl}/api/cep/v2/{$zipCode}"),
|
|
|
$pending,
|
|
$pending,
|
|
|
));
|
|
));
|
|
|
- } catch (Throwable) {
|
|
|
|
|
|
|
+ } catch (Throwable $exception) {
|
|
|
|
|
+ Log::warning('ZipCodeCoordinates Brasil API preload failed', [
|
|
|
|
|
+ 'pending_count' => count($pending),
|
|
|
|
|
+ 'error' => $exception->getMessage(),
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
$responses = [];
|
|
$responses = [];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -108,6 +152,14 @@ class ZipCodeCoordinatesService
|
|
|
? $this->coordinatesFromResponse($response)
|
|
? $this->coordinatesFromResponse($response)
|
|
|
: null;
|
|
: null;
|
|
|
|
|
|
|
|
|
|
+ Log::info('ZipCodeCoordinates Brasil API preload response', [
|
|
|
|
|
+ 'zip_code' => $zipCode,
|
|
|
|
|
+ 'has_response' => $response instanceof Response,
|
|
|
|
|
+ 'status' => $response instanceof Response ? $response->status() : null,
|
|
|
|
|
+ 'found' => $coordinates !== null,
|
|
|
|
|
+ 'coordinates' => $coordinates,
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
$this->remember($zipCode, $coordinates);
|
|
$this->remember($zipCode, $coordinates);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -155,14 +207,33 @@ class ZipCodeCoordinatesService
|
|
|
private function requestCoordinates(string $zipCode): ?array
|
|
private function requestCoordinates(string $zipCode): ?array
|
|
|
{
|
|
{
|
|
|
try {
|
|
try {
|
|
|
|
|
+ Log::info('ZipCodeCoordinates calling Brasil API', [
|
|
|
|
|
+ 'zip_code' => $zipCode,
|
|
|
|
|
+ 'base_url' => rtrim((string) config('services.brasil_api.base_url'), '/'),
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
$response = Http::baseUrl(rtrim((string) config('services.brasil_api.base_url'), '/'))
|
|
$response = Http::baseUrl(rtrim((string) config('services.brasil_api.base_url'), '/'))
|
|
|
->acceptJson()
|
|
->acceptJson()
|
|
|
->connectTimeout((int) config('services.brasil_api.connect_timeout', 2))
|
|
->connectTimeout((int) config('services.brasil_api.connect_timeout', 2))
|
|
|
->timeout((int) config('services.brasil_api.timeout', 3))
|
|
->timeout((int) config('services.brasil_api.timeout', 3))
|
|
|
->get("api/cep/v2/{$zipCode}");
|
|
->get("api/cep/v2/{$zipCode}");
|
|
|
|
|
|
|
|
- return $this->coordinatesFromResponse($response);
|
|
|
|
|
- } catch (Throwable) {
|
|
|
|
|
|
|
+ $coordinates = $this->coordinatesFromResponse($response);
|
|
|
|
|
+
|
|
|
|
|
+ Log::info('ZipCodeCoordinates Brasil API response', [
|
|
|
|
|
+ 'zip_code' => $zipCode,
|
|
|
|
|
+ 'status' => $response->status(),
|
|
|
|
|
+ 'found' => $coordinates !== null,
|
|
|
|
|
+ 'coordinates' => $coordinates,
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ return $coordinates;
|
|
|
|
|
+ } catch (Throwable $exception) {
|
|
|
|
|
+ Log::warning('ZipCodeCoordinates Brasil API request failed', [
|
|
|
|
|
+ 'zip_code' => $zipCode,
|
|
|
|
|
+ 'error' => $exception->getMessage(),
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|