Browse Source

CLDC-1464 Add completion date validations (#1232)

* feat: add 22/23 year validation

* feat: wip commit

* feat: update i18n

* feat: add one year gap exchange/completion validations

* tests: add tests for new validations

* test: add setup validations tests

* test: update factory to pass saledate validation

* feat: update seeds

* feat: update tests to reflect sales logs shouldn't exist in new service before 2022

* feat: merge with main, improve date behaviour

* refactor: cleanup

* feat: update tests

* feat: enforce saledate not after 22/23 collection year end
pull/1255/head
natdeanlewissoftwire 2 years ago committed by GitHub
parent
commit
65b1533aba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      app/models/sales_log.rb
  2. 14
      app/models/validations/sales/sale_information_validations.rb
  3. 9
      app/models/validations/sales/setup_validations.rb
  4. 15
      config/locales/en.yml
  5. 6
      db/seeds.rb
  6. 8
      spec/factories/sales_log.rb
  7. 8
      spec/models/validations/sales/sale_information_validations_spec.rb
  8. 49
      spec/models/validations/sales/setup_validations_spec.rb
  9. 26
      spec/requests/sales_logs_controller_spec.rb

1
app/models/sales_log.rb

@ -1,4 +1,5 @@
class SalesLogValidator < ActiveModel::Validator class SalesLogValidator < ActiveModel::Validator
include Validations::Sales::SetupValidations
include Validations::Sales::HouseholdValidations include Validations::Sales::HouseholdValidations
include Validations::Sales::PropertyValidations include Validations::Sales::PropertyValidations
include Validations::SharedValidations include Validations::SharedValidations

14
app/models/validations/sales/sale_information_validations.rb

@ -3,7 +3,7 @@ module Validations::Sales::SaleInformationValidations
return if record.saledate.blank? || record.hodate.blank? return if record.saledate.blank? || record.hodate.blank?
unless record.saledate > record.hodate unless record.saledate > record.hodate
record.errors.add :hodate, "Practical completion or handover date must be before exchange date" record.errors.add :hodate, I18n.t("validations.sale_information.hodate.must_be_before_exdate")
end end
end end
@ -23,11 +23,15 @@ module Validations::Sales::SaleInformationValidations
def validate_exchange_date(record) def validate_exchange_date(record)
return unless record.exdate && record.saledate return unless record.exdate && record.saledate
record.errors.add(:exdate, I18n.t("validations.sale_information.exdate.must_be_before_saledate")) if record.exdate > record.saledate if record.exdate > record.saledate
record.errors.add :exdate, I18n.t("validations.sale_information.exdate.must_be_before_saledate")
return if (record.saledate.to_date - record.exdate.to_date).to_i / 365 < 1 record.errors.add :saledate, I18n.t("validations.sale_information.saledate.must_be_after_exdate")
end
record.errors.add(:exdate, I18n.t("validations.sale_information.exdate.must_be_less_than_1_year_from_saledate")) if record.saledate - record.exdate >= 1.year
record.errors.add :exdate, I18n.t("validations.sale_information.exdate.must_be_less_than_1_year_from_saledate")
record.errors.add :saledate, I18n.t("validations.sale_information.saledate.must_be_less_than_1_year_from_exdate")
end
end end
def validate_previous_property_unit_type(record) def validate_previous_property_unit_type(record)

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

@ -0,0 +1,9 @@
module Validations::Sales::SetupValidations
def validate_saledate(record)
return unless record.saledate
unless Time.zone.local(2022, 4, 1) <= record.saledate && record.saledate < Time.zone.local(2023, 4, 1)
record.errors.add :saledate, I18n.t("validations.setup.saledate.financial_year")
end
end
end

15
config/locales/en.yml

@ -147,6 +147,8 @@ en:
setup: setup:
intermediate_rent_product_name: intermediate_rent_product_name:
blank: "Enter name of other intermediate rent product" blank: "Enter name of other intermediate rent product"
saledate:
financial_year: "Date must be from 22/23 financial year"
startdate: startdate:
later_than_14_days_after: "The tenancy start date must not be later than 14 days from today’s date" later_than_14_days_after: "The tenancy start date must not be later than 14 days from today’s date"
before_scheme_end_date: "The tenancy start date must be before the end date for this supported housing scheme" before_scheme_end_date: "The tenancy start date must be before the end date for this supported housing scheme"
@ -415,15 +417,16 @@ en:
proplen: proplen:
social_homebuy: "Social HomeBuy buyers should not have lived here before" social_homebuy: "Social HomeBuy buyers should not have lived here before"
rent_to_buy: "Rent to Buy buyers should not have lived here before" rent_to_buy: "Rent to Buy buyers should not have lived here before"
hodate:
must_be_before_exdate: "Practical completion or handover date must be before exchange date"
exdate: exdate:
must_be_before_saledate: must_be_before_saledate: "Contract exchange date must be less than 1 year before completion date"
Contract exchange date must be less than 1 year before completion date must_be_less_than_1_year_from_saledate: "Contract exchange date must be less than 1 year before completion date"
must_be_less_than_1_year_from_saledate: saledate:
Contract exchange date must be less than 1 year before completion date must_be_after_exdate: "Completion date must be less than 1 year after contract exchange date"
must_be_less_than_1_year_from_exdate: "Completion date must be less than 1 year after contract exchange date"
previous_property_beds: previous_property_beds:
property_type_bedsit: "Bedsit bedroom maximum 1" property_type_bedsit: "Bedsit bedroom maximum 1"
previous_property_type:
property_type_bedsit: "A bedsit can not have more than 1 bedroom"
discounted_ownership_value: "Mortgage, deposit, and grant total must equal £%{value_with_discount}" discounted_ownership_value: "Mortgage, deposit, and grant total must equal £%{value_with_discount}"
monthly_rent: monthly_rent:
higher_than_expected: "Basic monthly rent must be between £0 and £9,999" higher_than_expected: "Basic monthly rent must be between £0 and £9,999"

6
db/seeds.rb

@ -148,7 +148,7 @@ unless Rails.env.test?
if (Rails.env.development? || Rails.env.review?) && SalesLog.count.zero? if (Rails.env.development? || Rails.env.review?) && SalesLog.count.zero?
SalesLog.find_or_create_by!( SalesLog.find_or_create_by!(
saledate: Date.new(1, 1, 1), saledate: Date.new(2023, 1, 1),
purchid: "1", purchid: "1",
ownershipsch: 1, ownershipsch: 1,
type: 2, type: 2,
@ -157,7 +157,7 @@ unless Rails.env.test?
) )
SalesLog.find_or_create_by!( SalesLog.find_or_create_by!(
saledate: Date.new(1, 1, 1), saledate: Date.new(2023, 1, 1),
purchid: "1", purchid: "1",
ownershipsch: 2, ownershipsch: 2,
type: 9, type: 9,
@ -166,7 +166,7 @@ unless Rails.env.test?
) )
SalesLog.find_or_create_by!( SalesLog.find_or_create_by!(
saledate: Date.new(1, 1, 1), saledate: Date.new(2023, 1, 1),
purchid: "1", purchid: "1",
ownershipsch: 3, ownershipsch: 3,
type: 10, type: 10,

8
spec/factories/sales_log.rb

@ -2,18 +2,18 @@ FactoryBot.define do
factory :sales_log do factory :sales_log do
created_by { FactoryBot.create(:user) } created_by { FactoryBot.create(:user) }
owning_organisation { created_by.organisation } owning_organisation { created_by.organisation }
created_at { Time.utc(2022, 2, 8, 16, 52, 15) } created_at { Time.utc(2023, 2, 8, 16, 52, 15) }
updated_at { Time.utc(2022, 2, 8, 16, 52, 15) } updated_at { Time.utc(2023, 2, 8, 16, 52, 15) }
trait :in_progress do trait :in_progress do
purchid { "PC123" } purchid { "PC123" }
ownershipsch { 2 } ownershipsch { 2 }
type { 8 } type { 8 }
saledate { Time.utc(2022, 2, 2, 10, 36, 49) } saledate { Time.utc(2023, 2, 2, 10, 36, 49) }
end end
trait :completed do trait :completed do
ownershipsch { 2 } ownershipsch { 2 }
type { 8 } type { 8 }
saledate { Time.utc(2022, 2, 2, 10, 36, 49) } saledate { Time.utc(2023, 2, 2, 10, 36, 49) }
companybuy { 1 } companybuy { 1 }
jointpur { 1 } jointpur { 1 }
beds { 2 } beds { 2 }

8
spec/models/validations/sales/sale_information_validations_spec.rb

@ -111,12 +111,15 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
context "when exdate more than 1 year before saledate" do context "when exdate more than 1 year before saledate" do
let(:record) { build(:sales_log, exdate: 2.years.ago, saledate: 1.month.ago) } let(:record) { build(:sales_log, exdate: 2.years.ago, saledate: 1.month.ago) }
it "does not add the error" do it "adds error" do
sale_information_validator.validate_exchange_date(record) sale_information_validator.validate_exchange_date(record)
expect(record.errors[:exdate]).to eq( expect(record.errors[:exdate]).to eq(
["Contract exchange date must be less than 1 year before completion date"], ["Contract exchange date must be less than 1 year before completion date"],
) )
expect(record.errors[:saledate]).to eq(
["Completion date must be less than 1 year after contract exchange date"],
)
end end
end end
@ -129,6 +132,9 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
expect(record.errors[:exdate]).to eq( expect(record.errors[:exdate]).to eq(
["Contract exchange date must be less than 1 year before completion date"], ["Contract exchange date must be less than 1 year before completion date"],
) )
expect(record.errors[:saledate]).to eq(
["Completion date must be less than 1 year after contract exchange date"],
)
end end
end end

49
spec/models/validations/sales/setup_validations_spec.rb

@ -0,0 +1,49 @@
require "rails_helper"
RSpec.describe Validations::Sales::SetupValidations do
subject(:setup_validator) { validator_class.new }
let(:validator_class) { Class.new { include Validations::Sales::SetupValidations } }
describe "#validate_saledate" do
context "when saledate is blank" do
let(:record) { FactoryBot.build(:sales_log, saledate: nil) }
it "does not add an error" do
setup_validator.validate_saledate(record)
expect(record.errors).to be_empty
end
end
context "when saledate is in the 22/23 financial year" do
let(:record) { FactoryBot.build(:sales_log, saledate: Time.zone.local(2023, 1, 1)) }
it "does not add an error" do
setup_validator.validate_saledate(record)
expect(record.errors).to be_empty
end
end
context "when saledate is before the 22/23 financial year" do
let(:record) { FactoryBot.build(:sales_log, saledate: Time.zone.local(2022, 1, 1)) }
it "adds error" do
setup_validator.validate_saledate(record)
expect(record.errors[:saledate]).to include(I18n.t("validations.setup.saledate.financial_year"))
end
end
context "when saledate is after the 22/23 financial year" do
let(:record) { FactoryBot.build(:sales_log, saledate: Time.zone.local(2023, 4, 1)) }
it "adds error" do
setup_validator.validate_saledate(record)
expect(record.errors[:saledate]).to include(I18n.t("validations.setup.saledate.financial_year"))
end
end
end
end

26
spec/requests/sales_logs_controller_spec.rb

@ -179,29 +179,29 @@ RSpec.describe SalesLogsController, type: :request do
end end
context "with year filter" do context "with year filter" do
let!(:sales_log_2021) do let!(:sales_log_2022) do
FactoryBot.create(:sales_log, :in_progress, FactoryBot.create(:sales_log, :in_progress,
owning_organisation: organisation, owning_organisation: organisation,
saledate: Time.zone.local(2022, 3, 1)) saledate: Time.zone.local(2022, 4, 1))
end end
let!(:sales_log_2022) do let!(:sales_log_2023) do
sales_log = FactoryBot.build(:sales_log, :completed, sales_log = FactoryBot.build(:sales_log, :completed,
owning_organisation: organisation, owning_organisation: organisation,
saledate: Time.zone.local(2022, 12, 1)) saledate: Time.zone.local(2023, 1, 1))
sales_log.save!(validate: false) sales_log.save!(validate: false)
sales_log sales_log
end end
it "shows sales logs for multiple selected years" do it "shows sales logs for multiple selected years" do
get "/sales-logs?years[]=2021&years[]=2022", headers: headers, params: {} get "/sales-logs?years[]=2021&years[]=2022", headers: headers, params: {}
expect(page).to have_link(sales_log_2021.id.to_s)
expect(page).to have_link(sales_log_2022.id.to_s) expect(page).to have_link(sales_log_2022.id.to_s)
expect(page).to have_link(sales_log_2023.id.to_s)
end end
it "shows sales logs for one selected year" do it "shows sales logs for one selected year" do
get "/sales-logs?years[]=2021", headers: headers, params: {} get "/sales-logs?years[]=2022", headers: headers, params: {}
expect(page).to have_link(sales_log_2021.id.to_s) expect(page).to have_link(sales_log_2022.id.to_s)
expect(page).not_to have_link(sales_log_2022.id.to_s) expect(page).to have_link(sales_log_2023.id.to_s)
end end
end end
@ -214,23 +214,23 @@ RSpec.describe SalesLogsController, type: :request do
Timecop.unfreeze Timecop.unfreeze
end end
let!(:sales_log_2021) do let!(:sales_log_2022) do
FactoryBot.create(:sales_log, :completed, FactoryBot.create(:sales_log, :completed,
owning_organisation: organisation, owning_organisation: organisation,
saledate: Time.zone.local(2022, 3, 1), saledate: Time.zone.local(2022, 4, 1),
created_by: user) created_by: user)
end end
let!(:sales_log_2022) do let!(:sales_log_2023) do
FactoryBot.create(:sales_log, FactoryBot.create(:sales_log,
owning_organisation: organisation, owning_organisation: organisation,
saledate: Time.zone.local(2022, 12, 1), saledate: Time.zone.local(2023, 1, 1),
created_by: user) created_by: user)
end end
it "shows sales logs for multiple selected statuses and years" do it "shows sales logs for multiple selected statuses and years" do
get "/sales-logs?years[]=2021&years[]=2022&status[]=in_progress&status[]=completed", headers: headers, params: {} get "/sales-logs?years[]=2021&years[]=2022&status[]=in_progress&status[]=completed", headers: headers, params: {}
expect(page).to have_link(sales_log_2021.id.to_s)
expect(page).to have_link(sales_log_2022.id.to_s) expect(page).to have_link(sales_log_2022.id.to_s)
expect(page).to have_link(sales_log_2023.id.to_s)
end end
end end
end end

Loading…
Cancel
Save