Some checks failed
Lock Threads / action (push) Has been cancelled
Mark stale issues and pull requests / stale (push) Has been cancelled
Publish Chatwoot EE docker images / build (linux/amd64, ubuntu-latest) (push) Has been cancelled
Publish Chatwoot EE docker images / build (linux/arm64, ubuntu-22.04-arm) (push) Has been cancelled
Publish Chatwoot EE docker images / merge (push) Has been cancelled
Publish Chatwoot CE docker images / build (linux/amd64, ubuntu-latest) (push) Has been cancelled
Publish Chatwoot CE docker images / build (linux/arm64, ubuntu-22.04-arm) (push) Has been cancelled
Publish Chatwoot CE docker images / merge (push) Has been cancelled
Run Chatwoot CE spec / lint-backend (push) Has been cancelled
Run Chatwoot CE spec / lint-frontend (push) Has been cancelled
Run Chatwoot CE spec / frontend-tests (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (0, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (1, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (10, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (11, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (12, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (13, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (14, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (15, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (2, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (3, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (4, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (5, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (6, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (7, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (8, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (9, 16) (push) Has been cancelled
Run Linux nightly installer / nightly (push) Has been cancelled
- Add Logistics component with progress tracking - Add OrderDetail component for order information - Support data-driven steps and actions - Add blue color scale to widget SCSS - Fix node overflow and progress bar rendering issues - Add English translations for dashboard components Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
114 lines
3.0 KiB
Ruby
114 lines
3.0 KiB
Ruby
class Api::V1::Accounts::AgentsController < Api::V1::Accounts::BaseController
|
|
before_action :fetch_agent, except: [:create, :index, :bulk_create]
|
|
before_action :check_authorization
|
|
before_action :validate_limit, only: [:create]
|
|
before_action :validate_limit_for_bulk_create, only: [:bulk_create]
|
|
|
|
def index
|
|
@agents = agents
|
|
end
|
|
|
|
def create
|
|
builder = AgentBuilder.new(
|
|
email: new_agent_params['email'],
|
|
name: new_agent_params['name'],
|
|
role: new_agent_params['role'],
|
|
availability: new_agent_params['availability'],
|
|
auto_offline: new_agent_params['auto_offline'],
|
|
inviter: current_user,
|
|
account: Current.account
|
|
)
|
|
|
|
@agent = builder.perform
|
|
end
|
|
|
|
def update
|
|
@agent.update!(agent_params.slice(:name).compact)
|
|
@agent.current_account_user.update!(agent_params.slice(*account_user_attributes).compact)
|
|
end
|
|
|
|
def destroy
|
|
@agent.current_account_user.destroy!
|
|
delete_user_record(@agent)
|
|
head :ok
|
|
end
|
|
|
|
def bulk_create
|
|
emails = params[:emails]
|
|
|
|
emails.each do |email|
|
|
builder = AgentBuilder.new(
|
|
email: email,
|
|
name: email.split('@').first,
|
|
inviter: current_user,
|
|
account: Current.account
|
|
)
|
|
begin
|
|
builder.perform
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
Rails.logger.info "[Agent#bulk_create] ignoring email #{email}, errors: #{e.record.errors}"
|
|
end
|
|
end
|
|
|
|
# This endpoint is used to bulk create agents during onboarding
|
|
# onboarding_step key in present in Current account custom attributes, since this is a one time operation
|
|
Current.account.custom_attributes.delete('onboarding_step')
|
|
Current.account.save!
|
|
head :ok
|
|
end
|
|
|
|
private
|
|
|
|
def check_authorization
|
|
super(User)
|
|
end
|
|
|
|
def fetch_agent
|
|
@agent = agents.find(params[:id])
|
|
end
|
|
|
|
def account_user_attributes
|
|
[:role, :availability, :auto_offline]
|
|
end
|
|
|
|
def allowed_agent_params
|
|
[:name, :email, :role, :availability, :auto_offline]
|
|
end
|
|
|
|
def agent_params
|
|
params.require(:agent).permit(allowed_agent_params)
|
|
end
|
|
|
|
def new_agent_params
|
|
params.require(:agent).permit(:email, :name, :role, :availability, :auto_offline)
|
|
end
|
|
|
|
def agents
|
|
@agents ||= Current.account.users.order_by_full_name.includes(:account_users, { avatar_attachment: [:blob] })
|
|
end
|
|
|
|
def validate_limit_for_bulk_create
|
|
limit_available = params[:emails].count <= available_agent_count
|
|
|
|
render_payment_required('Account limit exceeded. Please purchase more licenses') unless limit_available
|
|
end
|
|
|
|
def validate_limit
|
|
render_payment_required('Account limit exceeded. Please purchase more licenses') unless can_add_agent?
|
|
end
|
|
|
|
def available_agent_count
|
|
Current.account.usage_limits[:agents] - agents.count
|
|
end
|
|
|
|
def can_add_agent?
|
|
available_agent_count.positive?
|
|
end
|
|
|
|
def delete_user_record(agent)
|
|
DeleteObjectJob.perform_later(agent) if agent.reload.account_users.blank?
|
|
end
|
|
end
|
|
|
|
Api::V1::Accounts::AgentsController.prepend_mod_with('Api::V1::Accounts::AgentsController')
|