feat: resolve outcodes from local DB before HTTP
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Services;
|
namespace App\Services;
|
||||||
|
|
||||||
|
use App\Models\Outcode;
|
||||||
use App\Models\Postcode;
|
use App\Models\Postcode;
|
||||||
use Illuminate\Support\Facades\Cache;
|
use Illuminate\Support\Facades\Cache;
|
||||||
use Illuminate\Support\Facades\Http;
|
use Illuminate\Support\Facades\Http;
|
||||||
@@ -35,7 +36,7 @@ class PostcodeService
|
|||||||
|
|
||||||
$result = match (true) {
|
$result = match (true) {
|
||||||
$this->isFullPostcode($query) => $this->lookupLocalPostcode($query) ?? $this->lookupPostcode($query),
|
$this->isFullPostcode($query) => $this->lookupLocalPostcode($query) ?? $this->lookupPostcode($query),
|
||||||
$this->isOutcode($query) => $this->lookupOutcode($query),
|
$this->isOutcode($query) => $this->lookupLocalOutcode($query) ?? $this->lookupOutcode($query),
|
||||||
default => $this->lookupPlace($query),
|
default => $this->lookupPlace($query),
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -74,6 +75,24 @@ class PostcodeService
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function lookupLocalOutcode(string $outcode): ?LocationResult
|
||||||
|
{
|
||||||
|
$normalised = strtoupper(trim($outcode));
|
||||||
|
|
||||||
|
$row = Outcode::find($normalised);
|
||||||
|
|
||||||
|
if ($row === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new LocationResult(
|
||||||
|
query: $outcode,
|
||||||
|
displayName: $normalised,
|
||||||
|
lat: $row->lat,
|
||||||
|
lng: $row->lng,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
private function formatPostcode(string $normalised): string
|
private function formatPostcode(string $normalised): string
|
||||||
{
|
{
|
||||||
// Insert the single space before the last 3 chars ("SW1A1AA" -> "SW1A 1AA").
|
// Insert the single space before the last 3 chars ("SW1A1AA" -> "SW1A 1AA").
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use App\Models\Outcode;
|
||||||
use App\Models\Postcode;
|
use App\Models\Postcode;
|
||||||
use App\Services\ApiLogger;
|
use App\Services\ApiLogger;
|
||||||
use App\Services\LocationResult;
|
use App\Services\LocationResult;
|
||||||
@@ -194,3 +195,24 @@ it('resolves a full postcode from local DB without calling HTTP', function (): v
|
|||||||
|
|
||||||
Http::assertNothingSent();
|
Http::assertNothingSent();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// --- Local DB (outcode) ---
|
||||||
|
|
||||||
|
it('resolves an outcode from local DB without calling HTTP', function (): void {
|
||||||
|
Outcode::create([
|
||||||
|
'outcode' => 'PE7',
|
||||||
|
'lat' => 52.536397,
|
||||||
|
'lng' => -0.210181,
|
||||||
|
]);
|
||||||
|
|
||||||
|
Http::fake();
|
||||||
|
|
||||||
|
$result = $this->service->resolve('PE7');
|
||||||
|
|
||||||
|
expect($result)->toBeInstanceOf(LocationResult::class)
|
||||||
|
->and($result->displayName)->toBe('PE7')
|
||||||
|
->and($result->lat)->toBe(52.536397)
|
||||||
|
->and($result->lng)->toBe(-0.210181);
|
||||||
|
|
||||||
|
Http::assertNothingSent();
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user