state.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://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. Name: :name,
  11. ID: :id,
  12. ValidID: :active,
  13. Comment: :note,
  14. }.freeze
  15. def initialize(state)
  16. import(state)
  17. end
  18. private
  19. def import(state)
  20. create_or_update(map(state))
  21. end
  22. def create_or_update(state)
  23. return if updated?(state)
  24. create(state)
  25. end
  26. def updated?(state)
  27. @local_state = ::Ticket::State.find_by(id: state[:id])
  28. return false if !@local_state
  29. log "update Ticket::State.find_by(id: #{state[:id]})"
  30. @local_state.update!(state)
  31. true
  32. end
  33. def create(state)
  34. log "add Ticket::State.find_by(id: #{state[:id]})"
  35. @local_state = ::Ticket::State.new(state)
  36. @local_state.id = state[:id]
  37. @local_state.save
  38. reset_primary_key_sequence('ticket_states')
  39. end
  40. def map(state)
  41. {
  42. created_by_id: 1,
  43. updated_by_id: 1,
  44. active: active?(state),
  45. state_type_id: state_type_id(state)
  46. }
  47. .merge(from_mapping(state))
  48. end
  49. def state_type_id(state)
  50. map_type(state)
  51. ::Ticket::StateType.lookup(name: state['TypeName']).id
  52. end
  53. def map_type(state)
  54. return if state['TypeName'] != 'pending auto'
  55. state['TypeName'] = 'pending action'
  56. end
  57. end
  58. end
  59. end