Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cron job to fetch and mail weekly report for ecraft #39

Open
wants to merge 1 commit into
base: bitespeed-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ gem 'reverse_markdown'
gem 'informers'

## Bitespeed specific
gem "intercom-rails"
gem 'intercom-rails'

### Gems required only in specific deployment environments ###
##############################################################
Expand Down
75 changes: 75 additions & 0 deletions app/jobs/weekly_conversation_report_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
class WeeklyConversationReportJob < ApplicationJob
queue_as :scheduled_jobs

# rubocop:disable Metrics/MethodLength
def perform
account_ids = [740] # enabled for ecraft
end_date = Date.yesterday
start_date = end_date - 6.days

account_ids.each do |account_id|
report = generate_report(account_id, start_date, end_date + 1.day)

if report.present?
Rails.logger.info "Data found for account_id: #{account_id}"

csv_content = generate_csv(report)

# upload csv_content via ActiveStorage and print the URL
blob = ActiveStorage::Blob.create_and_upload!(
io: StringIO.new(csv_content),
filename: "weekly_conversation_report_#{account_id}_#{start_date}_to_#{end_date}.csv",
content_type: 'text/csv'
)

csv_url = Rails.application.routes.url_helpers.url_for(blob)

# send email with the CSV URL
mailer = AdministratorNotifications::ChannelNotificationsMailer.with(account: Account.find(account_id))
mailer.weekly_conversation_report(csv_url, start_date, end_date).deliver_now
else
Rails.logger.info "No data found for account_id: #{account_id}"
end
end
end

private

def generate_report(account_id, start_date, end_date)
# Using ActiveRecord::Base directly for sanitization
sql = ActiveRecord::Base.send(:sanitize_sql_array, [<<-SQL.squish, { account_id: account_id, start_date: start_date, end_date: end_date }])
SELECT
u.name AS agent_name,
COUNT(*) AS all,
SUM(CASE WHEN c.status = 0 THEN 1 ELSE 0 END) AS open,
SUM(CASE WHEN c.status = 1 THEN 1 ELSE 0 END) AS resolved,
SUM(CASE WHEN c.status = 2 THEN 1 ELSE 0 END) AS pending,
SUM(CASE WHEN c.status = 3 THEN 1 ELSE 0 END) AS snoozed
FROM
conversations c
JOIN
account_users au ON c.assignee_id = au.user_id
JOIN
users u ON au.user_id = u.id
WHERE
c.account_id = :account_id
AND c.updated_at >= :start_date AND c.updated_at < :end_date
GROUP BY
u.name
ORDER BY
u.name
SQL

ActiveRecord::Base.connection.exec_query(sql)
end
# rubocop:enable Metrics/MethodLength

def generate_csv(results)
CSV.generate(headers: true) do |csv|
csv << ['Agent Name', 'Total', 'Open', 'Resolved', 'Pending', 'Snoozed']
results.each do |row|
csv << [row['agent_name'], row['all'], row['open'], row['resolved'], row['pending'], row['snoozed']]
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ def contact_import_complete(resource)
send_mail_with_liquid(to: admin_emails, subject: subject) and return
end

def weekly_conversation_report(csv_url, start_date, end_date)
return unless smtp_config_set_or_development?

subject = "Weekly Conversation Report for #{start_date} to #{end_date}"
@action_url = csv_url
send_mail_with_liquid(to: admin_emails, subject: subject) and return
end

def contact_import_failed
return unless smtp_config_set_or_development?

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<p>Hello,</p>

<p>Here is the weekly conversation report. </p>

<p>
Click <a href="{{action_url}}">here</a> to download.
</p>

<p>Regards,<br>BiteSpeed</p>
2 changes: 1 addition & 1 deletion config/initializers/intercom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
# A Proc that given a user returns true if the user should be excluded
# from imports and Javascript inclusion, false otherwise.
#
config.user.exclude_if = proc { |user| user.type == 'SuperAdmin'}
config.user.exclude_if = proc { |user| user.type == 'SuperAdmin' }

# == User Custom Data
# A hash of additional data you wish to send about your users.
Expand Down
6 changes: 6 additions & 0 deletions config/schedule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ remove_stale_contact_inboxes_job.rb:
cron: '30 22 * * *'
class: 'Internal::RemoveStaleContactInboxesJob'
queue: scheduled_jobs

# executed at every Monday at 0330 UTC
weekly_conversation_report_job:
cron: '30 3 * * 1'
class: 'WeeklyConversationReportJob'
queue: scheduled_jobs
Loading