diff --git a/app/constants/db_enums.rb b/app/constants/db_enums.rb index 2a65aa984..ee5769409 100644 --- a/app/constants/db_enums.rb +++ b/app/constants/db_enums.rb @@ -740,4 +740,28 @@ module DbEnums "Supported Housing" => 2, } end + + def self.org_type + { + "LA" => 1, + "PRP" => 2, + } + end + + def self.lettype + { + "Social Rent General Needs PRP" => 1, + "Social Rent Supported Housing PRP" => 2, + "Social Rent General Needs LA" => 3, + "Social Rent Supported Housing LA" => 4, + "Affordable Rent General Needs PRP" => 5, + "Affordable Rent Supported Housing PRP" => 6, + "Affordable Rent General Needs LA" => 7, + "Affordable Rent Supported Housing LA" => 8, + "Intermediate Rent General Needs PRP" => 9, + "Intermediate Rent Supported Housing PRP" => 10, + "Intermediate Rent General Needs LA" => 11, + "Intermediate Rent Supported Housing LA" => 12, + } + end end diff --git a/app/models/case_log.rb b/app/models/case_log.rb index e792d40c7..ea0278cf0 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -111,8 +111,9 @@ class CaseLog < ApplicationRecord enum incref: DbEnums.polar, _suffix: true enum renttype: DbEnums.renttype, _suffix: true enum needstype: DbEnums.needstype, _suffix: true + enum lettype: DbEnums.lettype, _suffix: true - AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at renttype].freeze + AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at renttype lettype].freeze OPTIONAL_FIELDS = %w[do_you_know_the_postcode do_you_know_the_local_authority first_time_property_let_as_social_housing].freeze @@ -191,6 +192,7 @@ private self.incref = 1 if net_income_known == "Prefer not to say" self.hhmemb = other_hhmemb + 1 if other_hhmemb.present? self.renttype = RENT_TYPE_MAPPING[rent_type] + self.lettype = "#{renttype} #{needstype} #{owning_organisation.org_type}" if renttype.present? && needstype.present? && owning_organisation.org_type.present? end def all_fields_completed? diff --git a/app/models/organisation.rb b/app/models/organisation.rb index 392f6366f..c550bf375 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -3,6 +3,9 @@ class Organisation < ApplicationRecord has_many :owned_case_logs, class_name: "CaseLog", foreign_key: "owning_organisation_id" has_many :managed_case_logs, class_name: "CaseLog", foreign_key: "managing_organisation_id" + include DbEnums + enum org_type: DbEnums.org_type, _suffix: true + def case_logs CaseLog.for_organisation(self) end diff --git a/docs/api/DLUHC-CORE-Data.v1.json b/docs/api/DLUHC-CORE-Data.v1.json index 7f85e2096..cea3e9076 100644 --- a/docs/api/DLUHC-CORE-Data.v1.json +++ b/docs/api/DLUHC-CORE-Data.v1.json @@ -306,7 +306,6 @@ "startertenancy": "No", "tenancylength": "No", "tenancy": "Secure (including flexible)", - "lettype": "Affordable Rent - General Needs", "landlord": "This landlord", "la": "Barnet", "previous_postcode": "NW1 5TY", diff --git a/spec/factories/case_log.rb b/spec/factories/case_log.rb index 9c8b7434a..80f05b7da 100644 --- a/spec/factories/case_log.rb +++ b/spec/factories/case_log.rb @@ -52,7 +52,7 @@ FactoryBot.define do startertenancy { "No" } tenancylength { 5 } tenancy { "Secure (including flexible)" } - lettype { "Affordable Rent - General Needs" } + lettype { "Affordable Rent General Needs LA" } landlord { "This landlord" } previous_postcode { "SE2 6RT" } rsnvac { "Tenant abandoned property" } diff --git a/spec/factories/organisation.rb b/spec/factories/organisation.rb index 91e4179e7..74bd0e44a 100644 --- a/spec/factories/organisation.rb +++ b/spec/factories/organisation.rb @@ -1,7 +1,7 @@ FactoryBot.define do factory :organisation do name { "DLUHC" } - org_type { 1 } + org_type { "LA" } address_line1 { "2 Marsham Street" } address_line2 { "London" } postcode { "SW1P 4DF" } diff --git a/spec/fixtures/complete_case_log.json b/spec/fixtures/complete_case_log.json index 8db69eb6e..e70a7a2f0 100644 --- a/spec/fixtures/complete_case_log.json +++ b/spec/fixtures/complete_case_log.json @@ -54,7 +54,6 @@ "startertenancy": "No", "tenancylength": "5", "tenancy": "Secure (including flexible)", - "lettype": "Affordable Rent - General Needs", "landlord": "This landlord", "la": "Barnet", "property_postcode": "NW1 5TY", @@ -126,9 +125,9 @@ "property_owner_organisation": "", "property_manager_organisation": "", "sale_or_letting": "", - "rent_type": "", + "rent_type": "Social Rent", "intermediate_rent_product_name": "", - "needstype": "", + "needstype": "General Needs", "sale_completion_date": "01/01/2020", "purchaser_code": "", "propcode": "123", @@ -143,7 +142,6 @@ "property_wheelchair_accessible": "Yes", "void_or_renewal_date": "05/05/2020", "tenant_same_property_renewal": "Yes", - "new_build_handover_date": "01/01/2019", - "renttype": 1 + "new_build_handover_date": "01/01/2019" } } diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index b05d925c1..064429a64 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -838,7 +838,7 @@ RSpec.describe Form, type: :model do describe "derived variables" do require "date" - let(:organisation) { FactoryBot.create(:organisation) } + let(:organisation) { FactoryBot.create(:organisation, org_type: "PRP") } let!(:case_log) do CaseLog.create({ managing_organisation: organisation, @@ -851,6 +851,7 @@ RSpec.describe Form, type: :model do net_income_known: "Prefer not to say", other_hhmemb: 6, rent_type: "London Living Rent", + needstype: "General Needs", }) end @@ -903,5 +904,13 @@ RSpec.describe Form, type: :model do expect(case_log.renttype).to eq("Intermediate Rent") expect(record_from_db["renttype"]).to eq(3) end + + it "correctly derives and saves lettype" do + case_log.reload + + record_from_db = ActiveRecord::Base.connection.execute("select lettype from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.lettype).to eq("Intermediate Rent General Needs PRP") + expect(record_from_db["lettype"]).to eq(9) + end end end