## Description Add account delete option in the user account settings. Fixes #1555 ## Type of change - [ ] New feature (non-breaking change which adds functionality)   ## Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules --------- Co-authored-by: Sojan Jose <sojan@pepalo.com> Co-authored-by: Sojan Jose <sojan.official@gmail.com> Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
122 lines
3.4 KiB
Ruby
122 lines
3.4 KiB
Ruby
class Enterprise::Api::V1::AccountsController < Api::BaseController
|
|
include BillingHelper
|
|
before_action :fetch_account
|
|
before_action :check_authorization
|
|
before_action :check_cloud_env, only: [:limits, :toggle_deletion]
|
|
|
|
def subscription
|
|
if stripe_customer_id.blank? && @account.custom_attributes['is_creating_customer'].blank?
|
|
@account.update(custom_attributes: { is_creating_customer: true })
|
|
Enterprise::CreateStripeCustomerJob.perform_later(@account)
|
|
end
|
|
head :no_content
|
|
end
|
|
|
|
def limits
|
|
limits = if default_plan?(@account)
|
|
{
|
|
'conversation' => {
|
|
'allowed' => 500,
|
|
'consumed' => conversations_this_month(@account)
|
|
},
|
|
'non_web_inboxes' => {
|
|
'allowed' => 0,
|
|
'consumed' => non_web_inboxes(@account)
|
|
},
|
|
'agents' => {
|
|
'allowed' => 2,
|
|
'consumed' => agents(@account)
|
|
}
|
|
}
|
|
else
|
|
default_limits
|
|
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 toggle_deletion
|
|
action_type = params[:action_type]
|
|
|
|
case action_type
|
|
when 'delete'
|
|
mark_for_deletion
|
|
when 'undelete'
|
|
unmark_for_deletion
|
|
else
|
|
render json: { error: 'Invalid action_type. Must be either "delete" or "undelete"' }, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
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
|
|
|
|
def default_limits
|
|
{
|
|
'conversation' => {},
|
|
'non_web_inboxes' => {},
|
|
'agents' => {},
|
|
'captain' => @account.usage_limits[:captain]
|
|
}
|
|
end
|
|
|
|
def fetch_account
|
|
@account = current_user.accounts.find(params[:id])
|
|
@current_account_user = @account.account_users.find_by(user_id: current_user.id)
|
|
end
|
|
|
|
def stripe_customer_id
|
|
@account.custom_attributes['stripe_customer_id']
|
|
end
|
|
|
|
def mark_for_deletion
|
|
reason = 'manual_deletion'
|
|
|
|
if @account.mark_for_deletion(reason)
|
|
render json: { message: 'Account marked for deletion' }, status: :ok
|
|
else
|
|
render json: { message: @account.errors.full_messages.join(', ') }, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def unmark_for_deletion
|
|
if @account.unmark_for_deletion
|
|
render json: { message: 'Account unmarked for deletion' }, status: :ok
|
|
else
|
|
render json: { message: @account.errors.full_messages.join(', ') }, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def render_invalid_billing_details
|
|
render_could_not_create_error('Please subscribe to a plan before viewing the billing details')
|
|
end
|
|
|
|
def create_stripe_billing_session(customer_id)
|
|
session = Enterprise::Billing::CreateSessionService.new.create_session(customer_id)
|
|
render_redirect_url(session.url)
|
|
end
|
|
|
|
def render_redirect_url(redirect_url)
|
|
render json: { redirect_url: redirect_url }
|
|
end
|
|
|
|
def pundit_user
|
|
{
|
|
user: current_user,
|
|
account: @account,
|
|
account_user: @current_account_user
|
|
}
|
|
end
|
|
end
|