tickets.rb 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. class Sequencer
  2. class Unit
  3. module Import
  4. module Zendesk
  5. class Tickets < Sequencer::Unit::Import::Zendesk::SubSequence::Object
  6. include ::Sequencer::Unit::Import::Zendesk::Mixin::IncrementalExport
  7. uses :user_map, :organization_map, :group_map, :ticket_field_map
  8. private
  9. def default_params
  10. super.merge(
  11. user_map: user_map,
  12. group_map: group_map,
  13. organization_map: organization_map,
  14. ticket_field_map: ticket_field_map,
  15. )
  16. end
  17. def resource_iteration
  18. super do |record|
  19. # call passed/originally intended block
  20. yield(record)
  21. # add hook to check if ticket count
  22. # update is needed because the request
  23. # might have changed
  24. update_ticket_count
  25. end
  26. end
  27. # The source if this is the limitation of not knowing
  28. # how much tickets there are in total before requesting the endpoint
  29. # This is caused by the Zendesk API which only returns max. 1000
  30. # per request
  31. def update_ticket_count
  32. update_import_job
  33. next_page
  34. end
  35. attr_accessor :previous_page
  36. def update_import_job
  37. return if !update_required?
  38. state.provide(import_job, updated_import_job)
  39. end
  40. def updated_import_job
  41. import_job.result[:Tickets].merge(
  42. total: import_job.result[:Tickets][:total] + current_request_count
  43. )
  44. end
  45. def update_required?
  46. return false if previous_page.blank?
  47. return false if previous_page == next_page
  48. current_request_count.present?
  49. end
  50. def current_request_count
  51. # access the internal instance method of the
  52. # Zendesk collection request to get the current
  53. # count of the endpoint (max. 1000)
  54. resource_collection_attribute.instance_variable_get(:@count)
  55. end
  56. def next_page
  57. # access the internal instance method of the
  58. # Zendesk collection request to get the next
  59. # page number of the endpoint
  60. resource_collection_attribute.instance_variable_get(:@next_page)
  61. end
  62. end
  63. end
  64. end
  65. end
  66. end