diff --git a/ee/app/finders/namespaces/trial_eligible_finder.rb b/ee/app/finders/namespaces/trial_eligible_finder.rb index 954ada8200d6cc4c787cf17beca5425bc1192b83..0ea107596d40e930a494b2375e499e512f6f02ed 100644 --- a/ee/app/finders/namespaces/trial_eligible_finder.rb +++ b/ee/app/finders/namespaces/trial_eligible_finder.rb @@ -18,12 +18,20 @@ def initial_scope if params[:user] && params[:namespace] raise ArgumentError, 'Only User or Namespace can be provided, not both' elsif params[:user] - params[:user].owned_groups.eligible_for_trial.ordered_by_name + params[:user] + .owned_groups + .in_specific_plans([no_subscription_plan_name, *::Plan::PLANS_ELIGIBLE_FOR_TRIAL]) + .ordered_by_name elsif params[:namespace] Namespace.id_in(params[:namespace]) else raise ArgumentError, 'User or Namespace must be provided' end end + + def no_subscription_plan_name + # Subscriptions aren't created until needed/looked at + nil + end end end diff --git a/ee/app/models/ee/namespace.rb b/ee/app/models/ee/namespace.rb index 506d207d4b8bd221a5c4230abdd3463ebf88ad90..57182150b75b67b0562ae832bd606a3de582fd8f 100644 --- a/ee/app/models/ee/namespace.rb +++ b/ee/app/models/ee/namespace.rb @@ -77,14 +77,9 @@ module Namespace .allow_cross_joins_across_databases(url: "https://gitlab.com/gitlab-org/gitlab/-/issues/419988") end - scope :not_in_default_plan, -> do - left_joins(gitlab_subscription: :hosted_plan) - .where.not(plans: { name: [nil, *::Plan.default_plans] }) - .allow_cross_joins_across_databases(url: "https://gitlab.com/gitlab-org/gitlab/-/issues/419988") - end - scope :in_specific_plans, ->(plan_names) do - left_joins(gitlab_subscription: :hosted_plan) + top_level + .left_joins(gitlab_subscription: :hosted_plan) .where(plans: { name: plan_names }) .allow_cross_joins_across_databases(url: "https://gitlab.com/gitlab-org/gitlab/-/issues/419988") end @@ -126,14 +121,6 @@ module Namespace .where(no_add_on.or(not_started_yet.or(not_a_trial.or(not_duo_pro.or(is_expired))))) end - scope :eligible_for_trial, -> do - left_joins(gitlab_subscription: :hosted_plan) - .top_level - .where( - plans: { name: [nil, *::Plan::PLANS_ELIGIBLE_FOR_TRIAL] } - ).allow_cross_joins_across_databases(url: "https://gitlab.com/gitlab-org/gitlab/-/issues/419988") - end - scope :with_feature_available_in_plan, ->(feature) do plans = GitlabSubscriptions::Features.saas_plans_with_feature(feature) matcher = ::Plan.by_name(plans) diff --git a/ee/spec/models/ee/namespace_spec.rb b/ee/spec/models/ee/namespace_spec.rb index b8bf70cb06e0947f2de309cc9bea728c8d1ce7b0..0d31739dce76f550c0b895c3aa897caa2cbbb205 100644 --- a/ee/spec/models/ee/namespace_spec.rb +++ b/ee/spec/models/ee/namespace_spec.rb @@ -350,38 +350,10 @@ end end - describe '.not_in_default_plan', :saas do - subject { described_class.not_in_default_plan.ids } - - where(:plan_name, :expect_in_default_plan) do - ::Plan::FREE | true - ::Plan::DEFAULT | true - ::Plan::BRONZE | false - ::Plan::SILVER | false - ::Plan::PREMIUM | false - ::Plan::GOLD | false - ::Plan::ULTIMATE | false - end - - with_them do - it 'returns expected result' do - namespace = create(:namespace_with_plan, plan: "#{plan_name}_plan") - - is_expected.to eq(expect_in_default_plan ? [] : [namespace.id]) - end - end - - it 'does not include namespace without subscription' do - create(:namespace) - - is_expected.to eq([]) - end - end - describe '.in_specific_plans', :saas do - let_it_be(:free_namespace) { create(:namespace_with_plan, plan: :free_plan) } - let_it_be(:premium_namespace) { create(:namespace_with_plan, plan: :premium_plan) } - let_it_be(:ultimate_namespace) { create(:namespace_with_plan, plan: :ultimate_plan) } + let_it_be(:free_namespace) { create(:group_with_plan, plan: :free_plan) } + let_it_be(:premium_namespace) { create(:group_with_plan, plan: :premium_plan) } + let_it_be(:ultimate_namespace) { create(:group_with_plan, plan: :ultimate_plan) } it 'returns namespaces with the specified plan names' do result = described_class.in_specific_plans(%w[ultimate premium]) @@ -414,6 +386,14 @@ expect(result).to contain_exactly(ultimate_namespace) end + + it 'does not return subgroups' do + subgroup = create(:group, parent: ultimate_namespace) + + result = described_class.id_in(subgroup).in_specific_plans([nil]) + + expect(result).to be_empty + end end describe '.not_duo_pro_or_no_add_on', :saas do @@ -486,42 +466,6 @@ end end - describe '.eligible_for_trial', :saas do - let_it_be(:namespace) { create :namespace } - - subject { described_class.eligible_for_trial } - - context 'when there is no subscription' do - it { is_expected.to contain_exactly(namespace) } - end - - context 'when there is a subscription' do - context 'with a plan that is eligible for a trial' do - where(plan: ::Plan::PLANS_ELIGIBLE_FOR_TRIAL) - - with_them do - before do - create :gitlab_subscription, plan, namespace: namespace - end - - it { is_expected.to contain_exactly(namespace) } - end - end - - context 'with a plan that is ineligible for a trial' do - where(plan: ::Plan::PAID_HOSTED_PLANS.without(::Plan::PREMIUM)) - - with_them do - before do - create :gitlab_subscription, plan, namespace: namespace - end - - it { is_expected.to be_empty } - end - end - end - end - describe '.namespace_settings_with_ai_features_enabled' do subject { described_class.namespace_settings_with_ai_features_enabled }