diff --git a/app/controllers/form_controller.rb b/app/controllers/form_controller.rb index ac5b46acf..eb58b656d 100644 --- a/app/controllers/form_controller.rb +++ b/app/controllers/form_controller.rb @@ -101,6 +101,14 @@ private else result[question.id] = question_params end + + if current_user.support? && question.id == "owning_organisation_id" && @log.managing_organisation.blank? + owning_organisation = Organisation.find(result["owning_organisation_id"]) + if owning_organisation&.managing_agents&.empty? + result["managing_organisation_id"] = owning_organisation.id + end + end + result end end diff --git a/spec/requests/form_controller_spec.rb b/spec/requests/form_controller_spec.rb index a74ffb026..2d79746bc 100644 --- a/spec/requests/form_controller_spec.rb +++ b/spec/requests/form_controller_spec.rb @@ -136,6 +136,32 @@ RSpec.describe FormController, type: :request do end end + context "when owning organisation doesn't have any managing agents" do + let(:params) do + { + id: lettings_log.id, + lettings_log: { + page: "stock_owner", + owning_organisation_id: managing_organisation.id, + }, + } + end + + before do + lettings_log.update!(owning_organisation: nil, created_by: nil, managing_organisation: nil) + lettings_log.reload + 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}/created-by") + follow_redirect! + lettings_log.reload + expect(lettings_log.owning_organisation).to eq(managing_organisation) + expect(lettings_log.managing_organisation).to eq(managing_organisation) + end + end + context "with valid managing organisation" do let(:params) do {