ticket.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. require_dependency 'ticket'
  2. module Import
  3. module OTRS
  4. class Ticket
  5. include Import::Helper
  6. include Import::OTRS::Helper
  7. MAPPING = {
  8. Changed: :updated_at,
  9. Created: :created_at,
  10. TicketNumber: :number,
  11. QueueID: :group_id,
  12. StateID: :state_id,
  13. PriorityID: :priority_id,
  14. Title: :title,
  15. TicketID: :id,
  16. FirstResponse: :first_response_at,
  17. #FirstResponseTimeDestinationDate: :first_response_escalation_at,
  18. #FirstResponseInMin: :first_response_in_min,
  19. #FirstResponseDiffInMin: :first_response_diff_in_min,
  20. Closed: :close_at,
  21. #SoltutionTimeDestinationDate: :close_escalation_at,
  22. #CloseTimeInMin: :close_in_min,
  23. #CloseTimeDiffInMin: :close_diff_in_min,
  24. }.freeze
  25. def initialize(ticket)
  26. fix(ticket)
  27. import(ticket)
  28. end
  29. private
  30. def import(ticket)
  31. Import::OTRS::ArticleCustomerFactory.import(ticket['Articles'])
  32. create_or_update(map(ticket))
  33. Import::OTRS::ArticleFactory.import(ticket['Articles'])
  34. Import::OTRS::HistoryFactory.import(ticket['History'])
  35. end
  36. def create_or_update(ticket)
  37. return if updated?(ticket)
  38. create(ticket)
  39. end
  40. def updated?(ticket)
  41. @local_ticket = ::Ticket.find_by(id: ticket[:id])
  42. return false if !@local_ticket
  43. log "update Ticket.find_by(id: #{ticket[:id]})"
  44. @local_ticket.update!(ticket)
  45. true
  46. end
  47. def create(ticket)
  48. log "add Ticket.find_by(id: #{ticket[:id]})"
  49. @local_ticket = ::Ticket.new(ticket)
  50. @local_ticket.id = ticket[:id]
  51. @local_ticket.save
  52. reset_primary_key_sequence('tickets')
  53. rescue ActiveRecord::RecordNotUnique
  54. log "Ticket #{ticket[:id]} is handled by another thead, skipping."
  55. end
  56. def map(ticket)
  57. ensure_map(default_map(ticket))
  58. end
  59. def ensure_map(mapped)
  60. return mapped if mapped[:title]
  61. mapped[:title] = '**EMPTY**'
  62. mapped
  63. end
  64. def default_map(ticket)
  65. {
  66. owner_id: owner_id(ticket),
  67. customer_id: customer_id(ticket),
  68. created_by_id: created_by_id(ticket),
  69. updated_by_id: 1,
  70. }
  71. .merge(from_mapping(ticket))
  72. .merge(dynamic_fields(ticket))
  73. end
  74. def dynamic_fields(ticket)
  75. result = {}
  76. ticket.each_key do |key|
  77. key_string = key.to_s
  78. next if !key_string.start_with?('DynamicField_')
  79. dynamic_field_name = key_string[13, key_string.length]
  80. next if Import::OTRS::DynamicFieldFactory.skip_field?( dynamic_field_name )
  81. dynamic_field_name = Import::OTRS::DynamicField.convert_name(dynamic_field_name)
  82. result[dynamic_field_name.to_sym] = ticket[key_string]
  83. end
  84. result
  85. end
  86. def owner_id(ticket)
  87. default = 1
  88. owner = ticket['Owner']
  89. return default if !owner
  90. user = user_lookup(owner)
  91. return user.id if user
  92. default
  93. end
  94. def customer_id(ticket)
  95. default = 1
  96. customer = ticket['CustomerUserID']
  97. return default if !customer
  98. user = user_lookup(customer)
  99. return user.id if user
  100. first_customer_id = first_customer_id(ticket['Articles'])
  101. return first_customer_id if first_customer_id
  102. default
  103. end
  104. def created_by_id(ticket)
  105. default = 1
  106. return ticket['CreateBy'] if ticket['CreateBy'].to_i != default
  107. return default if ticket['Articles'].blank?
  108. return default if ticket['Articles'].first['SenderType'] != 'customer'
  109. customer_id(ticket)
  110. end
  111. def user_lookup(login)
  112. ::User.find_by(login: login.downcase)
  113. end
  114. def first_customer_id(articles)
  115. user_id = nil
  116. articles.each do |article|
  117. next if article['SenderType'] != 'customer'
  118. next if article['From'].blank?
  119. user = Import::OTRS::ArticleCustomer.find(article)
  120. break if !user
  121. user_id = user.id
  122. break
  123. end
  124. user_id
  125. end
  126. # cleanup invalid values
  127. def fix(ticket)
  128. utf8_encode(ticket)
  129. fix_timestamps(ticket)
  130. fix_close_time(ticket)
  131. end
  132. def fix_timestamps(ticket)
  133. ticket.each do |key, value|
  134. next if value != '0000-00-00 00:00:00'
  135. ticket[key] = nil
  136. end
  137. end
  138. # fix OTRS 3.1 bug, no close time if ticket is created
  139. def fix_close_time(ticket)
  140. return if ticket['StateType'] != 'closed'
  141. return if ticket['Closed']
  142. return if ticket['Closed'].present?
  143. ticket['Closed'] = ticket['Created']
  144. end
  145. end
  146. end
  147. end