state.rb 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Copyright (C) 2012-2024 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. return if skip?(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 skip?(state)
  28. if state['TypeName'].eql?('removed')
  29. log "skip Ticket::State.find_by(id: #{state[:id]}) due to state #{state['Name']} and state type #{state['TypeName']}"
  30. return true
  31. end
  32. false
  33. end
  34. def updated?(state)
  35. @local_state = ::Ticket::State.find_by(id: state[:id])
  36. return false if !@local_state
  37. log "update Ticket::State.find_by(id: #{state[:id]})"
  38. @local_state.update!(state)
  39. true
  40. end
  41. def create(state)
  42. log "add Ticket::State.find_by(id: #{state[:id]})"
  43. @local_state = ::Ticket::State.new(state)
  44. @local_state.id = state[:id]
  45. @local_state.save
  46. reset_primary_key_sequence('ticket_states')
  47. end
  48. def map(state)
  49. mapped_state_type_id = state_type_id(state)
  50. {
  51. created_by_id: 1,
  52. updated_by_id: 1,
  53. active: active?(state),
  54. state_type_id: mapped_state_type_id,
  55. ignore_escalation: ignore_escalation?(mapped_state_type_id)
  56. }
  57. .merge(from_mapping(state))
  58. end
  59. def state_type_id(state)
  60. map_type(state)
  61. ::Ticket::StateType.lookup(name: state['TypeName']).id
  62. end
  63. def ignore_escalation?(state_type_id)
  64. ::Ticket::StateType.names_in_category(:work_on).exclude?(::Ticket::StateType.lookup(id: state_type_id).name)
  65. end
  66. def map_type(state)
  67. return if state['TypeName'] != 'pending auto'
  68. state['TypeName'] = 'pending action'
  69. end
  70. end
  71. end
  72. end