From 68eb307d51eebdfe47c956cff66739c5ae6584a1 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Tue, 17 Oct 2023 08:05:42 +0100 Subject: [PATCH] CLDC-2626 Communicate permissions around editing schemes (#1970) * Communicate permissions around editing schemes * lint * Move location text --- app/helpers/locations_helper.rb | 8 ++++++ app/helpers/schemes_helper.rb | 8 ++++++ app/views/locations/show.html.erb | 2 ++ app/views/schemes/show.html.erb | 1 + spec/helpers/locations_helper_spec.rb | 35 +++++++++++++++++++++++++++ spec/helpers/schemes_helper_spec.rb | 35 +++++++++++++++++++++++++++ 6 files changed, 89 insertions(+) diff --git a/app/helpers/locations_helper.rb b/app/helpers/locations_helper.rb index 235029e9d..6a76e8943 100644 --- a/app/helpers/locations_helper.rb +++ b/app/helpers/locations_helper.rb @@ -83,6 +83,14 @@ module LocationsHelper user.support? || user.organisation == scheme.owning_organisation end + def edit_location_text(scheme, user) + if user.data_provider? + "If you think this location should be updated, ask a data coordinator to make the changes. Find your data coordinators on the #{link_to('users page', users_path)}.".html_safe + elsif user.data_coordinator? && user.organisation.parent_organisations.include?(scheme.owning_organisation) + "This location belongs to your stock owner #{scheme.owning_organisation.name}." + end + end + private ActivePeriod = Struct.new(:from, :to) diff --git a/app/helpers/schemes_helper.rb b/app/helpers/schemes_helper.rb index d111b402c..6b069fbbb 100644 --- a/app/helpers/schemes_helper.rb +++ b/app/helpers/schemes_helper.rb @@ -45,6 +45,14 @@ module SchemesHelper [OpenStruct.new(id: "", name: "Select an option")] end + def edit_scheme_text(scheme, user) + if user.data_provider? + "If you think this scheme should be updated, ask a data coordinator to make the changes. Find your data coordinators on the #{link_to('users page', users_path)}.".html_safe + elsif user.data_coordinator? && user.organisation.parent_organisations.include?(scheme.owning_organisation) + "This scheme belongs to your stock owner #{scheme.owning_organisation.name}." + end + end + private ActivePeriod = Struct.new(:from, :to) diff --git a/app/views/locations/show.html.erb b/app/views/locations/show.html.erb index fcee0a175..5d6044f06 100644 --- a/app/views/locations/show.html.erb +++ b/app/views/locations/show.html.erb @@ -11,6 +11,8 @@
+ <%= govuk_inset_text(text: edit_location_text(@scheme, current_user)) %> + <%= govuk_summary_list do |summary_list| %> <% display_location_attributes(@location).each do |attr| %> <%= summary_list.row do |row| %> diff --git a/app/views/schemes/show.html.erb b/app/views/schemes/show.html.erb index 7115dc07e..ce5a35f70 100644 --- a/app/views/schemes/show.html.erb +++ b/app/views/schemes/show.html.erb @@ -14,6 +14,7 @@ <%= render SubNavigationComponent.new(items: scheme_items(request.path, @scheme.id, "Locations")) %>

Scheme

+ <%= govuk_inset_text(text: edit_scheme_text(@scheme, current_user)) %> <%= govuk_summary_list do |summary_list| %> <% display_scheme_attributes(@scheme).each do |attr| %> diff --git a/spec/helpers/locations_helper_spec.rb b/spec/helpers/locations_helper_spec.rb index 948cda3c5..b86194918 100644 --- a/spec/helpers/locations_helper_spec.rb +++ b/spec/helpers/locations_helper_spec.rb @@ -308,4 +308,39 @@ RSpec.describe LocationsHelper do end end end + + describe "edit_location_text" do + let(:parent_organisation) { FactoryBot.create(:organisation, name: "Parent") } + let(:child_organisation) { FactoryBot.create(:organisation, name: "Child") } + let(:scheme) { FactoryBot.create(:scheme, owning_organisation: parent_organisation) } + let(:data_coordinator) { FactoryBot.create(:user, :data_coordinator, organisation: child_organisation) } + let(:data_provider) { FactoryBot.create(:user, :data_provider, organisation: child_organisation) } + let(:location) { FactoryBot.build(:location, scheme:) } + + before do + create(:organisation_relationship, child_organisation:, parent_organisation:) + end + + context "with data coordinator user" do + it "returns correct edit location text for a parent organisation location" do + expect(edit_location_text(scheme, data_coordinator)).to include("This location belongs to your stock owner Parent.") + end + + it "returns nil when viewing your organisation location" do + data_coordinator.update!(organisation: parent_organisation) + expect(edit_location_text(scheme, data_coordinator)).to be_nil + end + end + + context "with data provider user" do + it "returns correct edit location text for a parent organisation location" do + expect(edit_location_text(scheme, data_provider)).to include("If you think this location should be updated, ask a data coordinator to make the changes. Find your data coordinators on the ") + end + + it "returns correct edit location text for your organisation location" do + data_provider.update!(organisation: parent_organisation) + expect(edit_location_text(scheme, data_provider)).to include("If you think this location should be updated, ask a data coordinator to make the changes. Find your data coordinators on the ") + end + end + end end diff --git a/spec/helpers/schemes_helper_spec.rb b/spec/helpers/schemes_helper_spec.rb index ef4b5fcec..5aa825e79 100644 --- a/spec/helpers/schemes_helper_spec.rb +++ b/spec/helpers/schemes_helper_spec.rb @@ -267,4 +267,39 @@ RSpec.describe SchemesHelper do end end end + + describe "edit_scheme_text" do + let(:parent_organisation) { FactoryBot.create(:organisation, name: "Parent") } + let(:child_organisation) { FactoryBot.create(:organisation, name: "Child") } + + let(:scheme) { FactoryBot.create(:scheme, owning_organisation: parent_organisation) } + let(:data_coordinator) { FactoryBot.create(:user, :data_coordinator, organisation: child_organisation) } + let(:data_provider) { FactoryBot.create(:user, :data_provider, organisation: child_organisation) } + + before do + create(:organisation_relationship, child_organisation:, parent_organisation:) + end + + context "with data coordinator user" do + it "returns correct edit scheme text for a parent organisation scheme" do + expect(edit_scheme_text(scheme, data_coordinator)).to include("This scheme belongs to your stock owner Parent.") + end + + it "returns nil when viewing your organisation scheme" do + data_coordinator.update!(organisation: parent_organisation) + expect(edit_scheme_text(scheme, data_coordinator)).to be_nil + end + end + + context "with data provider user" do + it "returns correct edit scheme text for a parent organisation scheme" do + expect(edit_scheme_text(scheme, data_provider)).to include("If you think this scheme should be updated, ask a data coordinator to make the changes. Find your data coordinators on the ") + end + + it "returns correct edit scheme text for your organisation scheme" do + data_provider.update!(organisation: parent_organisation) + expect(edit_scheme_text(scheme, data_provider)).to include("If you think this scheme should be updated, ask a data coordinator to make the changes. Find your data coordinators on the ") + end + end + end end