diff --git a/app/controllers/form_controller.rb b/app/controllers/form_controller.rb index c1f5e700d..a2f669551 100644 --- a/app/controllers/form_controller.rb +++ b/app/controllers/form_controller.rb @@ -4,6 +4,26 @@ class FormController < ApplicationController before_action :find_resource_by_named_id, except: %i[review] before_action :check_collection_period, only: %i[submit_form show_page] + def update_owning_org(log) + return unless current_user + return if current_user.support? + + stock_owners = current_user.organisation.stock_owners + + if current_user.organisation.holds_own_stock? + return if stock_owners.count >= 1 + + log.update!(owning_organisation: current_user.organisation) + else + return if stock_owners.count.zero? + return if stock_owners.count > 1 + + log.update!(owning_organisation: stock_owners.first) + end + + false + end + def submit_form if @log @page = form.get_page(params[@log.model_name.param_key][:page]) @@ -11,6 +31,7 @@ class FormController < ApplicationController mandatory_questions_with_no_response = mandatory_questions_with_no_response(responses_for_page) if mandatory_questions_with_no_response.empty? && @log.update(responses_for_page.merge(updated_by: current_user)) + update_owning_org(@log) flash[:notice] = "You have successfully updated #{@page.questions.map(&:check_answer_label).first.downcase}" if previous_interruption_screen_page_id.present? redirect_to(successful_redirect_path) else diff --git a/app/models/form/lettings/pages/stock_owner.rb b/app/models/form/lettings/pages/stock_owner.rb index d92824f0e..852b46523 100644 --- a/app/models/form/lettings/pages/stock_owner.rb +++ b/app/models/form/lettings/pages/stock_owner.rb @@ -10,7 +10,7 @@ class Form::Lettings::Pages::StockOwner < ::Form::Page ] end - def routed_to?(log, current_user) + def routed_to?(_log, current_user) return false unless current_user return true if current_user.support? @@ -18,13 +18,9 @@ class Form::Lettings::Pages::StockOwner < ::Form::Page if current_user.organisation.holds_own_stock? return true if stock_owners.count >= 1 - - log.update!(owning_organisation: current_user.organisation) else return false if stock_owners.count.zero? return true if stock_owners.count > 1 - - log.update!(owning_organisation: stock_owners.first) end false