Merge pull request #7441 from ivanscinote/SCI-10583-ik

Implement user settings cleanup job for Step deletion [SCI-10583]
This commit is contained in:
Martin Artnik 2024-05-09 11:11:13 +02:00 committed by GitHub
commit 5b68001c1d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 1 deletions

View file

@ -0,0 +1,20 @@
# frozen_string_literal: true
class CleanupUserSettingsJob < ApplicationJob
queue_as :default
def perform(record_type, record_id)
raise ArgumentError, 'Invalid record_type' unless %w(task_step_states results_order).include?(record_type)
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

View file

@ -25,7 +25,7 @@ class Step < ApplicationRecord
after_destroy :adjust_positions_after_destroy, unless: -> { skip_position_adjust }
# conditional touch excluding step completion updates
after_destroy :touch_protocol
after_destroy :touch_protocol, :remove_from_user_settings
after_save :touch_protocol
after_touch :touch_protocol
@ -189,6 +189,10 @@ class Step < ApplicationRecord
private
def remove_from_user_settings
CleanupUserSettingsJob.perform_later('task_step_states', id)
end
def touch_protocol
# if only step completion attributes were changed, do not touch protocol
return if saved_changes.keys.sort == %w(completed completed_on updated_at)