Browse Source

bulk upload validation for owning org

bulk-upload-owning-org
Phil Lee 2 years ago
parent
commit
3d2502ebe9
  1. 13
      app/models/organisation.rb
  2. 24
      app/services/bulk_upload/lettings/row_parser.rb
  3. 4
      spec/factories/organisation.rb
  4. 30
      spec/services/bulk_upload/lettings/row_parser_spec.rb

13
app/models/organisation.rb

@ -17,8 +17,21 @@ class Organisation < ApplicationRecord
has_many :managing_agent_relationships, foreign_key: :parent_organisation_id, class_name: "OrganisationRelationship"
has_many :managing_agents, through: :managing_agent_relationships, source: :child_organisation
def affiliated_stock_owners
ids = []
if holds_own_stock? && persisted?
ids << id
end
ids.concat(stock_owners.pluck(:id))
Organisation.where(id: ids)
end
scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") }
scope :search_by, ->(param) { search_by_name(param) }
has_paper_trail
auto_strip_attributes :name

24
app/services/bulk_upload/lettings/row_parser.rb

@ -154,6 +154,9 @@ class BulkUpload::Lettings::RowParser
validate :validate_no_disabled_needs_conjunction
validate :validate_dont_know_disabled_needs_conjunction
validate :validate_no_and_dont_know_disabled_needs_conjunction
validate :validate_owning_org_permitted
validate :validate_owning_org_owns_stock
validate :validate_owning_org_exists
def valid?
errors.clear
@ -182,6 +185,27 @@ class BulkUpload::Lettings::RowParser
private
def validate_owning_org_owns_stock
if owning_organisation && !owning_organisation.holds_own_stock?
errors.delete(:field_111)
errors.add(:field_111, "The owning organisation code provided is for an organisation that does not own stock")
end
end
def validate_owning_org_exists
if owning_organisation.nil?
errors.delete(:field_111)
errors.add(:field_111, "The owning organisation code is incorrect")
end
end
def validate_owning_org_permitted
if owning_organisation && !bulk_upload.user.organisation.affiliated_stock_owners.include?(owning_organisation)
errors.delete(:field_111)
errors.add(:field_111, "You do not have permission to add logs for this owning organisation")
end
end
def validate_no_and_dont_know_disabled_needs_conjunction
if field_59 == 1 && field_60 == 1
errors.add(:field_59, I18n.t("validations.household.housingneeds.no_and_dont_know_disabled_needs_conjunction"))

4
spec/factories/organisation.rb

@ -13,6 +13,10 @@ FactoryBot.define do
trait :with_old_visible_id do
old_visible_id { rand(9_999_999).to_s }
end
trait :does_not_own_stock do
holds_own_stock { false }
end
end
factory :organisation_rent_period do

30
spec/services/bulk_upload/lettings/row_parser_spec.rb

@ -462,6 +462,36 @@ RSpec.describe BulkUpload::Lettings::RowParser do
end
end
describe "#field_111" do # owning org
context "when cannot find owning org" do
let(:attributes) { { bulk_upload:, field_111: "donotexist" } }
it "is not permitted" do
expect(parser.errors[:field_111]).to eql(["The owning organisation code is incorrect"])
end
end
context "when org is not stock owning" do
let(:owning_org) { create(:organisation, :with_old_visible_id, :does_not_own_stock) }
let(:attributes) { { bulk_upload:, field_111: owning_org.old_visible_id } }
it "is not permitted" do
expect(parser.errors[:field_111]).to eql(["The owning organisation code provided is for an organisation that does not own stock"])
end
end
context "when not affiliated with owning org" do
let(:unaffiliated_org) { create(:organisation, :with_old_visible_id) }
let(:attributes) { { bulk_upload:, field_111: unaffiliated_org.old_visible_id } }
it "is not permitted" do
expect(parser.errors[:field_111]).to eql(["You do not have permission to add logs for this owning organisation"])
end
end
end
describe "#field_134" do
context "when an unpermitted value" do
let(:attributes) { { bulk_upload:, field_134: 3 } }

Loading…
Cancel
Save