Browse Source

Fix how landlord is derived

pull/470/head
Stéphane Meny 3 years ago
parent
commit
02e8831029
No known key found for this signature in database
GPG Key ID: 9D0AFEA988527923
  1. 11
      app/models/case_log.rb
  2. 2
      app/models/validations/household_validations.rb
  3. 73
      app/services/imports/case_logs_import_service.rb
  4. 3
      spec/factories/case_log.rb
  5. 2
      spec/fixtures/exports/case_logs.xml
  6. 58
      spec/models/case_log_spec.rb
  7. 2
      spec/models/validations/household_validations_spec.rb

11
app/models/case_log.rb

@ -349,7 +349,7 @@ class CaseLog < ApplicationRecord
referral == 3 referral == 3
end end
def is_prevten_la_general_needs? def is_prevten_general_needs_tenancy?
# 30: Fixed term Local Authority General Needs tenancy # 30: Fixed term Local Authority General Needs tenancy
# 31: Lifetime Local Authority General Needs tenancy # 31: Lifetime Local Authority General Needs tenancy
# 32: Fixed term Private Registered Provider General Needs tenancy # 32: Fixed term Private Registered Provider General Needs tenancy
@ -473,7 +473,7 @@ private
self.referral = 0 self.referral = 0
self.layear = 1 self.layear = 1
if is_general_needs? if is_general_needs?
# TODO: Check value since 30/31 are LA and 32/33 are PRP (fixed term VS lifetime) # fixed term
self.prevten = 32 if managing_organisation.provider_type == "PRP" self.prevten = 32 if managing_organisation.provider_type == "PRP"
self.prevten = 30 if managing_organisation.provider_type == "LA" self.prevten = 30 if managing_organisation.provider_type == "LA"
end end
@ -485,8 +485,11 @@ private
self["ecstat#{idx}"] = nil self["ecstat#{idx}"] = nil
end end
end end
self.landlord = 1 if owning_organisation.provider_type == "LA" if owning_organisation == managing_organisation
self.landlord = 2 if owning_organisation.provider_type == "PRP" self.landlord = 1
else
self.landlord = 2
end
end end
def process_postcode_changes! def process_postcode_changes!

2
app/models/validations/household_validations.rb

@ -105,7 +105,7 @@ module Validations::HouseholdValidations
record.errors.add :homeless, I18n.t("validations.household.homeless.other.internal_transfer") record.errors.add :homeless, I18n.t("validations.household.homeless.other.internal_transfer")
end end
if record.is_internal_transfer? && record.this_landlord? && record.is_prevten_la_general_needs? if record.is_internal_transfer? && record.this_landlord? && record.is_prevten_general_needs_tenancy?
record.errors.add :referral, I18n.t("validations.household.referral.la_general_needs.internal_transfer") record.errors.add :referral, I18n.t("validations.household.referral.la_general_needs.internal_transfer")
record.errors.add :prevten, I18n.t("validations.household.prevten.la_general_needs.internal_transfer") record.errors.add :prevten, I18n.t("validations.household.prevten.la_general_needs.internal_transfer")
end end

73
app/services/imports/case_logs_import_service.rb

