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 ImportFreshdeskController < ApplicationController
|
|
|
|
|
|
|
|
def url_check
|
|
|
|
return if setup_done_response
|
|
|
|
|
|
|
|
# validate
|
|
|
|
if params[:url].blank? || params[:url] !~ %r{^(http|https)://.+?$}
|
|
|
|
render json: {
|
|
|
|
result: 'invalid',
|
2021-11-15 15:58:19 +00:00
|
|
|
message: __('Invalid URL!'),
|
2021-05-25 12:30:12 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2021-07-06 17:43:56 +00:00
|
|
|
endpoint = "#{params[:url]}/api/v2"
|
|
|
|
endpoint.gsub!(%r{([^:])//+}, '\\1/')
|
|
|
|
|
2021-07-20 13:31:46 +00:00
|
|
|
response = UserAgent.request("#{endpoint}/contacts", verify_ssl: true)
|
2021-05-25 12:30:12 +00:00
|
|
|
|
2021-07-06 17:43:56 +00:00
|
|
|
if response.header.nil? || !response.header['x-freshdesk-api-version']
|
2021-05-25 12:30:12 +00:00
|
|
|
render json: {
|
|
|
|
result: 'invalid',
|
|
|
|
message_human: url_check_human_error_message(response.error.to_s),
|
|
|
|
message: response.error.to_s,
|
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
Setting.set('import_freshdesk_endpoint', endpoint)
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
result: 'ok',
|
|
|
|
url: params[:url],
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def credentials_check
|
|
|
|
return if setup_done_response
|
|
|
|
|
|
|
|
if !params[:token]
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
result: 'invalid',
|
2021-11-15 15:58:19 +00:00
|
|
|
message_human: __('Incomplete credentials'),
|
2021-05-25 12:30:12 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
Setting.set('import_freshdesk_endpoint_key', params[:token])
|
|
|
|
|
2022-01-05 07:40:22 +00:00
|
|
|
connection_result = Sequencer.process('Import::Freshdesk::ConnectionTest')
|
2021-05-25 12:30:12 +00:00
|
|
|
|
2022-01-05 07:40:22 +00:00
|
|
|
if !connection_result[:connected]
|
2021-05-25 12:30:12 +00:00
|
|
|
|
|
|
|
Setting.set('import_freshdesk_endpoint_key', nil)
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
result: 'invalid',
|
2021-11-15 15:58:19 +00:00
|
|
|
message_human: __('Invalid credentials!'),
|
2021-05-25 12:30:12 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-01-05 07:40:22 +00:00
|
|
|
permission_result = Sequencer.process('Import::Freshdesk::PermissionCheck')
|
|
|
|
|
|
|
|
if !permission_result[:permission_present]
|
|
|
|
|
|
|
|
Setting.set('import_freshdesk_endpoint_key', nil)
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
result: 'invalid',
|
|
|
|
message_human: __('Missing administrator permission!'),
|
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2021-05-25 12:30:12 +00:00
|
|
|
render json: {
|
|
|
|
result: 'ok',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_start
|
|
|
|
return if setup_done_response
|
|
|
|
|
|
|
|
Setting.set('import_mode', true)
|
|
|
|
Setting.set('import_backend', 'freshdesk')
|
|
|
|
|
|
|
|
job = ImportJob.create(name: 'Import::Freshdesk')
|
|
|
|
AsyncImportJob.perform_later(job)
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
result: 'ok',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_status
|
|
|
|
job = ImportJob.find_by(name: 'Import::Freshdesk')
|
|
|
|
|
|
|
|
if job.finished_at.present?
|
|
|
|
Setting.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
model_show_render_item(job)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def setup_done
|
|
|
|
count = User.all.count()
|
|
|
|
done = true
|
|
|
|
if count <= 2
|
|
|
|
done = false
|
|
|
|
end
|
|
|
|
done
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_done_response
|
|
|
|
if !setup_done
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
setup_done: true,
|
|
|
|
}
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def url_check_human_error_message(error)
|
|
|
|
translation_map = {
|
2021-11-15 15:58:19 +00:00
|
|
|
'No such file' => __('Hostname not found!'),
|
|
|
|
'getaddrinfo: nodename nor servname provided, or not known' => __('Hostname not found!'),
|
|
|
|
'No route to host' => __('No route to host!'),
|
|
|
|
'Connection refused' => __('Connection refused!'),
|
2021-05-25 12:30:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
message_human = ''
|
|
|
|
translation_map.each do |key, message|
|
|
|
|
if error.match?(%r{#{Regexp.escape(key)}}i)
|
|
|
|
message_human = message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
message_human
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|