diff --git a/app/models/case_log.rb b/app/models/case_log.rb
index 86ea4e7c2..ac956fd20 100644
--- a/app/models/case_log.rb
+++ b/app/models/case_log.rb
@@ -351,6 +351,13 @@ private
self.vmonth = property_void_date.month
self.vyear = property_void_date.year
end
+ if rsnvac.present?
+ self.newprop = if has_first_let_vacancy_reason?
+ 1
+ else
+ 2
+ end
+ end
self.incref = 1 if net_income_refused?
self.hhmemb = other_hhmemb + 1 if other_hhmemb.present?
self.renttype = RENT_TYPE_MAPPING[rent_type]
diff --git a/db/migrate/20220325114252_add_newprop_derived_variable.rb b/db/migrate/20220325114252_add_newprop_derived_variable.rb
new file mode 100644
index 000000000..0e30498a0
--- /dev/null
+++ b/db/migrate/20220325114252_add_newprop_derived_variable.rb
@@ -0,0 +1,7 @@
+class AddNewpropDerivedVariable < ActiveRecord::Migration[7.0]
+ def change
+ change_table :case_logs, bulk: true do |t|
+ t.column :newprop, :integer
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 545cecd7d..30a624de6 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -230,6 +230,7 @@ ActiveRecord::Schema[7.0].define(version: 202202071123100) do
t.string "relat6"
t.string "relat7"
t.string "relat8"
+ t.integer "newprop"
t.index ["managing_organisation_id"], name: "index_case_logs_on_managing_organisation_id"
t.index ["owning_organisation_id"], name: "index_case_logs_on_owning_organisation_id"
end
diff --git a/spec/fixtures/exports/case_logs.xml b/spec/fixtures/exports/case_logs.xml
index 639768427..c6e79746a 100644
--- a/spec/fixtures/exports/case_logs.xml
+++ b/spec/fixtures/exports/case_logs.xml
@@ -172,5 +172,6 @@
+ 2
diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb
index 3b506e02e..b768f12dd 100644
--- a/spec/models/case_log_spec.rb
+++ b/spec/models/case_log_spec.rb
@@ -1577,6 +1577,38 @@ RSpec.describe CaseLog do
end
end
end
+
+ context "when the data provider is filling in the reason for the property being vacant" do
+ let!(:first_let_case_log) do
+ described_class.create({
+ managing_organisation: organisation,
+ owning_organisation: organisation,
+ first_time_property_let_as_social_housing: 1,
+ })
+ end
+
+ let!(:relet_case_log) do
+ described_class.create({
+ managing_organisation: organisation,
+ owning_organisation: organisation,
+ first_time_property_let_as_social_housing: 0,
+ })
+ end
+
+ it "the newprop variable is correctly derived and saved as 1 for a first let vacancy reason" do
+ first_let_case_log.update!({ rsnvac: 15 })
+ record_from_db = ActiveRecord::Base.connection.execute("select newprop from case_logs where id=#{first_let_case_log.id}").to_a[0]
+ expect(record_from_db["newprop"]).to eq(1)
+ expect(first_let_case_log["newprop"]).to eq(1)
+ end
+
+ it "the newprop variable is correctly derived and saved as 2 for anything that is not a first let vacancy reason" do
+ relet_case_log.update!({ rsnvac: 2 })
+ record_from_db = ActiveRecord::Base.connection.execute("select newprop from case_logs where id=#{relet_case_log.id}").to_a[0]
+ expect(record_from_db["newprop"]).to eq(2)
+ expect(relet_case_log["newprop"]).to eq(2)
+ end
+ end
end
describe "resetting invalidated fields" do