diff --git a/schemas/location.py b/schemas/location.py index 59654528..50fe28dd 100644 --- a/schemas/location.py +++ b/schemas/location.py @@ -101,6 +101,9 @@ class GeoJSONProperties(BaseModel): elevation_unit: str = "ft" vertical_datum: str = "NAVD88" elevation_method: ElevationMethod | None + county: str | None = None + state: str | None = None + quad_name: str | None = None utm_coordinates: GeoJSONUTMCoordinates = Field( default_factory=GeoJSONUTMCoordinates ) @@ -154,6 +157,9 @@ def populate_fields(cls, data: Any) -> Any: data_dict["properties"]["notes"] = data_dict.get("notes") data_dict["properties"]["elevation"] = convert_m_to_ft(elevation_m) data_dict["properties"]["elevation_method"] = data_dict.get("elevation_method") + data_dict["properties"]["county"] = data_dict.get("county") + data_dict["properties"]["state"] = data_dict.get("state") + data_dict["properties"]["quad_name"] = data_dict.get("quad_name") data_dict["properties"]["nma_location_notes"] = data_dict.get( "nma_location_notes" ) diff --git a/schemas/thing.py b/schemas/thing.py index cd3483fd..baa4ec6c 100644 --- a/schemas/thing.py +++ b/schemas/thing.py @@ -28,6 +28,8 @@ WellPumpType, FormationCode, OriginType, + Role, + ContactType, ) from schemas import BaseCreateModel, BaseUpdateModel, BaseResponseModel, PastOrTodayDate from schemas.group import GroupResponse @@ -229,6 +231,13 @@ def remove_records_with_end_date(cls, monitoring_frequencies): return active_frequencies +class WellContactSummaryResponse(BaseResponseModel): + name: str | None = None + organization: str | None = None + role: Role + contact_type: ContactType + + class WellResponse(BaseThingResponse): """ Response schema for well details. @@ -262,6 +271,7 @@ class WellResponse(BaseThingResponse): aquifers: list[dict] = [] water_notes: list[NoteResponse] = [] construction_notes: list[NoteResponse] = [] + contacts: list[WellContactSummaryResponse] = [] permissions: list[PermissionHistoryResponse] formation_completion_code: FormationCode | None nma_formation_zone: str | None diff --git a/services/thing_helper.py b/services/thing_helper.py index 221cb121..14db8706 100644 --- a/services/thing_helper.py +++ b/services/thing_helper.py @@ -28,6 +28,7 @@ from db import ( LocationThingAssociation, Thing, + ThingContactAssociation, Location, WellScreen, WellPurpose, @@ -55,6 +56,9 @@ selectinload(Thing.location_associations).selectinload( LocationThingAssociation.location ), + selectinload(Thing.contact_associations).selectinload( + ThingContactAssociation.contact + ), selectinload(Thing.well_purposes), selectinload(Thing.well_casing_materials), selectinload(Thing.links), diff --git a/tests/conftest.py b/tests/conftest.py index 2705e72a..5818707b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -133,7 +133,10 @@ def location(): loc = Location( point="POINT(-107.949533 33.809665)", elevation=2464.9, + county="Sierra", release_status="draft", + state="NM", + quad_name="Hillsboro Peak", ) session.add(loc) diff --git a/tests/test_thing.py b/tests/test_thing.py index 10f8e05f..2dde25fb 100644 --- a/tests/test_thing.py +++ b/tests/test_thing.py @@ -668,6 +668,45 @@ def test_get_water_well_details_payload_404_not_found(): assert response.json()["detail"] == "Thing with ID 999999 not found." +def test_get_water_well_by_id_includes_location_properties( + water_well_thing, +): + response = client.get(f"/thing/water-well/{water_well_thing.id}") + + assert response.status_code == 200 + data = response.json() + + assert data["current_location"]["properties"]["county"] == "Sierra" + assert data["current_location"]["properties"]["state"] == "NM" + assert data["current_location"]["properties"]["quad_name"] == "Hillsboro Peak" + + +def test_get_water_wells_includes_contact_summary( + water_well_thing, + contact, +): + response = client.get("/thing/water-well") + + assert response.status_code == 200 + data = response.json() + + well = next( + (item for item in data["items"] if item["id"] == water_well_thing.id), None + ) + assert well is not None, f"Well {water_well_thing.id} not found in response" + assert well["contacts"] == [ + { + "id": contact.id, + "created_at": contact.created_at.astimezone(timezone.utc).strftime(DT_FMT), + "release_status": contact.release_status, + "name": contact.name, + "organization": contact.organization, + "contact_type": contact.contact_type, + "role": contact.role, + } + ] + + def test_get_water_well_by_id_404_not_found(water_well_thing): bad_id = 99999 response = client.get(f"/thing/water-well/{bad_id}")