20180410000001_cleanup_user_preferences_notification_sound2.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. class CleanupUserPreferencesNotificationSound2 < ActiveRecord::Migration[5.1]
  2. def local_to_h!(value)
  3. if value.class == ActionController::Parameters
  4. value = value.permit!.to_h
  5. end
  6. if value.class == Hash || value.class == ActiveSupport::HashWithIndifferentAccess
  7. value.each_key do |local_key|
  8. value[local_key] = local_to_h!(value[local_key])
  9. end
  10. end
  11. value
  12. end
  13. def local_clear_preferences(user)
  14. return false if !user
  15. return false if !user.preferences
  16. return false if !user.preferences[:notification_sound]
  17. return false if !user.preferences[:notification_sound][:enabled]
  18. if user.preferences[:notification_sound][:enabled] == 'true'
  19. user.preferences[:notification_sound][:enabled] = true
  20. user.save!
  21. return true
  22. end
  23. return false if user.preferences[:notification_sound][:enabled] != 'false'
  24. user.preferences[:notification_sound][:enabled] = false
  25. user.save!
  26. true
  27. end
  28. def up
  29. User.with_permissions('ticket.agent').each do |user|
  30. local_to_h!(user.preferences)
  31. user.save!
  32. end
  33. items = SearchIndexBackend.search('preferences.notification_sound.enabled:*', 3000, 'User') || []
  34. items.each do |item|
  35. next if !item[:id]
  36. user = User.find_by(id: item[:id])
  37. local_to_h!(user.preferences)
  38. local_clear_preferences(user)
  39. end
  40. Organization.all.each do |organization|
  41. organization.members.each do |user|
  42. local_to_h!(user.preferences)
  43. local_clear_preferences(user)
  44. end
  45. end
  46. Delayed::Job.limit(2_000).each do |job|
  47. Delayed::Worker.new.run(job)
  48. end
  49. end
  50. end