20201110000001_mention_init.rb 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. class MentionInit < ActiveRecord::Migration[5.2]
  2. def change
  3. # return if it's a new setup
  4. return if !Setting.exists?(name: 'system_init_done')
  5. create_table :mentions do |t|
  6. t.references :mentionable, polymorphic: true, null: false
  7. t.column :user_id, :integer, null: false
  8. t.column :updated_by_id, :integer, null: false
  9. t.column :created_by_id, :integer, null: false
  10. t.timestamps limit: 3, null: false
  11. end
  12. add_index :mentions, %i[mentionable_id mentionable_type user_id], unique: true, name: 'index_mentions_mentionable_user'
  13. add_foreign_key :mentions, :users, column: :created_by_id
  14. add_foreign_key :mentions, :users, column: :updated_by_id
  15. add_foreign_key :mentions, :users, column: :user_id
  16. Mention.reset_column_information
  17. create_overview
  18. update_users
  19. end
  20. def create_overview
  21. Overview.create_if_not_exists(
  22. name: 'My subscribed Tickets',
  23. link: 'my_subscribed_tickets',
  24. prio: 1025,
  25. role_ids: Role.with_permissions('ticket.agent').pluck(:id),
  26. condition: { 'ticket.mention_user_ids'=>{ 'operator' => 'is', 'pre_condition' => 'current_user.id', 'value' => '', 'value_completion' => '' } },
  27. order: {
  28. by: 'created_at',
  29. direction: 'ASC',
  30. },
  31. view: {
  32. d: %w[title customer group created_at],
  33. s: %w[title customer group created_at],
  34. m: %w[number title customer group created_at],
  35. view_mode_default: 's',
  36. },
  37. created_by_id: 1,
  38. updated_by_id: 1,
  39. )
  40. end
  41. def update_users
  42. User.with_permissions('ticket.agent').each do |user|
  43. next if user.preferences.blank?
  44. next if user.preferences['notification_config'].blank?
  45. next if user.preferences['notification_config']['matrix'].blank?
  46. update_matrix(user.preferences['notification_config']['matrix'])
  47. user.save!
  48. end
  49. end
  50. def update_matrix(matrix)
  51. matrix_type_defaults.each do |type, default|
  52. matrix[type] ||= {}
  53. matrix[type]['criteria'] ||= {}
  54. matrix[type]['criteria']['subscribed'] = default
  55. end
  56. end
  57. def matrix_type_defaults
  58. @matrix_type_defaults ||= {
  59. 'create' => true,
  60. 'update' => true,
  61. 'reminder_reached' => false,
  62. 'escalation' => false,
  63. }
  64. end
  65. end