article.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_attributes(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. end
  63. def map_default(article)
  64. {
  65. created_by_id: 1,
  66. updated_by_id: 1,
  67. }
  68. .merge(from_mapping(article))
  69. .merge(article_type(article))
  70. .merge(article_sender_type(article))
  71. end
  72. def map_content_type(mapped)
  73. # if no content type is set make sure to remove it
  74. # so Zammad can set the default content type
  75. mapped.delete(:content_type) if mapped[:content_type].blank?
  76. return mapped if !mapped[:content_type]
  77. mapped[:content_type].sub!(/[;,]\s?.+?$/, '')
  78. mapped
  79. end
  80. def article_type(article)
  81. @article_types[article['ArticleType']] || @article_types['note-internal']
  82. end
  83. def article_sender_type(article)
  84. {
  85. sender_id: @sender_type_id[article['SenderType']] || @sender_type_id['note-internal']
  86. }
  87. end
  88. def initialize_article_sender_types
  89. @sender_type_id = {
  90. 'customer' => article_sender_type_id_lookup('Customer'),
  91. 'agent' => article_sender_type_id_lookup('Agent'),
  92. 'system' => article_sender_type_id_lookup('System'),
  93. }
  94. end
  95. def article_sender_type_id_lookup(name)
  96. ::Ticket::Article::Sender.find_by(name: name).id
  97. end
  98. def initialize_article_types
  99. @article_types = {
  100. 'email-external' => {
  101. type_id: article_type_id_lookup('email'),
  102. internal: false
  103. },
  104. 'email-internal' => {
  105. type_id: article_type_id_lookup('email'),
  106. internal: true
  107. },
  108. 'note-external' => {
  109. type_id: article_type_id_lookup('note'),
  110. internal: false
  111. },
  112. 'note-internal' => {
  113. type_id: article_type_id_lookup('note'),
  114. internal: true
  115. },
  116. 'phone' => {
  117. type_id: article_type_id_lookup('phone'),
  118. internal: false
  119. },
  120. 'webrequest' => {
  121. type_id: article_type_id_lookup('web'),
  122. internal: false
  123. },
  124. }
  125. end
  126. def article_type_id_lookup(name)
  127. ::Ticket::Article::Type.lookup(name: name).id
  128. end
  129. end
  130. end
  131. end