@ -45,8 +45,8 @@ module Imports
# Required fields for status complete or logic to work # Required fields for status complete or logic to work
# Note: order matters when we derive from previous values (attributes parameter) # Note: order matters when we derive from previous values (attributes parameter)
attributes["startdate"] = compose_date(xml_doc, "DAY", "MONTH", "YEAR") attributes["startdate"] = compose_date(xml_doc, "DAY", "MONTH", "YEAR")
attributes["owning_organisation_id"] = find_organisation_id(xml_doc, "OWNINGORGID") attributes["owning_organisation_id"] = find_organisation_id(xml_doc, "OWNINGORGID", "OWNINGORGNAME", "HCNUM")
attributes["managing_organisation_id"] = find_organisation_id(xml_doc, "MANINGORGID") attributes["managing_organisation_id"] = find_organisation_id(xml_doc, "MANINGORGID", "MANINGORGNAME", "MANHCNUM")
attributes["startertenancy"] = unsafe_string_as_integer(xml_doc, "_2a") attributes["startertenancy"] = unsafe_string_as_integer(xml_doc, "_2a")
attributes["tenancy"] = unsafe_string_as_integer(xml_doc, "Q2b") attributes["tenancy"] = unsafe_string_as_integer(xml_doc, "Q2b")
attributes["tenancyother"] = string_or_nil(xml_doc, "Q2ba") attributes["tenancyother"] = string_or_nil(xml_doc, "Q2ba")
@ -230,79 +230,22 @@ module Imports
end end
end end
# def let_type(xml_doc, attributes) def find_organisation_id(xml_doc, id_field, name_field, reg_field)
# # "1 Private Registered Provider" or "2 Local Authority" old_visible_id = unsafe_string_as_integer(xml_doc, id_field)
# # We do not store providertype since it comes from the organisation import
# landlord = field_value(xml_doc, "xmlns", "Landlord").to_i
#
# if attributes["renttype"] == SR_AR_IR[:social_rent] &&
# attributes["needstype"] == GN_SH[:general_needs] &&
# landlord == PRP_LA[:private_registered_provider]
# 1
# elsif attributes["renttype"] == SR_AR_IR[:social_rent] &&
# attributes["needstype"] == GN_SH[:supported_housing] &&
# landlord == PRP_LA[:private_registered_provider]
# 2
# elsif attributes["renttype"] == SR_AR_IR[:social_rent] &&
# attributes["needstype"] == GN_SH[:general_needs] &&
# landlord == PRP_LA[:local_authority]
# 3
# elsif attributes["renttype"] == SR_AR_IR[:social_rent] &&
# attributes["needstype"] == GN_SH[:supported_housing] &&
# landlord == PRP_LA[:local_authority]
# 4
# elsif attributes["renttype"] == SR_AR_IR[:affordable_rent] &&
# attributes["needstype"] == GN_SH[:general_needs] &&
# landlord == PRP_LA[:private_registered_provider]
# 5
# elsif attributes["renttype"] == SR_AR_IR[:affordable_rent] &&
# attributes["needstype"] == GN_SH[:supported_housing] &&
# landlord == PRP_LA[:private_registered_provider]
# 6
# elsif attributes["renttype"] == SR_AR_IR[:affordable_rent] &&
# attributes["needstype"] == GN_SH[:general_needs] &&
# landlord == PRP_LA[:local_authority]
# 7
# elsif attributes["renttype"] == SR_AR_IR[:affordable_rent] &&
# attributes["needstype"] == GN_SH[:supported_housing] &&
# landlord == PRP_LA[:local_authority]
# 8
# elsif attributes["renttype"] == SR_AR_IR[:intermediate_rent] &&
# attributes["needstype"] == GN_SH[:general_needs] &&
# landlord == PRP_LA[:private_registered_provider]
# 9
# elsif attributes["renttype"] == SR_AR_IR[:intermediate_rent] &&
# attributes["needstype"] == GN_SH[:supported_housing] &&
# landlord == PRP_LA[:private_registered_provider]
# 10
# elsif attributes["renttype"] == SR_AR_IR[:intermediate_rent] &&
# attributes["needstype"] == GN_SH[:general_needs] &&
# landlord == PRP_LA[:local_authority]
# 11
# elsif attributes["renttype"] == SR_AR_IR[:intermediate_rent] &&
# attributes["needstype"] == GN_SH[:supported_housing] &&
# landlord == PRP_LA[:local_authority]
# 12
# else
# raise "Could not infer rent type with rentype:#{attributes['renttype']} / needstype:#{attributes['needstype']} / landlord:#{landlord}"
# end
# end
def find_organisation_id(xml_doc, field)
old_visible_id = field_value(xml_doc, "xmlns", field).to_i
landlord = field_value(xml_doc, "xmlns", "Landlord").to_i
organisation = Organisation.find_by(old_visible_id:) organisation = Organisation.find_by(old_visible_id:)
# Quick hack: should be removed when all organisations are imported # Quick hack: should be removed when all organisations are imported
# Will fail in the future if the organisation is missing # Will fail in the future if the organisation is missing
if organisation.nil? if organisation.nil?
organisation = Organisation.new organisation = Organisation.new
organisation.old_visible_id = old_visible_id organisation.old_visible_id = old_visible_id
organisation.provider_type = if landlord == 2 let_type = unsafe_string_as_integer(xml_doc, "landlord")
organisation.provider_type = if let_type == PRP_LA[:local_authority]
1 1
else else
2 2
end end
organisation.name = string_or_nil(xml_doc, name_field)
organisation.housing_registration_no = string_or_nil(xml_doc, reg_field)
organisation.save! organisation.save!
end end
organisation.id organisation.id

3
spec/factories/case_log.rb

@ -2,6 +2,9 @@ FactoryBot.define do
factory :case_log do factory :case_log do
owning_organisation { FactoryBot.create(:organisation) } owning_organisation { FactoryBot.create(:organisation) }
managing_organisation { FactoryBot.create(:organisation) } managing_organisation { FactoryBot.create(:organisation) }
trait :same_landlord do
managing_organisation { owning_organisation }
end
trait :about_completed do trait :about_completed do
renewal { 0 } renewal { 0 }
needstype { 1 } needstype { 1 }

