2016-11-25 16:10:37 +00:00
|
|
|
module Import
|
|
|
|
module OTRS
|
|
|
|
class ArticleCustomer
|
|
|
|
include Import::Helper
|
|
|
|
|
|
|
|
def initialize(article)
|
2016-12-12 09:34:07 +00:00
|
|
|
import(article)
|
2016-11-25 16:10:37 +00:00
|
|
|
rescue Exceptions::UnprocessableEntity => e
|
|
|
|
log "ERROR: Can't extract customer from Article #{article[:id]}"
|
|
|
|
end
|
|
|
|
|
2016-12-12 09:34:07 +00:00
|
|
|
class << self
|
2016-11-25 16:10:37 +00:00
|
|
|
|
2016-12-12 09:34:07 +00:00
|
|
|
def find(article)
|
2017-03-27 10:47:33 +00:00
|
|
|
email = local_email(article['From'])
|
2017-03-28 09:28:07 +00:00
|
|
|
return if !email
|
2016-12-12 09:34:07 +00:00
|
|
|
user = ::User.find_by(email: email)
|
|
|
|
user ||= ::User.find_by(login: email)
|
|
|
|
user
|
|
|
|
end
|
2016-11-25 16:10:37 +00:00
|
|
|
|
2017-03-27 10:47:33 +00:00
|
|
|
def local_email(from)
|
|
|
|
# TODO: should get unified with User#check_email
|
2017-03-28 09:28:07 +00:00
|
|
|
email = extract_email(from)
|
|
|
|
return if !email
|
|
|
|
email.downcase
|
2017-03-27 10:47:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-12-12 09:34:07 +00:00
|
|
|
def extract_email(from)
|
|
|
|
Mail::Address.new(from).address
|
|
|
|
rescue
|
2017-03-16 14:06:24 +00:00
|
|
|
return from if from !~ /<\s*([^>]+)/
|
|
|
|
$1.strip
|
2016-12-12 09:34:07 +00:00
|
|
|
end
|
2016-11-25 16:10:37 +00:00
|
|
|
end
|
|
|
|
|
2016-12-12 09:34:07 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def import(article)
|
|
|
|
find_or_create(article)
|
2016-11-25 16:10:37 +00:00
|
|
|
end
|
|
|
|
|
2016-12-12 09:34:07 +00:00
|
|
|
def find_or_create(article)
|
|
|
|
return if self.class.find(article)
|
|
|
|
create(article)
|
2016-11-25 16:10:37 +00:00
|
|
|
end
|
|
|
|
|
2016-12-12 09:34:07 +00:00
|
|
|
def create(article)
|
2017-03-27 10:47:33 +00:00
|
|
|
email = self.class.local_email(article['From'])
|
2016-11-25 16:10:37 +00:00
|
|
|
::User.create(
|
|
|
|
login: email,
|
2016-12-12 09:34:07 +00:00
|
|
|
firstname: extract_display_name(article['From']),
|
2016-11-25 16:10:37 +00:00
|
|
|
lastname: '',
|
|
|
|
email: email,
|
|
|
|
password: '',
|
|
|
|
active: true,
|
|
|
|
role_ids: roles,
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
rescue ActiveRecord::RecordNotUnique
|
|
|
|
log "User #{email} was handled by another thread, taking this."
|
|
|
|
|
2016-12-12 09:34:07 +00:00
|
|
|
return if self.class.find(article)
|
2016-11-25 16:10:37 +00:00
|
|
|
|
|
|
|
log "User #{email} wasn't created sleep and retry."
|
|
|
|
sleep rand 3
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
|
|
|
|
def roles
|
|
|
|
[
|
|
|
|
Role.find_by(name: 'Customer').id
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_display_name(from)
|
|
|
|
# do extra decoding because we needed to use field.value
|
|
|
|
Mail::Field.new('X-From', parsed_display_name(from)).to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def parsed_display_name(from)
|
|
|
|
parsed_address = Mail::Address.new(from)
|
|
|
|
return parsed_address.display_name if parsed_address.display_name
|
2017-11-23 08:09:44 +00:00
|
|
|
return from if parsed_address.comments.blank?
|
2016-11-25 16:10:37 +00:00
|
|
|
parsed_address.comments[0]
|
|
|
|
rescue
|
|
|
|
from
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|