state.rb 1.7 KB

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