feat: Add APIs for limit check in accounts (#7242)
This commit is contained in:
@@ -110,4 +110,99 @@ RSpec.describe 'Enterprise Billing APIs', type: :request do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /enterprise/api/v1/accounts/{account.id}/limits' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
get "/enterprise/api/v1/accounts/#{account.id}/limits", as: :json
|
||||
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
context 'when it is an agent' do
|
||||
it 'returns unauthorized' do
|
||||
get "/enterprise/api/v1/accounts/#{account.id}/limits",
|
||||
headers: agent.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an admin' do
|
||||
before do
|
||||
create(:conversation, account: account)
|
||||
create(:channel_api, account: account)
|
||||
InstallationConfig.where(name: 'DEPLOYMENT_ENV').first_or_create(value: 'cloud')
|
||||
InstallationConfig.where(name: 'CHATWOOT_CLOUD_PLANS').first_or_create(value: [{ 'name': 'Hacker' }])
|
||||
end
|
||||
|
||||
it 'returns the limits if the plan is default' do
|
||||
account.update!(custom_attributes: { plan_name: 'Hacker' })
|
||||
get "/enterprise/api/v1/accounts/#{account.id}/limits",
|
||||
headers: admin.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expected_response = {
|
||||
'id' => account.id,
|
||||
'limits' => {
|
||||
'conversation' => {
|
||||
'allowed' => 500,
|
||||
'consumed' => 1
|
||||
},
|
||||
'non_web_inboxes' => {
|
||||
'allowed' => 0,
|
||||
'consumed' => 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(JSON.parse(response.body)).to eq(expected_response)
|
||||
end
|
||||
|
||||
it 'returns nil if the plan is not default' do
|
||||
account.update!(custom_attributes: { plan_name: 'Startups' })
|
||||
get "/enterprise/api/v1/accounts/#{account.id}/limits",
|
||||
headers: admin.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expected_response = {
|
||||
'id' => account.id,
|
||||
'limits' => {
|
||||
'conversation' => {},
|
||||
'non_web_inboxes' => {}
|
||||
}
|
||||
}
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(JSON.parse(response.body)).to eq(expected_response)
|
||||
end
|
||||
|
||||
it 'returns limits if a plan is not configured' do
|
||||
get "/enterprise/api/v1/accounts/#{account.id}/limits",
|
||||
headers: admin.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expected_response = {
|
||||
'id' => account.id,
|
||||
'limits' => {
|
||||
'conversation' => {
|
||||
'allowed' => 500,
|
||||
'consumed' => 1
|
||||
},
|
||||
'non_web_inboxes' => {
|
||||
'allowed' => 0,
|
||||
'consumed' => 1
|
||||
}
|
||||
}
|
||||
}
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(JSON.parse(response.body)).to eq(expected_response)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -16,6 +16,8 @@ describe Enterprise::Billing::HandleStripeEventService do
|
||||
'id' => 'test', 'product' => 'plan_id', 'name' => 'plan_name'
|
||||
})
|
||||
allow(subscription).to receive(:[]).with('quantity').and_return('10')
|
||||
allow(subscription).to receive(:[]).with('status').and_return('active')
|
||||
allow(subscription).to receive(:[]).with('current_period_end').and_return(1_686_567_520)
|
||||
allow(subscription).to receive(:customer).and_return('cus_123')
|
||||
create(:installation_config, {
|
||||
name: 'CHATWOOT_CLOUD_PLANS',
|
||||
@@ -44,7 +46,9 @@ describe Enterprise::Billing::HandleStripeEventService do
|
||||
'stripe_price_id' => 'test',
|
||||
'stripe_product_id' => 'plan_id',
|
||||
'plan_name' => 'Hacker',
|
||||
'subscribed_quantity' => '10'
|
||||
'subscribed_quantity' => '10',
|
||||
'subscription_ends_on' => '2023-06-12T10:58:40.000Z',
|
||||
'subscription_status' => 'active'
|
||||
})
|
||||
end
|
||||
|
||||
@@ -57,7 +61,9 @@ describe Enterprise::Billing::HandleStripeEventService do
|
||||
'stripe_price_id' => 'test',
|
||||
'stripe_product_id' => 'plan_id',
|
||||
'plan_name' => 'Hacker',
|
||||
'subscribed_quantity' => '10'
|
||||
'subscribed_quantity' => '10',
|
||||
'subscription_ends_on' => '2023-06-12T10:58:40.000Z',
|
||||
'subscription_status' => 'active'
|
||||
})
|
||||
expect(account).not_to be_feature_enabled('channel_email')
|
||||
expect(account).not_to be_feature_enabled('help_center')
|
||||
@@ -94,7 +100,9 @@ describe Enterprise::Billing::HandleStripeEventService do
|
||||
'stripe_price_id' => 'test',
|
||||
'stripe_product_id' => 'plan_id_2',
|
||||
'plan_name' => 'Startups',
|
||||
'subscribed_quantity' => '10'
|
||||
'subscribed_quantity' => '10',
|
||||
'subscription_ends_on' => '2023-06-12T10:58:40.000Z',
|
||||
'subscription_status' => 'active'
|
||||
})
|
||||
expect(account).to be_feature_enabled('channel_email')
|
||||
expect(account).to be_feature_enabled('help_center')
|
||||
|
||||
48
spec/helpers/billing_helper_spec.rb
Normal file
48
spec/helpers/billing_helper_spec.rb
Normal file
@@ -0,0 +1,48 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe BillingHelper do
|
||||
describe '#conversations_this_month' do
|
||||
let(:user) { create(:user) }
|
||||
let(:account) { create(:account, custom_attributes: { 'plan_name' => 'Hacker' }) }
|
||||
|
||||
before do
|
||||
create(:installation_config, {
|
||||
name: 'CHATWOOT_CLOUD_PLANS',
|
||||
value: [
|
||||
{
|
||||
'name' => 'Hacker',
|
||||
'product_id' => ['plan_id'],
|
||||
'price_ids' => ['price_1']
|
||||
},
|
||||
{
|
||||
'name' => 'Startups',
|
||||
'product_id' => ['plan_id_2'],
|
||||
'price_ids' => ['price_2']
|
||||
}
|
||||
]
|
||||
})
|
||||
end
|
||||
|
||||
it 'counts only the conversations created this month' do
|
||||
create_list(:conversation, 5, account: account, created_at: Time.zone.today - 1.day)
|
||||
create_list(:conversation, 3, account: account, created_at: 2.months.ago)
|
||||
expect(helper.send(:conversations_this_month, account)).to eq(5)
|
||||
end
|
||||
|
||||
it 'counts only non web widget channels' do
|
||||
create(:inbox, account: account, channel_type: Channel::WebWidget)
|
||||
expect(account.inboxes.count).to eq(1)
|
||||
expect(helper.send(:non_web_inboxes, account)).to eq(0)
|
||||
|
||||
create(:inbox, account: account, channel_type: Channel::Api)
|
||||
expect(account.inboxes.count).to eq(2)
|
||||
expect(helper.send(:non_web_inboxes, account)).to eq(1)
|
||||
end
|
||||
|
||||
it 'returns true for the default plan name' do
|
||||
expect(helper.send(:default_plan?, account)).to be(true)
|
||||
account.custom_attributes['plan_name'] = 'Startups'
|
||||
expect(helper.send(:default_plan?, account)).to be(false)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user