2018-04-26 08:55:53 +00:00
|
|
|
require_dependency 'ticket'
|
2016-11-25 16:10:37 +00:00
|
|
|
|
|
|
|
module Import
|
|
|
|
module OTRS
|
|
|
|
class Ticket
|
|
|
|
include Import::Helper
|
|
|
|
include Import::OTRS::Helper
|
|
|
|
|
|
|
|
MAPPING = {
|
|
|
|
Changed: :updated_at,
|
|
|
|
Created: :created_at,
|
|
|
|
TicketNumber: :number,
|
|
|
|
QueueID: :group_id,
|
|
|
|
StateID: :state_id,
|
|
|
|
PriorityID: :priority_id,
|
|
|
|
Title: :title,
|
|
|
|
TicketID: :id,
|
|
|
|
FirstResponse: :first_response_at,
|
|
|
|
#FirstResponseTimeDestinationDate: :first_response_escalation_at,
|
|
|
|
#FirstResponseInMin: :first_response_in_min,
|
|
|
|
#FirstResponseDiffInMin: :first_response_diff_in_min,
|
|
|
|
Closed: :close_at,
|
|
|
|
#SoltutionTimeDestinationDate: :close_escalation_at,
|
|
|
|
#CloseTimeInMin: :close_in_min,
|
|
|
|
#CloseTimeDiffInMin: :close_diff_in_min,
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
def initialize(ticket)
|
|
|
|
fix(ticket)
|
|
|
|
import(ticket)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def import(ticket)
|
|
|
|
Import::OTRS::ArticleCustomerFactory.import(ticket['Articles'])
|
|
|
|
|
|
|
|
create_or_update(map(ticket))
|
|
|
|
|
|
|
|
Import::OTRS::ArticleFactory.import(ticket['Articles'])
|
|
|
|
Import::OTRS::HistoryFactory.import(ticket['History'])
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_or_update(ticket)
|
|
|
|
return if updated?(ticket)
|
|
|
|
create(ticket)
|
|
|
|
end
|
|
|
|
|
|
|
|
def updated?(ticket)
|
|
|
|
@local_ticket = ::Ticket.find_by(id: ticket[:id])
|
|
|
|
return false if !@local_ticket
|
|
|
|
log "update Ticket.find_by(id: #{ticket[:id]})"
|
2017-09-11 11:16:08 +00:00
|
|
|
@local_ticket.update!(ticket)
|
2016-11-25 16:10:37 +00:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def create(ticket)
|
|
|
|
log "add Ticket.find_by(id: #{ticket[:id]})"
|
|
|
|
@local_ticket = ::Ticket.new(ticket)
|
|
|
|
@local_ticket.id = ticket[:id]
|
|
|
|
@local_ticket.save
|
|
|
|
reset_primary_key_sequence('tickets')
|
|
|
|
rescue ActiveRecord::RecordNotUnique
|
|
|
|
log "Ticket #{ticket[:id]} is handled by another thead, skipping."
|
|
|
|
end
|
|
|
|
|
|
|
|
def map(ticket)
|
2016-11-29 09:05:49 +00:00
|
|
|
ensure_map(default_map(ticket))
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_map(mapped)
|
|
|
|
return mapped if mapped[:title]
|
|
|
|
mapped[:title] = '**EMPTY**'
|
|
|
|
mapped
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_map(ticket)
|
2016-11-25 16:10:37 +00:00
|
|
|
{
|
|
|
|
owner_id: owner_id(ticket),
|
|
|
|
customer_id: customer_id(ticket),
|
2016-12-12 09:34:07 +00:00
|
|
|
created_by_id: created_by_id(ticket),
|
2016-11-25 16:10:37 +00:00
|
|
|
updated_by_id: 1,
|
|
|
|
}
|
|
|
|
.merge(from_mapping(ticket))
|
|
|
|
.merge(dynamic_fields(ticket))
|
|
|
|
end
|
|
|
|
|
|
|
|
def dynamic_fields(ticket)
|
|
|
|
result = {}
|
2017-11-23 08:09:44 +00:00
|
|
|
ticket.each_key do |key|
|
2016-11-25 16:10:37 +00:00
|
|
|
|
|
|
|
key_string = key.to_s
|
|
|
|
|
|
|
|
next if !key_string.start_with?('DynamicField_')
|
|
|
|
dynamic_field_name = key_string[13, key_string.length]
|
|
|
|
|
|
|
|
next if Import::OTRS::DynamicFieldFactory.skip_field?( dynamic_field_name )
|
|
|
|
dynamic_field_name = Import::OTRS::DynamicField.convert_name(dynamic_field_name)
|
|
|
|
|
|
|
|
result[dynamic_field_name.to_sym] = ticket[key_string]
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-11-25 16:10:37 +00:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def owner_id(ticket)
|
|
|
|
default = 1
|
|
|
|
owner = ticket['Owner']
|
|
|
|
|
|
|
|
return default if !owner
|
|
|
|
user = user_lookup(owner)
|
|
|
|
|
|
|
|
return user.id if user
|
|
|
|
default
|
|
|
|
end
|
|
|
|
|
|
|
|
def customer_id(ticket)
|
|
|
|
default = 1
|
|
|
|
customer = ticket['CustomerUserID']
|
|
|
|
|
|
|
|
return default if !customer
|
|
|
|
|
2016-12-12 09:34:07 +00:00
|
|
|
user = user_lookup(customer)
|
2016-11-25 16:10:37 +00:00
|
|
|
return user.id if user
|
|
|
|
|
|
|
|
first_customer_id = first_customer_id(ticket['Articles'])
|
|
|
|
return first_customer_id if first_customer_id
|
|
|
|
|
|
|
|
default
|
|
|
|
end
|
|
|
|
|
2016-12-12 09:34:07 +00:00
|
|
|
def created_by_id(ticket)
|
|
|
|
default = 1
|
|
|
|
return ticket['CreateBy'] if ticket['CreateBy'].to_i != default
|
|
|
|
return default if ticket['Articles'].blank?
|
|
|
|
return default if ticket['Articles'].first['SenderType'] != 'customer'
|
|
|
|
customer_id(ticket)
|
|
|
|
end
|
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
def user_lookup(login)
|
|
|
|
::User.find_by(login: login.downcase)
|
|
|
|
end
|
|
|
|
|
|
|
|
def first_customer_id(articles)
|
|
|
|
user_id = nil
|
2017-10-01 12:25:52 +00:00
|
|
|
articles.each do |article|
|
2016-12-12 09:34:07 +00:00
|
|
|
next if article['SenderType'] != 'customer'
|
2017-11-23 08:09:44 +00:00
|
|
|
next if article['From'].blank?
|
2016-12-12 09:34:07 +00:00
|
|
|
user = Import::OTRS::ArticleCustomer.find(article)
|
|
|
|
break if !user
|
|
|
|
user_id = user.id
|
2016-11-25 16:10:37 +00:00
|
|
|
break
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-11-25 16:10:37 +00:00
|
|
|
user_id
|
|
|
|
end
|
|
|
|
|
|
|
|
# cleanup invalid values
|
|
|
|
def fix(ticket)
|
|
|
|
utf8_encode(ticket)
|
|
|
|
fix_timestamps(ticket)
|
|
|
|
fix_close_time(ticket)
|
|
|
|
end
|
|
|
|
|
|
|
|
def fix_timestamps(ticket)
|
2017-10-01 12:25:52 +00:00
|
|
|
ticket.each do |key, value|
|
2016-11-25 16:10:37 +00:00
|
|
|
next if value != '0000-00-00 00:00:00'
|
|
|
|
ticket[key] = nil
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-11-25 16:10:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# fix OTRS 3.1 bug, no close time if ticket is created
|
|
|
|
def fix_close_time(ticket)
|
|
|
|
return if ticket['StateType'] != 'closed'
|
|
|
|
return if ticket['Closed']
|
2017-11-23 08:09:44 +00:00
|
|
|
return if ticket['Closed'].present?
|
2016-11-25 16:10:37 +00:00
|
|
|
ticket['Closed'] = ticket['Created']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|