demarches-normaliennes/app/lib/helpscout/user_conversations_adapter.rb
Pierre de La Morinerie 86b9e2d092 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.
2019-01-03 17:27:56 +01:00

55 lines
1.5 KiB
Ruby

# Fetch and compute monthly reports about the users conversations on Helpscout
class Helpscout::UserConversationsAdapter
EXCLUDED_TAGS = ['openlab', 'bizdev', 'admin', 'webinaire']
def initialize(from, to)
@from = from
@to = to
end
def can_fetch_reports?
api_client.ready?
end
# Return an array of monthly reports
def reports
@reports ||= (@from..@to)
.group_by { |date| [date.year, date.month] }
.keys
.map { |key| { year: key[0], month: key[1] } }
.map { |interval| report(interval[:year], interval[:month]) }
end
private
def report(year, month)
report = fetch_conversations_report(year, month)
total_conversations = report.dig(:current, :totalConversations)
excluded_conversations = report
.dig(:tags, :top)
.select { |tag| tag[:name]&.in?(EXCLUDED_TAGS) }
.map { |tag| tag[:count] }
.sum
{
start_date: report.dig(:current, :startDate).to_datetime,
end_date: report.dig(:current, :endDate).to_datetime,
conversations_count: total_conversations - excluded_conversations
}
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
Rails.cache.fetch("helpscout-conversation-report-#{year}-#{month}") do
api_client.conversations_report(year, month)
end
end
end