2024-06-13 13:27:32 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class HelpscoutCreateConversationJob < ApplicationJob
|
2024-09-24 21:24:22 +02:00
|
|
|
queue_as :critical # user feedback is critical
|
2024-10-23 16:58:15 +02:00
|
|
|
|
|
|
|
def max_attempts = 15 # ~10h
|
|
|
|
|
2024-06-13 13:27:32 +02:00
|
|
|
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-10-23 17:05:27 +02:00
|
|
|
rescue StandardError
|
|
|
|
contact_form.delete if executions >= max_attempts
|
|
|
|
|
|
|
|
raise
|
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?
|
2024-10-23 16:57:54 +02:00
|
|
|
return if contact_form.piece_jointe.byte_size.zero? # HS don't support empty attachment
|
2024-07-30 19:14:04 +02:00
|
|
|
|
|
|
|
contact_form.piece_jointe
|
|
|
|
end
|
2024-06-13 13:27:32 +02:00
|
|
|
end
|