article.rb 4.3 KB

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