status.rb 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Whatsapp::Webhook::Message::Status
  3. include Mixin::RequiredSubPaths
  4. attr_reader :data, :channel, :ticket, :related_article
  5. def initialize(data:, channel:)
  6. @data = data
  7. @channel = channel
  8. end
  9. def process
  10. @related_article = find_related_article
  11. raise Whatsapp::Webhook::Payload::ProcessableError, __('No related article found to process the status message on.') if @related_article.nil?
  12. @ticket = @related_article.ticket
  13. create_article
  14. update_related_article
  15. update_ticket
  16. end
  17. private
  18. def body
  19. raise NotImplementedError
  20. end
  21. def status
  22. @status ||= @data[:entry]
  23. .first[:changes]
  24. .first[:value][:statuses]
  25. .first
  26. end
  27. def find_related_article
  28. Ticket::Article.where(message_id: status[:id])&.first
  29. end
  30. def update_related_article?
  31. true
  32. end
  33. def article_timestamp_key
  34. raise NotImplementedError
  35. end
  36. def update_related_article_attributes
  37. preferences = @related_article.preferences
  38. preferences[:whatsapp][article_timestamp_key] = status[:timestamp]
  39. { preferences: }
  40. end
  41. def update_related_article
  42. return if !update_related_article? || update_related_article_attributes.blank?
  43. @related_article.update!(update_related_article_attributes)
  44. end
  45. def create_article?
  46. false
  47. end
  48. def create_article
  49. return if !create_article?
  50. Ticket::Article.create!(
  51. ticket_id: @ticket.id,
  52. type_id: Ticket::Article::Type.lookup(name: 'note').id,
  53. sender_id: Ticket::Article::Sender.lookup(name: 'System').id,
  54. from: "#{@channel.options[:name]} (#{@channel.options[:phone_number]})",
  55. internal: true,
  56. body: "Unable to handle WhatsApp message: #{body}",
  57. content_type: 'text/plain',
  58. preferences: {
  59. delivery_article_id_related: @related_article.id,
  60. delivery_message: true,
  61. },
  62. updated_by_id: 1,
  63. created_by_id: 1,
  64. )
  65. end
  66. def update_ticket?
  67. false
  68. end
  69. def update_ticket_attributes
  70. raise NotImplementedError
  71. end
  72. def update_ticket
  73. return if !update_ticket? || update_ticket_attributes.blank?
  74. @ticket.update!(update_ticket_attributes)
  75. end
  76. end