2016-11-25 16:10:37 +00:00
|
|
|
module Import
|
|
|
|
module OTRS
|
|
|
|
class Article
|
|
|
|
include Import::Helper
|
|
|
|
include Import::OTRS::Helper
|
|
|
|
|
|
|
|
MAPPING = {
|
2016-12-09 12:16:33 +00:00
|
|
|
TicketID: :ticket_id,
|
|
|
|
ArticleID: :id,
|
|
|
|
Body: :body,
|
|
|
|
From: :from,
|
|
|
|
To: :to,
|
|
|
|
Cc: :cc,
|
|
|
|
Subject: :subject,
|
|
|
|
InReplyTo: :in_reply_to,
|
|
|
|
MessageID: :message_id,
|
|
|
|
#ReplyTo: :reply_to,
|
|
|
|
References: :references,
|
|
|
|
ContentType: :content_type,
|
|
|
|
Changed: :updated_at,
|
|
|
|
Created: :created_at,
|
|
|
|
ChangedBy: :updated_by_id,
|
|
|
|
CreatedBy: :created_by_id,
|
2016-11-25 16:10:37 +00:00
|
|
|
}.freeze
|
|
|
|
|
|
|
|
def initialize(article)
|
|
|
|
initialize_article_sender_types
|
|
|
|
initialize_article_types
|
|
|
|
|
|
|
|
utf8_encode(article)
|
|
|
|
import(article)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def import(article)
|
|
|
|
create_or_update(map(article))
|
|
|
|
|
2016-12-09 12:54:50 +00:00
|
|
|
return if article['Attachments'].blank?
|
2016-11-25 16:10:37 +00:00
|
|
|
|
|
|
|
Import::OTRS::Article::AttachmentFactory.import(
|
|
|
|
attachments: article['Attachments'],
|
|
|
|
local_article: @local_article
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_or_update(article)
|
|
|
|
return if updated?(article)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
create(article)
|
|
|
|
end
|
|
|
|
|
|
|
|
def updated?(article)
|
|
|
|
@local_article = ::Ticket::Article.find_by(id: article[:id])
|
|
|
|
return false if !@local_article
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
log "update Ticket::Article.find_by(id: #{article[:id]})"
|
2017-09-11 11:16:08 +00:00
|
|
|
@local_article.update!(article)
|
2016-11-25 16:10:37 +00:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def create(article)
|
|
|
|
log "add Ticket::Article.find_by(id: #{article[:id]})"
|
|
|
|
@local_article = ::Ticket::Article.new(article)
|
|
|
|
@local_article.id = article[:id]
|
|
|
|
@local_article.save
|
|
|
|
reset_primary_key_sequence('ticket_articles')
|
|
|
|
rescue ActiveRecord::RecordNotUnique
|
|
|
|
log "Ticket #{article[:ticket_id]} (article #{article[:id]}) is handled by another thead, skipping."
|
|
|
|
end
|
|
|
|
|
|
|
|
def map(article)
|
2016-12-09 12:54:50 +00:00
|
|
|
mapped = map_default(article)
|
2017-01-13 08:20:38 +00:00
|
|
|
map_content_type(mapped)
|
2017-03-27 10:10:52 +00:00
|
|
|
mapped[:body] ||= ''
|
|
|
|
mapped
|
2016-12-09 12:54:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def map_default(article)
|
2016-11-25 16:10:37 +00:00
|
|
|
{
|
|
|
|
created_by_id: 1,
|
|
|
|
updated_by_id: 1,
|
|
|
|
}
|
|
|
|
.merge(from_mapping(article))
|
|
|
|
.merge(article_type(article))
|
|
|
|
.merge(article_sender_type(article))
|
|
|
|
end
|
|
|
|
|
2017-01-13 08:20:38 +00:00
|
|
|
def map_content_type(mapped)
|
|
|
|
# if no content type is set make sure to remove it
|
|
|
|
# so Zammad can set the default content type
|
|
|
|
mapped.delete(:content_type) if mapped[:content_type].blank?
|
|
|
|
return mapped if !mapped[:content_type]
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2021-05-12 11:37:44 +00:00
|
|
|
mapped[:content_type].sub!(%r{[;,]\s?.+?$}, '')
|
2017-01-13 08:20:38 +00:00
|
|
|
mapped
|
|
|
|
end
|
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
def article_type(article)
|
|
|
|
@article_types[article['ArticleType']] || @article_types['note-internal']
|
|
|
|
end
|
|
|
|
|
|
|
|
def article_sender_type(article)
|
|
|
|
{
|
|
|
|
sender_id: @sender_type_id[article['SenderType']] || @sender_type_id['note-internal']
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize_article_sender_types
|
|
|
|
@sender_type_id = {
|
|
|
|
'customer' => article_sender_type_id_lookup('Customer'),
|
|
|
|
'agent' => article_sender_type_id_lookup('Agent'),
|
|
|
|
'system' => article_sender_type_id_lookup('System'),
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def article_sender_type_id_lookup(name)
|
|
|
|
::Ticket::Article::Sender.find_by(name: name).id
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize_article_types
|
|
|
|
@article_types = {
|
|
|
|
'email-external' => {
|
|
|
|
type_id: article_type_id_lookup('email'),
|
|
|
|
internal: false
|
|
|
|
},
|
|
|
|
'email-internal' => {
|
|
|
|
type_id: article_type_id_lookup('email'),
|
|
|
|
internal: true
|
|
|
|
},
|
2018-12-19 17:31:51 +00:00
|
|
|
'note-external' => {
|
2016-11-25 16:10:37 +00:00
|
|
|
type_id: article_type_id_lookup('note'),
|
|
|
|
internal: false
|
|
|
|
},
|
2018-12-19 17:31:51 +00:00
|
|
|
'note-internal' => {
|
2016-11-25 16:10:37 +00:00
|
|
|
type_id: article_type_id_lookup('note'),
|
|
|
|
internal: true
|
|
|
|
},
|
2018-12-19 17:31:51 +00:00
|
|
|
'phone' => {
|
2016-11-25 16:10:37 +00:00
|
|
|
type_id: article_type_id_lookup('phone'),
|
|
|
|
internal: false
|
|
|
|
},
|
2018-12-19 17:31:51 +00:00
|
|
|
'webrequest' => {
|
2016-11-25 16:10:37 +00:00
|
|
|
type_id: article_type_id_lookup('web'),
|
|
|
|
internal: false
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def article_type_id_lookup(name)
|
|
|
|
::Ticket::Article::Type.lookup(name: name).id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|