notification_sms.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Ticket::PerformChanges::Action::NotificationSms < Ticket::PerformChanges::Action
  3. def self.phase
  4. :after_save
  5. end
  6. def execute(...)
  7. send_sms_notification
  8. end
  9. private
  10. def send_sms_notification
  11. if recipients.blank?
  12. Rails.logger.debug { "No SMS recipients found for Ticket# #{record.number}" }
  13. return
  14. end
  15. channel = Channel.find_by(area: 'Sms::Notification')
  16. if !channel.active?
  17. Rails.logger.info "Found possible SMS recipient(s) (#{recipients_to}) for Ticket##{record.number} but SMS channel is not active."
  18. return
  19. end
  20. create_notification_article(channel)
  21. end
  22. def create_notification_article(channel)
  23. # The attribute content_type is not needed for SMS.
  24. article = Ticket::Article.new(
  25. ticket_id: id,
  26. subject: 'SMS notification',
  27. to: recipients_to,
  28. body: body,
  29. internal: execution_data['internal'] || false, # default to public if value was not set
  30. sender: Ticket::Article::Sender.find_by(name: 'System'),
  31. type: Ticket::Article::Type.find_by(name: 'sms'),
  32. preferences: {
  33. perform_origin: origin,
  34. sms_recipients: recipients_mobile,
  35. channel_id: channel.id,
  36. },
  37. updated_by_id: 1,
  38. created_by_id: 1,
  39. )
  40. article.history_change_source_attribute(performable, 'created')
  41. article.save!
  42. end
  43. def body
  44. NotificationFactory::Renderer.new(
  45. objects: notification_factory_template_objects,
  46. template: execution_data['body'],
  47. escape: false,
  48. locale: locale,
  49. timezone: timezone,
  50. ).render.html2text.tr(' ', ' ') # convert non-breaking space to simple space
  51. end
  52. def recipients
  53. @recipients ||= Array(execution_data['recipient'])
  54. .each_with_object([]) { |recipient_type, sum| sum.concat(Array(recipients_by_type(recipient_type))) }
  55. .map { |user_or_id| user_or_id.is_a?(User) ? user_or_id : User.lookup(id: user_or_id) }
  56. .uniq(&:id)
  57. .select { |user| user.mobile.present? }
  58. end
  59. def recipients_to
  60. @recipients_to ||= recipients.map { |recipient| "#{recipient.fullname} (#{recipient.mobile})" }.join(', ')
  61. end
  62. def recipients_mobile
  63. @recipients_mobile ||= recipients.map(&:mobile)
  64. end
  65. def recipients_by_type(recipient_type)
  66. case recipient_type
  67. when 'article_last_sender'
  68. recipients_by_type_article_last_sender
  69. when 'ticket_customer'
  70. record.customer_id
  71. when 'ticket_owner'
  72. record.owner_id
  73. when 'ticket_agents'
  74. recipients_by_type_ticket_agents
  75. when %r{\Auserid_(\d+)\z}
  76. return $1 if User.exists?($1)
  77. Rails.logger.warn "Can't find configured #{origin} sms recipient user with ID '#{$1}'"
  78. nil
  79. else
  80. Rails.logger.error "Unknown sms notification recipient '#{recipient_type}'"
  81. nil
  82. end
  83. end
  84. def recipients_by_type_article_last_sender
  85. return nil if article.blank?
  86. if article.origin_by_id
  87. article.origin_by_id
  88. elsif article.created_by_id
  89. article.created_by_id
  90. end
  91. end
  92. def recipients_by_type_ticket_agents
  93. User.group_access(record.group_id, 'full').sort_by(&:login)
  94. end
  95. end