Files
chatwoot/app/controllers/api/v1/accounts/accounts_controller.rb
Sojan Jose bab9d663d2 Feature: API for updating account settings (#645)
* Feature: API for updating account settings

- API to update account locale
- API to update account name
- API to show account info
2020-03-29 12:16:31 +05:30

55 lines
1.5 KiB
Ruby

class Api::V1::Accounts::AccountsController < Api::BaseController
include AuthHelper
skip_before_action :verify_authenticity_token, only: [:create]
skip_before_action :authenticate_user!, :set_current_user, :check_subscription, :handle_with_exception,
only: [:create], raise: false
before_action :check_signup_enabled, only: [:create]
before_action :check_authorization, except: [:create]
before_action :fetch_account, except: [:create]
rescue_from CustomExceptions::Account::InvalidEmail,
CustomExceptions::Account::UserExists,
CustomExceptions::Account::UserErrors,
with: :render_error_response
def create
@user = AccountBuilder.new(
account_name: account_params[:account_name],
email: account_params[:email]
).perform
if @user
send_auth_headers(@user)
render 'devise/auth.json', locals: { resource: @user }
else
render_error_response(CustomExceptions::Account::SignupFailed.new({}))
end
end
def show
render 'api/v1/accounts/show.json'
end
def update
@account.update!(account_params.slice(:name, :locale))
end
private
def check_authorization
authorize(Account)
end
def fetch_account
@account = current_user.accounts.find(params[:id])
end
def account_params
params.permit(:account_name, :email, :name, :locale)
end
def check_signup_enabled
raise ActionController::RoutingError, 'Not Found' if ENV.fetch('ENABLE_ACCOUNT_SIGNUP', true) == 'false'
end
end