chore: Reorganize the installation config settings (#8794)

- Reorganizing installation config settings to move more configurations into UI from environment variables
- Changes to installation config to support premium plans in the enterprise edition
- Fixes the broken premium indicator in account/show and accounts/edit page
This commit is contained in:
Sojan Jose
2024-01-31 16:48:42 +04:00
committed by GitHub
parent ee3f734b7b
commit 390cd756e8
20 changed files with 398 additions and 62 deletions

View File

@@ -0,0 +1,60 @@
class Internal::ReconcilePlanConfigService
def perform
remove_premium_config_reset_warning
return if ChatwootHub.pricing_plan != 'community'
create_premium_config_reset_warning if premium_config_reset_required?
# We will have this enabled in the future
# reconcile_premium_config
reconcile_premium_features
end
private
def config_path
@config_path ||= Rails.root.join('enterprise/config')
end
def premium_config
@premium_config ||= YAML.safe_load(File.read("#{config_path}/premium_installation_config.yml")).freeze
end
def remove_premium_config_reset_warning
Redis::Alfred.delete(Redis::Alfred::CHATWOOT_INSTALLATION_CONFIG_RESET_WARNING)
end
def create_premium_config_reset_warning
Redis::Alfred.set(Redis::Alfred::CHATWOOT_INSTALLATION_CONFIG_RESET_WARNING, true)
end
def premium_config_reset_required?
premium_config.any? do |config|
config = config.with_indifferent_access
existing_config = InstallationConfig.find_by(name: config[:name])
existing_config&.value != config[:value] if existing_config.present?
end
end
def reconcile_premium_config
premium_config.each do |config|
new_config = config.with_indifferent_access
existing_config = InstallationConfig.find_by(name: new_config[:name])
next if existing_config&.value == new_config[:value]
existing_config&.update!(value: new_config[:value])
end
end
def premium_features
@premium_features ||= YAML.safe_load(File.read("#{config_path}/premium_features.yml")).freeze
end
def reconcile_premium_features
Account.find_in_batches do |accounts|
accounts.each do |account|
account.disable_features!(*premium_features)
end
end
end
end