mapping.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Sequencer::Unit::Import::Freshdesk::Agent::Mapping < Sequencer::Unit::Base
  3. include ::Sequencer::Unit::Import::Common::Mapping::Mixin::ProvideMapped
  4. extend ::Sequencer::Unit::Import::Freshdesk::Requester
  5. uses :resource, :id_map
  6. def process
  7. contact = resource['contact']
  8. provide_mapped do
  9. {
  10. login: contact['email'],
  11. firstname: contact['name'],
  12. lastname: '', # makes sure name guessing is triggered for updating existing users.
  13. email: contact['email'],
  14. phone: contact['phone'],
  15. active: !contact['deleted'],
  16. group_ids: group_ids,
  17. password: password,
  18. last_login: contact['last_login_at'],
  19. role_ids: ::Role.where(name: role_names).pluck(:id),
  20. }
  21. end
  22. end
  23. def self.admin_id
  24. @admin_id ||= begin
  25. token_user = self.token_user
  26. token_user.try(:[], 'id')
  27. end
  28. end
  29. def self.token_user
  30. response = request(
  31. api_path: 'agents/me',
  32. )
  33. JSON.parse(response.body)
  34. rescue => e
  35. logger.error e
  36. nil
  37. end
  38. private
  39. def group_ids
  40. Array(resource['group_ids']).map do |group_id|
  41. id_map['Group'][group_id]
  42. end
  43. end
  44. def role_names
  45. return %w[Agent Admin] if token_user?
  46. 'Agent'
  47. end
  48. def password
  49. return Setting.get('import_freshdesk_endpoint_key') if token_user?
  50. nil
  51. end
  52. def token_user?
  53. self.class.admin_id == resource['id']
  54. end
  55. end