mirror of
https://github.com/chatwoot/chatwoot.git
synced 2026-06-04 21:02:35 +08:00
# Pull Request Template ## Description fixes: https://linear.app/chatwoot/issue/AI-151/captains-super-admin-config-dont-get-applied-into-rails-without ## Type of change - [x] Bug fix (non-breaking change which fixes an issue) ## How Has This Been Tested? Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration. specs and locally To test locally: go to super admin -> settings -> captain -> Change endpoint to something incorrect go to local app -> captain -> playground -> try chatting (should fail due to incorrect endpoint) now in super admin captain settings, set the correct endpoint then chat in playground. Now it should work. Current develop code doesn't reflect the changes in installation config for captain instantly, needs a server restart. ## Checklist: - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my code - [x] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [x] Any dependent changes have been merged and published in downstream modules
79 lines
3.2 KiB
Ruby
79 lines
3.2 KiB
Ruby
class SuperAdmin::AppConfigsController < SuperAdmin::ApplicationController
|
|
before_action :set_config
|
|
before_action :allowed_configs
|
|
def show
|
|
# ref: https://github.com/rubocop/rubocop/issues/7767
|
|
# rubocop:disable Style/HashTransformValues
|
|
@app_config = InstallationConfig.where(name: @allowed_configs)
|
|
.pluck(:name, :serialized_value)
|
|
.map { |name, serialized_value| [name, serialized_value['value']] }
|
|
.to_h
|
|
# rubocop:enable Style/HashTransformValues
|
|
@installation_configs = ConfigLoader.new.general_configs.each_with_object({}) do |config_hash, result|
|
|
result[config_hash['name']] = config_hash.except('name')
|
|
end
|
|
end
|
|
|
|
def create
|
|
errors = []
|
|
params['app_config'].each do |key, value|
|
|
next unless @allowed_configs.include?(key)
|
|
|
|
i = InstallationConfig.where(name: key).first_or_create(value: value, locked: false)
|
|
i.value = value
|
|
errors.concat(i.errors.full_messages) unless i.save
|
|
end
|
|
|
|
if errors.any?
|
|
redirect_to super_admin_app_config_path(config: @config), alert: errors.join(', ')
|
|
else
|
|
redirect_to super_admin_settings_path, flash: success_flash
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def set_config
|
|
@config = params[:config] || 'general'
|
|
end
|
|
|
|
def allowed_configs
|
|
mapping = {
|
|
'facebook' => %w[FB_APP_ID FB_VERIFY_TOKEN FB_APP_SECRET IG_VERIFY_TOKEN FACEBOOK_API_VERSION ENABLE_MESSENGER_CHANNEL_HUMAN_AGENT],
|
|
'shopify' => %w[SHOPIFY_CLIENT_ID SHOPIFY_CLIENT_SECRET],
|
|
'microsoft' => %w[AZURE_APP_ID AZURE_APP_SECRET],
|
|
'email' => %w[MAILER_INBOUND_EMAIL_DOMAIN ACCOUNT_EMAILS_LIMIT ACCOUNT_EMAILS_PLAN_LIMITS],
|
|
'linear' => %w[LINEAR_CLIENT_ID LINEAR_CLIENT_SECRET],
|
|
'slack' => %w[SLACK_CLIENT_ID SLACK_CLIENT_SECRET],
|
|
'instagram' => %w[INSTAGRAM_APP_ID INSTAGRAM_APP_SECRET INSTAGRAM_VERIFY_TOKEN INSTAGRAM_API_VERSION ENABLE_INSTAGRAM_CHANNEL_HUMAN_AGENT],
|
|
'tiktok' => %w[TIKTOK_APP_ID TIKTOK_APP_SECRET TIKTOK_API_VERSION],
|
|
'whatsapp_embedded' => %w[WHATSAPP_APP_ID WHATSAPP_APP_SECRET WHATSAPP_CONFIGURATION_ID WHATSAPP_API_VERSION],
|
|
'notion' => %w[NOTION_CLIENT_ID NOTION_CLIENT_SECRET],
|
|
'google' => %w[GOOGLE_OAUTH_CLIENT_ID GOOGLE_OAUTH_CLIENT_SECRET GOOGLE_OAUTH_REDIRECT_URI ENABLE_GOOGLE_OAUTH_LOGIN],
|
|
'captain' => %w[CAPTAIN_OPEN_AI_API_KEY CAPTAIN_OPEN_AI_MODEL CAPTAIN_OPEN_AI_ENDPOINT]
|
|
}
|
|
|
|
@allowed_configs = mapping.fetch(
|
|
@config,
|
|
%w[ENABLE_ACCOUNT_SIGNUP FIREBASE_PROJECT_ID FIREBASE_CREDENTIALS WEBHOOK_TIMEOUT MAXIMUM_FILE_UPLOAD_SIZE WIDGET_TOKEN_EXPIRY]
|
|
)
|
|
end
|
|
|
|
def success_notice
|
|
message = "#{@config.titleize} settings updated successfully"
|
|
return message unless restart_required_config_saved?
|
|
|
|
"#{message.delete_suffix('.')}. Restart Chatwoot web and worker processes to apply this change everywhere."
|
|
end
|
|
|
|
def success_flash
|
|
restart_required_config_saved? ? { success: success_notice } : { notice: success_notice }
|
|
end
|
|
|
|
def restart_required_config_saved?
|
|
params.fetch('app_config', {}).keys.intersect?(InstallationConfig::RESTART_REQUIRED_CONFIG_KEYS)
|
|
end
|
|
end
|
|
|
|
SuperAdmin::AppConfigsController.prepend_mod_with('SuperAdmin::AppConfigsController')
|