2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2021-05-25 12:30:12 +00:00
|
|
|
class Sequencer
|
|
|
|
class Unit
|
|
|
|
module Import
|
|
|
|
module Freshdesk
|
|
|
|
module Agent
|
|
|
|
class Mapping < Sequencer::Unit::Base
|
|
|
|
include ::Sequencer::Unit::Import::Common::Mapping::Mixin::ProvideMapped
|
|
|
|
extend ::Sequencer::Unit::Import::Freshdesk::Requester
|
|
|
|
|
|
|
|
uses :resource, :id_map
|
|
|
|
|
|
|
|
def process
|
|
|
|
contact = resource['contact']
|
|
|
|
|
|
|
|
provide_mapped do
|
|
|
|
{
|
2021-05-26 15:15:58 +00:00
|
|
|
login: contact['email'],
|
|
|
|
firstname: contact['name'],
|
|
|
|
email: contact['email'],
|
|
|
|
phone: contact['phone'],
|
2021-08-03 15:03:22 +00:00
|
|
|
active: !contact['deleted'],
|
2021-05-26 15:15:58 +00:00
|
|
|
group_ids: group_ids,
|
|
|
|
password: password,
|
|
|
|
last_login: contact['last_login_at'],
|
|
|
|
role_ids: ::Role.where(name: role_names).pluck(:id),
|
2021-05-25 12:30:12 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.admin_id
|
|
|
|
@admin_id ||= begin
|
|
|
|
token_user = self.token_user
|
|
|
|
token_user.try(:[], 'id')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.token_user
|
|
|
|
response = request(
|
|
|
|
api_path: 'agents/me',
|
|
|
|
)
|
|
|
|
|
|
|
|
JSON.parse(response.body)
|
|
|
|
rescue => e
|
|
|
|
logger.error e
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def group_ids
|
|
|
|
Array(resource['group_ids']).map do |group_id|
|
|
|
|
id_map['Group'][group_id]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def role_names
|
|
|
|
return %w[Agent Admin] if token_user?
|
|
|
|
|
|
|
|
'Agent'
|
|
|
|
end
|
|
|
|
|
|
|
|
def password
|
|
|
|
return Setting.get('import_freshdesk_endpoint_key') if token_user?
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def token_user?
|
|
|
|
self.class.admin_id == resource['id']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|