diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index 44341b48f..53055cf82 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -273,7 +273,7 @@ private required_params[:sensitive] = required_params[:sensitive].to_i if required_params[:sensitive] - if current_user.data_coordinator? + if current_user.data_coordinator? && current_user.organisation.stock_owners.nil? required_params[:owning_organisation_id] = current_user.organisation_id end required_params @@ -291,10 +291,6 @@ private @scheme end - def user_allowed_action? - current_user.support? || current_user.organisation == @scheme&.owning_organisation || current_user.organisation.parent_organisations.exists?(@scheme&.owning_organisation_id) - end - def redirect_if_scheme_confirmed redirect_to @scheme if @scheme.confirmed? end diff --git a/app/helpers/check_answers_helper.rb b/app/helpers/check_answers_helper.rb index ed07f3b56..80f4326f7 100644 --- a/app/helpers/check_answers_helper.rb +++ b/app/helpers/check_answers_helper.rb @@ -14,7 +14,7 @@ module CheckAnswersHelper def can_change_scheme_answer?(attribute_name, scheme) return false unless current_user.support? || current_user.data_coordinator? - editable_attributes = current_user.support? ? ["Name", "Confidential information", "Housing stock owned by"] : ["Name", "Confidential information"] + editable_attributes = ["Name", "Confidential information", "Housing stock owned by"] !scheme.confirmed? || editable_attributes.include?(attribute_name) end diff --git a/app/helpers/schemes_helper.rb b/app/helpers/schemes_helper.rb index f7da6d3f7..ab6f4b6f2 100644 --- a/app/helpers/schemes_helper.rb +++ b/app/helpers/schemes_helper.rb @@ -1,6 +1,6 @@ module SchemesHelper - def display_scheme_attributes(scheme, user) - base_attributes = [ + def display_scheme_attributes(scheme) + [ { name: "Scheme code", value: scheme.id_to_display }, { name: "Name", value: scheme.service_name, edit: true }, { name: "Status", value: status_tag_from_resource(scheme) }, @@ -16,16 +16,6 @@ module SchemesHelper { name: "Intended length of stay", value: scheme.intended_stay }, { name: "Availability", value: scheme_availability(scheme) }, ] - - if user.data_coordinator? - base_attributes.delete_if { |item| item[:name] == "Housing stock owned by" } - end - - if scheme.has_other_client_group == "Yes" - base_attributes.append - end - - base_attributes end def scheme_availability(scheme) diff --git a/app/policies/scheme_policy.rb b/app/policies/scheme_policy.rb index 39842a160..30aa1543c 100644 --- a/app/policies/scheme_policy.rb +++ b/app/policies/scheme_policy.rb @@ -12,7 +12,7 @@ class SchemePolicy if scheme == Scheme true else - user.organisation.parent_organisations.exists?(scheme&.owning_organisation_id) || scheme&.owning_organisation == user.organisation + scheme_owned_by_user_org_or_stock_owner end end @@ -27,7 +27,7 @@ class SchemePolicy def update? return true if user.support? - user.data_coordinator? && (scheme&.owning_organisation == user.organisation) + user.data_coordinator? && scheme_owned_by_user_org_or_stock_owner end %w[ @@ -37,7 +37,7 @@ class SchemePolicy define_method method_name do return true if user.support? - user.organisation.parent_organisations.exists?(scheme&.owning_organisation_id) || scheme&.owning_organisation == user.organisation + scheme_owned_by_user_org_or_stock_owner end end @@ -57,7 +57,13 @@ class SchemePolicy define_method method_name do return true if user.support? - user.data_coordinator? && scheme&.owning_organisation == user.organisation + user.data_coordinator? && scheme_owned_by_user_org_or_stock_owner end end + +private + + def scheme_owned_by_user_org_or_stock_owner + scheme&.owning_organisation == user.organisation || user.organisation.stock_owners.exists?(scheme&.owning_organisation_id) + end end diff --git a/app/views/schemes/check_answers.html.erb b/app/views/schemes/check_answers.html.erb index 7924768f0..dccefc565 100644 --- a/app/views/schemes/check_answers.html.erb +++ b/app/views/schemes/check_answers.html.erb @@ -9,7 +9,6 @@

Scheme

<% @scheme.check_details_attributes.each do |attr| %> - <% next if current_user.data_coordinator? && attr[:name] == ("owned by") %> <%= render partial: "scheme_summary_list_row", locals: { scheme: @scheme, attribute: attr, change_link: @scheme.confirmed? ? scheme_edit_name_path(@scheme) : scheme_details_path(@scheme, check_answers: true) } %> <% end %> diff --git a/app/views/schemes/details.html.erb b/app/views/schemes/details.html.erb index 0e4f3a7d9..06c841dc7 100644 --- a/app/views/schemes/details.html.erb +++ b/app/views/schemes/details.html.erb @@ -50,7 +50,9 @@ <% organisations = Organisation.all.map { |org| OpenStruct.new(id: org.id, name: org.name) } %> - <% if current_user.support? %> + <% if current_user.data_coordinator? && current_user.organisation.stock_owners.nil? %> + <%= f.hidden_field :owning_organisation_id, value: current_user.organisation.id %> + <% else %> <%= f.govuk_collection_select :owning_organisation_id, organisations, :id, diff --git a/app/views/schemes/edit_name.html.erb b/app/views/schemes/edit_name.html.erb index 1dc8bc147..f9b9af99c 100644 --- a/app/views/schemes/edit_name.html.erb +++ b/app/views/schemes/edit_name.html.erb @@ -27,7 +27,9 @@ <% organisations = Organisation.all.map { |org| OpenStruct.new(id: org.id, name: org.name) } %> - <% if current_user.support? %> + <% if current_user.data_coordinator? && current_user.organisation.stock_owners.nil? %> + <%= f.hidden_field :owning_organisation_id, value: current_user.organisation.id %> + <% else %> <%= f.govuk_collection_select :owning_organisation_id, organisations, :id, diff --git a/app/views/schemes/new.html.erb b/app/views/schemes/new.html.erb index 81c5febbf..d9c80bb6a 100644 --- a/app/views/schemes/new.html.erb +++ b/app/views/schemes/new.html.erb @@ -30,10 +30,6 @@ <% organisations = Organisation.all.map { |org| OpenStruct.new(id: org.id, name: org.name) } %> <% answer_options = null_option + organisations %> - <% if current_user.data_coordinator? %> - <%= f.hidden_field :owning_organisation_id, value: current_user.organisation.id %> - <% end %> - <% scheme_types_selection = Scheme.scheme_types.keys.excluding("Missing").map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } %> <%= f.govuk_collection_radio_buttons :scheme_type, scheme_types_selection, @@ -52,8 +48,10 @@ :description, legend: { text: "Is this scheme registered under the Care Standards Act 2000?", size: "m" } %> - <% if current_user.support? %> - <%= f.govuk_collection_select :owning_organisation_id, + <% if current_user.data_coordinator? && current_user.organisation.stock_owners.nil? %> + <%= f.hidden_field :owning_organisation_id, value: current_user.organisation.id %> + <% else %> + <%= f.govuk_collection_select :owning_organisation_id, answer_options, :id, :name, diff --git a/app/views/schemes/show.html.erb b/app/views/schemes/show.html.erb index 66209fa28..7115dc07e 100644 --- a/app/views/schemes/show.html.erb +++ b/app/views/schemes/show.html.erb @@ -16,7 +16,7 @@

Scheme

<%= govuk_summary_list do |summary_list| %> - <% display_scheme_attributes(@scheme, current_user).each do |attr| %> + <% display_scheme_attributes(@scheme).each do |attr| %> <%= summary_list.row do |row| %> <% row.key { attr[:name] } %> <% row.value do %>