2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-12-18 10:35:23 +01:00
|
|
|
# Fetch and compute monthly reports about the users conversations on Helpscout
|
|
|
|
class Helpscout::UserConversationsAdapter
|
|
|
|
def initialize(from, to)
|
|
|
|
@from = from
|
|
|
|
@to = to
|
|
|
|
end
|
|
|
|
|
2019-01-03 17:24:24 +01:00
|
|
|
def can_fetch_reports?
|
|
|
|
api_client.ready?
|
|
|
|
end
|
|
|
|
|
2018-12-18 10:35:23 +01:00
|
|
|
# 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)
|
2019-09-11 17:16:48 +02:00
|
|
|
report = fetch_productivity_report(year, month)
|
2018-12-18 10:35:23 +01:00
|
|
|
|
|
|
|
{
|
2019-09-11 17:16:48 +02:00
|
|
|
start_date: report.dig(:current, :startDate).to_datetime,
|
|
|
|
end_date: report.dig(:current, :endDate).to_datetime,
|
|
|
|
replies_sent: report.dig(:current, :repliesSent)
|
2018-12-18 10:35:23 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-01-03 17:24:24 +01:00
|
|
|
def api_client
|
|
|
|
@api_client ||= Helpscout::API.new
|
|
|
|
end
|
|
|
|
|
2019-09-11 17:16:48 +02:00
|
|
|
def fetch_productivity_report(year, month)
|
2019-11-25 14:39:42 +01:00
|
|
|
if year == Time.zone.today.year && month == Time.zone.today.month
|
2018-12-18 10:35:23 +01:00
|
|
|
raise ArgumentError, 'The report for the current month will change in the future, and cannot be cached.'
|
|
|
|
end
|
|
|
|
|
2019-09-11 17:16:48 +02:00
|
|
|
Rails.cache.fetch("helpscout-productivity-report-#{year}-#{month}") do
|
|
|
|
api_client.productivity_report(year, month)
|
2018-12-18 10:35:23 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|