Browse Source

Add organisation validation to sales log

pull/1826/head
Kat 3 years ago
parent
commit
9b2998c9bb
  1. 17
      app/models/validations/sales/setup_validations.rb
  2. 48
      spec/models/validations/sales/setup_validations_spec.rb

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

@ -41,6 +41,23 @@ module Validations::Sales::SetupValidations
end
end
def validate_organisation(record)
return unless record.saledate && record.owning_organisation
if record.owning_organisation.present?
if record.owning_organisation&.merge_date.present? && record.owning_organisation.merge_date < record.saledate
record.errors.add :owning_organisation_id, I18n.t("validations.setup.owning_organisation.inactive_merged_organisation",
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)
elsif record.owning_organisation&.absorbed_organisations.present? && record.owning_organisation.created_at > record.saledate
record.errors.add :owning_organisation_id, I18n.t("validations.setup.owning_organisation.inactive_absorbing_organisation",
owning_organisation: record.owning_organisation.name,
owning_organisation_available_from: record.owning_organisation.created_at.to_formatted_s(:govuk_date))
end
end
end
private
def active_collection_start_date

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

@ -233,4 +233,52 @@ RSpec.describe Validations::Sales::SetupValidations do
end
end
end
describe "#validate_organisation" do
let(:record) { build(:sales_log) }
context "when organisations are merged" do
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))
merged_organisation.update!(merge_date: Time.zone.local(2023, 2, 2), absorbing_organisation:)
example.run
Timecop.return
end
context "and owning organisation is no longer active" do
it "does not allow organisation that has been merged" do
record.saledate = Time.zone.local(2023, 3, 1)
record.owning_organisation_id = merged_organisation.id
setup_validator.validate_organisation(record)
expect(record.errors["owning_organisation_id"]).to include(match "The owning organisation must be active on the tenancy start date. Merged org became inactive on 2 February 2023 and was replaced by Absorbing org.")
end
it "allows organisation before it has been merged" do
record.saledate = Time.zone.local(2023, 1, 1)
record.owning_organisation_id = merged_organisation.id
setup_validator.validate_organisation(record)
expect(record.errors["owning_organisation_id"]).to be_empty
end
end
context "and owning organisation is not yet active during the saledate" do
it "does not allow absorbing organisation before it had been created" do
record.saledate = Time.zone.local(2023, 1, 1)
record.owning_organisation_id = absorbing_organisation.id
setup_validator.validate_organisation(record)
expect(record.errors["owning_organisation_id"]).to include(match "The owning organisation must be active on the tenancy start date. Absorbing org became active on 1 February 2023.")
end
it "allows absorbing organisation after it has been created" do
record.saledate = Time.zone.local(2023, 2, 2)
record.owning_organisation_id = absorbing_organisation.id
setup_validator.validate_organisation(record)
expect(record.errors["owning_organisation_id"]).to be_empty
end
end
end
end
end

Loading…
Cancel
Save