Browse Source

Update managing organisation for sales bulk upload

pull/2077/head
Kat 3 years ago
parent
commit
23b0bdc325
  1. 25
      app/services/bulk_upload/sales/year2023/row_parser.rb
  2. 63
      spec/services/bulk_upload/sales/year2023/row_parser_spec.rb

25
app/services/bulk_upload/sales/year2023/row_parser.rb

@ -455,10 +455,12 @@ class BulkUpload::Sales::Year2023::RowParser
validate :validate_owning_org_data_given, on: :after_log validate :validate_owning_org_data_given, on: :after_log
validate :validate_owning_org_exists, on: :after_log validate :validate_owning_org_exists, on: :after_log
validate :validate_owning_org_owns_stock, on: :after_log if FeatureToggle.sales_managing_organisation_enabled?
validate :validate_owning_org_permitted, on: :after_log validate :validate_owning_org_permitted, on: :after_log
validate :validate_created_by_exists, on: :after_log validate :validate_created_by_exists, on: :after_log
validate :validate_created_by_related, on: :after_log validate :validate_created_by_related, on: :after_log
validate :validate_managing_org_related, on: :after_log if FeatureToggle.sales_managing_organisation_enabled?
validate :validate_relevant_collection_window, on: :after_log validate :validate_relevant_collection_window, on: :after_log
validate :validate_incomplete_soft_validations, on: :after_log validate :validate_incomplete_soft_validations, on: :after_log
@ -896,7 +898,7 @@ private
attributes["othtype"] = field_11 attributes["othtype"] = field_11
attributes["owning_organisation"] = owning_organisation attributes["owning_organisation"] = owning_organisation
attributes["managing_organisation"] = owning_organisation attributes["managing_organisation"] = managing_organisation
attributes["created_by"] = created_by || bulk_upload.user attributes["created_by"] = created_by || bulk_upload.user
attributes["hhregres"] = field_73 attributes["hhregres"] = field_73
attributes["hhregresstill"] = field_74 attributes["hhregresstill"] = field_74
@ -1196,10 +1198,27 @@ private
def validate_created_by_related def validate_created_by_related
return unless created_by return unless created_by
return if created_by.organisation == owning_organisation || created_by.organisation == owning_organisation&.absorbing_organisation return if created_by.organisation == owning_organisation || created_by.organisation == managing_organisation
return if created_by.organisation == owning_organisation&.absorbing_organisation || created_by.organisation == managing_organisation&.absorbing_organisation
block_log_creation! block_log_creation!
errors.add(:field_2, "User must be related to owning organisation", category: :setup) errors.add(:field_2, "User must be related to owning organisation or managing organisation", category: :setup)
end
def managing_organisation
return owning_organisation if created_by&.organisation&.absorbed_organisations&.include?(owning_organisation)
created_by&.organisation || bulk_upload.user.organisation
end
def validate_managing_org_related
if owning_organisation && managing_organisation && !owning_organisation.can_be_managed_by?(organisation: managing_organisation)
block_log_creation!
if errors[:field_2].blank?
errors.add(:field_2, "This user belongs to an organisation that does not have a relationship with the owning organisation", category: :setup)
end
end
end end
def setup_question?(question) def setup_question?(question)

63
spec/services/bulk_upload/sales/year2023/row_parser_spec.rb

@ -1166,8 +1166,67 @@ RSpec.describe BulkUpload::Sales::Year2023::RowParser do
describe "#managing_organisation_id" do describe "#managing_organisation_id" do
let(:attributes) { setup_section_params } let(:attributes) { setup_section_params }
it "is correctly set" do context "when user is part of the owning organisation" do
expect(parser.log.managing_organisation_id).to be(owning_org.id) it "sets managing organisation to the users organisation" do
parser.valid?
expect(parser.log.owning_organisation_id).to be(owning_org.id)
expect(parser.log.managing_organisation_id).to be(owning_org.id)
end
end
context "when user is part of an organisation affiliated with owning org" do
let(:managing_agent) { create(:organisation) }
let(:user) { create(:user, organisation: managing_agent) }
let(:attributes) { setup_section_params }
before do
create(:organisation_relationship, child_organisation: managing_agent, parent_organisation: owning_org)
end
it "is not permitted as setup error" do
parser.valid?
expect(parser.log.owning_organisation_id).to be(owning_org.id)
expect(parser.log.managing_organisation_id).to be(managing_agent.id)
end
end
context "when user is part of an organisation not affiliated with owning org" do
let(:unaffiliated_org) { create(:organisation) }
let(:user) { create(:user, organisation: unaffiliated_org) }
let(:attributes) { setup_section_params }
it "is not permitted as setup error" do
parser.valid?
setup_errors = parser.errors.select { |e| e.options[:category] == :setup }
expect(setup_errors.find { |e| e.attribute == :field_2 }.message).to eql("This user belongs to an organisation that does not have a relationship with the owning organisation")
end
it "blocks log creation" do
parser.valid?
expect(parser).to be_block_log_creation
end
end
end
end
describe "#owning_organisation_id" do
let(:attributes) { setup_section_params }
context "when owning organisation does not own stock" do
let(:owning_org) { create(:organisation, :with_old_visible_id, holds_own_stock: false) }
let(:attributes) { setup_section_params }
it "is not permitted as setup error" do
parser.valid?
setup_errors = parser.errors.select { |e| e.options[:category] == :setup }
expect(setup_errors.find { |e| e.attribute == :field_1 }.message).to eql("The owning organisation code provided is for an organisation that does not own stock")
end
it "blocks log creation" do
parser.valid?
expect(parser).to be_block_log_creation
end end
end end
end end

Loading…
Cancel
Save