2024-04-08 17:56:53 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class CleanupUserSettingsJob < ApplicationJob
|
|
|
|
queue_as :default
|
|
|
|
|
|
|
|
def perform(record_type, record_id)
|
2024-07-30 15:31:21 +08:00
|
|
|
unless %w(task_step_states results_order result_states).include?(record_type)
|
|
|
|
raise ArgumentError, 'Invalid record_type'
|
|
|
|
end
|
2024-04-08 17:56:53 +08:00
|
|
|
|
|
|
|
sanitized_record_id = record_id.to_i.to_s
|
|
|
|
raise ArgumentError, 'Invalid record_id' unless sanitized_record_id == record_id.to_s
|
|
|
|
|
|
|
|
sql = <<-SQL.squish
|
|
|
|
UPDATE users
|
|
|
|
SET settings = (settings#>>'{}')::jsonb #- '{#{record_type},#{sanitized_record_id}}'
|
|
|
|
WHERE (settings#>>'{}')::jsonb->'#{record_type}' ? '#{sanitized_record_id}';
|
|
|
|
SQL
|
|
|
|
|
|
|
|
ActiveRecord::Base.connection.execute(sql)
|
|
|
|
end
|
|
|
|
end
|