feat: resolve outcodes from local DB before HTTP

This commit is contained in:
Ovidiu U
2026-04-22 12:13:52 +01:00
parent 9fa9ea7835
commit 1e3b246172
2 changed files with 42 additions and 1 deletions

View File

@@ -2,6 +2,7 @@
namespace App\Services;
use App\Models\Outcode;
use App\Models\Postcode;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
@@ -35,7 +36,7 @@ class PostcodeService
$result = match (true) {
$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),
};
@@ -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
{
// Insert the single space before the last 3 chars ("SW1A1AA" -> "SW1A 1AA").