Fixed bug: Attachment import RecordNotUnique exception handling fails sometimes.
This commit is contained in:
parent
a286b9e17e
commit
71c7b9d45f
1 changed files with 84 additions and 79 deletions
|
@ -592,93 +592,96 @@ module Import::OTRS
|
||||||
}
|
}
|
||||||
|
|
||||||
record['Articles'].each do |article|
|
record['Articles'].each do |article|
|
||||||
ActiveRecord::Base.transaction do
|
|
||||||
|
|
||||||
# get article values
|
retries = 3
|
||||||
article_new = {
|
begin
|
||||||
created_by_id: 1,
|
|
||||||
updated_by_id: 1,
|
|
||||||
}
|
|
||||||
|
|
||||||
map[:Article].each { |key, value|
|
ActiveRecord::Base.transaction do
|
||||||
next if !article.key?(key.to_s)
|
|
||||||
article_new[value] = article[key.to_s]
|
|
||||||
}
|
|
||||||
|
|
||||||
if article_new[:sender] == 'customer'
|
# get article values
|
||||||
article_new[:sender_id] = Ticket::Article::Sender.lookup( name: 'Customer' ).id
|
article_new = {
|
||||||
article_new.delete( :sender )
|
created_by_id: 1,
|
||||||
end
|
updated_by_id: 1,
|
||||||
if article_new[:sender] == 'agent'
|
}
|
||||||
article_new[:sender_id] = Ticket::Article::Sender.lookup( name: 'Agent' ).id
|
|
||||||
article_new.delete( :sender )
|
|
||||||
end
|
|
||||||
if article_new[:sender] == 'system'
|
|
||||||
article_new[:sender_id] = Ticket::Article::Sender.lookup( name: 'System' ).id
|
|
||||||
article_new.delete( :sender )
|
|
||||||
end
|
|
||||||
|
|
||||||
if article_new[:type] == 'email-external'
|
map[:Article].each { |key, value|
|
||||||
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'email' ).id
|
next if !article.key?(key.to_s)
|
||||||
article_new[:internal] = false
|
article_new[value] = article[key.to_s]
|
||||||
elsif article_new[:type] == 'email-internal'
|
}
|
||||||
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'email' ).id
|
|
||||||
article_new[:internal] = true
|
|
||||||
elsif article_new[:type] == 'note-external'
|
|
||||||
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'note' ).id
|
|
||||||
article_new[:internal] = false
|
|
||||||
elsif article_new[:type] == 'note-internal'
|
|
||||||
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'note' ).id
|
|
||||||
article_new[:internal] = true
|
|
||||||
elsif article_new[:type] == 'phone'
|
|
||||||
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'phone' ).id
|
|
||||||
article_new[:internal] = false
|
|
||||||
elsif article_new[:type] == 'webrequest'
|
|
||||||
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'web' ).id
|
|
||||||
article_new[:internal] = false
|
|
||||||
else
|
|
||||||
article_new[:type_id] = 9
|
|
||||||
end
|
|
||||||
article_new.delete( :type )
|
|
||||||
article_object = Ticket::Article.find_by( id: article_new[:id] )
|
|
||||||
|
|
||||||
# set state types
|
if article_new[:sender] == 'customer'
|
||||||
if article_object
|
article_new[:sender_id] = Ticket::Article::Sender.lookup( name: 'Customer' ).id
|
||||||
log "update Ticket::Article.find(#{article_new[:id]})"
|
article_new.delete( :sender )
|
||||||
article_object.update_attributes(article_new)
|
end
|
||||||
else
|
if article_new[:sender] == 'agent'
|
||||||
log "add Ticket::Article.find(#{article_new[:id]})"
|
article_new[:sender_id] = Ticket::Article::Sender.lookup( name: 'Agent' ).id
|
||||||
begin
|
article_new.delete( :sender )
|
||||||
article_object = Ticket::Article.new(article_new)
|
end
|
||||||
article_object.id = article_new[:id]
|
if article_new[:sender] == 'system'
|
||||||
article_object.save
|
article_new[:sender_id] = Ticket::Article::Sender.lookup( name: 'System' ).id
|
||||||
rescue ActiveRecord::RecordNotUnique
|
article_new.delete( :sender )
|
||||||
log "Ticket #{ticket_new[:id]} (article #{article_new[:id]}) is handled by another thead, skipping."
|
|
||||||
next
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
next if !article['Attachments']
|
if article_new[:type] == 'email-external'
|
||||||
next if article['Attachments'].empty?
|
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'email' ).id
|
||||||
|
article_new[:internal] = false
|
||||||
|
elsif article_new[:type] == 'email-internal'
|
||||||
|
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'email' ).id
|
||||||
|
article_new[:internal] = true
|
||||||
|
elsif article_new[:type] == 'note-external'
|
||||||
|
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'note' ).id
|
||||||
|
article_new[:internal] = false
|
||||||
|
elsif article_new[:type] == 'note-internal'
|
||||||
|
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'note' ).id
|
||||||
|
article_new[:internal] = true
|
||||||
|
elsif article_new[:type] == 'phone'
|
||||||
|
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'phone' ).id
|
||||||
|
article_new[:internal] = false
|
||||||
|
elsif article_new[:type] == 'webrequest'
|
||||||
|
article_new[:type_id] = Ticket::Article::Type.lookup( name: 'web' ).id
|
||||||
|
article_new[:internal] = false
|
||||||
|
else
|
||||||
|
article_new[:type_id] = 9
|
||||||
|
end
|
||||||
|
article_new.delete( :type )
|
||||||
|
article_object = Ticket::Article.find_by( id: article_new[:id] )
|
||||||
|
|
||||||
# TODO: refactor
|
# set state types
|
||||||
# check if there are attachments present
|
if article_object
|
||||||
if !article_object.attachments.empty?
|
log "update Ticket::Article.find(#{article_new[:id]})"
|
||||||
|
article_object.update_attributes(article_new)
|
||||||
|
else
|
||||||
|
log "add Ticket::Article.find(#{article_new[:id]})"
|
||||||
|
begin
|
||||||
|
article_object = Ticket::Article.new(article_new)
|
||||||
|
article_object.id = article_new[:id]
|
||||||
|
article_object.save
|
||||||
|
rescue ActiveRecord::RecordNotUnique
|
||||||
|
log "Ticket #{ticket_new[:id]} (article #{article_new[:id]}) is handled by another thead, skipping."
|
||||||
|
next
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# skip attachments if count is equal
|
next if !article['Attachments']
|
||||||
next if article_object.attachments.count == article['Attachments'].count
|
next if article['Attachments'].empty?
|
||||||
|
|
||||||
# if the count differs delete all so we
|
# TODO: refactor
|
||||||
# can have a fresh start
|
# check if there are attachments present
|
||||||
article_object.attachments.each(&:delete)
|
if !article_object.attachments.empty?
|
||||||
end
|
|
||||||
|
|
||||||
# import article attachments
|
# skip attachments if count is equal
|
||||||
article['Attachments'].each { |attachment|
|
next if article_object.attachments.count == article['Attachments'].count
|
||||||
|
|
||||||
filename = Base64.decode64(attachment['Filename'])
|
# if the count differs delete all so we
|
||||||
|
# can have a fresh start
|
||||||
|
article_object.attachments.each(&:delete)
|
||||||
|
end
|
||||||
|
|
||||||
|
# import article attachments
|
||||||
|
article['Attachments'].each { |attachment|
|
||||||
|
|
||||||
|
filename = Base64.decode64(attachment['Filename'])
|
||||||
|
|
||||||
begin
|
|
||||||
Store.add(
|
Store.add(
|
||||||
object: 'Ticket::Article',
|
object: 'Ticket::Article',
|
||||||
o_id: article_object.id,
|
o_id: article_object.id,
|
||||||
|
@ -691,11 +694,13 @@ module Import::OTRS
|
||||||
},
|
},
|
||||||
created_by_id: 1,
|
created_by_id: 1,
|
||||||
)
|
)
|
||||||
rescue ActiveRecord::RecordNotUnique
|
}
|
||||||
log "Ticket #{ticket_new[:id]} (article #{article_object.id}, filename #{filename}) is handled by another thead, skipping."
|
end
|
||||||
next
|
rescue ActiveRecord::RecordNotUnique => e
|
||||||
end
|
log "Ticket #{ticket_new[:id]} - RecordNotUnique: #{e}"
|
||||||
}
|
sleep rand 3
|
||||||
|
retry if !(retries -= 1).zero?
|
||||||
|
raise
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue