2013-02-01 19:58:53 +00:00
|
|
|
module Import
|
|
|
|
end
|
2013-01-07 08:46:38 +00:00
|
|
|
module Import::OTRS
|
2015-05-07 11:57:19 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
result = request_json( :Subaction => 'List', 1)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
{ some json structure }
|
|
|
|
|
|
|
|
result = request_json( :Subaction => 'List' )
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
"some data string"
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.request_json(data, data_only = false)
|
|
|
|
response = post(data)
|
|
|
|
if !response
|
|
|
|
fail "Can't connect to Zammad Migrator"
|
|
|
|
end
|
|
|
|
if !response.success?
|
|
|
|
fail "Can't connect to Zammad Migrator"
|
|
|
|
end
|
|
|
|
result = json(response)
|
|
|
|
if !result
|
|
|
|
fail 'Invalid response'
|
|
|
|
end
|
|
|
|
if data_only
|
|
|
|
result['Result']
|
|
|
|
else
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
start get request to backend, add auth data automatically
|
|
|
|
|
|
|
|
result = request('Subaction=List')
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
"some data string"
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2013-01-07 08:46:38 +00:00
|
|
|
def self.request(part)
|
2015-05-07 11:57:19 +00:00
|
|
|
url = Setting.get('import_otrs_endpoint') + part + ';Key=' + Setting.get('import_otrs_endpoint_key')
|
|
|
|
log 'GET: ' + url
|
|
|
|
response = UserAgent.get(
|
2013-09-19 14:17:25 +00:00
|
|
|
url,
|
2015-05-07 11:57:19 +00:00
|
|
|
{},
|
2013-09-19 14:17:25 +00:00
|
|
|
{
|
2015-05-07 11:57:19 +00:00
|
|
|
open_timeout: 10,
|
|
|
|
read_timeout: 60,
|
2015-04-27 13:42:53 +00:00
|
|
|
user: Setting.get('import_otrs_user'),
|
|
|
|
password: Setting.get('import_otrs_password'),
|
2013-09-19 14:17:25 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if !response.success?
|
2015-05-07 11:57:19 +00:00
|
|
|
log "ERROR: #{response.error}"
|
2013-01-25 22:17:50 +00:00
|
|
|
return
|
|
|
|
end
|
2015-04-30 17:20:27 +00:00
|
|
|
response
|
2013-01-07 08:46:38 +00:00
|
|
|
end
|
2015-05-07 11:57:19 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
start post request to backend, add auth data automatically
|
|
|
|
|
|
|
|
result = request('Subaction=List')
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
"some data string"
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.post(data, url = nil)
|
|
|
|
if !url
|
|
|
|
url = Setting.get('import_otrs_endpoint')
|
|
|
|
data['Action'] = 'ZammadMigrator'
|
|
|
|
end
|
2013-01-23 23:53:39 +00:00
|
|
|
data['Key'] = Setting.get('import_otrs_endpoint_key')
|
2015-05-07 11:57:19 +00:00
|
|
|
log 'POST: ' + url
|
|
|
|
log 'PARAMS: ' + data.inspect
|
|
|
|
open_timeout = 10
|
|
|
|
read_timeout = 120
|
|
|
|
if data.empty?
|
|
|
|
open_timeout = 6
|
|
|
|
read_timeout = 20
|
|
|
|
end
|
|
|
|
response = UserAgent.post(
|
2013-09-19 14:17:25 +00:00
|
|
|
url,
|
2015-05-07 11:57:19 +00:00
|
|
|
data,
|
2013-09-19 14:17:25 +00:00
|
|
|
{
|
2015-05-07 11:57:19 +00:00
|
|
|
open_timeout: open_timeout,
|
|
|
|
read_timeout: read_timeout,
|
2015-04-27 13:42:53 +00:00
|
|
|
user: Setting.get('import_otrs_user'),
|
|
|
|
password: Setting.get('import_otrs_password'),
|
2013-09-19 14:17:25 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if !response.success?
|
2015-05-07 11:57:19 +00:00
|
|
|
log "ERROR: #{response.error}"
|
2013-09-19 14:17:25 +00:00
|
|
|
return
|
2013-04-15 21:33:11 +00:00
|
|
|
end
|
2015-04-30 17:20:27 +00:00
|
|
|
response
|
2013-01-23 23:53:39 +00:00
|
|
|
end
|
2013-01-07 08:46:38 +00:00
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
start post request to backend, add auth data automatically
|
|
|
|
|
|
|
|
result = json('some response string')
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2013-01-07 08:46:38 +00:00
|
|
|
def self.json(response)
|
|
|
|
data = Encode.conv( 'utf8', response.body.to_s )
|
|
|
|
JSON.parse( data )
|
|
|
|
end
|
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
start auth on OTRS - just for experimental reasons
|
|
|
|
|
|
|
|
result = auth(username, password)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
{ ..user structure.. }
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2013-01-23 23:53:39 +00:00
|
|
|
def self.auth(username, password)
|
2015-05-07 11:57:19 +00:00
|
|
|
url = Setting.get('import_otrs_endpoint')
|
|
|
|
url.gsub!('ZammadMigrator', 'ZammadSSO')
|
|
|
|
response = post( { Action: 'ZammadSSO', Subaction: 'Auth', User: username, Pw: password }, url )
|
2013-01-25 22:17:50 +00:00
|
|
|
return if !response
|
2013-09-19 14:17:25 +00:00
|
|
|
return if !response.success?
|
2013-01-23 23:53:39 +00:00
|
|
|
|
|
|
|
result = json(response)
|
2015-04-30 17:20:27 +00:00
|
|
|
result
|
2013-01-23 23:53:39 +00:00
|
|
|
end
|
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
request session data - just for experimental reasons
|
|
|
|
|
|
|
|
result = session(session_id)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
{ ..session structure.. }
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2013-02-17 18:28:32 +00:00
|
|
|
def self.session(session_id)
|
2015-05-07 11:57:19 +00:00
|
|
|
url = Setting.get('import_otrs_endpoint')
|
|
|
|
url.gsub!('ZammadMigrator', 'ZammadSSO')
|
|
|
|
response = post( { Action: 'ZammadSSO', Subaction: 'SessionCheck', SessionID: session_id }, url )
|
2013-02-17 18:28:32 +00:00
|
|
|
return if !response
|
2013-09-19 14:17:25 +00:00
|
|
|
return if !response.success?
|
2013-02-17 18:28:32 +00:00
|
|
|
result = json(response)
|
2015-04-30 17:20:27 +00:00
|
|
|
result
|
2013-02-17 18:28:32 +00:00
|
|
|
end
|
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
=begin
|
2014-03-11 09:23:56 +00:00
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
load objects from otrs
|
|
|
|
|
|
|
|
result = load('SysConfig')
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
[
|
|
|
|
{ ..object1.. },
|
|
|
|
{ ..object2.. },
|
|
|
|
{ ..object3.. },
|
|
|
|
]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.load( object, limit = '', offset = '', diff = 0 )
|
|
|
|
request_json( { Subaction: 'Export', Object: object, Limit: limit, Offset: offset, Diff: diff }, 1 )
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
start get request to backend to check connection
|
|
|
|
|
|
|
|
result = connection_test
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
true | false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.connection_test
|
2015-05-07 12:10:38 +00:00
|
|
|
request_json({})
|
2015-05-07 11:57:19 +00:00
|
|
|
end
|
2014-03-11 09:23:56 +00:00
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
get object statistic from server ans save it in cache
|
|
|
|
|
|
|
|
result = statistic('Subaction=List')
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
{
|
|
|
|
'Ticket' => 1234,
|
|
|
|
'User' => 123,
|
|
|
|
'SomeObject' => 999,
|
|
|
|
}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.statistic
|
|
|
|
|
|
|
|
# check cache
|
|
|
|
cache = Cache.get('import_otrs_stats')
|
|
|
|
if cache
|
|
|
|
return cache
|
2014-03-11 09:23:56 +00:00
|
|
|
end
|
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
# retrive statistic
|
2015-05-07 12:10:38 +00:00
|
|
|
statistic = request_json( { Subaction: 'List' }, 1)
|
2015-05-07 11:57:19 +00:00
|
|
|
if statistic
|
|
|
|
Cache.write('import_otrs_stats', statistic)
|
2014-03-11 09:23:56 +00:00
|
|
|
end
|
2015-05-07 11:57:19 +00:00
|
|
|
statistic
|
|
|
|
end
|
2014-03-11 09:23:56 +00:00
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
return current import state
|
|
|
|
|
|
|
|
result = current_state
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
{
|
|
|
|
:Ticket => {
|
|
|
|
:total => 1234,
|
|
|
|
:done => 13,
|
|
|
|
},
|
|
|
|
:Base => {
|
|
|
|
:total => 1234,
|
|
|
|
:done => 13,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.current_state
|
2015-05-07 12:10:38 +00:00
|
|
|
data = statistic
|
2015-05-07 11:57:19 +00:00
|
|
|
base = Group.count + Ticket::State.count + Ticket::Priority.count
|
|
|
|
base_total = data['Queue'] + data['State'] + data['Priority']
|
|
|
|
user = User.count
|
|
|
|
user_total = data['User'] + data['CustomerUser']
|
|
|
|
data = {
|
|
|
|
Base: {
|
|
|
|
done: base,
|
|
|
|
total: base_total || 0,
|
|
|
|
},
|
|
|
|
User: {
|
|
|
|
done: user,
|
|
|
|
total: user_total || 0,
|
|
|
|
},
|
|
|
|
Ticket: {
|
|
|
|
done: Ticket.count,
|
|
|
|
total: data['Ticket'] || 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
data
|
2014-03-11 09:23:56 +00:00
|
|
|
end
|
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
#
|
|
|
|
# start import
|
|
|
|
#
|
|
|
|
# Import::OTRS.start
|
|
|
|
#
|
2013-01-07 08:46:38 +00:00
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
def self.start
|
|
|
|
log 'Start import...'
|
2013-01-07 08:46:38 +00:00
|
|
|
|
|
|
|
# check if system is in import mode
|
|
|
|
if !Setting.get('import_mode')
|
2015-05-07 11:27:07 +00:00
|
|
|
fail 'System is not in import mode!'
|
2013-01-07 08:46:38 +00:00
|
|
|
end
|
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
result = request_json({})
|
|
|
|
if !result['Success']
|
|
|
|
'API key not valid!'
|
|
|
|
end
|
|
|
|
|
|
|
|
# set settings
|
|
|
|
settings = load('SysConfig')
|
|
|
|
setting(settings)
|
|
|
|
|
|
|
|
# dynamic fields
|
|
|
|
dynamic_fields = load('DynamicField')
|
|
|
|
#settings(dynamic_fields, settings)
|
|
|
|
|
|
|
|
# email accounts
|
|
|
|
#accounts = load('PostMasterAccount')
|
|
|
|
#account(accounts)
|
|
|
|
|
|
|
|
# email filter
|
|
|
|
#filters = load('PostMasterFilter')
|
|
|
|
#filter(filters)
|
2013-01-07 08:46:38 +00:00
|
|
|
|
|
|
|
# create states
|
2015-05-07 11:57:19 +00:00
|
|
|
states = load('State')
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
state(states)
|
|
|
|
end
|
2013-01-07 08:46:38 +00:00
|
|
|
|
|
|
|
# create priorities
|
2015-05-07 11:57:19 +00:00
|
|
|
priorities = load('Priority')
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
priority(priorities)
|
|
|
|
end
|
2013-01-07 08:46:38 +00:00
|
|
|
|
|
|
|
# create groups
|
2015-05-07 11:57:19 +00:00
|
|
|
queues = load('Queue')
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
ticket_group(queues)
|
|
|
|
end
|
|
|
|
|
|
|
|
# get agents groups
|
|
|
|
groups = load('Group')
|
|
|
|
|
|
|
|
# get agents roles
|
|
|
|
roles = load('Role')
|
2013-01-07 08:46:38 +00:00
|
|
|
|
|
|
|
# create agents
|
2015-05-07 11:57:19 +00:00
|
|
|
users = load('User')
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
user(users, groups, roles, queues)
|
|
|
|
end
|
2013-01-07 08:46:38 +00:00
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
# create organizations
|
|
|
|
organizations = load('Customer')
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
organization(organizations)
|
|
|
|
end
|
2013-01-07 08:46:38 +00:00
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
# create customers
|
|
|
|
count = 0
|
|
|
|
steps = 50
|
|
|
|
run = true
|
|
|
|
while run
|
|
|
|
count += steps
|
|
|
|
records = load('CustomerUser', steps, count - steps)
|
|
|
|
if !records || !records[0]
|
|
|
|
log 'all customers imported.'
|
|
|
|
run = false
|
|
|
|
next
|
|
|
|
end
|
|
|
|
customer(records, organizations)
|
|
|
|
end
|
2013-01-25 23:06:21 +00:00
|
|
|
|
2013-02-01 19:58:53 +00:00
|
|
|
Thread.abort_on_exception = true
|
2015-05-07 11:57:19 +00:00
|
|
|
thread_count = 8
|
|
|
|
threads = {}
|
|
|
|
count = 0
|
|
|
|
locks = { User: {} }
|
2013-01-25 23:06:21 +00:00
|
|
|
(1..thread_count).each {|thread|
|
|
|
|
threads[thread] = Thread.new {
|
2015-05-07 11:57:19 +00:00
|
|
|
Thread.current[:thread_no] = thread
|
2013-01-25 23:06:21 +00:00
|
|
|
sleep thread * 3
|
2015-05-07 11:57:19 +00:00
|
|
|
log "Started import thread# #{thread} ..."
|
2013-02-17 20:59:57 +00:00
|
|
|
run = true
|
2015-05-07 11:57:19 +00:00
|
|
|
steps = 20
|
2013-02-17 20:59:57 +00:00
|
|
|
while run
|
2015-05-07 11:57:19 +00:00
|
|
|
count += steps
|
|
|
|
log "loading... thread# #{thread} ..."
|
|
|
|
offset = count - steps
|
|
|
|
if offset != 0
|
|
|
|
offset = count - steps + 1
|
|
|
|
end
|
|
|
|
records = load( 'Ticket', steps, count - steps)
|
|
|
|
if !records || !records[0]
|
|
|
|
log "... thread# #{thread}, no more work."
|
2013-02-17 20:59:57 +00:00
|
|
|
run = false
|
2015-05-07 11:57:19 +00:00
|
|
|
next
|
2013-02-17 20:59:57 +00:00
|
|
|
end
|
2015-05-07 11:57:19 +00:00
|
|
|
_ticket_result(records, locks, thread)
|
2013-01-25 23:06:21 +00:00
|
|
|
end
|
2015-05-07 11:57:19 +00:00
|
|
|
ActiveRecord::Base.connection.close
|
2013-01-25 23:06:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
(1..thread_count).each {|thread|
|
|
|
|
threads[thread].join
|
|
|
|
}
|
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
Setting.set( 'system_init_done', true )
|
|
|
|
#Setting.set( 'import_mode', false )
|
|
|
|
|
|
|
|
true
|
2015-05-04 14:21:13 +00:00
|
|
|
end
|
2013-01-07 08:46:38 +00:00
|
|
|
|
2013-05-07 20:45:00 +00:00
|
|
|
def self.diff_worker
|
2013-03-05 06:45:14 +00:00
|
|
|
return if !Setting.get('import_mode')
|
2013-03-10 16:45:48 +00:00
|
|
|
return if Setting.get('import_otrs_endpoint') == 'http://otrs_host/otrs'
|
2015-05-07 12:10:38 +00:00
|
|
|
diff
|
2013-02-19 19:04:35 +00:00
|
|
|
end
|
|
|
|
|
2013-02-17 20:59:57 +00:00
|
|
|
def self.diff
|
2015-05-07 11:57:19 +00:00
|
|
|
log 'Start diff...'
|
2013-02-17 20:59:57 +00:00
|
|
|
|
|
|
|
# check if system is in import mode
|
|
|
|
if !Setting.get('import_mode')
|
2015-05-07 11:27:07 +00:00
|
|
|
fail 'System is not in import mode!'
|
2013-02-17 20:59:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# create states
|
2015-05-07 11:57:19 +00:00
|
|
|
states = load('State')
|
|
|
|
state(states)
|
2013-02-17 20:59:57 +00:00
|
|
|
|
|
|
|
# create priorities
|
2015-05-07 11:57:19 +00:00
|
|
|
priorities = load('Priority')
|
|
|
|
priority(priorities)
|
2013-02-17 20:59:57 +00:00
|
|
|
|
|
|
|
# create groups
|
2015-05-07 11:57:19 +00:00
|
|
|
queues = load('Queue')
|
|
|
|
ticket_group(queues)
|
|
|
|
|
|
|
|
# get agents groups
|
|
|
|
groups = load('Group')
|
|
|
|
|
|
|
|
# get agents roles
|
|
|
|
roles = load('Role')
|
2013-02-17 20:59:57 +00:00
|
|
|
|
|
|
|
# create agents
|
2015-05-07 11:57:19 +00:00
|
|
|
users = load('User')
|
|
|
|
user(users, groups, roles, queues)
|
2013-02-17 20:59:57 +00:00
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
# create organizations
|
|
|
|
organizations = load('Customer')
|
|
|
|
organization(organizations)
|
2013-02-17 20:59:57 +00:00
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
# get changed tickets
|
2015-05-07 12:10:38 +00:00
|
|
|
ticket_diff
|
2013-02-17 20:59:57 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
def self.ticket_diff
|
|
|
|
count = 0
|
|
|
|
run = true
|
|
|
|
steps = 20
|
|
|
|
locks = { User: {} }
|
|
|
|
while run
|
|
|
|
count += steps
|
|
|
|
log 'loading... diff ...'
|
|
|
|
offset = count - steps
|
|
|
|
if offset != 0
|
|
|
|
offset = count - steps + 1
|
|
|
|
end
|
|
|
|
records = load( 'Ticket', steps, count - steps, 1 )
|
|
|
|
if !records || !records[0]
|
|
|
|
log '... no more work.'
|
|
|
|
run = false
|
|
|
|
next
|
|
|
|
end
|
|
|
|
_ticket_result(records, locks)
|
|
|
|
end
|
2013-01-07 08:46:38 +00:00
|
|
|
|
2013-02-17 20:59:57 +00:00
|
|
|
end
|
2013-06-04 12:52:56 +00:00
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
def self._ticket_result(result, locks, _thread = '-')
|
2013-01-07 08:46:38 +00:00
|
|
|
map = {
|
2015-04-27 13:42:53 +00:00
|
|
|
Ticket: {
|
|
|
|
Changed: :updated_at,
|
|
|
|
Created: :created_at,
|
|
|
|
CreateBy: :created_by_id,
|
|
|
|
TicketNumber: :number,
|
|
|
|
QueueID: :group_id,
|
|
|
|
StateID: :state_id,
|
|
|
|
PriorityID: :priority_id,
|
|
|
|
Owner: :owner,
|
|
|
|
CustomerUserID: :customer,
|
|
|
|
Title: :title,
|
|
|
|
TicketID: :id,
|
|
|
|
FirstResponse: :first_response,
|
2015-05-07 20:49:15 +00:00
|
|
|
#FirstResponseTimeDestinationDate: :first_response_escal_date,
|
|
|
|
#FirstResponseInMin: :first_response_in_min,
|
|
|
|
#FirstResponseDiffInMin: :first_response_diff_in_min,
|
2015-04-27 13:42:53 +00:00
|
|
|
Closed: :close_time,
|
2015-05-07 20:49:15 +00:00
|
|
|
#SoltutionTimeDestinationDate: :close_time_escal_date,
|
|
|
|
#CloseTimeInMin: :close_time_in_min,
|
|
|
|
#CloseTimeDiffInMin: :close_time_diff_in_min,
|
2013-01-07 08:46:38 +00:00
|
|
|
},
|
2015-04-27 13:42:53 +00:00
|
|
|
Article: {
|
|
|
|
SenderType: :sender,
|
|
|
|
ArticleType: :type,
|
|
|
|
TicketID: :ticket_id,
|
|
|
|
ArticleID: :id,
|
|
|
|
Body: :body,
|
|
|
|
From: :from,
|
|
|
|
To: :to,
|
|
|
|
Cc: :cc,
|
|
|
|
Subject: :subject,
|
|
|
|
InReplyTo: :in_reply_to,
|
|
|
|
MessageID: :message_id,
|
2015-05-07 20:49:15 +00:00
|
|
|
#ReplyTo: :reply_to,
|
2015-04-27 13:42:53 +00:00
|
|
|
References: :references,
|
|
|
|
Changed: :updated_at,
|
|
|
|
Created: :created_at,
|
|
|
|
ChangedBy: :updated_by_id,
|
|
|
|
CreatedBy: :created_by_id,
|
2013-01-07 08:46:38 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
result.each {|record|
|
2013-02-01 19:58:53 +00:00
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
# cleanup values
|
|
|
|
_cleanup(record)
|
2013-02-01 19:58:53 +00:00
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
_utf8_encode(record)
|
|
|
|
|
|
|
|
ticket_new = {
|
|
|
|
title: '',
|
|
|
|
created_by_id: 1,
|
|
|
|
updated_by_id: 1,
|
|
|
|
}
|
|
|
|
map[:Ticket].each { |key, value|
|
|
|
|
next if !record.key?(key.to_s)
|
|
|
|
ticket_new[value] = record[key.to_s]
|
|
|
|
}
|
|
|
|
ticket_old = Ticket.where( id: ticket_new[:id] ).first
|
|
|
|
|
|
|
|
# find owner
|
|
|
|
if ticket_new[:owner]
|
|
|
|
user = User.lookup( login: ticket_new[:owner].downcase )
|
|
|
|
if user
|
|
|
|
ticket_new[:owner_id] = user.id
|
2013-02-01 19:58:53 +00:00
|
|
|
else
|
2015-05-07 11:57:19 +00:00
|
|
|
ticket_new[:owner_id] = 1
|
2013-01-07 08:46:38 +00:00
|
|
|
end
|
2015-05-07 11:57:19 +00:00
|
|
|
ticket_new.delete(:owner)
|
|
|
|
end
|
|
|
|
|
|
|
|
# find customer
|
|
|
|
if ticket_new[:customer]
|
|
|
|
user = User.lookup( login: ticket_new[:customer].downcase )
|
|
|
|
if user
|
|
|
|
ticket_new[:customer_id] = user.id
|
2013-02-01 19:58:53 +00:00
|
|
|
else
|
2015-05-07 11:57:19 +00:00
|
|
|
ticket_new[:customer_id] = 1
|
2013-01-07 08:46:38 +00:00
|
|
|
end
|
2015-05-07 11:57:19 +00:00
|
|
|
ticket_new.delete(:customer)
|
|
|
|
else
|
|
|
|
ticket_new[:customer_id] = 1
|
|
|
|
end
|
2013-01-07 08:46:38 +00:00
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
# set state types
|
|
|
|
if ticket_old
|
|
|
|
log "update Ticket.find(#{ticket_new[:id]})"
|
|
|
|
ticket_old.update_attributes(ticket_new)
|
|
|
|
else
|
|
|
|
log "add Ticket.find(#{ticket_new[:id]})"
|
|
|
|
ticket = Ticket.new(ticket_new)
|
|
|
|
ticket.id = ticket_new[:id]
|
|
|
|
ticket.save
|
|
|
|
end
|
|
|
|
|
|
|
|
# utf8 encode
|
|
|
|
record['Articles'].each { |article|
|
|
|
|
_utf8_encode(article)
|
|
|
|
}
|
|
|
|
|
|
|
|
# lookup customers to create first
|
|
|
|
record['Articles'].each { |article|
|
|
|
|
_article_based_customers(article, locks)
|
|
|
|
}
|
|
|
|
|
|
|
|
ActiveRecord::Base.transaction do
|
2013-02-01 19:58:53 +00:00
|
|
|
record['Articles'].each { |article|
|
2013-06-04 12:52:56 +00:00
|
|
|
|
2013-02-01 19:58:53 +00:00
|
|
|
# get article values
|
|
|
|
article_new = {
|
2015-04-27 13:42:53 +00:00
|
|
|
created_by_id: 1,
|
|
|
|
updated_by_id: 1,
|
2013-02-01 19:58:53 +00:00
|
|
|
}
|
2015-05-07 11:57:19 +00:00
|
|
|
|
2015-04-27 14:53:29 +00:00
|
|
|
map[:Article].each { |key, value|
|
2015-05-07 11:57:19 +00:00
|
|
|
next if !article.key?(key.to_s)
|
|
|
|
article_new[value] = article[key.to_s]
|
2013-02-01 19:58:53 +00:00
|
|
|
}
|
2013-06-04 12:52:56 +00:00
|
|
|
|
2014-06-08 22:01:20 +00:00
|
|
|
if article_new[:sender] == 'customer'
|
2015-04-27 13:42:53 +00:00
|
|
|
article_new[:sender_id] = Ticket::Article::Sender.lookup( name: 'Customer' ).id
|
2014-06-08 22:01:20 +00:00
|
|
|
article_new.delete( :sender )
|
2013-01-07 22:27:32 +00:00
|
|
|
end
|
2014-06-08 22:01:20 +00:00
|
|
|
if article_new[:sender] == 'agent'
|
2015-04-27 13:42:53 +00:00
|
|
|
article_new[:sender_id] = Ticket::Article::Sender.lookup( name: 'Agent' ).id
|
2014-06-08 22:01:20 +00:00
|
|
|
article_new.delete( :sender )
|
2013-02-01 19:58:53 +00:00
|
|
|
end
|
2014-06-08 22:01:20 +00:00
|
|
|
if article_new[:sender] == 'system'
|
2015-04-27 13:42:53 +00:00
|
|
|
article_new[:sender_id] = Ticket::Article::Sender.lookup( name: 'System' ).id
|
2014-06-08 22:01:20 +00:00
|
|
|
article_new.delete( :sender )
|
2013-02-01 19:58:53 +00:00
|
|
|
end
|
2013-06-04 12:52:56 +00:00
|
|
|
|
2014-06-08 22:01:20 +00:00
|
|
|
if article_new[:type] == 'email-external'
|
2015-04-27 13:42:53 +00:00
|
|
|
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'email' ).id
|
2013-02-01 19:58:53 +00:00
|
|
|
article_new[:internal] = false
|
2014-06-08 22:01:20 +00:00
|
|
|
elsif article_new[:type] == 'email-internal'
|
2015-04-27 13:42:53 +00:00
|
|
|
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'email' ).id
|
2013-02-01 19:58:53 +00:00
|
|
|
article_new[:internal] = true
|
2014-06-08 22:01:20 +00:00
|
|
|
elsif article_new[:type] == 'note-external'
|
2015-04-27 13:42:53 +00:00
|
|
|
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'note' ).id
|
2013-02-01 19:58:53 +00:00
|
|
|
article_new[:internal] = false
|
2014-06-08 22:01:20 +00:00
|
|
|
elsif article_new[:type] == 'note-internal'
|
2015-04-27 13:42:53 +00:00
|
|
|
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'note' ).id
|
2013-02-01 19:58:53 +00:00
|
|
|
article_new[:internal] = true
|
2014-06-08 22:01:20 +00:00
|
|
|
elsif article_new[:type] == 'phone'
|
2015-04-27 13:42:53 +00:00
|
|
|
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'phone' ).id
|
2013-02-01 19:58:53 +00:00
|
|
|
article_new[:internal] = false
|
2014-06-08 22:01:20 +00:00
|
|
|
elsif article_new[:type] == 'webrequest'
|
2015-04-27 13:42:53 +00:00
|
|
|
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'web' ).id
|
2013-02-01 19:58:53 +00:00
|
|
|
article_new[:internal] = false
|
|
|
|
else
|
2014-06-08 22:01:20 +00:00
|
|
|
article_new[:type_id] = 9
|
2013-02-01 19:58:53 +00:00
|
|
|
end
|
2014-06-08 22:01:20 +00:00
|
|
|
article_new.delete( :type )
|
2015-04-27 13:42:53 +00:00
|
|
|
article_old = Ticket::Article.where( id: article_new[:id] ).first
|
2015-05-07 11:57:19 +00:00
|
|
|
|
2013-02-01 19:58:53 +00:00
|
|
|
# set state types
|
|
|
|
if article_old
|
2015-05-07 11:57:19 +00:00
|
|
|
log "update Ticket::Article.find(#{article_new[:id]})"
|
2013-02-01 19:58:53 +00:00
|
|
|
article_old.update_attributes(article_new)
|
|
|
|
else
|
2015-05-07 11:57:19 +00:00
|
|
|
log "add Ticket::Article.find(#{article_new[:id]})"
|
2013-02-01 19:58:53 +00:00
|
|
|
article = Ticket::Article.new(article_new)
|
|
|
|
article.id = article_new[:id]
|
|
|
|
article.save
|
|
|
|
end
|
|
|
|
}
|
2015-05-07 11:57:19 +00:00
|
|
|
end
|
2013-06-04 12:52:56 +00:00
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
#puts "HS: #{record['History'].inspect}"
|
|
|
|
record['History'].each { |history|
|
|
|
|
if history['HistoryType'] == 'NewTicket'
|
|
|
|
#puts "HS.add( #{history.inspect} )"
|
|
|
|
res = History.add(
|
|
|
|
id: history['HistoryID'],
|
|
|
|
o_id: history['TicketID'],
|
|
|
|
history_type: 'created',
|
|
|
|
history_object: 'Ticket',
|
|
|
|
created_at: history['CreateTime'],
|
|
|
|
created_by_id: history['CreateBy']
|
|
|
|
)
|
|
|
|
#puts "res #{res.inspect}"
|
|
|
|
end
|
|
|
|
if history['HistoryType'] == 'StateUpdate'
|
|
|
|
data = history['Name']
|
|
|
|
# "%%new%%open%%"
|
|
|
|
from = nil
|
|
|
|
to = nil
|
|
|
|
if data =~ /%%(.+?)%%(.+?)%%/
|
|
|
|
from = $1
|
|
|
|
to = $2
|
|
|
|
state_from = Ticket::State.lookup( name: from )
|
|
|
|
state_to = Ticket::State.lookup( name: to )
|
|
|
|
if state_from
|
|
|
|
from_id = state_from.id
|
2013-02-01 19:58:53 +00:00
|
|
|
end
|
2015-05-07 11:57:19 +00:00
|
|
|
if state_to
|
|
|
|
to_id = state_to.id
|
2013-02-01 19:58:53 +00:00
|
|
|
end
|
|
|
|
end
|
2015-05-07 11:57:19 +00:00
|
|
|
History.add(
|
|
|
|
id: history['HistoryID'],
|
|
|
|
o_id: history['TicketID'],
|
|
|
|
history_type: 'updated',
|
|
|
|
history_object: 'Ticket',
|
|
|
|
history_attribute: 'state',
|
|
|
|
value_from: from,
|
|
|
|
id_from: from_id,
|
|
|
|
value_to: to,
|
|
|
|
id_to: to_id,
|
|
|
|
created_at: history['CreateTime'],
|
|
|
|
created_by_id: history['CreateBy']
|
|
|
|
)
|
|
|
|
end
|
|
|
|
if history['HistoryType'] == 'Move'
|
|
|
|
data = history['Name']
|
|
|
|
# "%%Queue1%%5%%Postmaster%%1"
|
|
|
|
from = nil
|
|
|
|
to = nil
|
|
|
|
if data =~ /%%(.+?)%%(.+?)%%(.+?)%%(.+?)$/
|
|
|
|
from = $1
|
|
|
|
from_id = $2
|
|
|
|
to = $3
|
|
|
|
to_id = $4
|
2013-02-01 19:58:53 +00:00
|
|
|
end
|
2015-05-07 09:04:40 +00:00
|
|
|
History.add(
|
|
|
|
id: history['HistoryID'],
|
2015-05-07 11:57:19 +00:00
|
|
|
o_id: history['TicketID'],
|
|
|
|
history_type: 'updated',
|
|
|
|
history_object: 'Ticket',
|
|
|
|
history_attribute: 'group',
|
|
|
|
value_from: from,
|
|
|
|
value_to: to,
|
|
|
|
id_from: from_id,
|
|
|
|
id_to: to_id,
|
2015-05-07 09:04:40 +00:00
|
|
|
created_at: history['CreateTime'],
|
|
|
|
created_by_id: history['CreateBy']
|
|
|
|
)
|
2015-05-07 11:57:19 +00:00
|
|
|
end
|
|
|
|
if history['HistoryType'] == 'PriorityUpdate'
|
|
|
|
data = history['Name']
|
|
|
|
# "%%3 normal%%3%%5 very high%%5"
|
|
|
|
from = nil
|
|
|
|
to = nil
|
|
|
|
if data =~ /%%(.+?)%%(.+?)%%(.+?)%%(.+?)$/
|
|
|
|
from = $1
|
|
|
|
from_id = $2
|
|
|
|
to = $3
|
|
|
|
to_id = $4
|
|
|
|
end
|
|
|
|
History.add(
|
|
|
|
id: history['HistoryID'],
|
|
|
|
o_id: history['TicketID'],
|
|
|
|
history_type: 'updated',
|
|
|
|
history_object: 'Ticket',
|
|
|
|
history_attribute: 'priority',
|
|
|
|
value_from: from,
|
|
|
|
value_to: to,
|
|
|
|
id_from: from_id,
|
|
|
|
id_to: to_id,
|
|
|
|
created_at: history['CreateTime'],
|
|
|
|
created_by_id: history['CreateBy']
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
next if !history['ArticleID']
|
|
|
|
next if history['ArticleID'] == 0
|
|
|
|
|
|
|
|
History.add(
|
|
|
|
id: history['HistoryID'],
|
|
|
|
o_id: history['ArticleID'],
|
|
|
|
history_type: 'created',
|
|
|
|
history_object: 'Ticket::Article',
|
|
|
|
related_o_id: history['TicketID'],
|
|
|
|
related_history_object: 'Ticket',
|
|
|
|
created_at: history['CreateTime'],
|
|
|
|
created_by_id: history['CreateBy']
|
|
|
|
)
|
|
|
|
}
|
2013-01-07 08:46:38 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
# sync ticket states
|
|
|
|
def self.state(records)
|
2013-01-07 08:46:38 +00:00
|
|
|
map = {
|
2015-04-27 13:42:53 +00:00
|
|
|
ChangeTime: :updated_at,
|
|
|
|
CreateTime: :created_at,
|
|
|
|
CreateBy: :created_by_id,
|
|
|
|
ChangeBy: :updated_by_id,
|
|
|
|
Name: :name,
|
|
|
|
ID: :id,
|
|
|
|
ValidID: :active,
|
|
|
|
Comment: :note,
|
2015-04-30 17:54:08 +00:00
|
|
|
}
|
2013-01-07 08:46:38 +00:00
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
# rename states to handle not uniq issues
|
2013-01-07 08:46:38 +00:00
|
|
|
Ticket::State.all.each {|state|
|
|
|
|
state.name = state.name + '_tmp'
|
|
|
|
state.save
|
|
|
|
}
|
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
records.each { |state|
|
2013-01-07 08:46:38 +00:00
|
|
|
_set_valid(state)
|
|
|
|
|
|
|
|
# get new attributes
|
|
|
|
state_new = {
|
2015-04-27 13:42:53 +00:00
|
|
|
created_by_id: 1,
|
|
|
|
updated_by_id: 1,
|
2013-01-07 08:46:38 +00:00
|
|
|
}
|
2015-04-27 14:53:29 +00:00
|
|
|
map.each { |key, value|
|
2015-05-07 11:57:19 +00:00
|
|
|
next if !state.key?(key.to_s)
|
|
|
|
state_new[value] = state[key.to_s]
|
2013-01-07 08:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# check if state already exists
|
2015-04-27 13:42:53 +00:00
|
|
|
state_old = Ticket::State.where( id: state_new[:id] ).first
|
2013-01-07 08:46:38 +00:00
|
|
|
|
|
|
|
# set state types
|
|
|
|
if state['TypeName'] == 'pending auto'
|
|
|
|
state['TypeName'] = 'pending action'
|
|
|
|
end
|
2015-04-27 13:42:53 +00:00
|
|
|
state_type = Ticket::StateType.where( name: state['TypeName'] ).first
|
2014-06-08 22:01:20 +00:00
|
|
|
state_new[:state_type_id] = state_type.id
|
2013-01-07 08:46:38 +00:00
|
|
|
if state_old
|
|
|
|
state_old.update_attributes(state_new)
|
|
|
|
else
|
|
|
|
state = Ticket::State.new(state_new)
|
|
|
|
state.id = state_new[:id]
|
|
|
|
state.save
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
# sync ticket priorities
|
|
|
|
def self.priority(records)
|
|
|
|
|
2013-01-07 08:46:38 +00:00
|
|
|
map = {
|
2015-04-27 13:42:53 +00:00
|
|
|
ChangeTime: :updated_at,
|
|
|
|
CreateTime: :created_at,
|
|
|
|
CreateBy: :created_by_id,
|
|
|
|
ChangeBy: :updated_by_id,
|
|
|
|
Name: :name,
|
|
|
|
ID: :id,
|
|
|
|
ValidID: :active,
|
|
|
|
Comment: :note,
|
2015-04-30 17:54:08 +00:00
|
|
|
}
|
2013-01-07 08:46:38 +00:00
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
records.each { |priority|
|
2013-01-07 08:46:38 +00:00
|
|
|
_set_valid(priority)
|
|
|
|
|
|
|
|
# get new attributes
|
|
|
|
priority_new = {
|
2015-04-27 13:42:53 +00:00
|
|
|
created_by_id: 1,
|
|
|
|
updated_by_id: 1,
|
2013-01-07 08:46:38 +00:00
|
|
|
}
|
2015-04-27 14:53:29 +00:00
|
|
|
map.each { |key, value|
|
2015-05-07 11:57:19 +00:00
|
|
|
next if !priority.key?(key.to_s)
|
|
|
|
priority_new[value] = priority[key.to_s]
|
2013-01-07 08:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# check if state already exists
|
2015-04-27 13:42:53 +00:00
|
|
|
priority_old = Ticket::Priority.where( id: priority_new[:id] ).first
|
2013-01-07 08:46:38 +00:00
|
|
|
|
|
|
|
# set state types
|
|
|
|
if priority_old
|
|
|
|
priority_old.update_attributes(priority_new)
|
|
|
|
else
|
|
|
|
priority = Ticket::Priority.new(priority_new)
|
|
|
|
priority.id = priority_new[:id]
|
|
|
|
priority.save
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
# sync ticket groups / queues
|
|
|
|
def self.ticket_group(records)
|
2013-01-07 08:46:38 +00:00
|
|
|
map = {
|
2015-04-27 13:42:53 +00:00
|
|
|
ChangeTime: :updated_at,
|
|
|
|
CreateTime: :created_at,
|
|
|
|
CreateBy: :created_by_id,
|
|
|
|
ChangeBy: :updated_by_id,
|
|
|
|
Name: :name,
|
|
|
|
QueueID: :id,
|
|
|
|
ValidID: :active,
|
|
|
|
Comment: :note,
|
2015-04-30 17:54:08 +00:00
|
|
|
}
|
2013-01-07 08:46:38 +00:00
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
records.each { |group|
|
2013-01-07 08:46:38 +00:00
|
|
|
_set_valid(group)
|
|
|
|
|
|
|
|
# get new attributes
|
|
|
|
group_new = {
|
2015-04-27 13:42:53 +00:00
|
|
|
created_by_id: 1,
|
|
|
|
updated_by_id: 1,
|
2013-01-07 08:46:38 +00:00
|
|
|
}
|
2015-04-27 14:53:29 +00:00
|
|
|
map.each { |key, value|
|
2015-05-07 11:57:19 +00:00
|
|
|
next if !group.key?(key.to_s)
|
|
|
|
group_new[value] = group[key.to_s]
|
2013-01-07 08:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# check if state already exists
|
2015-04-27 13:42:53 +00:00
|
|
|
group_old = Group.where( id: group_new[:id] ).first
|
2013-01-07 08:46:38 +00:00
|
|
|
|
|
|
|
# set state types
|
|
|
|
if group_old
|
|
|
|
group_old.update_attributes(group_new)
|
|
|
|
else
|
|
|
|
group = Group.new(group_new)
|
|
|
|
group.id = group_new[:id]
|
|
|
|
group.save
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
2015-05-07 11:57:19 +00:00
|
|
|
|
|
|
|
# sync agents
|
|
|
|
def self.user(records, groups, roles, queues)
|
|
|
|
|
2013-01-07 08:46:38 +00:00
|
|
|
map = {
|
2015-04-27 13:42:53 +00:00
|
|
|
ChangeTime: :updated_at,
|
|
|
|
CreateTime: :created_at,
|
|
|
|
CreateBy: :created_by_id,
|
|
|
|
ChangeBy: :updated_by_id,
|
|
|
|
UserID: :id,
|
|
|
|
ValidID: :active,
|
|
|
|
Comment: :note,
|
|
|
|
UserEmail: :email,
|
|
|
|
UserFirstname: :firstname,
|
|
|
|
UserLastname: :lastname,
|
|
|
|
UserLogin: :login,
|
|
|
|
UserPw: :password,
|
2015-04-30 17:54:08 +00:00
|
|
|
}
|
2013-01-07 08:46:38 +00:00
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
records.each { |user|
|
2015-04-30 17:14:51 +00:00
|
|
|
_set_valid(user)
|
2013-01-07 08:46:38 +00:00
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
# get roles
|
|
|
|
role_ids = get_roles_ids(user, groups, roles, queues)
|
|
|
|
|
|
|
|
# get groups
|
|
|
|
group_ids = get_queue_ids(user, groups, roles, queues)
|
|
|
|
|
2015-04-30 17:14:51 +00:00
|
|
|
# get new attributes
|
|
|
|
user_new = {
|
|
|
|
created_by_id: 1,
|
|
|
|
updated_by_id: 1,
|
|
|
|
source: 'OTRS Import',
|
2015-05-07 11:57:19 +00:00
|
|
|
role_ids: role_ids,
|
|
|
|
group_ids: group_ids,
|
2015-04-30 17:14:51 +00:00
|
|
|
}
|
|
|
|
map.each { |key, value|
|
2015-05-07 11:57:19 +00:00
|
|
|
next if !user.key?(key.to_s)
|
|
|
|
user_new[value] = user[key.to_s]
|
2015-04-30 17:14:51 +00:00
|
|
|
}
|
2013-01-07 08:46:38 +00:00
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
# set pw
|
|
|
|
if user_new[:password]
|
|
|
|
user_new[:password] = "{sha2}#{user_new[:password]}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# check if agent already exists
|
2015-04-30 17:14:51 +00:00
|
|
|
user_old = User.where( id: user_new[:id] ).first
|
2013-01-07 08:46:38 +00:00
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
# check if login is already used
|
|
|
|
login_in_use = User.where( "login = ? AND id != #{user_new[:id]}", user_new[:login].downcase ).count
|
|
|
|
if login_in_use > 0
|
|
|
|
user_new[:login] = "#{user_new[:login]}_#{user_new[:id]}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# create / update agent
|
2015-04-30 17:14:51 +00:00
|
|
|
if user_old
|
2015-05-07 11:57:19 +00:00
|
|
|
log "update User.find(#{user_old[:id]})"
|
|
|
|
|
|
|
|
# only update roles if different (reduce sql statements)
|
|
|
|
if user_old.role_ids == user_new[:role_ids]
|
|
|
|
user_new.delete( :role_ids )
|
|
|
|
end
|
|
|
|
|
2015-04-30 17:14:51 +00:00
|
|
|
user_old.update_attributes(user_new)
|
|
|
|
else
|
2015-05-07 11:57:19 +00:00
|
|
|
log "add User.find(#{user_new[:id]})"
|
2015-04-30 17:14:51 +00:00
|
|
|
user = User.new(user_new)
|
|
|
|
user.id = user_new[:id]
|
|
|
|
user.save
|
|
|
|
end
|
2013-01-07 08:46:38 +00:00
|
|
|
}
|
|
|
|
end
|
2015-05-07 11:57:19 +00:00
|
|
|
|
|
|
|
def self.get_queue_ids(user, _groups, _roles, queues)
|
|
|
|
queue_ids = []
|
|
|
|
|
|
|
|
# lookup by groups
|
|
|
|
user['GroupIDs'].each {|group_id, permissions|
|
|
|
|
queues.each {|queue_lookup|
|
|
|
|
|
|
|
|
next if queue_lookup['GroupID'] != group_id
|
|
|
|
next if !permissions
|
|
|
|
next if !permissions.include?('rw')
|
|
|
|
|
|
|
|
queue_ids.push queue_lookup['QueueID']
|
2015-04-30 17:54:08 +00:00
|
|
|
}
|
2015-05-07 11:57:19 +00:00
|
|
|
}
|
2013-01-07 08:46:38 +00:00
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
# lookup by roles
|
2013-01-07 08:46:38 +00:00
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
# roles of user
|
2015-05-07 20:49:15 +00:00
|
|
|
# groups of roles
|
|
|
|
# queues of group
|
2013-01-25 23:06:21 +00:00
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
queue_ids
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.get_roles_ids(user, groups, roles, _queues)
|
|
|
|
roles = ['Agent']
|
|
|
|
role_ids = []
|
|
|
|
user['GroupIDs'].each {|group_id, permissions|
|
|
|
|
groups.each {|group_lookup|
|
|
|
|
|
|
|
|
next if group_id != group_lookup['ID']
|
|
|
|
next if !permissions
|
|
|
|
|
|
|
|
if group_lookup['Name'] == 'admin' && permissions.include?('rw')
|
|
|
|
roles.push 'Admin'
|
|
|
|
end
|
|
|
|
|
|
|
|
next if group_lookup['Name'] !~ /^(stats|report)/
|
|
|
|
next if !( permissions.include?('ro') || permissions.include?('rw') )
|
|
|
|
|
|
|
|
roles.push 'Report'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
roles.each {|role|
|
|
|
|
role_lookup = Role.lookup( name: role )
|
|
|
|
next if !role_lookup
|
|
|
|
role_ids.push role_lookup.id
|
|
|
|
}
|
|
|
|
role_ids
|
|
|
|
end
|
|
|
|
|
|
|
|
# sync customers
|
|
|
|
|
|
|
|
def self.customer(records, organizations)
|
|
|
|
map = {
|
|
|
|
ChangeTime: :updated_at,
|
|
|
|
CreateTime: :created_at,
|
|
|
|
CreateBy: :created_by_id,
|
|
|
|
ChangeBy: :updated_by_id,
|
|
|
|
ValidID: :active,
|
|
|
|
UserComment: :note,
|
|
|
|
UserEmail: :email,
|
|
|
|
UserFirstname: :firstname,
|
|
|
|
UserLastname: :lastname,
|
|
|
|
UserLogin: :login,
|
|
|
|
UserPassword: :password,
|
|
|
|
UserPhone: :phone,
|
|
|
|
UserFax: :fax,
|
|
|
|
UserMobile: :mobile,
|
|
|
|
UserStreet: :street,
|
|
|
|
UserZip: :zip,
|
|
|
|
UserCity: :city,
|
|
|
|
UserCountry: :country,
|
|
|
|
}
|
|
|
|
|
|
|
|
role_agent = Role.lookup( name: 'Agent' )
|
|
|
|
role_customer = Role.lookup( name: 'Customer' )
|
|
|
|
|
|
|
|
records.each { |user|
|
|
|
|
_set_valid(user)
|
|
|
|
|
|
|
|
# get new attributes
|
|
|
|
user_new = {
|
|
|
|
created_by_id: 1,
|
|
|
|
updated_by_id: 1,
|
|
|
|
source: 'OTRS Import',
|
|
|
|
organization_id: get_organization_id(user, organizations),
|
|
|
|
role_ids: [ role_customer.id ],
|
|
|
|
}
|
|
|
|
map.each { |key, value|
|
|
|
|
next if !user.key?(key.to_s)
|
|
|
|
user_new[value] = user[key.to_s]
|
|
|
|
}
|
|
|
|
|
|
|
|
# check if customer already exists
|
|
|
|
user_old = User.where( login: user_new[:login] ).first
|
2013-01-07 08:46:38 +00:00
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
# create / update agent
|
|
|
|
if user_old
|
|
|
|
|
|
|
|
# do not update user if it is already agent
|
|
|
|
if !user_old.role_ids.include?( role_agent.id )
|
2013-01-07 08:46:38 +00:00
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
# only update roles if different (reduce sql statements)
|
|
|
|
if user_old.role_ids == user_new[:role_ids]
|
|
|
|
user_new.delete( :role_ids )
|
|
|
|
end
|
|
|
|
log "update User.find(#{user_old[:id]})"
|
2013-01-07 08:46:38 +00:00
|
|
|
user_old.update_attributes(user_new)
|
|
|
|
end
|
2015-05-07 11:57:19 +00:00
|
|
|
else
|
|
|
|
log "add User.find(#{user_new[:id]})"
|
|
|
|
user = User.new(user_new)
|
|
|
|
user.save
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.get_organization_id(user, organizations)
|
|
|
|
organization_id = nil
|
|
|
|
if user['UserCustomerID']
|
|
|
|
organizations.each {|organization|
|
|
|
|
next if user['UserCustomerID'] != organization['CustomerID']
|
|
|
|
organization = Organization.where(name: organization['CustomerCompanyName'] ).first
|
|
|
|
organization_id = organization.id
|
2013-01-07 08:46:38 +00:00
|
|
|
}
|
|
|
|
end
|
2015-05-07 11:57:19 +00:00
|
|
|
organization_id
|
|
|
|
end
|
|
|
|
|
|
|
|
# sync organizations
|
|
|
|
def self.organization(records)
|
|
|
|
map = {
|
|
|
|
ChangeTime: :updated_at,
|
|
|
|
CreateTime: :created_at,
|
|
|
|
CreateBy: :created_by_id,
|
|
|
|
ChangeBy: :updated_by_id,
|
|
|
|
CustomerCompanyName: :name,
|
|
|
|
ValidID: :active,
|
|
|
|
CustomerCompanyComment: :note,
|
|
|
|
}
|
|
|
|
|
|
|
|
records.each { |organization|
|
|
|
|
_set_valid(organization)
|
|
|
|
|
|
|
|
# get new attributes
|
|
|
|
organization_new = {
|
|
|
|
created_by_id: 1,
|
|
|
|
updated_by_id: 1,
|
|
|
|
}
|
|
|
|
map.each { |key, value|
|
|
|
|
next if !organization.key?(key.to_s)
|
|
|
|
organization_new[value] = organization[key.to_s]
|
|
|
|
}
|
|
|
|
|
|
|
|
# check if state already exists
|
|
|
|
organization_old = Organization.where( name: organization_new[:name] ).first
|
|
|
|
|
|
|
|
# set state types
|
|
|
|
if organization_old
|
|
|
|
organization_old.update_attributes(organization_new)
|
|
|
|
else
|
|
|
|
organization = Organization.new(organization_new)
|
|
|
|
organization.id = organization_new[:id]
|
|
|
|
organization.save
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# sync settings
|
|
|
|
def self.setting(records)
|
|
|
|
|
|
|
|
records.each { |setting|
|
|
|
|
|
|
|
|
# fqdn
|
|
|
|
if setting['Key'] == 'FQDN'
|
|
|
|
Setting.set( 'fqdn', setting['Value'] )
|
|
|
|
end
|
|
|
|
|
|
|
|
# http type
|
|
|
|
if setting['Key'] == 'HttpType'
|
|
|
|
Setting.set( 'http_type', setting['Value'] )
|
|
|
|
end
|
|
|
|
|
|
|
|
# system id
|
|
|
|
if setting['Key'] == 'SystemID'
|
|
|
|
Setting.set( 'system_id', setting['Value'] )
|
|
|
|
end
|
|
|
|
|
|
|
|
# organization
|
|
|
|
if setting['Key'] == 'Organization'
|
|
|
|
Setting.set( 'organization', setting['Value'] )
|
|
|
|
end
|
|
|
|
|
|
|
|
# sending emails
|
|
|
|
if setting['Key'] == 'SendmailModule'
|
|
|
|
# TODO
|
|
|
|
end
|
|
|
|
|
|
|
|
# number generater
|
|
|
|
if setting['Key'] == 'Ticket::NumberGenerator'
|
|
|
|
if setting['Value'] == 'Kernel::System::Ticket::Number::DateChecksum'
|
|
|
|
Setting.set( 'ticket_number', 'Ticket::Number::Date' )
|
|
|
|
Setting.set( 'ticket_number_date', { checksum: true } )
|
|
|
|
elsif setting['Value'] == 'Kernel::System::Ticket::Number::Date'
|
|
|
|
Setting.set( 'ticket_number', 'Ticket::Number::Date' )
|
|
|
|
Setting.set( 'ticket_number_date', { checksum: false } )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# ticket hook
|
|
|
|
if setting['Key'] == 'Ticket::Hook'
|
|
|
|
Setting.set( 'ticket_hook', setting['Value'] )
|
|
|
|
end
|
|
|
|
}
|
2013-01-07 08:46:38 +00:00
|
|
|
end
|
2015-05-07 11:57:19 +00:00
|
|
|
|
|
|
|
# log
|
|
|
|
def self.log(message)
|
|
|
|
thread_no = Thread.current[:thread_no] || '-'
|
|
|
|
Rails.logger.info "thread##{thread_no}: #{message}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# set translate valid ids to active = true|false
|
2013-01-07 08:46:38 +00:00
|
|
|
def self._set_valid(record)
|
2015-05-07 11:57:19 +00:00
|
|
|
|
|
|
|
# map
|
|
|
|
if record['ValidID'].to_s == '3'
|
2015-04-30 17:14:51 +00:00
|
|
|
record['ValidID'] = false
|
2015-05-07 11:57:19 +00:00
|
|
|
elsif record['ValidID'].to_s == '2'
|
|
|
|
record['ValidID'] = false
|
|
|
|
elsif record['ValidID'].to_s == '1'
|
2015-04-30 17:14:51 +00:00
|
|
|
record['ValidID'] = true
|
2015-05-07 11:57:19 +00:00
|
|
|
elsif record['ValidID'].to_s == '0'
|
2015-04-30 17:14:51 +00:00
|
|
|
record['ValidID'] = false
|
2015-05-07 11:57:19 +00:00
|
|
|
|
|
|
|
# fallback
|
|
|
|
else
|
|
|
|
record['ValidID'] = true
|
2015-04-30 17:14:51 +00:00
|
|
|
end
|
2013-01-07 08:46:38 +00:00
|
|
|
end
|
2015-05-07 11:57:19 +00:00
|
|
|
|
|
|
|
# cleanup invalid values
|
|
|
|
def self._cleanup(record)
|
|
|
|
record.each {|key, value|
|
|
|
|
if value == '0000-00-00 00:00:00'
|
|
|
|
record[key] = nil
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# fix OTRS 3.1 bug, no close time if ticket is created
|
|
|
|
if record['StateType'] == 'closed' && ( !record['Closed'] || record['Closed'].empty? )
|
|
|
|
record['Closed'] = record['Created']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# utf8 convert
|
|
|
|
def self._utf8_encode(data)
|
|
|
|
data.each { |key, value|
|
|
|
|
next if !value
|
|
|
|
next if value.class != String
|
|
|
|
data[key] = Encode.conv( 'utf8', value )
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# create customers for article
|
|
|
|
def self._article_based_customers(article, locks)
|
|
|
|
|
|
|
|
# create customer/sender if needed
|
|
|
|
return if article['sender'] != 'customer'
|
|
|
|
return if article['created_by_id'].to_i != 1
|
|
|
|
return if article['from'].empty?
|
|
|
|
|
|
|
|
email = nil
|
|
|
|
begin
|
|
|
|
email = Mail::Address.new( article['from'] ).address
|
|
|
|
rescue
|
|
|
|
email = article['from']
|
|
|
|
if article['from'] =~ /<(.+?)>/
|
|
|
|
email = $1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# create article user if not exists
|
|
|
|
while locks[:User][ email ]
|
|
|
|
log "user #{email} is locked"
|
|
|
|
sleep 1
|
|
|
|
end
|
|
|
|
|
|
|
|
# lock user
|
|
|
|
locks[:User][ email ] = true
|
|
|
|
|
|
|
|
user = User.where( email: email ).first
|
|
|
|
if !user
|
|
|
|
user = User.where( login: email ).first
|
|
|
|
end
|
|
|
|
if !user
|
|
|
|
begin
|
|
|
|
display_name = Mail::Address.new( article['from'] ).display_name ||
|
|
|
|
( Mail::Address.new( article['from'] ).comments && Mail::Address.new( article['from'] ).comments[0] )
|
|
|
|
rescue
|
|
|
|
display_name = article['from']
|
|
|
|
end
|
|
|
|
|
|
|
|
# do extra decoding because we needed to use field.value
|
|
|
|
display_name = Mail::Field.new( 'X-From', display_name ).to_s
|
|
|
|
|
|
|
|
roles = Role.lookup( name: 'Customer' )
|
|
|
|
user = User.create(
|
|
|
|
login: email,
|
|
|
|
firstname: display_name,
|
|
|
|
lastname: '',
|
|
|
|
email: email,
|
|
|
|
password: '',
|
|
|
|
active: true,
|
|
|
|
role_ids: [roles.id],
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
article['created_by_id'] = user.id
|
|
|
|
|
|
|
|
# unlock user
|
|
|
|
locks[:User][ email ] = false
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2013-01-07 08:46:38 +00:00
|
|
|
end
|