mapping.rb 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Sequencer::Unit::Import::Freshdesk::Conversation::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 and senders 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 incoming_map
  24. @incoming_map ||= {
  25. true => ::Ticket::Article::Sender.select(:id).find_by(name: 'Customer')&.id,
  26. false => ::Ticket::Article::Sender.select(:id).find_by(name: 'Agent')&.id,
  27. }.freeze
  28. end
  29. def process
  30. provide_mapped do
  31. {
  32. from: resource['from_email'],
  33. to: resource['to_emails']&.join(', '),
  34. cc: resource['cc_emails']&.join(', '),
  35. ticket_id: ticket_id,
  36. body: resource['body'],
  37. content_type: 'text/html',
  38. internal: resource['private'].present?,
  39. message_id: resource['id'],
  40. updated_by_id: user_id,
  41. created_by_id: user_id,
  42. sender_id: incoming_map[ resource['incoming'] ],
  43. type_id: source_map[ resource['source'] ] || default_type_id,
  44. created_at: resource['created_at'],
  45. updated_at: resource['updated_at'],
  46. }
  47. end
  48. end
  49. private
  50. def ticket_id
  51. id_map['Ticket'][resource['ticket_id']]
  52. end
  53. def user_id
  54. id_map['User'][resource['user_id']] || 1
  55. end
  56. def default_type_id
  57. @default_type_id ||= ::Ticket::Article::Type.select(:id).find_by(name: 'note')&.id
  58. end
  59. end