20201110000001_mention_init.rb 2.5 KB

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