2
spec/fixtures/exports/case_logs.xml vendored

@ -44,7 +44,7 @@
<startertenancy>0</startertenancy> <startertenancy>0</startertenancy>
<tenancylength>5</tenancylength> <tenancylength>5</tenancylength>
<tenancy>1</tenancy> <tenancy>1</tenancy>
<landlord>1</landlord> <landlord>3</landlord>
<ppostcode_full>SE26RT</ppostcode_full> <ppostcode_full>SE26RT</ppostcode_full>
<rsnvac>6</rsnvac> <rsnvac>6</rsnvac>
<unittype_gn>7</unittype_gn> <unittype_gn>7</unittype_gn>

58
spec/models/case_log_spec.rb

@ -2,7 +2,7 @@ require "rails_helper"
RSpec.describe CaseLog do RSpec.describe CaseLog do
let(:owning_organisation) { FactoryBot.create(:organisation) } let(:owning_organisation) { FactoryBot.create(:organisation) }
let(:managing_organisation) { owning_organisation } let(:different_managing_organisation) { FactoryBot.create(:organisation) }
describe "#form" do describe "#form" do
let(:case_log) { FactoryBot.build(:case_log) } let(:case_log) { FactoryBot.build(:case_log) }
@ -33,7 +33,7 @@ RSpec.describe CaseLog do
let(:case_log) do let(:case_log) do
described_class.create( described_class.create(
owning_organisation:, owning_organisation:,
managing_organisation:, managing_organisation: owning_organisation,
) )
end end
@ -191,11 +191,10 @@ RSpec.describe CaseLog do
end end
describe "derived variables" do describe "derived variables" do
let(:organisation) { FactoryBot.create(:organisation, provider_type: "PRP") }
let!(:case_log) do let!(:case_log) do
described_class.create({ described_class.create({
managing_organisation: organisation, managing_organisation: owning_organisation,
owning_organisation: organisation, owning_organisation:,
postcode_full: "M1 1AE", postcode_full: "M1 1AE",
ppostcode_full: "M2 2AE", ppostcode_full: "M2 2AE",
startdate: Time.gm(2021, 10, 10), startdate: Time.gm(2021, 10, 10),
@ -235,17 +234,15 @@ RSpec.describe CaseLog do
expect(record_from_db["renttype"]).to eq(3) expect(record_from_db["renttype"]).to eq(3)
end end
context "when the owning organisation is a PRP" do context "when the owning and managing organisations are different" do
it "correctly derives and saves landlord based on owning_organisation provider_type" do it "correctly derives and saves landlord" do
record_from_db = ActiveRecord::Base.connection.execute("select landlord from case_logs where id=#{case_log.id}").to_a[0] record_from_db = ActiveRecord::Base.connection.execute("select landlord from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.landlord).to eq(2) expect(case_log.landlord).to eq(2)
expect(record_from_db["landlord"]).to eq(2) expect(record_from_db["landlord"]).to eq(2)
end end
end end
context "when the owning organisation is an LA" do context "when the owning and managing organisations are the same" do
let(:organisation) { FactoryBot.create(:organisation) }
it "correctly derives and saves landlord based on owning_organisation provider_type" do it "correctly derives and saves landlord based on owning_organisation provider_type" do
record_from_db = ActiveRecord::Base.connection.execute("select landlord from case_logs where id=#{case_log.id}").to_a[0] record_from_db = ActiveRecord::Base.connection.execute("select landlord from case_logs where id=#{case_log.id}").to_a[0]
expect(case_log.landlord).to eq(1) expect(case_log.landlord).to eq(1)
@ -255,6 +252,7 @@ RSpec.describe CaseLog do
context "when deriving lettype" do context "when deriving lettype" do
context "when the owning organisation is a PRP" do context "when the owning organisation is a PRP" do
before { case_log.owning_organisation.update!(provider_type: 2) }
context "when the rent type is intermediate rent and supported housing" do context "when the rent type is intermediate rent and supported housing" do
it "correctly derives and saves lettype" do it "correctly derives and saves lettype" do
case_log.update!(rent_type: 4, needstype: 0) case_log.update!(rent_type: 4, needstype: 0)
@ -1145,8 +1143,8 @@ RSpec.describe CaseLog do
let!(:address_case_log) do let!(:address_case_log) do
described_class.create({ described_class.create({
managing_organisation: organisation, managing_organisation: owning_organisation,
owning_organisation: organisation, owning_organisation:,
postcode_known: 1, postcode_known: 1,
postcode_full: "M1 1AE", postcode_full: "M1 1AE",
}) })
@ -1232,8 +1230,8 @@ RSpec.describe CaseLog do
let!(:address_case_log) do let!(:address_case_log) do
described_class.create({ described_class.create({
managing_organisation: organisation, managing_organisation: owning_organisation,
owning_organisation: organisation, owning_organisation:,
previous_postcode_known: 1, previous_postcode_known: 1,
ppostcode_full: "M1 1AE", ppostcode_full: "M1 1AE",
}) })
@ -1316,8 +1314,8 @@ RSpec.describe CaseLog do
context "when saving rent and charges" do context "when saving rent and charges" do
let!(:case_log) do let!(:case_log) do
described_class.create({ described_class.create({
managing_organisation: organisation, managing_organisation: owning_organisation,
owning_organisation: organisation, owning_organisation:,
brent: 5.77, brent: 5.77,
scharge: 10.01, scharge: 10.01,
pscharge: 3, pscharge: 3,
@ -1334,8 +1332,8 @@ RSpec.describe CaseLog do
context "when validating household members derived vars" do context "when validating household members derived vars" do
let!(:household_case_log) do let!(:household_case_log) do
described_class.create!({ described_class.create!({
managing_organisation: organisation, managing_organisation: owning_organisation,
owning_organisation: organisation, owning_organisation:,
hhmemb: 3, hhmemb: 3,
relat2: "X", relat2: "X",
relat3: "C", relat3: "C",
@ -1387,8 +1385,8 @@ RSpec.describe CaseLog do
context "when it is a renewal" do context "when it is a renewal" do
let!(:case_log) do let!(:case_log) do
described_class.create({ described_class.create({
managing_organisation: organisation, managing_organisation: owning_organisation,
owning_organisation: organisation, owning_organisation:,
renewal: 1, renewal: 1,
startdate: Time.zone.local(2021, 4, 10), startdate: Time.zone.local(2021, 4, 10),
}) })
@ -1431,8 +1429,8 @@ RSpec.describe CaseLog do
context "when answering the household characteristics questions" do context "when answering the household characteristics questions" do
let!(:case_log) do let!(:case_log) do
described_class.create({ described_class.create({
managing_organisation: organisation, managing_organisation: owning_organisation,
owning_organisation: organisation, owning_organisation:,
age1_known: 1, age1_known: 1,
sex1: "R", sex1: "R",
relat2: "R", relat2: "R",
@ -1450,8 +1448,8 @@ RSpec.describe CaseLog do
context "when the data provider is filling in household needs" do context "when the data provider is filling in household needs" do
let!(:case_log) do let!(:case_log) do
described_class.create({ described_class.create({
managing_organisation: organisation, managing_organisation: owning_organisation,
owning_organisation: organisation, owning_organisation:,
}) })
end end
@ -1477,8 +1475,8 @@ RSpec.describe CaseLog do
context "when it is supported housing and a care home charge has been supplied" do context "when it is supported housing and a care home charge has been supplied" do
let!(:case_log) do let!(:case_log) do
described_class.create({ described_class.create({
managing_organisation: organisation, managing_organisation: owning_organisation,
owning_organisation: organisation, owning_organisation:,
needstype: 0, needstype: 0,
}) })
end end
@ -1631,16 +1629,16 @@ RSpec.describe CaseLog do
context "when the data provider is filling in the reason for the property being vacant" do context "when the data provider is filling in the reason for the property being vacant" do
let!(:first_let_case_log) do let!(:first_let_case_log) do
described_class.create({ described_class.create({
managing_organisation: organisation, managing_organisation: owning_organisation,
owning_organisation: organisation, owning_organisation:,
first_time_property_let_as_social_housing: 1, first_time_property_let_as_social_housing: 1,
}) })
end end
let!(:relet_case_log) do let!(:relet_case_log) do
described_class.create({ described_class.create({
managing_organisation: organisation, managing_organisation: owning_organisation,
owning_organisation: organisation, owning_organisation:,
first_time_property_let_as_social_housing: 0, first_time_property_let_as_social_housing: 0,
}) })
end end

2
spec/models/validations/household_validations_spec.rb

@ -4,7 +4,7 @@ RSpec.describe Validations::HouseholdValidations do
subject(:household_validator) { validator_class.new } subject(:household_validator) { validator_class.new }
let(:validator_class) { Class.new { include Validations::HouseholdValidations } } let(:validator_class) { Class.new { include Validations::HouseholdValidations } }
let(:record) { FactoryBot.create(:case_log) } let(:record) { FactoryBot.create(:case_log, :same_landlord) }
describe "reasonable preference validations" do describe "reasonable preference validations" do
context "when reasonable preference is homeless" do context "when reasonable preference is homeless" do

Loading…
Cancel
Save