Browse Source

CLDC-3179 Update routing for previous postcode (#2207)

* Update routing for previous postcode

* Infer ppostcode_full for discounted sales

* Escape characters

* Update previous la routing

* Set prevloc

* Move postcode inferance

* Remove validation for 2024

* Update postgres in dockerfile
pull/2238/head
kosiakkatrina 11 months ago committed by GitHub
parent
commit
ac16c74303
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 9
      app/models/derived_variables/sales_log_variables.rb
  2. 6
      app/models/form/sales/pages/last_accommodation.rb
  3. 6
      app/models/form/sales/pages/last_accommodation_la.rb
  4. 9
      app/models/validations/sales/household_validations.rb
  5. 1
      app/models/validations/sales/property_validations.rb
  6. 35
      spec/models/form/sales/pages/last_accommodation_la_spec.rb
  7. 33
      spec/models/form/sales/pages/last_accommodation_spec.rb
  8. 54
      spec/models/sales_log_spec.rb
  9. 58
      spec/models/validations/sales/household_validations_spec.rb
  10. 12
      spec/models/validations/sales/property_validations_spec.rb

9
app/models/derived_variables/sales_log_variables.rb

@ -17,6 +17,15 @@ module DerivedVariables::SalesLogVariables
self.hoyear = hodate.year self.hoyear = hodate.year
end end
self.deposit = value if outright_sale? && mortgage_not_used? self.deposit = value if outright_sale? && mortgage_not_used?
if saledate && form.start_year_after_2024? && discounted_ownership_sale?
self.ppostcode_full = postcode_full
self.ppcodenk = 0 if postcode_full.present?
self.prevloc = la
self.is_previous_la_inferred = is_la_inferred
self.previous_la_known = la_known
end
self.pcode1, self.pcode2 = postcode_full.split if postcode_full.present? self.pcode1, self.pcode2 = postcode_full.split if postcode_full.present?
self.ppostc1, self.ppostc2 = ppostcode_full.split if ppostcode_full.present? self.ppostc1, self.ppostc2 = ppostcode_full.split if ppostcode_full.present?
self.totchild = total_child self.totchild = total_child

6
app/models/form/sales/pages/last_accommodation.rb

@ -10,4 +10,10 @@ class Form::Sales::Pages::LastAccommodation < ::Form::Page
Form::Sales::Questions::PreviousPostcode.new(nil, nil, self), Form::Sales::Questions::PreviousPostcode.new(nil, nil, self),
] ]
end end
def routed_to?(log, _user)
return false if log.form.start_year_after_2024? && log.discounted_ownership_sale?
super
end
end end

6
app/models/form/sales/pages/last_accommodation_la.rb

@ -13,4 +13,10 @@ class Form::Sales::Pages::LastAccommodationLa < ::Form::Page
Form::Sales::Questions::Prevloc.new(nil, nil, self), Form::Sales::Questions::Prevloc.new(nil, nil, self),
] ]
end end
def routed_to?(log, _user)
return false if log.form.start_year_after_2024? && log.discounted_ownership_sale?
super
end
end end

9
app/models/validations/sales/household_validations.rb

@ -11,15 +11,6 @@ module Validations::Sales::HouseholdValidations
shared_validate_partner_count(record, 6) shared_validate_partner_count(record, 6)
end end
def validate_previous_postcode(record)
return unless record.postcode_full && record.ppostcode_full && record.discounted_ownership_sale?
unless record.postcode_full == record.ppostcode_full
record.errors.add :postcode_full, :postcodes_not_matching, message: I18n.t("validations.household.postcode.discounted_ownership")
record.errors.add :ppostcode_full, :postcodes_not_matching, message: I18n.t("validations.household.postcode.discounted_ownership")
end
end
def validate_buyers_living_in_property(record) def validate_buyers_living_in_property(record)
return unless record.form.start_date.year >= 2023 return unless record.form.start_date.year >= 2023

1
app/models/validations/sales/property_validations.rb

