2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
module Import
|
|
|
|
module OTRS
|
|
|
|
class Ticket
|
|
|
|
include Import::Helper
|
|
|
|
include Import::OTRS::Helper
|
|
|
|
|
|
|
|
MAPPING = {
|
2018-12-19 17:31:51 +00:00
|
|
|
Changed: :updated_at,
|
|
|
|
Created: :created_at,
|
|
|
|
TicketNumber: :number,
|
|
|
|
QueueID: :group_id,
|
|
|
|
StateID: :state_id,
|
|
|
|
PriorityID: :priority_id,
|
|
|
|
Title: :title,
|
|
|
|
TicketID: :id,
|
2016-11-25 16:10:37 +00:00
|
|
|
FirstResponse: :first_response_at,
|
2021-07-16 13:44:10 +00:00
|
|
|
# FirstResponseTimeDestinationDate: :first_response_escalation_at,
|
|
|
|
# FirstResponseInMin: :first_response_in_min,
|
|
|
|
# FirstResponseDiffInMin: :first_response_diff_in_min,
|
2018-12-19 17:31:51 +00:00
|
|
|
Closed: :close_at,
|
2021-07-16 13:44:10 +00:00
|
|
|
# SoltutionTimeDestinationDate: :close_escalation_at,
|
|
|
|
# CloseTimeInMin: :close_in_min,
|
|
|
|
# CloseTimeDiffInMin: :close_diff_in_min,
|
2016-11-25 16:10:37 +00:00
|
|
|
}.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)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
create(ticket)
|
|
|
|
end
|
|
|
|
|
|
|
|
def updated?(ticket)
|
|
|
|
@local_ticket = ::Ticket.find_by(id: ticket[:id])
|
|
|
|
return false if !@local_ticket
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
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]
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-11-29 09:05:49 +00:00
|
|
|
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_')
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
dynamic_field_name = key_string[13, key_string.length]
|
|
|
|
|
2021-07-16 13:38:01 +00:00
|
|
|
next if Import::OTRS::DynamicFieldFactory.skip_field?(dynamic_field_name)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
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
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
user = user_lookup(owner)
|
|
|
|
|
|
|
|
return user.id if user
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
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'
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-12-12 09:34:07 +00:00
|
|
|
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?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-12-12 09:34:07 +00:00
|
|
|
user = Import::OTRS::ArticleCustomer.find(article)
|
|
|
|
break if !user
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-12-12 09:34:07 +00:00
|
|
|
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'
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-11-25 16:10:37 +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?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-11-25 16:10:37 +00:00
|
|
|
ticket['Closed'] = ticket['Created']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|