diff --git a/enterprise/app/models/enterprise/account/plan_usage_and_limits.rb b/enterprise/app/models/enterprise/account/plan_usage_and_limits.rb index ce03efa41..92d8410c4 100644 --- a/enterprise/app/models/enterprise/account/plan_usage_and_limits.rb +++ b/enterprise/app/models/enterprise/account/plan_usage_and_limits.rb @@ -15,9 +15,9 @@ module Enterprise::Account::PlanUsageAndLimits } end - def increment_response_usage + def increment_response_usage(credits: 1) current_usage = custom_attributes[CAPTAIN_RESPONSES_USAGE].to_i || 0 - custom_attributes[CAPTAIN_RESPONSES_USAGE] = current_usage + 1 + custom_attributes[CAPTAIN_RESPONSES_USAGE] = current_usage + credits save end diff --git a/spec/enterprise/models/enterprise/account/plan_usage_and_limits_spec.rb b/spec/enterprise/models/enterprise/account/plan_usage_and_limits_spec.rb new file mode 100644 index 000000000..31f8820f6 --- /dev/null +++ b/spec/enterprise/models/enterprise/account/plan_usage_and_limits_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Enterprise::Account::PlanUsageAndLimits do + let(:account) { create(:account) } + + describe '#increment_response_usage' do + it 'increments usage by 1 by default' do + initial_usage = account.custom_attributes['captain_responses_usage'].to_i + account.increment_response_usage + account.reload + expect(account.custom_attributes['captain_responses_usage']).to eq(initial_usage + 1) + end + + it 'increments usage by the specified credits' do + initial_usage = account.custom_attributes['captain_responses_usage'].to_i + account.increment_response_usage(credits: 3) + account.reload + expect(account.custom_attributes['captain_responses_usage']).to eq(initial_usage + 3) + end + + it 'handles zero initial usage' do + account.custom_attributes['captain_responses_usage'] = nil + account.save! + account.increment_response_usage(credits: 2) + account.reload + expect(account.custom_attributes['captain_responses_usage']).to eq(2) + end + end +end