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
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue