ticket.rb 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # https://developer.zendesk.com/rest_api/docs/core/tickets
  2. # https://developer.zendesk.com/rest_api/docs/core/ticket_comments#ticket-comments
  3. # https://developer.zendesk.com/rest_api/docs/core/ticket_audits#the-via-object
  4. # https://developer.zendesk.com/rest_api/docs/help_center/article_attachments
  5. # https://developer.zendesk.com/rest_api/docs/core/ticket_audits # v2
  6. module Import
  7. module Zendesk
  8. class Ticket
  9. include Import::Helper
  10. def initialize(ticket)
  11. create_or_update(ticket)
  12. Import::Zendesk::Ticket::TagFactory.import(ticket.tags, @local_ticket, ticket)
  13. Import::Zendesk::Ticket::CommentFactory.import(ticket.comments, @local_ticket, ticket)
  14. end
  15. private
  16. def create_or_update(ticket)
  17. mapped_ticket = local_ticket_fields(ticket)
  18. return if updated?(mapped_ticket)
  19. create(mapped_ticket)
  20. end
  21. def updated?(ticket)
  22. @local_ticket = ::Ticket.find_by(id: ticket[:id])
  23. return false if !@local_ticket
  24. @local_ticket.update_attributes(ticket)
  25. true
  26. end
  27. def create(ticket)
  28. @local_ticket = ::Ticket.create(ticket)
  29. reset_primary_key_sequence('tickets')
  30. end
  31. def local_ticket_fields(ticket)
  32. local_user_id = Import::Zendesk::UserFactory.local_id( ticket.requester_id ) || 1
  33. {
  34. id: ticket.id,
  35. title: ticket.subject,
  36. owner_id: Import::Zendesk::UserFactory.local_id( ticket.assignee ) || 1,
  37. note: ticket.description,
  38. group_id: Import::Zendesk::GroupFactory.local_id( ticket.group_id ) || 1,
  39. customer_id: local_user_id,
  40. organization_id: Import::Zendesk::OrganizationFactory.local_id( ticket.organization_id ),
  41. priority: Import::Zendesk::Priority.lookup(ticket),
  42. state: Import::Zendesk::State.lookup(ticket),
  43. pending_time: ticket.due_at,
  44. updated_at: ticket.updated_at,
  45. created_at: ticket.created_at,
  46. updated_by_id: local_user_id,
  47. created_by_id: local_user_id,
  48. create_article_sender_id: Import::Zendesk::Ticket::Comment::Sender.local_id(local_user_id),
  49. create_article_type_id: Import::Zendesk::Ticket::Comment::Type.local_id(ticket),
  50. }.merge(custom_fields(ticket))
  51. end
  52. def custom_fields(ticket)
  53. custom_fields = ticket.custom_fields
  54. fields = {}
  55. return fields if !custom_fields
  56. custom_fields.each do |custom_field|
  57. field_name = Import::Zendesk::TicketFieldFactory.local_id(custom_field['id'])
  58. field_value = custom_field['value']
  59. next if field_value.nil?
  60. fields[ field_name.to_sym ] = field_value
  61. end
  62. fields
  63. end
  64. end
  65. end
  66. end