From b7f80a8b883184d2a5c91da79eacb752b6fc8627 Mon Sep 17 00:00:00 2001 From: Kat Date: Thu, 6 Jul 2023 16:01:07 +0100 Subject: [PATCH] Fix a bug with not finding a managing organisation field for sales --- 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 eb58b656d..721be5de6 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 0f086d27a..2d1281c9e 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 {