diff --git a/app/helpers/schemes_helper.rb b/app/helpers/schemes_helper.rb index 0e318d283..1133d201c 100644 --- a/app/helpers/schemes_helper.rb +++ b/app/helpers/schemes_helper.rb @@ -85,6 +85,13 @@ module SchemesHelper end end + def display_duplicate_schemes_banner?(organisation, current_user) + return unless organisation.absorbed_organisations.merged_during_open_collection_period.any? + return unless current_user.data_coordinator? || current_user.support? + + organisation.owned_schemes.duplicate_sets.any? || organisation.owned_schemes.any? { |scheme| scheme.locations.duplicate_sets.any? } + end + private ActivePeriod = Struct.new(:from, :to) diff --git a/spec/helpers/schemes_helper_spec.rb b/spec/helpers/schemes_helper_spec.rb index 0df032c3f..0a37a1b38 100644 --- a/spec/helpers/schemes_helper_spec.rb +++ b/spec/helpers/schemes_helper_spec.rb @@ -118,4 +118,101 @@ RSpec.describe SchemesHelper do end end end + + describe "display_duplicate_schemes_banner?" do + let(:organisation) { create(:organisation) } + let(:current_user) { create(:user, :support) } + + context "when organisation has not absorbed other organisations" do + context "and it has duplicate schemes" do + before do + create_list(:scheme, 2, :duplicate, owning_organisation: organisation) + end + + it "does not display the banner" do + expect(display_duplicate_schemes_banner?(organisation, current_user)).to be_falsey + end + end + end + + context "when organisation has absorbed other organisations in open collection year" do + before do + build(:organisation, merge_date: Time.zone.today, absorbing_organisation_id: organisation.id).save(validate: false) + end + + context "and it has duplicate schemes" do + before do + create_list(:scheme, 2, :duplicate, owning_organisation: organisation) + end + + it "displays the banner" do + expect(display_duplicate_schemes_banner?(organisation, current_user)).to be_truthy + end + end + + context "and it has duplicate locations" do + let(:scheme) { create(:scheme, owning_organisation: organisation) } + + before do + create_list(:location, 2, postcode: "AB1 2CD", mobility_type: "A", scheme:) + end + + it "displays the banner" do + expect(display_duplicate_schemes_banner?(organisation, current_user)).to be_truthy + end + end + + context "and it has no duplicate schemes or locations" do + it "does not display the banner" do + expect(display_duplicate_schemes_banner?(organisation, current_user)).to be_falsey + end + end + + context "and it is viewed by data provider" do + let(:current_user) { create(:user, :data_provider) } + + before do + create_list(:scheme, 2, :duplicate, owning_organisation: organisation) + end + + it "does not display the banner" do + expect(display_duplicate_schemes_banner?(organisation, current_user)).to be_falsey + end + end + end + + context "when organisation has absorbed other organisations in closed collection year" do + before do + build(:organisation, merge_date: Time.zone.today - 2.years, absorbing_organisation_id: organisation.id).save(validate: false) + end + + context "and it has duplicate schemes" do + before do + create_list(:scheme, 2, :duplicate, owning_organisation: organisation) + end + + it "does not display the banner" do + expect(display_duplicate_schemes_banner?(organisation, current_user)).to be_falsey + end + end + + context "and it has duplicate locations" do + let(:scheme) { create(:scheme, owning_organisation: organisation) } + + before do + create(:location, postcode: "AB1 2CD", mobility_type: "A", scheme:) + end + + it "does not display the banner" do + expect(display_duplicate_schemes_banner?(organisation, current_user)).to be_falsey + end + end + + context "and it has no duplicate schemes or locations" do + it "does not display the banner" do + expect(display_duplicate_schemes_banner?(organisation, current_user)).to be_falsey + end + end + end + end end