iachat/enterprise/app/services/internal/reconcile_plan_config_service.rb
Gabriel Jablonski 52a55827c3 chore: lint files (#2)
* chore: lint files

* chore: suppress warning

* chore: disable suggest extensions

* chore: do not stage changes in pre-commit

* chore: remove git add from FE lint and `-a` flag from rubocop on husky
2025-04-03 23:28:38 -03:00

60 lines
1.7 KiB
Ruby

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?
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("#{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("#{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