@ -1,5 +1,6 @@
module Validations::Sales::PropertyValidations module Validations::Sales::PropertyValidations
def validate_postcodes_match_if_discounted_ownership(record) def validate_postcodes_match_if_discounted_ownership(record)
return unless record.saledate && !record.form.start_year_after_2024?
return unless record.ppostcode_full.present? && record.postcode_full.present? return unless record.ppostcode_full.present? && record.postcode_full.present?
if record.discounted_ownership_sale? && record.ppostcode_full != record.postcode_full if record.discounted_ownership_sale? && record.ppostcode_full != record.postcode_full

35
spec/models/form/sales/pages/last_accommodation_la_spec.rb

@ -5,8 +5,22 @@ RSpec.describe Form::Sales::Pages::LastAccommodationLa, type: :model do
let(:page_id) { nil } let(:page_id) { nil }
let(:page_definition) { nil } let(:page_definition) { nil }
let(:subsection) { instance_double(Form::Subsection, form: instance_double(Form, start_date:)) } let(:subsection) { instance_double(Form::Subsection, form: instance_double(Form, depends_on_met: true)) }
let(:start_date) { Time.utc(2022, 4, 1) } let(:start_date) { Time.utc(2022, 4, 1) }
let(:log) { create(:sales_log, :completed, saledate: now) }
let(:now) { Time.zone.local(2023, 4, 4) }
before do
Timecop.freeze(now)
Singleton.__init__(FormHandler)
allow(subsection).to receive(:depends_on).and_return(nil)
allow(subsection).to receive(:enabled?).and_return(true)
end
after do
Timecop.return
Singleton.__init__(FormHandler)
end
it "has correct subsection" do it "has correct subsection" do
expect(page.subsection).to eq(subsection) expect(page.subsection).to eq(subsection)
@ -33,4 +47,23 @@ RSpec.describe Form::Sales::Pages::LastAccommodationLa, type: :model do
"is_previous_la_inferred" => false, "is_previous_la_inferred" => false,
}]) }])
end end
it "is routed to" do
log.ownershipsch = 2
expect(page).to be_routed_to(log, nil)
end
context "with 2024 form" do
let(:now) { Time.zone.local(2024, 4, 4) }
it "is routed to for 2024 non discounted sale logs" do
log.update!(ownershipsch: 1)
expect(page).to be_routed_to(log, nil)
end
it "is not routed to for 2024 discounted sale logs" do
log.update!(ownershipsch: 2)
expect(page).not_to be_routed_to(log, nil)
end
end
end end

33
spec/models/form/sales/pages/last_accommodation_spec.rb

@ -3,10 +3,24 @@ require "rails_helper"
RSpec.describe Form::Sales::Pages::LastAccommodation, type: :model do RSpec.describe Form::Sales::Pages::LastAccommodation, type: :model do
subject(:page) { described_class.new(page_id, page_definition, subsection) } subject(:page) { described_class.new(page_id, page_definition, subsection) }
let(:log) { create(:sales_log, :completed, saledate: now) }
let(:now) { Time.zone.local(2023, 4, 4) }
let(:page_id) { nil } let(:page_id) { nil }
let(:page_definition) { nil } let(:page_definition) { nil }
let(:subsection) { instance_double(Form::Subsection) } let(:subsection) { instance_double(Form::Subsection) }
before do
Timecop.freeze(now)
Singleton.__init__(FormHandler)
allow(subsection).to receive(:depends_on).and_return(nil)
end
after do
Timecop.return
Singleton.__init__(FormHandler)
end
it "has correct subsection" do it "has correct subsection" do
expect(page.subsection).to eq(subsection) expect(page.subsection).to eq(subsection)
end end
@ -30,4 +44,23 @@ RSpec.describe Form::Sales::Pages::LastAccommodation, type: :model do
it "has correct depends_on" do it "has correct depends_on" do
expect(page.depends_on).to be_nil expect(page.depends_on).to be_nil
end end
it "is routed to" do
log.ownershipsch = 2
expect(page).to be_routed_to(log, nil)
end
context "with 2024 form" do
let(:now) { Time.zone.local(2024, 4, 4) }
it "is routed to for 2024 non discounted sale logs" do
log.update!(ownershipsch: 1)
expect(page).to be_routed_to(log, nil)
end
it "is not routed to for 2024 discounted sale logs" do
log.update!(ownershipsch: 2)
expect(page).not_to be_routed_to(log, nil)
end
end
end end

