Merge pull request #3247 from betagouv/fix-stats-without-helpscout

Fix crash on /stats page when HelpScout env vars are not defined
This commit is contained in:
Pierre de La Morinerie 2019-01-03 17:34:19 +01:00 committed by GitHub
commit 053083390d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 8 deletions

View file

@ -168,7 +168,12 @@ class StatsController < ApplicationController
from = Date.new(2018, 1)
to = Date.today.prev_month
Helpscout::UserConversationsAdapter.new(from, to)
adapter = Helpscout::UserConversationsAdapter.new(from, to)
if !adapter.can_fetch_reports?
return nil
end
adapter
.reports
.map do |monthly_report|
start_date = monthly_report[:start_date].to_time.localtime

View file

@ -7,6 +7,15 @@ class Helpscout::API
PHONES = 'phones'
OAUTH2_TOKEN = 'oauth2/token'
def ready?
required_secrets = [
Rails.application.secrets.helpscout[:mailbox_id],
Rails.application.secrets.helpscout[:client_id],
Rails.application.secrets.helpscout[:client_secret]
]
required_secrets.all?(&:present?)
end
def add_tags(conversation_id, tags)
call_api(:put, "#{CONVERSATIONS}/#{conversation_id}/#{TAGS}", {
tags: tags
@ -69,7 +78,7 @@ class Helpscout::API
response = call_api(:get, 'reports/conversations?' + params.to_query)
if !response.success?
raise StandardError, "Error while fetching conversation report: #{response.status} '#{response.body}'"
raise StandardError, "Error while fetching conversation report: #{response.response_code} '#{response.body}'"
end
parse_response_body(response)

View file

@ -7,6 +7,10 @@ class Helpscout::UserConversationsAdapter
@to = to
end
def can_fetch_reports?
api_client.ready?
end
# Return an array of monthly reports
def reports
@reports ||= (@from..@to)
@ -35,15 +39,17 @@ class Helpscout::UserConversationsAdapter
}
end
def api_client
@api_client ||= Helpscout::API.new
end
def fetch_conversations_report(year, month)
if year == Date.today.year && month == Date.today.month
raise ArgumentError, 'The report for the current month will change in the future, and cannot be cached.'
end
@helpscout_api ||= Helpscout::API.new
Rails.cache.fetch("helpscout-conversation-report-#{year}-#{month}") do
@helpscout_api.conversations_report(year, month)
api_client.conversations_report(year, month)
end
end
end

View file

@ -1,10 +1,30 @@
require 'spec_helper'
describe Helpscout::UserConversationsAdapter do
describe '#reports', vcr: { cassette_name: 'helpscout_conversations_reports' } do
let(:from) { Date.new(2017, 11) }
let(:to) { Date.new(2017, 12) }
let(:from) { Date.new(2017, 11) }
let(:to) { Date.new(2017, 12) }
describe '#can_fetch_reports?' do
context 'when a required secret is missing' do
before do
Rails.application.secrets.helpscout[:mailbox_id] = nil
end
it { expect(described_class.new(from, to).can_fetch_reports?).to be false }
end
context 'when all required secrets are present' do
before do
Rails.application.secrets.helpscout[:mailbox_id] = '9999'
Rails.application.secrets.helpscout[:client_id] = '1234'
Rails.application.secrets.helpscout[:client_secret] = '5678'
end
it { expect(described_class.new(from, to).can_fetch_reports?).to be true }
end
end
describe '#reports', vcr: { cassette_name: 'helpscout_conversations_reports' } do
before { Rails.cache.clear }
subject { described_class.new(from, to) }