adds_metadata_whatsapp.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. # Adds certain (missing) meta data when creating email articles.
  3. module Ticket::Article::AddsMetadataWhatsapp
  4. extend ActiveSupport::Concern
  5. included do
  6. before_create :ticket_article_add_metadata_whatsapp
  7. end
  8. private
  9. # We need to add a real article type layer, to handle such situations in the future.
  10. def ticket_article_add_metadata_whatsapp
  11. return if !neither_importing_nor_postmaster?
  12. return if !sender_needs_metadata?
  13. return if !type_whatsapp_needs_metadata?
  14. metadata_whatsapp_process_from_and_to
  15. end
  16. def type_whatsapp_needs_metadata?
  17. return false if !type_id
  18. type = Ticket::Article::Type.lookup(id: type_id)
  19. return false if type&.name != 'whatsapp message'
  20. true
  21. end
  22. def whatsapp_from_name(channel)
  23. if created_by_id != 1
  24. return "#{created_by.firstname} #{created_by.lastname} via #{channel.options[:name]} (#{channel.options[:phone_number]})"
  25. end
  26. "#{channel.options[:name]} (#{channel.options[:phone_number]})"
  27. end
  28. def whatsapp_to_name
  29. phone_number = ticket.preferences.dig('whatsapp', 'from', 'phone_number')
  30. display_name = ticket.preferences.dig('whatsapp', 'from', 'display_name')
  31. "#{display_name} (+#{phone_number})"
  32. end
  33. def metadata_whatsapp_process_from_and_to
  34. channel = Channel.lookup(id: ticket.preferences['channel_id'])
  35. return if !channel
  36. self.from = whatsapp_from_name(channel)
  37. self.to = whatsapp_to_name
  38. end
  39. end