54
spec/models/sales_log_spec.rb

@ -657,7 +657,7 @@ RSpec.describe SalesLog, type: :model do
end end
before do before do
WebMock.stub_request(:get, /api.postcodes.io\/postcodes\/CA101AA/) WebMock.stub_request(:get, /api\.postcodes\.io\/postcodes\/CA101AA/)
.to_return(status: 200, body: '{"status":200,"result":{"admin_district":"Cumberland","codes":{"admin_district":"E06000064"}}}', headers: {}) .to_return(status: 200, body: '{"status":200,"result":{"admin_district":"Cumberland","codes":{"admin_district":"E06000064"}}}', headers: {})
Timecop.freeze(2023, 5, 1) Timecop.freeze(2023, 5, 1)
@ -687,7 +687,7 @@ RSpec.describe SalesLog, type: :model do
end end
before do before do
WebMock.stub_request(:get, /api.postcodes.io\/postcodes\/CA101AA/) WebMock.stub_request(:get, /api\.postcodes\.io\/postcodes\/CA101AA/)
.to_return(status: 200, body: '{"status":200,"result":{"admin_district":"Eden","codes":{"admin_district":"E07000030"}}}', headers: {}) .to_return(status: 200, body: '{"status":200,"result":{"admin_district":"Eden","codes":{"admin_district":"E07000030"}}}', headers: {})
Timecop.freeze(2023, 5, 2) Timecop.freeze(2023, 5, 2)
@ -703,6 +703,56 @@ RSpec.describe SalesLog, type: :model do
expect(address_sales_log_23_24.la).to eq("E06000064") expect(address_sales_log_23_24.la).to eq("E06000064")
expect(record_from_db["la"]).to eq("E06000064") expect(record_from_db["la"]).to eq("E06000064")
end end
it "does not set previous postcode or previous la for discounted sale" do
address_sales_log_23_24.update!(ownershipsch: 2, ppostcode_full: nil, prevloc: nil)
record_from_db = described_class.find(address_sales_log_23_24.id)
expect(address_sales_log_23_24.ppostcode_full).to eq(nil)
expect(record_from_db["ppostcode_full"]).to eq(nil)
expect(record_from_db["prevloc"]).to eq(nil)
end
end
context "with 24/25 logs" do
let(:address_sales_log_24_25) do
described_class.create({
owning_organisation:,
created_by: created_by_user,
ppcodenk: 1,
postcode_full: "CA10 1AA",
ppostcode_full: nil,
prevloc: nil,
saledate: Time.zone.local(2024, 5, 2),
})
end
before do
WebMock.stub_request(:get, /api\.postcodes\.io\/postcodes\/CA101AA/)
.to_return(status: 200, body: '{"status":200,"result":{"admin_district":"Eden","codes":{"admin_district":"E07000030"}}}', headers: {})
Timecop.freeze(2024, 5, 2)
Singleton.__init__(FormHandler)
end
after do
Timecop.unfreeze
end
it "sets previous postcode for discounted sale" do
address_sales_log_24_25.update!(ownershipsch: 2, ppostcode_full: nil)
record_from_db = described_class.find(address_sales_log_24_25.id)
expect(address_sales_log_24_25.ppostcode_full).to eq("CA10 1AA")
expect(record_from_db["ppostcode_full"]).to eq("CA10 1AA")
expect(record_from_db["prevloc"]).to eq("E06000064")
end
it "does not set previous postcode for non discounted sale" do
address_sales_log_24_25.update!(ownershipsch: 1, ppostcode_full: nil)
record_from_db = described_class.find(address_sales_log_24_25.id)
expect(address_sales_log_24_25.ppostcode_full).to eq(nil)
expect(record_from_db["ppostcode_full"]).to eq(nil)
expect(record_from_db["prevloc"]).to eq(nil)
end
end end
it "errors if the property postcode is emptied" do it "errors if the property postcode is emptied" do

58
spec/models/validations/sales/household_validations_spec.rb

