From 4975e50092d4d0e06f164ef5361277ad5152c48b Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 26 Jun 2023 16:56:32 +0100 Subject: [PATCH] Set managing org to owning org if there are no managing agents --- app/controllers/form_controller.rb | 8 ++++++++ spec/requests/form_controller_spec.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/app/controllers/form_controller.rb b/app/controllers/form_controller.rb index 0219b3c57..9d4cb5763 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.present? && !owning_organisation.has_managing_agents? + 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 a2c06c4f1..50758e05a 100644 --- a/spec/requests/form_controller_spec.rb +++ b/spec/requests/form_controller_spec.rb @@ -135,6 +135,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 {