Commit 5993cf43 authored by Mark Chao's avatar Mark Chao

Update seats in used and owed during cron update

parent becb7685
......@@ -17,16 +17,20 @@ class UpdateMaxSeatsUsedForGitlabComSubscriptionsWorker # rubocop:disable Scalab
subscriptions.each do |subscription|
seats_in_use = subscription.calculate_seats_in_use
max_seats_used = [subscription.max_seats_used, seats_in_use].max
subscription.max_seats_used = max_seats_used
seats_owed = subscription.calculate_seats_owed
next if subscription.max_seats_used >= seats_in_use
tuples << [subscription.id, seats_in_use]
tuples << [subscription.id, max_seats_used, seats_in_use, seats_owed]
end
if tuples.present?
GitlabSubscription.connection.execute <<-EOF
UPDATE gitlab_subscriptions AS s SET max_seats_used = v.max_seats_used
FROM (VALUES #{tuples.map { |tuple| "(#{tuple.join(', ')})" }.join(', ')}) AS v(id, max_seats_used)
UPDATE gitlab_subscriptions AS s
SET max_seats_used = v.max_seats_used,
seats_in_use = v.seats_in_use,
seats_owed = v.seats_owed
FROM (VALUES #{tuples.map { |tuple| "(#{tuple.join(', ')})" }.join(', ')}) AS v(id, max_seats_used, seats_in_use, seats_owed)
WHERE s.id = v.id
EOF
end
......
......@@ -5,11 +5,11 @@ require 'spec_helper'
RSpec.describe UpdateMaxSeatsUsedForGitlabComSubscriptionsWorker do
subject { described_class.new }
let!(:user) { create(:user) }
let!(:group) { create(:group) }
let!(:bronze_plan) { create(:bronze_plan) }
let!(:early_adopter_plan) { create(:early_adopter_plan) }
let!(:gitlab_subscription) { create(:gitlab_subscription, namespace: group) }
let_it_be(:user) { create(:user) }
let_it_be(:group) { create(:group) }
let_it_be(:bronze_plan) { create(:bronze_plan) }
let_it_be(:early_adopter_plan) { create(:early_adopter_plan) }
let_it_be(:gitlab_subscription, refind: true) { create(:gitlab_subscription, namespace: group, seats: 1) }
let(:db_is_read_only) { false }
let(:subscription_attrs) { nil }
......@@ -56,19 +56,41 @@ RSpec.describe UpdateMaxSeatsUsedForGitlabComSubscriptionsWorker do
include_examples 'keeps original max_seats_used value'
end
context 'with a paid plan' do
context 'with a paid plan', :aggregate_failures do
let_it_be(:other_user) { create(:user) }
let_it_be(:other_group) { create(:group) }
let_it_be(:other_gitlab_subscription, refind: true) { create(:gitlab_subscription, namespace: other_group, seats: 1) }
before do
group.add_developer(other_user)
other_group.add_developer(other_user)
gitlab_subscription.update!(hosted_plan: bronze_plan)
other_gitlab_subscription.update!(hosted_plan: bronze_plan)
end
it 'only updates max_seats_used if active users count is greater than it' do
expect { subject.perform }.to change { gitlab_subscription.reload.max_seats_used }.to(1)
expect do
subject.perform
gitlab_subscription.reload
other_gitlab_subscription.reload
end.to change(gitlab_subscription, :max_seats_used).from(0).to(2)
.and change(gitlab_subscription, :seats_in_use).from(0).to(2)
.and change(gitlab_subscription, :seats_owed).from(0).to(1)
.and change(other_gitlab_subscription, :max_seats_used).from(0).to(1)
.and change(other_gitlab_subscription, :seats_in_use).from(0).to(1)
.and not_change(other_gitlab_subscription, :seats_owed).from(0)
end
it 'does not update max_seats_used if active users count is lower than it' do
gitlab_subscription.update_attribute(:max_seats_used, 5)
expect { subject.perform }.not_to change { gitlab_subscription.reload.max_seats_used }
gitlab_subscription.update_column(:max_seats_used, 5)
expect do
subject.perform
gitlab_subscription.reload
end.to change(gitlab_subscription, :seats_in_use).from(0).to(2)
.and change(gitlab_subscription, :seats_owed).from(0).to(4)
.and not_change(gitlab_subscription, :max_seats_used).from(5)
end
end
end
......
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