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

Weekly supervisor digest mailer #1346

Merged
merged 9 commits into from
Nov 15, 2020
Merged
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
14 changes: 14 additions & 0 deletions app/decorators/casa_case_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ def case_contacts_ordered_by_occurred_at
object.case_contacts.sort_by(&:occurred_at)
end

def case_contacts_latest
object.case_contacts.max_by(&:occurred_at)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a scope on the model, to avoid having to pull all the case contacts every time.

end

def successful_contacts_this_week
this_week = Date.today - 7.days..Date.today
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we already had something like this somewhere... I want to have all this logic in one central place. I will take a look around.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like it if we could split out a helper or something(?) to hold all our date-related logic like this

Existing in user:

  def recent_contacts_made(days_counter = 60)
    case_contacts.where(contact_made: true, occurred_at: days_counter.days.ago..Date.today).size
  end

object.case_contacts.where(occurred_at: this_week).where(contact_made: true).count
end

def unsuccessful_contacts_this_week
this_week = Date.today - 7.days..Date.today
object.case_contacts.where(occurred_at: this_week).where(contact_made: false).count
end

def court_report_select_option
[
"#{object.case_number} - #{object.has_transitioned? ? "transition" : "non-transition"}",
Expand Down
8 changes: 8 additions & 0 deletions app/mailers/supervisor_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class SupervisorMailer < ApplicationMailer
default from: "CASA Admin <no-reply@casa-r4g-staging.herokuapp.com>"

def weekly_digest(supervisor)
@supervisor = supervisor
mail(to: @supervisor.email, subject: "Weekly summary of volunteer's activities for the weeek of #{Date.today - 7.days}")
end
end
49 changes: 49 additions & 0 deletions app/views/supervisor_mailer/weekly_digest.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<meta itemprop="name" content="Court Report Due Reminder Email" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
<table width="100%" cellpadding="0" cellspacing="0" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
<tr style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0;">
<td class="content-block" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 18px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">
<%= @supervisor.display_name %>,
</td>
</tr>
<tr style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; margin: 0;">
<td class="content-block" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 16px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">
Here's a summary of what happened with your volunteers this last week.
</td>
</tr>
<% @supervisor.volunteers.each do |volunteer| %>

<% volunteer.case_assignments_with_cases.each do |case_assignment| %>
<% casa_case = case_assignment.casa_case %>
<tr style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; margin: 0; text-align: left;">
<td class="content-block" style="font-family: Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; vertical-align: top; margin: 0; padding: 0 0 20px;" valign="top">
<b><%= "Summary for #{volunteer.display_name} Case #{casa_case.case_number}" %></b>
<br>
<% successful_contacts = casa_case.decorate.successful_contacts_this_week %>
<% unsuccessful_contacts = casa_case.decorate.unsuccessful_contacts_this_week %>
<% if successful_contacts + unsuccessful_contacts > 0 %>
<%= "Number of succesful case contacts made this week: #{successful_contacts}" %>
<br>
<%= "Number of unsuccesful case contacts made this week: #{unsuccessful_contacts} " %>
<br>
<% recent_contact = casa_case.decorate.case_contacts_latest %>
<%= "Most recent contact attempted:" %>
<br>
<%= " - Date: #{recent_contact&.occurred_at&.strftime("%B %e, %Y")}" %>
<br>
<%= " - Type: #{recent_contact&.decorate.contact_types}" %>
<br>
<%= " - Duration: #{recent_contact&.duration_minutes}" %>
<br>
<%= " - Contact Made: #{recent_contact&.contact_made}" %>
<br>
<%= " - Medium Type: #{recent_contact&.medium_type}" %>
<br>
<%= " - Notes: #{recent_contact&.notes}" %>
<% else %>
No contact attempts for this week.
<% end %>
</td>
</tr>
<% end %>
<% end %>
</table>
8 changes: 8 additions & 0 deletions lib/mailers/previews/supervisor_mailer_preview.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Preview all emails at http://localhost:3000/rails/mailers/supervisor_mailer
# :nocov:
class SupervisorMailerPreview < ActionMailer::Preview
def weekly_digest
SupervisorMailer.weekly_digest(User.last)
end
end
# :nocov:
6 changes: 6 additions & 0 deletions lib/tasks/send_supervisor_digest.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
desc "Send an email to supervisors each week to share an overview of their volunteers' activities"
task send_supervisor_digest: :environment do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need a cron for this or similar- I assume heroku has something for this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote a mailer for reminders when court reports are due. I believe I was told to create a task that would be run by heroku. I never followed up about that to see if that was scheduled. I would assume heroku has something for that, but I'm not super familiar. I can look it up and try to set that up once this gets merged so we can set it up for this one and the court report reminder.

Supervisor.each do |supervisor|
SupervisorMailer.weekly_digest(supervisor)
end
end