From 6f139e15a4584fa6c513d31807d7779b540a3012 Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 28 Feb 2024 16:31:54 +0000 Subject: [PATCH] Update display_actions --- .../create_log_actions_component.rb | 11 +++++-- .../create_log_actions_component_spec.rb | 30 ++++++++++++++++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/app/components/create_log_actions_component.rb b/app/components/create_log_actions_component.rb index 11c63c3a9..c975dc73b 100644 --- a/app/components/create_log_actions_component.rb +++ b/app/components/create_log_actions_component.rb @@ -14,9 +14,8 @@ class CreateLogActionsComponent < ViewComponent::Base def display_actions? return false if bulk_upload.present? return true if user.support? - return false if !user.organisation.holds_own_stock? && user.organisation.stock_owners.empty? && user.organisation.absorbed_organisations.empty? - user.organisation.data_protection_confirmed? + organisation_or_stock_owner_signed_dsa_and_holds_own_stock?(user.organisation) end def create_button_href @@ -54,4 +53,12 @@ class CreateLogActionsComponent < ViewComponent::Base bulk_upload_sales_log_path(id: "start") end end + + def organisation_or_stock_owner_signed_dsa_and_holds_own_stock?(organisation) + return true if organisation.data_protection_confirmed? && organisation.holds_own_stock? + return true if organisation.stock_owners.any? { |stock_owner| stock_owner.data_protection_confirmed? && stock_owner.holds_own_stock? } + return true if organisation.absorbed_organisations.any? { |stock_owner| stock_owner.data_protection_confirmed? && stock_owner.holds_own_stock? } + + false + end end diff --git a/spec/components/create_log_actions_component_spec.rb b/spec/components/create_log_actions_component_spec.rb index 6572c93f9..5ad3d4bc3 100644 --- a/spec/components/create_log_actions_component_spec.rb +++ b/spec/components/create_log_actions_component_spec.rb @@ -74,7 +74,7 @@ RSpec.describe CreateLogActionsComponent, type: :component do end context "when has data sharing agremeent" do - let(:user) { create(:user, :support) } + let(:user) { create(:user) } it "renders actions" do expect(component.display_actions?).to eq(true) @@ -114,6 +114,34 @@ RSpec.describe CreateLogActionsComponent, type: :component do expect(component.create_button_href).to eq("/sales-logs") end end + + context "when organisation doesn't own stock" do + before do + user.organisation.update!(holds_own_stock: false) + end + + context "and stock owners that have signed data sharing agreement" do + before do + parent_organisation = create(:organisation) + create(:organisation_relationship, child_organisation: user.organisation, parent_organisation:) + end + + it "renders actions" do + expect(component.display_actions?).to eq(true) + end + end + + context "and no stock owners have signed data sharing agreement" do + before do + parent_organisation = create(:organisation, :without_dpc) + create(:organisation_relationship, child_organisation: user.organisation, parent_organisation:) + end + + it "does not render actions" do + expect(component.display_actions?).to eq(false) + end + end + end end end end