Browse Source

Update sales validations

pull/2000/head
Kat 3 years ago
parent
commit
a7934107e0
  1. 8
      app/models/validations/sales/setup_validations.rb
  2. 28
      spec/models/validations/sales/setup_validations_spec.rb

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

@ -37,7 +37,7 @@ module Validations::Sales::SetupValidations
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))
owning_organisation_available_from: record.owning_organisation.available_from.to_formatted_s(:govuk_date))
end
end
@ -50,10 +50,10 @@ module Validations::Sales::SetupValidations
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.to_date > record.saledate.to_date
elsif record.owning_organisation&.absorbed_organisations.present? && record.owning_organisation.available_from.present? && record.owning_organisation.available_from.to_date > record.saledate.to_date
record.errors.add :owning_organisation_id, I18n.t("validations.setup.owning_organisation.inactive_absorbing_organisation_sales",
owning_organisation: record.owning_organisation.name,
owning_organisation_available_from: record.owning_organisation.created_at.to_formatted_s(:govuk_date))
owning_organisation_available_from: record.owning_organisation.available_from.to_formatted_s(:govuk_date))
end
end
end
@ -104,6 +104,6 @@ private
end
def absorbing_owning_organisation_inactive?(record)
record.owning_organisation&.absorbed_organisations.present? && record.owning_organisation.created_at.to_date > record.saledate.to_date
record.owning_organisation&.absorbed_organisations.present? && record.owning_organisation.available_from.present? && record.owning_organisation.available_from.to_date > record.saledate.to_date
end
end

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

@ -188,7 +188,7 @@ RSpec.describe Validations::Sales::SetupValidations do
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(:absorbing_organisation) { create(:organisation, created_at: Time.zone.local(2023, 2, 1), available_from: Time.zone.local(2023, 2, 1), name: "Absorbing org") }
let(:merged_organisation) { create(:organisation, name: "Merged org") }
around do |example|
@ -218,19 +218,27 @@ RSpec.describe Validations::Sales::SetupValidations do
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
it "does not allow saledate before absorbing organisation has become available" 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
it "allows saledate after absorbing organisation has become available" 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
it "allows saledate if available from is not given" do
record.saledate = Time.zone.local(2023, 1, 1)
absorbing_organisation.update!(available_from: nil)
record.owning_organisation_id = absorbing_organisation.id
setup_validator.validate_merged_organisations_saledate(record)
expect(record.errors["saledate"]).to be_empty
end
end
end
@ -238,7 +246,7 @@ RSpec.describe Validations::Sales::SetupValidations 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(:absorbing_organisation) { create(:organisation, created_at: Time.zone.local(2023, 2, 1), available_from: Time.zone.local(2023, 2, 1), name: "Absorbing org") }
let(:merged_organisation) { create(:organisation, name: "Merged org") }
around do |example|
@ -265,19 +273,27 @@ RSpec.describe Validations::Sales::SetupValidations do
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
it "does not allow absorbing organisation before it has become available" 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 sale completion date. Absorbing org became active on 1 February 2023.")
end
it "allows absorbing organisation after it has been created" do
it "allows absorbing organisation after it has become available" 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
it "allows absorbing organisation if available from is not given" do
record.saledate = Time.zone.local(2023, 1, 1)
absorbing_organisation.update!(available_from: nil)
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

Loading…
Cancel
Save