From 1a1d591b876d680c650d35fa21ff3aafc768aabe Mon Sep 17 00:00:00 2001 From: Anthony Navarro Date: Thu, 23 Apr 2026 14:57:06 -0700 Subject: [PATCH] Parse state from api call and fix test --- app/Services/StationDetailService.php | 29 ++++++++++++++++++- .../Services/StationDetailServiceTest.php | 2 +- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/app/Services/StationDetailService.php b/app/Services/StationDetailService.php index 7978e97..1df6ef0 100644 --- a/app/Services/StationDetailService.php +++ b/app/Services/StationDetailService.php @@ -31,6 +31,25 @@ */ class StationDetailService { + /** + * FIPS numeric code → 2-letter state/territory abbreviation. + * + * @var array + */ + private const FIPS_STATES = [ + '01' => 'AL', '02' => 'AK', '04' => 'AZ', '05' => 'AR', '06' => 'CA', + '08' => 'CO', '09' => 'CT', '10' => 'DE', '11' => 'DC', '12' => 'FL', + '13' => 'GA', '15' => 'HI', '16' => 'ID', '17' => 'IL', '18' => 'IN', + '19' => 'IA', '20' => 'KS', '21' => 'KY', '22' => 'LA', '23' => 'ME', + '24' => 'MD', '25' => 'MA', '26' => 'MI', '27' => 'MN', '28' => 'MS', + '29' => 'MO', '30' => 'MT', '31' => 'NE', '32' => 'NV', '33' => 'NH', + '34' => 'NJ', '35' => 'NM', '36' => 'NY', '37' => 'NC', '38' => 'ND', + '39' => 'OH', '40' => 'OK', '41' => 'OR', '42' => 'PA', '44' => 'RI', + '45' => 'SC', '46' => 'SD', '47' => 'TN', '48' => 'TX', '49' => 'UT', + '50' => 'VT', '51' => 'VA', '53' => 'WA', '54' => 'WV', '55' => 'WI', + '56' => 'WY', '72' => 'PR', '78' => 'VI', '66' => 'GU', '60' => 'AS', + ]; + /** * @param USGSWaterServices $client Raw USGS NWIS IV API client. */ @@ -128,11 +147,19 @@ private function upsertStation(string $siteNo, array $sourceInfo): UsgsStation { $geoLoc = $sourceInfo['geoLocation']['geogLocation'] ?? []; + $fips = null; + foreach ($sourceInfo['siteProperty'] ?? [] as $prop) { + if (($prop['name'] ?? '') === 'stateCd') { + $fips = (string) ($prop['value'] ?? ''); + break; + } + } + UsgsStation::updateOrCreate( ['site_no' => $siteNo], [ 'name' => (string) ($sourceInfo['siteName'] ?? $siteNo), - 'state' => null, + 'state' => $fips !== null ? (self::FIPS_STATES[$fips] ?? null) : null, 'county' => null, 'huc' => null, 'site_type' => 'ST', diff --git a/tests/Feature/Services/StationDetailServiceTest.php b/tests/Feature/Services/StationDetailServiceTest.php index 146b75b..aa83bca 100644 --- a/tests/Feature/Services/StationDetailServiceTest.php +++ b/tests/Feature/Services/StationDetailServiceTest.php @@ -32,7 +32,7 @@ public function test_load_station_persists_station_and_returns_chart_data(): voi $this->assertDatabaseHas('usgs_stations', [ 'site_no' => '01646500', 'name' => 'Potomac River near Washington DC', - 'state' => null, + 'state' => 'VA', 'huc' => null, ]);