From c32bd841626bd877da10488213174ade1331b8cf Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Wed, 12 Jul 2023 09:39:30 +0100 Subject: [PATCH] Fix a bug with not finding a managing organisation field for sales (#1758) --- app/controllers/form_controller.rb | 2 +- spec/requests/form_controller_spec.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/app/controllers/form_controller.rb b/app/controllers/form_controller.rb index 93cfda2f0..f016291ff 100644 --- a/app/controllers/form_controller.rb +++ b/app/controllers/form_controller.rb @@ -102,7 +102,7 @@ private result[question.id] = question_params end - if current_user.support? && question.id == "owning_organisation_id" && @log.managing_organisation.blank? + if current_user.support? && question.id == "owning_organisation_id" && @log.lettings? && @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 diff --git a/spec/requests/form_controller_spec.rb b/spec/requests/form_controller_spec.rb index a6143f91a..78ca078ca 100644 --- a/spec/requests/form_controller_spec.rb +++ b/spec/requests/form_controller_spec.rb @@ -163,6 +163,32 @@ RSpec.describe FormController, type: :request do end end + context "when submitting a sales log for organisation that doesn't have any managing agents" do + let(:sales_log) { create(:sales_log) } + let(:params) do + { + id: sales_log.id, + sales_log: { + page: "organisation", + owning_organisation_id: managing_organisation.id, + }, + } + end + + before do + sales_log.update!(owning_organisation: nil, created_by: nil) + sales_log.reload + end + + it "correctly sets owning organisation" do + post "/sales-logs/#{sales_log.id}/organisation", params: params + expect(response).to redirect_to("/sales-logs/#{sales_log.id}/created-by") + follow_redirect! + sales_log.reload + expect(sales_log.owning_organisation).to eq(managing_organisation) + end + end + context "with valid managing organisation" do let(:params) do {