Commit fc8902d2 authored by Vijay Hawoldar's avatar Vijay Hawoldar

Only refresh max_seats_used and use DB value

parent 78dae45f
......@@ -71,9 +71,9 @@ class GitlabSubscription < ApplicationRecord
end
# Refresh seat related attribute (without persisting them)
def refresh_seat_attributes!(new_term_reset: false)
def refresh_seat_attributes!
self.seats_in_use = calculate_seats_in_use
self.max_seats_used = new_term_reset ? seats_in_use : [max_seats_used, seats_in_use].max
self.max_seats_used = [max_seats_used, seats_in_use].max
self.seats_owed = calculate_seats_owed
end
......@@ -179,12 +179,13 @@ class GitlabSubscription < ApplicationRecord
ElasticsearchIndexedNamespace.safe_find_or_create_by!(namespace_id: namespace_id)
end
# If the subscription starts a new term, the dates will change. We reset seat counts
# so that we don't carry over max_seats_used/owed from the previous term
# If the subscription starts a new term, the dates will change. We reset max_seats_used
# and seats_owed so that we don't carry it over from the previous term
def reset_seats_for_new_term
return unless new_term?
refresh_seat_attributes!(new_term_reset: true)
self.max_seats_used = attributes['seats_in_use']
self.seats_owed = calculate_seats_owed
end
def new_term?
......
......@@ -179,16 +179,6 @@ RSpec.describe GitlabSubscription, :saas do
.and not_change(gitlab_subscription, :max_seats_used)
.and not_change(gitlab_subscription, :seats_owed)
end
context 'when resetting for a new term' do
it 'sets max_seats_used to seats_in_use' do
expect do
gitlab_subscription.refresh_seat_attributes!(new_term_reset: true)
end.to change(gitlab_subscription, :seats_in_use).from(0).to(1)
.and change(gitlab_subscription, :max_seats_used).from(2).to(1)
.and not_change(gitlab_subscription, :seats_owed)
end
end
end
context 'when current seats in use is higher than seats and max_seats_used' do
......@@ -201,16 +191,6 @@ RSpec.describe GitlabSubscription, :saas do
.and change(gitlab_subscription, :max_seats_used).from(2).to(4)
.and change(gitlab_subscription, :seats_owed).from(0).to(1)
end
context 'when resetting for a new term' do
it 'sets max_seats_used to seats_in_use' do
expect do
gitlab_subscription.refresh_seat_attributes!(new_term_reset: true)
end.to change(gitlab_subscription, :seats_in_use).from(0).to(4)
.and change(gitlab_subscription, :max_seats_used).from(2).to(4)
.and change(gitlab_subscription, :seats_owed).from(0).to(1)
end
end
end
end
......@@ -454,7 +434,7 @@ RSpec.describe GitlabSubscription, :saas do
context 'before_update', :freeze_time do
let(:gitlab_subscription) do
create(:gitlab_subscription, max_seats_used: 42, seats: 13, seats_owed: 29, start_date: Date.today - 1.year)
create(:gitlab_subscription, seats_in_use: 20, max_seats_used: 42, seats: 13, seats_owed: 29, start_date: Date.today - 1.year)
end
it 'logs previous state to gitlab subscription history' do
......@@ -470,10 +450,6 @@ RSpec.describe GitlabSubscription, :saas do
end
context 'when start and end dates change' do
before do
allow(gitlab_subscription).to receive(:seats_in_use).and_return(20)
end
context 'when start_date is after the old end_date' do
it 'resets seats attributes' do
new_start = gitlab_subscription.end_date + 1.year
......@@ -485,6 +461,7 @@ RSpec.describe GitlabSubscription, :saas do
.and change(gitlab_subscription, :end_date).to(new_end)
.and change(gitlab_subscription, :max_seats_used).from(42).to(20)
.and change(gitlab_subscription, :seats_owed).from(29).to(7)
.and not_change(gitlab_subscription, :seats_in_use)
end
end
......@@ -501,6 +478,7 @@ RSpec.describe GitlabSubscription, :saas do
.and change(gitlab_subscription, :end_date).to(new_end)
.and change(gitlab_subscription, :max_seats_used).from(42).to(20)
.and change(gitlab_subscription, :seats_owed).from(29).to(7)
.and not_change(gitlab_subscription, :seats_in_use)
end
end
......
......@@ -685,7 +685,7 @@ RSpec.describe API::Namespaces do
gitlab_subscription.update!(seats: 20, max_seats_used: 42, seats_owed: 22)
new_start = gitlab_subscription.end_date + 1.year
new_end = gitlab_subscription.end_date + 1.year
new_end = new_start + 1.year
new_term_params = params.merge(start_date: new_start, end_date: new_end)
expect(gitlab_subscription.seats_in_use).to eq 0
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment