Browse Source

Add merged orgs sale dat validations

pull/1826/head
Kat 3 years ago
parent
commit
24d157e2aa
  1. 25
      app/models/validations/sales/setup_validations.rb
  2. 2
      config/locales/en.yml
  3. 48
      spec/models/validations/sales/setup_validations_spec.rb

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

@ -24,6 +24,23 @@ module Validations::Sales::SetupValidations
end
end
def validate_merged_organisations_saledate(record)
return unless record.saledate && date_valid?("saledate", record)
if merged_owning_organisation_inactive?(record)
record.errors.add :saledate, I18n.t("validations.setup.saledate.invalid_merged_organisations_saledate",
owning_organisation: record.owning_organisation.name,
owning_organisation_merge_date: record.owning_organisation.merge_date.to_formatted_s(:govuk_date),
owning_absorbing_organisation: record.owning_organisation.absorbing_organisation.name)
end
if absorbing_owning_organisation_inactive?(record)
record.errors.add :saledate, I18n.t("validations.setup.saledate.invalid_absorbing_organisations_saledate",
owning_organisation: record.owning_organisation.name,
owning_organisation_available_from: record.owning_organisation.created_at.to_formatted_s(:govuk_date))
end
end
private
def active_collection_start_date
@ -64,4 +81,12 @@ private
)
end
end
def merged_owning_organisation_inactive?(record)
record.owning_organisation&.merge_date.present? && record.owning_organisation.merge_date < record.saledate
end
def absorbing_owning_organisation_inactive?(record)
record.owning_organisation&.absorbed_organisations.present? && record.owning_organisation.created_at > record.saledate
end
end

2
config/locales/en.yml

@ -239,6 +239,8 @@ en:
previous_and_current_collection_year:
"Enter a date within the %{previous_start_year_short}/%{previous_end_year_short} or %{previous_end_year_short}/%{current_end_year_short} collection years, which is between %{previous_start_year_long} and %{current_end_year_long}"
year_not_two_digits: "Sale completion year must be 2 digits"
invalid_merged_organisations_saledate: "Enter a date when the owning organisation was active. %{owning_organisation} became inactive on %{owning_organisation_merge_date} and was replaced by %{owning_absorbing_organisation}."
invalid_absorbing_organisations_saledate: "Enter a date when the owning organisation was active. %{owning_organisation} became active on %{owning_organisation_available_from}."
type:
percentage_bought_must_be_at_least_threshold: "The minimum increase in equity while staircasing is %{threshold}% for this shared ownership type"

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

@ -185,4 +185,52 @@ RSpec.describe Validations::Sales::SetupValidations do
end
end
end
describe "#validate_merged_organisations_saledate" do
let(:record) { build(:sales_log) }
let(:absorbing_organisation) { create(:organisation, created_at: Time.zone.local(2023, 2, 1), name: "Absorbing org") }
let(:merged_organisation) { create(:organisation, name: "Merged org") }
around do |example|
Timecop.freeze(Time.zone.local(2023, 5, 1))
example.run
Timecop.return
end
before do
merged_organisation.update!(absorbing_organisation:, merge_date: Time.zone.local(2023, 2, 2))
end
context "and owning organisation is no longer active" do
it "does not allow saledate after organisation has been merged" do
record.saledate = Time.zone.local(2023, 3, 1)
record.owning_organisation_id = merged_organisation.id
setup_validator.validate_merged_organisations_saledate(record)
expect(record.errors["saledate"]).to include(match "Enter a date when the owning organisation was active. Merged org became inactive on 2 February 2023 and was replaced by Absorbing org.")
end
it "allows saledate before organisation has been merged" do
record.saledate = Time.zone.local(2023, 1, 1)
record.owning_organisation_id = merged_organisation.id
setup_validator.validate_merged_organisations_saledate(record)
expect(record.errors["saledate"]).to be_empty
end
end
context "and owning organisation is not yet active during the saledate" do
it "does not allow saledate before absorbing organisation has been created" do
record.saledate = Time.zone.local(2023, 1, 1)
record.owning_organisation_id = absorbing_organisation.id
setup_validator.validate_merged_organisations_saledate(record)
expect(record.errors["saledate"]).to include(match "Enter a date when the owning organisation was active. Absorbing org became active on 1 February 2023.")
end
it "allows saledate after absorbing organisation has been created" do
record.saledate = Time.zone.local(2023, 2, 2)
record.owning_organisation_id = absorbing_organisation.id
setup_validator.validate_merged_organisations_saledate(record)
expect(record.errors["saledate"]).to be_empty
end
end
end
end

Loading…
Cancel
Save