diff --git a/app/models/derived_variables/lettings_log_variables.rb b/app/models/derived_variables/lettings_log_variables.rb index d29f21ce2..960ecad52 100644 --- a/app/models/derived_variables/lettings_log_variables.rb +++ b/app/models/derived_variables/lettings_log_variables.rb @@ -128,6 +128,22 @@ private incref: 1, }, }, + { + conditions: { + net_income_known: 0, + }, + derived_values: { + incref: 0, + }, + }, + { + conditions: { + net_income_known: 1, + }, + derived_values: { + incref: 2, + }, + }, ].freeze def clear_inapplicable_derived_values! diff --git a/lib/tasks/correct_incref_values.rake b/lib/tasks/correct_incref_values.rake new file mode 100644 index 000000000..0b1921998 --- /dev/null +++ b/lib/tasks/correct_incref_values.rake @@ -0,0 +1,6 @@ +desc "Alter incref values for non imported lettings logs in the database" +task correct_incref_values: :environment do + LettingsLog.where(old_id: nil, net_income_known: 0).update!(incref: 0) + LettingsLog.where(old_id: nil, net_income_known: 1).update!(incref: 2) + LettingsLog.where(old_id: nil, net_income_known: 2).update!(incref: 1) +end diff --git a/spec/lib/tasks/correct_incref_values_spec.rb b/spec/lib/tasks/correct_incref_values_spec.rb new file mode 100644 index 000000000..fc046dcf6 --- /dev/null +++ b/spec/lib/tasks/correct_incref_values_spec.rb @@ -0,0 +1,36 @@ +require "rails_helper" +require "rake" + +RSpec.describe "correct_incref_values" do + describe ":correct_incref_values", type: :task do + subject(:task) { Rake::Task["correct_incref_values"] } + + before do + Rake.application.rake_require("tasks/correct_incref_values") + Rake::Task.define_task(:environment) + task.reenable + end + + context "when the rake task is run" do + let!(:lettings_log) { create(:lettings_log, :completed) } + + it "updates lettings logs with net_income_known 0 (yes) to have incref 0 (no)" do + lettings_log.update!(net_income_known: 0, incref: nil) + task.invoke + expect(lettings_log.reload.incref).to eq(0) + end + + it "updates lettings logs with net_income_known 1 (no) to have incref 2 (don't know)" do + lettings_log.update!(net_income_known: 1, incref: nil) + task.invoke + expect(lettings_log.reload.incref).to eq(2) + end + + it "updates lettings logs with net_income_known 2 (prefers not to say) to have incref 1 (yes)" do + lettings_log.update!(net_income_known: 2, incref: nil) + task.invoke + expect(lettings_log.reload.incref).to eq(1) + end + end + end +end diff --git a/spec/models/lettings_log_spec.rb b/spec/models/lettings_log_spec.rb index b34377543..1ae394c3d 100644 --- a/spec/models/lettings_log_spec.rb +++ b/spec/models/lettings_log_spec.rb @@ -248,8 +248,13 @@ RSpec.describe LettingsLog do end it "correctly derives and saves incref" do - record_from_db = described_class.find(lettings_log.id) - expect(record_from_db["incref"]).to eq(1) + expect(lettings_log.reload.incref).to eq(1) + + lettings_log.update!(net_income_known: 1) + expect(lettings_log.reload.incref).to eq(2) + + lettings_log.update!(net_income_known: 0) + expect(lettings_log.reload.incref).to eq(0) end it "correctly derives and saves renttype" do