diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index 40729b7d4..52cb324d2 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -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 diff --git a/app/lib/helpscout/api.rb b/app/lib/helpscout/api.rb index c01fb29b6..e7cdbc2db 100644 --- a/app/lib/helpscout/api.rb +++ b/app/lib/helpscout/api.rb @@ -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 diff --git a/app/lib/helpscout/user_conversations_adapter.rb b/app/lib/helpscout/user_conversations_adapter.rb index 6b265e13e..f62662a0d 100644 --- a/app/lib/helpscout/user_conversations_adapter.rb +++ b/app/lib/helpscout/user_conversations_adapter.rb @@ -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 diff --git a/spec/lib/helpscout/user_conversations_adapter_spec.rb b/spec/lib/helpscout/user_conversations_adapter_spec.rb index 126b92590..7978bbb54 100644 --- a/spec/lib/helpscout/user_conversations_adapter_spec.rb +++ b/spec/lib/helpscout/user_conversations_adapter_spec.rb @@ -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) }