Skip to content
代码片段 群组 项目
提交 0b0a7655 编辑于 作者: Adam Hegyi's avatar Adam Hegyi
浏览文件

Merge branch 'qzhao-handle-reset-for-ultimate-trial-paid-customer-plan-switch' into 'master'

Handle reset_seat_statistics for ultimate-trial-paid-customer plan

See merge request https://gitlab.com/gitlab-org/gitlab/-/merge_requests/137243



Merged-by: default avatarAdam Hegyi <ahegyi@gitlab.com>
Approved-by: default avatarPaulo Barros <pbarros@gitlab.com>
Reviewed-by: default avatarEtienne Baqué <ebaque@gitlab.com>
Reviewed-by: default avatarPaulo Barros <pbarros@gitlab.com>
Co-authored-by: default avatarPaulo Barros <pbarros@gitlab.com>
Co-authored-by: default avatarQingyu Zhao <qzhao@gitlab.com>
No related branches found
No related tags found
无相关合并请求
...@@ -173,6 +173,7 @@ def index_namespace ...@@ -173,6 +173,7 @@ def index_namespace
# If the subscription changes, we reset max_seats_used and seats_owed # If the subscription changes, we reset max_seats_used and seats_owed
# if they're out of date, so that we don't carry them over from the previous term/subscription. # if they're out of date, so that we don't carry them over from the previous term/subscription.
# One exception is the plan switch between `premium` and `ultimate_trial_paid_customer`.
def reset_seat_statistics def reset_seat_statistics
return unless reset_seat_statistics? return unless reset_seat_statistics?
...@@ -200,6 +201,7 @@ def new_term? ...@@ -200,6 +201,7 @@ def new_term?
end end
def reset_seat_statistics? def reset_seat_statistics?
return reset_involves_ultimate_trial_paid_customer_plan? if involves_ultimate_trial_paid_customer_plan?
return false unless has_a_paid_hosted_plan? return false unless has_a_paid_hosted_plan?
return true if new_term? return true if new_term?
return true if trial_changed? && !trial return true if trial_changed? && !trial
...@@ -207,6 +209,45 @@ def reset_seat_statistics? ...@@ -207,6 +209,45 @@ def reset_seat_statistics?
max_seats_used_changed_at.present? && max_seats_used_changed_at.to_date < start_date max_seats_used_changed_at.present? && max_seats_used_changed_at.to_date < start_date
end end
def ultimate_trial_paid_customer_plan_id
strong_memoize(:ultimate_trial_paid_customer_plan_id) do
::Plan.find_by(name: ::Plan::ULTIMATE_TRIAL_PAID_CUSTOMER)&.id
end
end
def premium_plan_id
strong_memoize(:premium_plan_id) do
::Plan.find_by(name: ::Plan::PREMIUM)&.id
end
end
def involves_ultimate_trial_paid_customer_plan?
return false unless ultimate_trial_paid_customer_plan_id
ultimate_trial_paid_customer_plan_id.in?([hosted_plan_id, hosted_plan_id_was])
end
def reset_involves_ultimate_trial_paid_customer_plan?
# Do not reset if plan unchanged on ultimate_trial_paid_customer_plan
return false unless hosted_plan_id_changed?
# Do not reset if switching to ultimate_trial_paid_customer_plan
return false if hosted_plan_id == ultimate_trial_paid_customer_plan_id
# Now hosted_plan_id_was must be ultimate_trial_paid_customer_plan_id
return false if hosted_plan_id == premium_plan_id && premium_plan_not_renewed?
# Either the premium plan was renewed, or it is upgrading to ultimate plan
true
end
def premium_plan_not_renewed?
previous_premium_gs = GitlabSubscriptionHistory
.where(gitlab_subscription_id: id, hosted_plan_id: premium_plan_id).order(:id).last
previous_premium_gs&.start_date == start_date && previous_premium_gs&.end_date == end_date
end
def tracked_attributes_changed? def tracked_attributes_changed?
changed.intersection(GitlabSubscriptionHistory::TRACKED_ATTRIBUTES).any? changed.intersection(GitlabSubscriptionHistory::TRACKED_ATTRIBUTES).any?
end end
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
RSpec.describe GitlabSubscription, :saas, feature_category: :subscription_management do RSpec.describe GitlabSubscription, :saas, feature_category: :subscription_management do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
%i[free_plan bronze_plan premium_plan ultimate_plan].each do |plan| # rubocop:disable RSpec/UselessDynamicDefinition -- `plan` used in `let_it_be` ['free', *Plan::PAID_HOSTED_PLANS].map { |name| "#{name}_plan" }.each do |plan| # rubocop:disable RSpec/UselessDynamicDefinition -- `plan` used in `let_it_be`
let_it_be(plan) { create(plan) } # rubocop:disable Rails/SaveBang let_it_be(plan) { create(plan) } # rubocop:disable Rails/SaveBang
end end
...@@ -231,7 +231,7 @@ ...@@ -231,7 +231,7 @@
end end
context 'with a trial plan' do context 'with a trial plan' do
let(:subscription_attrs) { { hosted_plan: bronze_plan, trial: true } } let(:subscription_attrs) { { hosted_plan: ultimate_trial_plan, trial: true } }
include_examples 'always returns a total of 0' include_examples 'always returns a total of 0'
end end
...@@ -337,23 +337,25 @@ ...@@ -337,23 +337,25 @@
subject { gitlab_subscription.seats_in_use } subject { gitlab_subscription.seats_in_use }
context 'with a paid hosted plan' do context 'with a paid hosted plan' do
let(:hosted_plan) { ultimate_plan } Plan::PAID_HOSTED_PLANS.each do |plan_name| # rubocop:disable RSpec/UselessDynamicDefinition -- `plan_name` used in `let`
let(:hosted_plan) { send("#{plan_name}_plan") }
it 'returns the previously calculated seats in use' do it "returns the previously calculated seats in use when plan is #{plan_name}" do
expect(subject).to eq(5) expect(subject).to eq(5)
end end
context 'when seats in use is 0' do context 'when seats in use is 0' do
let(:seats_in_use) { 0 } let(:seats_in_use) { 0 }
it 'returns 0 too' do it "returns 0 too when plan is #{plan_name}" do
expect(subject).to eq(0) expect(subject).to eq(0)
end
end end
end end
end end
context 'with a trial plan' do context 'with a trial plan' do
let(:hosted_plan) { ultimate_plan } let(:hosted_plan) { ultimate_trial_plan }
let(:trial) { true } let(:trial) { true }
it 'returns the current seats in use' do it 'returns the current seats in use' do
...@@ -806,6 +808,76 @@ ...@@ -806,6 +808,76 @@
it_behaves_like 'does not reset seat statistics' it_behaves_like 'does not reset seat statistics'
end end
end end
context 'when starts ultimate_trial_paid_customer plan' do
before do
gitlab_subscription.update!(hosted_plan: premium_plan)
end
it 'does not reset seat statistics' do
expect do
gitlab_subscription.update!(hosted_plan: ultimate_trial_paid_customer_plan)
end.to not_change(gitlab_subscription, :max_seats_used)
.and not_change(gitlab_subscription, :max_seats_used_changed_at)
.and not_change(gitlab_subscription, :seats_owed)
end
end
context 'when updates max_seats_used_changed_at during ultimate_trial_paid_customer plan' do
before do
gitlab_subscription.update!(hosted_plan: ultimate_trial_paid_customer_plan)
end
it 'does not reset seat statistics' do
expect do
gitlab_subscription.update!(max_seats_used_changed_at: Date.current)
end.to not_change(gitlab_subscription, :max_seats_used)
.and not_change(gitlab_subscription, :seats_owed)
end
end
context 'when switches back to premium plan from ultimate_trial_paid_customer plan' do
before do
gitlab_subscription.update!(hosted_plan: premium_plan)
gitlab_subscription.update!(hosted_plan: ultimate_trial_paid_customer_plan)
end
it 'does not reset seat statistics' do
expect do
gitlab_subscription.update!(hosted_plan: premium_plan)
end.to not_change(gitlab_subscription, :max_seats_used)
.and not_change(gitlab_subscription, :max_seats_used_changed_at)
.and not_change(gitlab_subscription, :seats_owed)
end
context 'when the premium_plan was renewed during the ultimate_trial_paid_customer trial' do
before do
gitlab_subscription.update!(start_date: Date.today + 2.years, end_date: Date.today + 3.years)
end
it 'resets seat statistics' do
expect do
gitlab_subscription.update!(hosted_plan: premium_plan)
end.to change(gitlab_subscription, :max_seats_used).from(42).to(1)
.and change(gitlab_subscription, :seats_owed).from(29).to(0)
.and change(gitlab_subscription, :seats_in_use).from(20).to(1)
end
end
end
context 'when upgrade to ultimate plan from ultimate_trial_paid_customer plan' do
before do
gitlab_subscription.update!(hosted_plan: ultimate_trial_paid_customer_plan)
end
it 'does not reset seat statistics' do
expect do
gitlab_subscription.update!(hosted_plan: ultimate_plan)
end.to change(gitlab_subscription, :max_seats_used).from(42).to(1)
.and change(gitlab_subscription, :seats_owed).from(29).to(0)
.and change(gitlab_subscription, :seats_in_use).from(20).to(1)
end
end
end end
context 'after_destroy_commit' do context 'after_destroy_commit' do
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册