stats: fix exception when HelpScout env vars are missing
This fixes the stats page, which used to raise an exception when HelpScout env vars are not present.
This commit is contained in:
parent
da675bb47e
commit
86b9e2d092
4 changed files with 47 additions and 7 deletions
|
@ -168,7 +168,12 @@ class StatsController < ApplicationController
|
||||||
from = Date.new(2018, 1)
|
from = Date.new(2018, 1)
|
||||||
to = Date.today.prev_month
|
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
|
.reports
|
||||||
.map do |monthly_report|
|
.map do |monthly_report|
|
||||||
start_date = monthly_report[:start_date].to_time.localtime
|
start_date = monthly_report[:start_date].to_time.localtime
|
||||||
|
|
|
@ -7,6 +7,15 @@ class Helpscout::API
|
||||||
PHONES = 'phones'
|
PHONES = 'phones'
|
||||||
OAUTH2_TOKEN = 'oauth2/token'
|
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)
|
def add_tags(conversation_id, tags)
|
||||||
call_api(:put, "#{CONVERSATIONS}/#{conversation_id}/#{TAGS}", {
|
call_api(:put, "#{CONVERSATIONS}/#{conversation_id}/#{TAGS}", {
|
||||||
tags: tags
|
tags: tags
|
||||||
|
|
|
@ -7,6 +7,10 @@ class Helpscout::UserConversationsAdapter
|
||||||
@to = to
|
@to = to
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def can_fetch_reports?
|
||||||
|
api_client.ready?
|
||||||
|
end
|
||||||
|
|
||||||
# Return an array of monthly reports
|
# Return an array of monthly reports
|
||||||
def reports
|
def reports
|
||||||
@reports ||= (@from..@to)
|
@reports ||= (@from..@to)
|
||||||
|
@ -35,15 +39,17 @@ class Helpscout::UserConversationsAdapter
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def api_client
|
||||||
|
@api_client ||= Helpscout::API.new
|
||||||
|
end
|
||||||
|
|
||||||
def fetch_conversations_report(year, month)
|
def fetch_conversations_report(year, month)
|
||||||
if year == Date.today.year && month == Date.today.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.'
|
raise ArgumentError, 'The report for the current month will change in the future, and cannot be cached.'
|
||||||
end
|
end
|
||||||
|
|
||||||
@helpscout_api ||= Helpscout::API.new
|
|
||||||
|
|
||||||
Rails.cache.fetch("helpscout-conversation-report-#{year}-#{month}") do
|
Rails.cache.fetch("helpscout-conversation-report-#{year}-#{month}") do
|
||||||
@helpscout_api.conversations_report(year, month)
|
api_client.conversations_report(year, month)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,10 +1,30 @@
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
describe Helpscout::UserConversationsAdapter do
|
describe Helpscout::UserConversationsAdapter do
|
||||||
describe '#reports', vcr: { cassette_name: 'helpscout_conversations_reports' } do
|
|
||||||
let(:from) { Date.new(2017, 11) }
|
let(:from) { Date.new(2017, 11) }
|
||||||
let(:to) { Date.new(2017, 12) }
|
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 }
|
before { Rails.cache.clear }
|
||||||
|
|
||||||
subject { described_class.new(from, to) }
|
subject { described_class.new(from, to) }
|
||||||
|
|
Loading…
Reference in a new issue