feat: Add APIs for limit check in accounts (#7242)

This commit is contained in:
Shivam Mishra
2023-06-16 08:41:40 +05:30
committed by GitHub
parent 0d465362ac
commit e8a27bea4b
9 changed files with 223 additions and 5 deletions

View File

@@ -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