feat: allow increment response to accept credits

This commit is contained in:
Shivam Mishra
2026-01-13 19:35:21 +05:30
parent eee6ccc6f5
commit 8d03354a4c
2 changed files with 33 additions and 2 deletions
@@ -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
@@ -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