@ -154,64 +154,6 @@ RSpec.describe Validations::Sales::HouseholdValidations do
end end
end end
describe "previous postcode validations" do
let(:record) { build(:sales_log) }
context "with a discounted sale" do
before do
record.ownershipsch = 2
end
it "adds an error when previous and current postcodes are not the same" do
record.postcode_full = "SO32 3PT"
record.ppostcode_full = "DN6 7FB"
household_validator.validate_previous_postcode(record)
expect(record.errors["postcode_full"])
.to include(match I18n.t("validations.household.postcode.discounted_ownership"))
expect(record.errors["ppostcode_full"])
.to include(match I18n.t("validations.household.postcode.discounted_ownership"))
end
it "allows same postcodes" do
record.postcode_full = "SO32 3PT"
record.ppostcode_full = "SO32 3PT"
household_validator.validate_previous_postcode(record)
expect(record.errors["postcode_full"]).to be_empty
expect(record.errors["ppostcode_full"]).to be_empty
end
it "does not add an error when postcode is missing" do
record.postcode_full = nil
record.ppostcode_full = "SO32 3PT"
household_validator.validate_previous_postcode(record)
expect(record.errors["postcode_full"]).to be_empty
expect(record.errors["ppostcode_full"]).to be_empty
end
it "does not add an error when previous postcode is missing" do
record.postcode_full = "SO32 3PT"
record.ppostcode_full = nil
household_validator.validate_previous_postcode(record)
expect(record.errors["postcode_full"]).to be_empty
expect(record.errors["ppostcode_full"]).to be_empty
end
end
context "without a discounted sale" do
before do
record.ownershipsch = 1
end
it "allows different postcodes" do
record.postcode_full = "SO32 3PT"
record.ppostcode_full = "DN6 7FB"
household_validator.validate_previous_postcode(record)
expect(record.errors["postcode_full"]).to be_empty
expect(record.errors["ppostcode_full"]).to be_empty
end
end
end
describe "validating fields about buyers living in the property" do describe "validating fields about buyers living in the property" do
let(:sales_log) { FactoryBot.create(:sales_log, :outright_sale_setup_complete, noint: 1, companybuy: 2, buylivein:, jointpur:, jointmore:, buy1livein:) } let(:sales_log) { FactoryBot.create(:sales_log, :outright_sale_setup_complete, noint: 1, companybuy: 2, buylivein:, jointpur:, jointmore:, buy1livein:) }

12
spec/models/validations/sales/property_validations_spec.rb

@ -19,7 +19,7 @@ RSpec.describe Validations::Sales::PropertyValidations do
end end
context "when ownership scheme is discounted ownership" do context "when ownership scheme is discounted ownership" do
let(:record) { build(:sales_log, ownershipsch: 2) } let(:record) { build(:sales_log, ownershipsch: 2, saledate: Time.zone.local(2023, 4, 5)) }
it "when ppostcode_full is not present no error is added" do it "when ppostcode_full is not present no error is added" do
record.postcode_full = "SW1A 1AA" record.postcode_full = "SW1A 1AA"
@ -54,6 +54,16 @@ RSpec.describe Validations::Sales::PropertyValidations do
expect(record.errors["ppostcode_full"]).to include(match I18n.t("validations.property.postcode.must_match_previous")) expect(record.errors["ppostcode_full"]).to include(match I18n.t("validations.property.postcode.must_match_previous"))
expect(record.errors["ownershipsch"]).to include(match I18n.t("validations.property.postcode.must_match_previous")) expect(record.errors["ownershipsch"]).to include(match I18n.t("validations.property.postcode.must_match_previous"))
end end
it "does not add error for 2024 log" do
record.postcode_full = "SW1A 1AA"
record.ppostcode_full = "SW1A 0AA"
record.saledate = Time.zone.local(2024, 4, 5)
property_validator.validate_postcodes_match_if_discounted_ownership(record)
expect(record.errors["postcode_full"]).to be_empty
expect(record.errors["ppostcode_full"]).to be_empty
expect(record.errors["ownershipsch"]).to be_empty
end
end end
end end

Loading…
Cancel
Save