state.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. module Import
  2. module OTRS
  3. class State
  4. include Import::Helper
  5. include Import::OTRS::Helper
  6. MAPPING = {
  7. ChangeTime: :updated_at,
  8. CreateTime: :created_at,
  9. CreateBy: :created_by_id,
  10. ChangeBy: :updated_by_id,
  11. Name: :name,
  12. ID: :id,
  13. ValidID: :active,
  14. Comment: :note,
  15. }.freeze
  16. def initialize(state)
  17. import(state)
  18. end
  19. private
  20. def import(state)
  21. create_or_update(map(state))
  22. end
  23. def create_or_update(state)
  24. return if updated?(state)
  25. create(state)
  26. end
  27. def updated?(state)
  28. @local_state = ::Ticket::State.find_by(id: state[:id])
  29. return false if !@local_state
  30. log "update Ticket::State.find_by(id: #{state[:id]})"
  31. @local_state.update!(state)
  32. true
  33. end
  34. def create(state)
  35. log "add Ticket::State.find_by(id: #{state[:id]})"
  36. @local_state = ::Ticket::State.new(state)
  37. @local_state.id = state[:id]
  38. @local_state.save
  39. reset_primary_key_sequence('ticket_states')
  40. end
  41. def map(state)
  42. {
  43. created_by_id: 1,
  44. updated_by_id: 1,
  45. active: active?(state),
  46. state_type_id: state_type_id(state)
  47. }
  48. .merge(from_mapping(state))
  49. end
  50. def state_type_id(state)
  51. map_type(state)
  52. ::Ticket::StateType.lookup(name: state['TypeName']).id
  53. end
  54. def map_type(state)
  55. return if state['TypeName'] != 'pending auto'
  56. state['TypeName'] = 'pending action'
  57. end
  58. end
  59. end
  60. end