2019-01-16 16:28:25 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# API Controller for resources of type Setting
|
2016-03-23 18:39:41 +01:00
|
|
|
class API::SettingsController < API::ApiController
|
2020-01-22 11:53:40 +01:00
|
|
|
before_action :authenticate_user!, only: %i[update bulk_update reset]
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
def index
|
2020-06-08 15:08:07 +02:00
|
|
|
@settings = policy_scope(Setting.where(name: names_as_string_to_array))
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
authorize Setting
|
2016-08-17 12:49:52 +02:00
|
|
|
@setting = Setting.find_or_initialize_by(name: params[:name])
|
2020-04-07 17:53:19 +02:00
|
|
|
render status: :not_modified and return if setting_params[:value] == @setting.value
|
2022-11-22 17:43:19 +01:00
|
|
|
render status: :locked, json: { error: I18n.t('settings.locked_setting') } and return unless SettingService.update_allowed?(@setting)
|
2020-04-07 17:53:19 +02:00
|
|
|
|
2023-02-17 16:56:17 +01:00
|
|
|
error = SettingService.check_before_update({ name: params[:name], value: setting_params[:value] })
|
|
|
|
render status: :unprocessable_entity, json: { error: error } and return if error
|
|
|
|
|
2023-02-20 17:12:38 +01:00
|
|
|
if SettingService.save_and_update(@setting, setting_params[:value], current_user)
|
2022-11-22 17:43:19 +01:00
|
|
|
SettingService.run_after_update([@setting])
|
2016-03-23 18:39:41 +01:00
|
|
|
render status: :ok
|
|
|
|
else
|
|
|
|
render json: @setting.errors.full_messages, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-16 14:39:47 +02:00
|
|
|
def bulk_update
|
|
|
|
authorize Setting
|
|
|
|
|
|
|
|
@settings = []
|
2023-01-24 16:48:05 +01:00
|
|
|
updated_settings = []
|
2022-10-12 16:48:39 +02:00
|
|
|
may_transaction params[:transactional] do
|
2021-06-10 10:39:42 +02:00
|
|
|
params[:settings].each do |setting|
|
2023-01-27 17:31:16 +01:00
|
|
|
next if !setting[:name] || !setting[:value] || setting[:value].blank?
|
2021-06-10 10:39:42 +02:00
|
|
|
|
|
|
|
db_setting = Setting.find_or_initialize_by(name: setting[:name])
|
2023-02-17 16:56:17 +01:00
|
|
|
if SettingService.update_allowed?(db_setting)
|
|
|
|
error = SettingService.check_before_update(setting)
|
|
|
|
if error
|
|
|
|
db_setting.errors.add(:-, "#{I18n.t("settings.#{setting[:name]}")}: #{error}")
|
2023-02-20 17:12:38 +01:00
|
|
|
elsif db_setting.value != setting[:value] && SettingService.save_and_update(db_setting, setting[:value], current_user)
|
|
|
|
updated_settings.push(db_setting)
|
2022-12-23 15:52:10 +01:00
|
|
|
end
|
2023-02-17 16:56:17 +01:00
|
|
|
else
|
|
|
|
db_setting.errors.add(:-, "#{I18n.t("settings.#{setting[:name]}")}: #{I18n.t('settings.locked_setting')}")
|
2021-06-10 10:39:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@settings.push db_setting
|
|
|
|
may_rollback(params[:transactional]) if db_setting.errors.keys.count.positive?
|
2021-03-30 15:56:36 +02:00
|
|
|
end
|
2019-09-16 14:39:47 +02:00
|
|
|
end
|
2023-01-24 16:48:05 +01:00
|
|
|
SettingService.run_after_update(updated_settings)
|
2019-09-16 14:39:47 +02:00
|
|
|
end
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
def show
|
2020-06-08 15:08:07 +02:00
|
|
|
authorize SettingContext.new(params[:name])
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
@setting = Setting.find_or_create_by(name: params[:name])
|
2019-01-14 15:00:33 +01:00
|
|
|
@show_history = params[:history] == 'true' && current_user.admin?
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
|
|
|
|
2020-06-08 15:08:07 +02:00
|
|
|
def test_present
|
|
|
|
authorize SettingContext.new(params[:name])
|
|
|
|
|
|
|
|
@setting = Setting.get(params[:name])
|
|
|
|
end
|
|
|
|
|
2020-01-22 11:53:40 +01:00
|
|
|
def reset
|
|
|
|
authorize Setting
|
|
|
|
|
|
|
|
setting = Setting.find_or_create_by(name: params[:name])
|
2022-11-22 17:43:19 +01:00
|
|
|
render status: :locked, json: { error: 'locked setting' } and return unless SettingService.update_allowed?(setting)
|
2020-06-15 13:20:48 +02:00
|
|
|
|
2020-01-22 11:53:40 +01:00
|
|
|
first_val = setting.history_values.order(created_at: :asc).limit(1).first
|
|
|
|
new_val = HistoryValue.create!(
|
|
|
|
setting_id: setting.id,
|
2022-10-12 16:48:39 +02:00
|
|
|
value: first_val&.value,
|
2020-01-22 11:53:40 +01:00
|
|
|
invoicing_profile_id: current_user.invoicing_profile.id
|
|
|
|
)
|
2022-11-22 17:43:19 +01:00
|
|
|
SettingService.run_after_update([setting])
|
2020-01-22 11:53:40 +01:00
|
|
|
render json: new_val, status: :ok
|
|
|
|
end
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
private
|
|
|
|
|
2018-12-17 16:02:02 +01:00
|
|
|
def setting_params
|
|
|
|
params.require(:setting).permit(:value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def names_as_string_to_array
|
|
|
|
params[:names][1..-2].split(',').map(&:strip).map { |param| param[1..-2] }.map(&:strip)
|
|
|
|
end
|
2021-06-10 10:39:42 +02:00
|
|
|
|
|
|
|
# run the given block in a transaction if `should` is true. Just run it normally otherwise
|
2022-10-12 16:48:39 +02:00
|
|
|
def may_transaction(should, &block)
|
2021-06-10 10:39:42 +02:00
|
|
|
if should == 'true'
|
2022-10-12 16:48:39 +02:00
|
|
|
ActiveRecord::Base.transaction(&block)
|
2021-06-10 10:39:42 +02:00
|
|
|
else
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# rollback the current DB transaction if `should` is true
|
|
|
|
def may_rollback(should)
|
|
|
|
raise ActiveRecord::Rollback if should == 'true'
|
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|