diff --git a/app/helpers/billing_helper.rb b/app/helpers/billing_helper.rb new file mode 100644 index 000000000..26669e38d --- /dev/null +++ b/app/helpers/billing_helper.rb @@ -0,0 +1,21 @@ +module BillingHelper + private + + def default_plan?(account) + installation_config = InstallationConfig.find_by(name: 'CHATWOOT_CLOUD_PLANS') + default_plan = installation_config&.value&.first + + # Return false if not plans are configured, so that no checks are enforced + return false if default_plan.blank? + + account.custom_attributes['plan_name'].nil? || account.custom_attributes['plan_name'] == default_plan['name'] + end + + def conversations_this_month(account) + account.conversations.where('created_at > ?', 30.days.ago).count + end + + def non_web_inboxes(account) + account.inboxes.where.not(channel_type: Channel::WebWidget.to_s).count + end +end diff --git a/app/policies/account_policy.rb b/app/policies/account_policy.rb index dcce5028a..a74a13a66 100644 --- a/app/policies/account_policy.rb +++ b/app/policies/account_policy.rb @@ -7,6 +7,10 @@ class AccountPolicy < ApplicationPolicy @account_user.administrator? || @account_user.agent? end + def limits? + @account_user.administrator? + end + def update? @account_user.administrator? end diff --git a/app/views/api/v1/models/_account.json.jbuilder b/app/views/api/v1/models/_account.json.jbuilder index da5ee25dd..9f89a5510 100644 --- a/app/views/api/v1/models/_account.json.jbuilder +++ b/app/views/api/v1/models/_account.json.jbuilder @@ -4,6 +4,8 @@ if resource.custom_attributes.present? json.custom_attributes do json.plan_name resource.custom_attributes['plan_name'] json.subscribed_quantity resource.custom_attributes['subscribed_quantity'] + json.subscription_status resource.custom_attributes['subscription_status'] + json.subscription_ends_on resource.custom_attributes['subscription_ends_on'] end end json.domain @account.domain diff --git a/config/routes.rb b/config/routes.rb index 9d862da2e..ab1fc4115 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -286,6 +286,7 @@ Rails.application.routes.draw do member do post :checkout post :subscription + get :limits end end end diff --git a/enterprise/app/controllers/enterprise/api/v1/accounts_controller.rb b/enterprise/app/controllers/enterprise/api/v1/accounts_controller.rb index b648b348c..f26616f29 100644 --- a/enterprise/app/controllers/enterprise/api/v1/accounts_controller.rb +++ b/enterprise/app/controllers/enterprise/api/v1/accounts_controller.rb @@ -1,6 +1,8 @@ class Enterprise::Api::V1::AccountsController < Api::BaseController + include BillingHelper before_action :fetch_account before_action :check_authorization + before_action :check_cloud_env, only: [:limits] def subscription if stripe_customer_id.blank? && @account.custom_attributes['is_creating_customer'].blank? @@ -10,12 +12,40 @@ class Enterprise::Api::V1::AccountsController < Api::BaseController head :no_content end + def limits + limits = { + 'conversation' => {}, + 'non_web_inboxes' => {} + } + + if default_plan?(@account) + limits = { + 'conversation' => { + 'allowed' => 500, + 'consumed' => conversations_this_month(@account) + }, + 'non_web_inboxes' => { + 'allowed' => 0, + 'consumed' => non_web_inboxes(@account) + } + } + end + + # include id in response to ensure that the store can be updated on the frontend + render json: { id: @account.id, limits: limits }, status: :ok + end + def checkout return create_stripe_billing_session(stripe_customer_id) if stripe_customer_id.present? render_invalid_billing_details end + def check_cloud_env + installation_config = InstallationConfig.find_by(name: 'DEPLOYMENT_ENV') + render json: { error: 'Not found' }, status: :not_found unless installation_config&.value == 'cloud' + end + private def fetch_account diff --git a/enterprise/app/services/enterprise/billing/handle_stripe_event_service.rb b/enterprise/app/services/enterprise/billing/handle_stripe_event_service.rb index e9101b22b..fecc52e03 100644 --- a/enterprise/app/services/enterprise/billing/handle_stripe_event_service.rb +++ b/enterprise/app/services/enterprise/billing/handle_stripe_event_service.rb @@ -18,16 +18,25 @@ class Enterprise::Billing::HandleStripeEventService # skipping self hosted plan events return if plan.blank? || account.blank? + update_account_attributes(subscription, plan) + + change_plan_features + end + + def update_account_attributes(subscription, plan) + # https://stripe.com/docs/api/subscriptions/object + account.update( custom_attributes: { stripe_customer_id: subscription.customer, stripe_price_id: subscription['plan']['id'], stripe_product_id: subscription['plan']['product'], plan_name: plan['name'], - subscribed_quantity: subscription['quantity'] + subscribed_quantity: subscription['quantity'], + subscription_status: subscription['status'], + subscription_ends_on: Time.zone.at(subscription['current_period_end']) } ) - change_plan_features end def process_subscription_deleted diff --git a/spec/enterprise/controllers/enterprise/api/v1/accounts_controller_spec.rb b/spec/enterprise/controllers/enterprise/api/v1/accounts_controller_spec.rb index 354aa5a50..2af04f9b1 100644 --- a/spec/enterprise/controllers/enterprise/api/v1/accounts_controller_spec.rb +++ b/spec/enterprise/controllers/enterprise/api/v1/accounts_controller_spec.rb @@ -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 diff --git a/spec/enterprise/services/enterprise/billing/handle_stripe_event_service_spec.rb b/spec/enterprise/services/enterprise/billing/handle_stripe_event_service_spec.rb index 0f9a932b1..08f37286a 100644 --- a/spec/enterprise/services/enterprise/billing/handle_stripe_event_service_spec.rb +++ b/spec/enterprise/services/enterprise/billing/handle_stripe_event_service_spec.rb @@ -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') diff --git a/spec/helpers/billing_helper_spec.rb b/spec/helpers/billing_helper_spec.rb new file mode 100644 index 000000000..c5c1cc237 --- /dev/null +++ b/spec/helpers/billing_helper_spec.rb @@ -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