From 23b0bdc32512fc706cb3f52f9c936cb74900d3eb Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 28 Nov 2023 11:21:31 +0000 Subject: [PATCH] Update managing organisation for sales bulk upload --- .../bulk_upload/sales/year2023/row_parser.rb | 25 +++++++- .../sales/year2023/row_parser_spec.rb | 63 ++++++++++++++++++- 2 files changed, 83 insertions(+), 5 deletions(-) diff --git a/app/services/bulk_upload/sales/year2023/row_parser.rb b/app/services/bulk_upload/sales/year2023/row_parser.rb index 18634c859..485a412ba 100644 --- a/app/services/bulk_upload/sales/year2023/row_parser.rb +++ b/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_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_created_by_exists, 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_incomplete_soft_validations, on: :after_log @@ -896,7 +898,7 @@ private attributes["othtype"] = field_11 attributes["owning_organisation"] = owning_organisation - attributes["managing_organisation"] = owning_organisation + attributes["managing_organisation"] = managing_organisation attributes["created_by"] = created_by || bulk_upload.user attributes["hhregres"] = field_73 attributes["hhregresstill"] = field_74 @@ -1196,10 +1198,27 @@ private def validate_created_by_related 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! - 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 def setup_question?(question) diff --git a/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb b/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb index b66785afa..4853afb6f 100644 --- a/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb +++ b/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 let(:attributes) { setup_section_params } - it "is correctly set" do - expect(parser.log.managing_organisation_id).to be(owning_org.id) + context "when user is part of the owning organisation" do + 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