The personal finance app for everyone (by everyone)
Ruby
Send the stored living area to RentCast on refresh (#4583)
Nathaniel Eliotaaf0e7f33f8b
diff --git a/app/controllers/properties_controller.rb b/app/controllers/properties_controller.rbindex 0a4b437289..e8c8f173cd 100644--- a/app/controllers/properties_controller.rb+++ b/app/controllers/properties_controller.rb@@ -22,6 +22,9 @@ class PropertiesController < ApplicationControllercurrency: property_params[:currency].presence || Current.family.currency))+ # Details typed into the manual wizard are user-set, so a later AVM link+ # sends the user's area rather than deferring to the provider's record.+ @account.accountable.lock_saved_attributes!@account.auto_share_with_family! if Current.family.share_all_by_default?redirect_to balances_property_path(@account)@@ -29,6 +32,7 @@ class PropertiesController < ApplicationControllerdef updateif @account.update(property_params)+ @account.accountable.lock_saved_attributes!@success_message = "Property details updated successfully."if @account.active?diff --git a/app/jobs/sync_property_valuations_job.rb b/app/jobs/sync_property_valuations_job.rbindex 6aed73c106..e59bc638b8 100644--- a/app/jobs/sync_property_valuations_job.rb+++ b/app/jobs/sync_property_valuations_job.rb@@ -82,6 +82,12 @@ class SyncPropertyValuationsJob < ApplicationJobpostal_code: address.postal_code}lookup_params[:external_id] = property.avm_external_id if provider_class.requires_external_id?+ # Only a user-set area overrides the provider's record; an area copied+ # from the provider at creation is left out so later corrections to the+ # provider's record still take effect.+ if provider_class.accepts_square_footage? && property.user_set_area? && property.area_in_sqft.present?+ lookup_params[:square_footage] = property.area_in_sqft+ endresponse = provider.fetch_property_valuation(**lookup_params)diff --git a/app/models/property.rb b/app/models/property.rbindex 734065d3da..b260c2857a 100644--- a/app/models/property.rb+++ b/app/models/property.rb@@ -34,10 +34,26 @@ class Property < ApplicationRecordendend+ SQFT_PER_SQM = 10.7639+def areaMeasurement.new(area_value, area_unit) if area_value.present?end+ # Living area in whole square feet, the unit US valuation providers take;+ # nil when no usable (positive) area is stored.+ def area_in_sqft+ return nil unless area_value.to_i.positive?++ area_unit.to_s.strip.downcase == "sqm" ? (area_value * SQFT_PER_SQM).round : area_value+ end++ # Whether the user has set the living area, rather than it being the figure+ # an AVM provider returned when the property was created.+ def user_set_area?+ locked?(:area_value) || locked?(:area_unit)+ end+def purchase_pricefirst_valuation_amountenddiff --git a/app/models/provider/property_valuation_concept.rb b/app/models/provider/property_valuation_concept.rbindex 46377a516b..1440d487cc 100644--- a/app/models/provider/property_valuation_concept.rb+++ b/app/models/provider/property_valuation_concept.rb@@ -57,6 +57,13 @@ module Provider::PropertyValuationConceptfalseend+ # Whether lookups take the property's living area (passed as+ # `square_footage:`) and value the property at that size instead of the+ # provider's own record of it.+ def accepts_square_footage?+ false+ end+def required_address_fields%i[line1 locality region postal_code]end@@ -79,8 +86,9 @@ module Provider::PropertyValuationConceptendend- # `external_id:` is only passed to providers that `requires_external_id?`.- def fetch_property_valuation(line1:, locality: nil, region: nil, postal_code: nil, external_id: nil)+ # `external_id:` is only passed to providers that `requires_external_id?`,+ # and `square_footage:` only to providers that `accepts_square_footage?`.+ def fetch_property_valuation(line1:, locality: nil, region: nil, postal_code: nil, external_id: nil, square_footage: nil)raise NotImplementedError, "Subclasses must implement #fetch_property_valuation"enddiff --git a/app/models/provider/rentcast.rb b/app/models/provider/rentcast.rbindex 16059d1fa9..4f3a93715b 100644--- a/app/models/provider/rentcast.rb+++ b/app/models/provider/rentcast.rb@@ -28,10 +28,16 @@ class Provider::Rentcast < Provider@api_key = api_key # pipelock:ignoreend+ def self.accepts_square_footage?+ true+ end+# Fetches the value estimate and the subject property's attributes in a# single request — `lookupSubjectAttributes` enriches the AVM response with# the property record, so a separate /v1/properties call isn't needed.- def fetch_property_valuation(line1:, locality: nil, region: nil, postal_code: nil)+ # A given `square_footage` overrides the record's living area, which can be+ # wrong (e.g. when it counts a converted garage).+ def fetch_property_valuation(line1:, locality: nil, region: nil, postal_code: nil, square_footage: nil)with_provider_response dothrottle_requestrecord_monthly_request!@@ -39,6 +45,7 @@ class Provider::Rentcast < Providerresponse = client.get("#{base_url}/v1/avm/value") do |req|req.params["address"] = [ line1, locality, region, postal_code ].map { |part| part.to_s.strip }.reject(&:empty?).join(", ")req.params["lookupSubjectAttributes"] = true+ req.params["squareFootage"] = square_footage if square_footage.present?endparsed = JSON.parse(response.body)diff --git a/test/controllers/properties_controller_test.rb b/test/controllers/properties_controller_test.rbindex fc16240243..7ac0d0ca2c 100644--- a/test/controllers/properties_controller_test.rb+++ b/test/controllers/properties_controller_test.rb@@ -39,6 +39,7 @@ class PropertiesControllerTest < ActionDispatch::IntegrationTestassert_equal 1990, created_account.accountable.year_builtassert_equal 1200, created_account.accountable.area_valueassert_equal "sqft", created_account.accountable.area_unit+ assert created_account.accountable.user_set_area?, "a manually entered area should count as user-set"assert_redirected_to balances_property_path(created_account)end@@ -73,6 +74,20 @@ class PropertiesControllerTest < ActionDispatch::IntegrationTestendend+ test "editing the living area marks it as user-set, so valuation refreshes send it" do+ property = @account.accountable+ assert_not property.user_set_area?++ patch property_path(@account), params: {+ account: { accountable_attributes: { id: property.id, area_value: 1364, area_unit: "sqft" } }+ }++ property.reload+ assert_equal 1364, property.area_value+ assert property.locked?(:area_value)+ assert property.user_set_area?+ end+# Tab view teststest "shows balances tab" doget balances_property_path(@account)diff --git a/test/jobs/sync_property_valuations_job_test.rb b/test/jobs/sync_property_valuations_job_test.rbindex fa005e69d4..7b153bbb27 100644--- a/test/jobs/sync_property_valuations_job_test.rb+++ b/test/jobs/sync_property_valuations_job_test.rb@@ -37,6 +37,56 @@ class SyncPropertyValuationsJobTest < ActiveSupport::TestCaseassert_equal "RentCast valuation", @account.entries.valuations.find_by(date: Date.current).nameend+ def expect_rentcast_lookup(params_matcher)+ provider = mock+ provider.stubs(:requests_remaining?).returns(true)+ provider.stubs(:valuation_currency).returns("USD")+ provider.expects(:fetch_property_valuation)+ .with(params_matcher)+ .returns(Provider::Response.new(success?: true, data: valuation_data, error: nil))+ Provider::Registry.stubs(:rentcast).returns(provider)+ end++ test "sends a user-set living area to providers that accept it" do+ @property.lock_attr!(:area_value)+ expect_rentcast_lookup(has_entry(:square_footage, 1000))++ SyncPropertyValuationsJob.new.perform+ end++ test "sends a user-set living area stored in square meters as square feet" do+ @property.update!(area_value: 100, area_unit: " SQM ")+ @property.lock_attr!(:area_unit)+ expect_rentcast_lookup(has_entry(:square_footage, 1076))++ SyncPropertyValuationsJob.new.perform+ end++ [ nil, 0, -5 ].each do |area|+ test "omits a user-set living area of #{area.inspect}" do+ @property.update!(area_value: area)+ @property.lock_attr!(:area_value)+ expect_rentcast_lookup(Not(has_key(:square_footage)))++ SyncPropertyValuationsJob.new.perform+ end+ end++ test "does not send the living area to providers that do not accept it" do+ @property.update!(avm_provider: "realie")+ @property.lock_attr!(:area_value)++ provider = mock+ provider.stubs(:requests_remaining?).returns(true)+ provider.stubs(:valuation_currency).returns("USD")+ provider.expects(:fetch_property_valuation)+ .with(Not(has_key(:square_footage)))+ .returns(Provider::Response.new(success?: true, data: valuation_data, error: nil))+ Provider::Registry.stubs(:realie).returns(provider)++ SyncPropertyValuationsJob.new.perform+ end+test "resolves each provider once per run across multiple properties" dosecond_account = @account.family.accounts.create!(name: "Second AVM Property",diff --git a/test/models/provider/rentcast_test.rb b/test/models/provider/rentcast_test.rbindex e0e1fde054..65afed0c52 100644--- a/test/models/provider/rentcast_test.rb+++ b/test/models/provider/rentcast_test.rb@@ -49,6 +49,29 @@ class Provider::RentcastTest < ActiveSupport::TestCaseassert_requested stubend+ test "values the property at a given living area instead of the looked-up one" do+ stub = stub_request(:get, "https://api.rentcast.io/v1/avm/value")+ .with(+ query: {+ "address" => "5500 Grand Lake Dr, San Antonio, TX, 78244",+ "lookupSubjectAttributes" => "true",+ "squareFootage" => "1364"+ }+ )+ .to_return(status: 200, body: avm_response_body)++ response = @provider.fetch_property_valuation(+ line1: "5500 Grand Lake Dr",+ locality: "San Antonio",+ region: "TX",+ postal_code: "78244",+ square_footage: 1364+ )++ assert response.success?+ assert_requested stub+ end+test "returns a friendly error when no property matches the address" dostub_request(:get, "https://api.rentcast.io/v1/avm/value").with(query: hash_including("address" => "1 Nowhere Ln"))