mapping.rb 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Sequencer::Unit::Import::Freshdesk::Description::Mapping < Sequencer::Unit::Base
  3. include ::Sequencer::Unit::Import::Common::Mapping::Mixin::ProvideMapped
  4. uses :resource, :id_map
  5. # Since the imports rely on a fresh Zammad installation, we
  6. # can require the default article types to be present.
  7. def source_map # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  8. @source_map ||= {
  9. 0 => ::Ticket::Article::Type.select(:id).find_by(name: 'email')&.id, # Reply
  10. 1 => ::Ticket::Article::Type.select(:id).find_by(name: 'email')&.id, # Email
  11. 2 => ::Ticket::Article::Type.select(:id).find_by(name: 'web')&.id, # Note
  12. 3 => ::Ticket::Article::Type.select(:id).find_by(name: 'phone')&.id, # Phone
  13. 4 => ::Ticket::Article::Type.select(:id).find_by(name: 'note')&.id, # UNKNOWN!
  14. 5 => ::Ticket::Article::Type.select(:id).find_by(name: 'twitter status')&.id, # Created from tweets
  15. 6 => ::Ticket::Article::Type.select(:id).find_by(name: 'web')&.id, # Created from survey feedback
  16. 7 => ::Ticket::Article::Type.select(:id).find_by(name: 'facebook feed post')&.id, # Created from Facebook post
  17. 8 => ::Ticket::Article::Type.select(:id).find_by(name: 'email')&.id, # Created from Forwarded Email
  18. 9 => ::Ticket::Article::Type.select(:id).find_by(name: 'note')&.id, # Created from Phone
  19. 10 => ::Ticket::Article::Type.select(:id).find_by(name: 'note')&.id, # Created from Mobihelp
  20. 11 => ::Ticket::Article::Type.select(:id).find_by(name: 'note')&.id, # E-Commerce
  21. }.freeze
  22. end
  23. def process
  24. provide_mapped do
  25. {
  26. from: from,
  27. to: resource['to_emails']&.join(', '),
  28. cc: resource['cc_emails']&.join(', '),
  29. ticket_id: ticket_id,
  30. body: resource['description'],
  31. content_type: 'text/html',
  32. internal: false,
  33. message_id: "ticketid#{resource['id']}@freshdesk.com",
  34. sender_id: ::Ticket::Article::Sender.select(:id).find_by(name: 'Customer').id,
  35. type_id: source_map[ resource['source'] ] || default_type_id,
  36. updated_by_id: requester_id,
  37. created_by_id: requester_id,
  38. created_at: resource['created_at'],
  39. updated_at: resource['updated_at'],
  40. }
  41. end
  42. end
  43. private
  44. def from
  45. return nil if resource['to_emails'].blank?
  46. ::User.find(requester_id).email
  47. end
  48. def requester_id
  49. id_map['User'][resource['requester_id']]
  50. end
  51. def ticket_id
  52. id_map['Ticket'][resource['id']]
  53. end
  54. def default_type_id
  55. @default_type_id ||= ::Ticket::Article::Type.select(:id).find_by(name: 'note')&.id
  56. end
  57. end