diff --git a/app/controllers/form_controller.rb b/app/controllers/form_controller.rb index 3df834add..e023a847a 100644 --- a/app/controllers/form_controller.rb +++ b/app/controllers/form_controller.rb @@ -102,9 +102,11 @@ private result[question.id] = question_params end - if current_user.support? && question.id == "owning_organisation_id" && @log.lettings? && @log.managing_organisation.blank? + if question.id == "owning_organisation_id" && @log.lettings? owning_organisation = Organisation.find(result["owning_organisation_id"]) - if owning_organisation&.managing_agents&.empty? + if current_user.support? && @log.managing_organisation.blank? && owning_organisation&.managing_agents&.empty? + result["managing_organisation_id"] = owning_organisation.id + elsif owning_organisation&.absorbing_organisation == current_user.organisation result["managing_organisation_id"] = owning_organisation.id end end diff --git a/spec/requests/form_controller_spec.rb b/spec/requests/form_controller_spec.rb index b0631a4f2..8e8a965cb 100644 --- a/spec/requests/form_controller_spec.rb +++ b/spec/requests/form_controller_spec.rb @@ -417,7 +417,7 @@ RSpec.describe FormController, type: :request do describe "Submit Form" do context "with a form page" do - let(:user) { create(:user) } + let(:user) { create(:user, :data_coordinator) } let(:support_user) { FactoryBot.create(:user, :support) } let(:organisation) { user.organisation } let(:lettings_log) do @@ -733,6 +733,34 @@ RSpec.describe FormController, type: :request do end end + context "when owning organisation is an organisation merged into user organisation" do + let(:params) do + { + id: lettings_log.id, + lettings_log: { + page: "stock_owner", + owning_organisation_id: merged_org.id, + }, + } + end + let(:merged_org) { create(:organisation) } + + before do + lettings_log.update!(owning_organisation: nil) + lettings_log.reload + merged_org.update!(merge_date: Time.zone.today, absorbing_organisation: organisation) + create(:organisation_relationship, parent_organisation: organisation) + end + + it "sets managing organisation to owning organisation" do + post "/lettings-logs/#{lettings_log.id}/stock-owner", params: params + expect(response).to redirect_to("/lettings-logs/#{lettings_log.id}/managing-organisation") + follow_redirect! + lettings_log.reload + expect(lettings_log.managing_organisation).to eq(merged_org) + end + end + context "when the question was accessed from a duplicate logs screen" do let(:lettings_log) { create(:lettings_log, :duplicate, created_by: user) } let(:duplicate_log) { create(:lettings_log, :duplicate, created_by: user) }