article.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. module Import
  2. module OTRS
  3. class Article
  4. include Import::Helper
  5. include Import::OTRS::Helper
  6. MAPPING = {
  7. TicketID: :ticket_id,
  8. ArticleID: :id,
  9. Body: :body,
  10. From: :from,
  11. To: :to,
  12. Cc: :cc,
  13. Subject: :subject,
  14. InReplyTo: :in_reply_to,
  15. MessageID: :message_id,
  16. #ReplyTo: :reply_to,
  17. References: :references,
  18. ContentType: :content_type,
  19. Changed: :updated_at,
  20. Created: :created_at,
  21. ChangedBy: :updated_by_id,
  22. CreatedBy: :created_by_id,
  23. }.freeze
  24. def initialize(article)
  25. initialize_article_sender_types
  26. initialize_article_types
  27. utf8_encode(article)
  28. import(article)
  29. end
  30. private
  31. def import(article)
  32. create_or_update(map(article))
  33. return if article['Attachments'].blank?
  34. Import::OTRS::Article::AttachmentFactory.import(
  35. attachments: article['Attachments'],
  36. local_article: @local_article
  37. )
  38. end
  39. def create_or_update(article)
  40. return if updated?(article)
  41. create(article)
  42. end
  43. def updated?(article)
  44. @local_article = ::Ticket::Article.find_by(id: article[:id])
  45. return false if !@local_article
  46. log "update Ticket::Article.find_by(id: #{article[:id]})"
  47. @local_article.update!(article)
  48. true
  49. end
  50. def create(article)
  51. log "add Ticket::Article.find_by(id: #{article[:id]})"
  52. @local_article = ::Ticket::Article.new(article)
  53. @local_article.id = article[:id]
  54. @local_article.save
  55. reset_primary_key_sequence('ticket_articles')
  56. rescue ActiveRecord::RecordNotUnique
  57. log "Ticket #{article[:ticket_id]} (article #{article[:id]}) is handled by another thead, skipping."
  58. end
  59. def map(article)
  60. mapped = map_default(article)
  61. map_content_type(mapped)
  62. mapped[:body] ||= ''
  63. mapped
  64. end
  65. def map_default(article)
  66. {
  67. created_by_id: 1,
  68. updated_by_id: 1,
  69. }
  70. .merge(from_mapping(article))
  71. .merge(article_type(article))
  72. .merge(article_sender_type(article))
  73. end
  74. def map_content_type(mapped)
  75. # if no content type is set make sure to remove it
  76. # so Zammad can set the default content type
  77. mapped.delete(:content_type) if mapped[:content_type].blank?
  78. return mapped if !mapped[:content_type]
  79. mapped[:content_type].sub!(/[;,]\s?.+?$/, '')
  80. mapped
  81. end
  82. def article_type(article)
  83. @article_types[article['ArticleType']] || @article_types['note-internal']
  84. end
  85. def article_sender_type(article)
  86. {
  87. sender_id: @sender_type_id[article['SenderType']] || @sender_type_id['note-internal']
  88. }
  89. end
  90. def initialize_article_sender_types
  91. @sender_type_id = {
  92. 'customer' => article_sender_type_id_lookup('Customer'),
  93. 'agent' => article_sender_type_id_lookup('Agent'),
  94. 'system' => article_sender_type_id_lookup('System'),
  95. }
  96. end
  97. def article_sender_type_id_lookup(name)
  98. ::Ticket::Article::Sender.find_by(name: name).id
  99. end
  100. def initialize_article_types
  101. @article_types = {
  102. 'email-external' => {
  103. type_id: article_type_id_lookup('email'),
  104. internal: false
  105. },
  106. 'email-internal' => {
  107. type_id: article_type_id_lookup('email'),
  108. internal: true
  109. },
  110. 'note-external' => {
  111. type_id: article_type_id_lookup('note'),
  112. internal: false
  113. },
  114. 'note-internal' => {
  115. type_id: article_type_id_lookup('note'),
  116. internal: true
  117. },
  118. 'phone' => {
  119. type_id: article_type_id_lookup('phone'),
  120. internal: false
  121. },
  122. 'webrequest' => {
  123. type_id: article_type_id_lookup('web'),
  124. internal: false
  125. },
  126. }
  127. end
  128. def article_type_id_lookup(name)
  129. ::Ticket::Article::Type.lookup(name: name).id
  130. end
  131. end
  132. end
  133. end