2024-06-13 13:27:32 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class HelpscoutCreateConversationJob < ApplicationJob
|
|
|
|
queue_as :default
|
|
|
|
|
|
|
|
class FileNotScannedYetError < StandardError
|
|
|
|
end
|
|
|
|
|
|
|
|
retry_on FileNotScannedYetError, wait: :exponentially_longer, attempts: 10
|
|
|
|
|
2024-07-30 19:14:04 +02:00
|
|
|
attr_reader :contact_form
|
2024-07-24 17:18:02 +02:00
|
|
|
attr_reader :api
|
|
|
|
|
2024-07-30 19:14:04 +02:00
|
|
|
def perform(contact_form)
|
|
|
|
@contact_form = contact_form
|
2024-06-13 13:27:32 +02:00
|
|
|
|
2024-07-30 19:14:04 +02:00
|
|
|
if contact_form.piece_jointe.attached?
|
|
|
|
raise FileNotScannedYetError if contact_form.piece_jointe.virus_scanner.pending?
|
2024-06-13 13:27:32 +02:00
|
|
|
end
|
|
|
|
|
2024-07-24 17:18:02 +02:00
|
|
|
@api = Helpscout::API.new
|
|
|
|
|
2024-07-30 19:14:04 +02:00
|
|
|
create_conversation
|
|
|
|
|
2024-08-01 19:33:57 +02:00
|
|
|
contact_form.delete
|
2024-07-24 17:18:02 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2024-07-30 19:14:04 +02:00
|
|
|
def create_conversation
|
2024-07-24 17:18:02 +02:00
|
|
|
response = api.create_conversation(
|
2024-08-01 16:38:39 +02:00
|
|
|
contact_form.email.presence || contact_form.user.email,
|
2024-07-30 19:14:04 +02:00
|
|
|
contact_form.subject,
|
|
|
|
contact_form.text,
|
|
|
|
safe_blob
|
2024-07-24 17:18:02 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if response.success?
|
|
|
|
conversation_id = response.headers['Resource-ID']
|
|
|
|
|
2024-07-30 19:14:04 +02:00
|
|
|
if contact_form.phone.present?
|
|
|
|
api.add_phone_number(contact_form.email, contact_form.phone)
|
2024-07-24 17:18:02 +02:00
|
|
|
end
|
|
|
|
|
2024-07-30 19:14:04 +02:00
|
|
|
api.add_tags(conversation_id, contact_form.tags)
|
2024-07-24 17:18:02 +02:00
|
|
|
else
|
|
|
|
fail "Error while creating conversation: #{response.response_code} '#{response.body}'"
|
|
|
|
end
|
2024-06-13 13:27:32 +02:00
|
|
|
end
|
2024-07-30 19:14:04 +02:00
|
|
|
|
|
|
|
def safe_blob
|
|
|
|
return if !contact_form.piece_jointe.virus_scanner&.safe?
|
|
|
|
|
|
|
|
contact_form.piece_jointe
|
|
|
|
end
|
2024-06-13 13:27:32 +02:00
|
|
|
end
|