mapping.rb 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class Sequencer::Unit::Import::Freshdesk::Ticket::Mapping < Sequencer::Unit::Base
  3. include ::Sequencer::Unit::Import::Common::Mapping::Mixin::ProvideMapped
  4. uses :resource, :id_map
  5. # Since the imports rely on a fresh Zammad installation, we
  6. # can require the default priority and state names to be present.
  7. def priority_map
  8. @priority_map ||= {
  9. 1 => ::Ticket::Priority.find_by(name: '1 low')&.id, # low
  10. 2 => ::Ticket::Priority.find_by(name: '2 normal')&.id, # medium
  11. 3 => ::Ticket::Priority.find_by(name: '3 high')&.id, # high
  12. 4 => ::Ticket::Priority.find_by(name: '3 high')&.id, # urgent
  13. }.freeze
  14. end
  15. def state_map
  16. @state_map ||= {
  17. 2 => ::Ticket::State.find_by(name: 'open')&.id, # open
  18. 3 => ::Ticket::State.find_by(name: 'pending reminder')&.id, # pending
  19. 4 => ::Ticket::State.find_by(name: 'closed')&.id, # resolved
  20. 5 => ::Ticket::State.find_by(name: 'closed')&.id, # closed
  21. }.freeze
  22. end
  23. def process
  24. provide_mapped do
  25. {
  26. title: resource['subject'],
  27. number: resource['id'],
  28. group_id: group_id,
  29. priority_id: priority_id,
  30. state_id: state_id,
  31. owner_id: owner_id,
  32. customer_id: customer_id,
  33. type: resource['type'],
  34. created_at: resource['created_at'],
  35. updated_at: resource['updated_at'],
  36. }
  37. end
  38. end
  39. private
  40. def group_id
  41. id_map.dig('Group', resource['group_id']) || ::Group.find_by(name: 'Support')&.id || 1
  42. end
  43. def customer_id
  44. id_map['User'][resource['requester_id']]
  45. end
  46. def owner_id
  47. id_map['User'][resource['responder_id']]
  48. end
  49. def state_id
  50. state_map.fetch(resource['status'], default_state_id)
  51. end
  52. def priority_id
  53. priority_map.fetch(resource['priority'], default_priority_id)
  54. end
  55. def default_state_id
  56. @default_state_id ||= ::Ticket::State.find_by(name: 'open')&.id
  57. end
  58. def default_priority_id
  59. @default_priority_id ||= ::Ticket::Priority.find_by(name: '2 normal')&.id
  60. end
  61. end