From 11d9705fdd1cb1fd97187e6b191fe95831246b51 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Tue, 11 Mar 2025 08:18:29 +0000 Subject: [PATCH] Update address search results (#2981) * Update address search results * Add search controller tests * Check if the query is nil when using the address-search api * Refactor --------- Co-authored-by: Manny Dinssa <44172848+Dinssa@users.noreply.github.com> --- app/controllers/address_search_controller.rb | 6 ++- .../address_search_controller_spec.rb | 40 +++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/app/controllers/address_search_controller.rb b/app/controllers/address_search_controller.rb index 616d5b702..74cda65c2 100644 --- a/app/controllers/address_search_controller.rb +++ b/app/controllers/address_search_controller.rb @@ -5,7 +5,9 @@ class AddressSearchController < ApplicationController def index query = params[:query] - if query.match?(/\A\d+\z/) && query.length > 5 + if query.nil? + render json: { error: "Query cannot be blank." }, status: :bad_request + elsif query.match?(/\A\d+\z/) && query.length > 5 # Query is all numbers and greater than 5 digits, assume it's a UPRN service = UprnClient.new(query) service.call @@ -38,7 +40,7 @@ class AddressSearchController < ApplicationController address_service.call uprn_service.call - results = ([uprn_service.result] || []) + (address_service.result || []) + results = [uprn_service.result, *address_service.result].compact if address_service.error.present? && uprn_service.error.present? render json: { error: "Address and UPRN are not recognised." }, status: :not_found diff --git a/spec/requests/address_search_controller_spec.rb b/spec/requests/address_search_controller_spec.rb index 5c2acd11a..7c8523abe 100644 --- a/spec/requests/address_search_controller_spec.rb +++ b/spec/requests/address_search_controller_spec.rb @@ -144,5 +144,45 @@ RSpec.describe AddressSearchController, type: :request do expect(sales_log.la).to eq(nil) end end + + context "when searching by address and UPRN" do + let(:sales_log) { create(:sales_log, :completed, manual_address_entry_selected: false, assigned_to: user) } + + context "and theres no uprn returned" do + before do + body = { results: [{ DPA: { "ADDRESS": "1, Test Street", "UPRN": "123" } }] }.to_json + uprn_body = { results: [{ DPA: nil }] }.to_json + WebMock.stub_request(:get, "https://api.os.uk/search/places/v1/find?key=OS_DATA_KEY&maxresults=10&minmatch=0.2&query=100") + .to_return(status: 200, body:, headers: {}) + WebMock.stub_request(:get, "https://api.os.uk/search/places/v1/uprn?dataset=DPA,LPI&key=OS_DATA_KEY&uprn=100") + .to_return(status: 200, body: uprn_body, headers: {}) + end + + it "returns the address results" do + get "/address-search?query=100" + + expect(response).to have_http_status(:ok) + expect(response.body).to eq([{ text: "1, Test Street", value: "123" }].to_json) + end + end + + context "and theres no address returned" do + before do + body = { results: [{ DPA: nil }] }.to_json + uprn_body = { results: [{ DPA: { "ADDRESS": "2, Test Street", UPRN: "321" } }] }.to_json + WebMock.stub_request(:get, "https://api.os.uk/search/places/v1/find?key=OS_DATA_KEY&maxresults=10&minmatch=0.2&query=100") + .to_return(status: 200, body:, headers: {}) + WebMock.stub_request(:get, "https://api.os.uk/search/places/v1/uprn?dataset=DPA,LPI&key=OS_DATA_KEY&uprn=100") + .to_return(status: 200, body: uprn_body, headers: {}) + end + + it "returns the address results" do + get "/address-search?query=100" + + expect(response).to have_http_status(:ok) + expect(response.body).to eq([{ text: "2, Test Street", value: "321" }].to_json) + end + end + end end end