2018-08-29 21:26:22 +02:00
|
|
|
class WebhookController < ActionController::Base
|
2023-01-11 17:31:31 +01:00
|
|
|
before_action :verify_helpscout_signature!, only: [:helpscout, :helpscout_support_dev]
|
2020-08-10 17:18:10 +02:00
|
|
|
skip_before_action :verify_authenticity_token
|
2018-08-29 21:26:22 +02:00
|
|
|
|
2023-01-11 17:31:31 +01:00
|
|
|
def sendinblue
|
|
|
|
if Rails.application.secrets.dig(:mattermost, :send_in_blue_outage_webhook_url).present?
|
|
|
|
send_mattermost_notification(
|
|
|
|
Rails.application.secrets.dig(:mattermost, :send_in_blue_outage_webhook_url),
|
|
|
|
message_to_mattermost_send_in_blue_channel
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-03 11:31:18 +01:00
|
|
|
def helpscout_support_dev
|
2023-01-11 17:31:31 +01:00
|
|
|
if tagged_dev? && status_active? && Rails.application.secrets.dig(:mattermost, :support_webhook_url).present?
|
|
|
|
send_mattermost_notification(
|
|
|
|
Rails.application.secrets.dig(:mattermost, :support_webhook_url),
|
|
|
|
message_to_mattermost_support_channel
|
|
|
|
)
|
2022-11-03 11:31:18 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
head :no_content
|
|
|
|
end
|
|
|
|
|
2018-08-29 21:26:22 +02:00
|
|
|
def helpscout
|
2019-03-19 15:49:54 +01:00
|
|
|
email = params[:customer][:email].downcase
|
2018-08-29 21:26:22 +02:00
|
|
|
user = User.find_by(email: email)
|
|
|
|
|
2019-11-04 16:18:09 +01:00
|
|
|
if user.nil?
|
|
|
|
head :not_found
|
|
|
|
|
|
|
|
else
|
|
|
|
instructeur = user.instructeur
|
|
|
|
administrateur = user.administrateur
|
|
|
|
|
2018-09-18 12:06:27 +02:00
|
|
|
url = manager_user_url(user)
|
2019-11-04 16:18:09 +01:00
|
|
|
html = [link_to_manager(user, url)]
|
2018-08-29 21:26:22 +02:00
|
|
|
|
2019-11-04 16:18:09 +01:00
|
|
|
if instructeur
|
|
|
|
url = manager_instructeur_url(instructeur)
|
|
|
|
html << link_to_manager(instructeur, url)
|
|
|
|
end
|
2018-08-29 21:26:22 +02:00
|
|
|
|
2019-11-04 16:18:09 +01:00
|
|
|
if administrateur
|
|
|
|
url = manager_administrateur_url(administrateur)
|
|
|
|
html << link_to_manager(administrateur, url)
|
|
|
|
end
|
2018-08-29 21:26:22 +02:00
|
|
|
|
2020-10-12 14:50:26 +02:00
|
|
|
html << email_link_to_manager(user)
|
|
|
|
|
2018-08-29 21:26:22 +02:00
|
|
|
render json: { html: html.join('<br>') }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-01-11 17:31:31 +01:00
|
|
|
def send_mattermost_notification(url, text)
|
2022-11-03 11:31:18 +01:00
|
|
|
Net::HTTP.post(
|
2023-01-11 17:31:31 +01:00
|
|
|
URI.parse(url),
|
2022-11-03 11:31:18 +01:00
|
|
|
{ "text": text }.to_json,
|
|
|
|
"Content-Type" => "application/json"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2023-01-11 17:31:31 +01:00
|
|
|
def message_to_mattermost_support_channel
|
2022-11-03 11:31:18 +01:00
|
|
|
%Q(
|
|
|
|
Nouveau bug taggué #dev : https://secure.helpscout.net/conversation/#{params["id"]}/#{params["number"]}?folderId=#{params["folderId"]}
|
|
|
|
|
|
|
|
> #{params['webhook']['preview']}
|
|
|
|
|
|
|
|
**personnes impliquées** : #{threads.map { |thread| thread['createdBy']['email'] }.uniq.join(", ")}
|
|
|
|
**utilisateur en attente depuis** : #{params['customerWaitingSince']['friendly']})
|
|
|
|
end
|
|
|
|
|
2023-01-11 17:31:31 +01:00
|
|
|
def message_to_mattermost_send_in_blue_channel
|
|
|
|
%Q{Incident sur SIB : #{params['title']}.
|
|
|
|
Etat de SIB: #{params['current_status']}
|
|
|
|
L'Incident a commencé à #{params['datetime_start']} et est p-e terminé a #{params['datetime_resolve']}
|
|
|
|
les composant suivants sont affectés : #{params["components"].map { _1['name'] }.join(", ")}}
|
|
|
|
end
|
|
|
|
|
2022-11-03 11:31:18 +01:00
|
|
|
def threads
|
|
|
|
params['_embedded']['threads']
|
|
|
|
end
|
|
|
|
|
|
|
|
def tagged_dev?
|
|
|
|
params["tags"].any? { _1['tag'].include?('dev') }
|
|
|
|
end
|
|
|
|
|
|
|
|
def status_active?
|
|
|
|
params["status"] == 'active'
|
|
|
|
end
|
|
|
|
|
2018-08-29 21:26:22 +02:00
|
|
|
def link_to_manager(model, url)
|
2019-03-05 18:16:13 +01:00
|
|
|
"<a target='_blank' href='#{url}' rel='noopener'>#{model.model_name.human}##{model.id}</a>"
|
2018-08-29 21:26:22 +02:00
|
|
|
end
|
|
|
|
|
2020-10-12 14:50:26 +02:00
|
|
|
def email_link_to_manager(user)
|
|
|
|
url = emails_manager_user_url(user)
|
|
|
|
"<a target='_blank' href='#{url}' rel='noopener'>Emails##{user.id}</a>"
|
|
|
|
end
|
|
|
|
|
2023-01-11 17:31:31 +01:00
|
|
|
def verify_helpscout_signature!
|
2018-08-29 21:26:22 +02:00
|
|
|
if generate_body_signature(request.body.read) != request.headers['X-Helpscout-Signature']
|
|
|
|
request_http_token_authentication
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_body_signature(body)
|
|
|
|
Base64.strict_encode64(OpenSSL::HMAC.digest('sha1',
|
|
|
|
Rails.application.secrets.helpscout[:webhook_secret],
|
|
|
|
body))
|
|
|
|
end
|
|
|
|
end
|