Skip to content

Commit

Permalink
Send recap emails to Athul with overview of Indian check-in
Browse files Browse the repository at this point in the history
  • Loading branch information
zachlatta committed Feb 3, 2018
1 parent 74d8182 commit 5e327d6
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
2 changes: 2 additions & 0 deletions api/app/jobs/close_check_ins_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def perform(interaction_ids = nil)

close check_in
end

AthulClubMailer.check_in_recap.deliver_later
end

# Not allowing when blocks with events is currently broken. Tracked in:
Expand Down
66 changes: 66 additions & 0 deletions api/app/mailers/athul_club_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# frozen_string_literal: true

class AthulClubMailer < ApplicationMailer
def check_in_recap
india_clubs = Club.india

@weeks = []
@clubs = []
@stats = {}

4.times.each do |i|
week_num = i + 1 # start counting at 1, not 0

week_start = week_num.weeks.ago.beginning_of_week
week_end = week_start.end_of_week

week_name = week_start.strftime('%m/%d') + ' - ' + \
week_end.strftime('%m/%d')

@weeks.push(
name: week_name,
start: week_start,
end: week_end
)
end

india_clubs.each do |club|
club_obj = {
name: club.name,
check_ins: []
}

@weeks.each do |week|
check_in = club.check_ins.find_by(
'? <= meeting_date AND meeting_date >= ?',
week[:start], week[:end]
)

if check_in.present?
club_obj[:check_ins].push(check_in.attendance)
else
club_obj[:check_ins].push('N/A')
end
end

@clubs.push(club_obj)
end

most_recent_check_ins = india_clubs.map do |c|
current_week = @weeks.first

c.check_ins.find_by(
'? <= meeting_date AND meeting_date >= ?',
current_week[:start], current_week[:end]
)
end.compact

@stats[:club_count] = india_clubs.count
@stats[:checked_in_count] = most_recent_check_ins.count
@stats[:attendance] = most_recent_check_ins.map(&:attendance).sum

mail to: '[email protected]',
cc: ['[email protected]', '[email protected]'],
subject: 'Indian Clubs Check-In Recap'
end
end
3 changes: 3 additions & 0 deletions api/app/models/club.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ class Club < ApplicationRecord
ACTIVE_STAGE = '5003'
DORMANT_STAGE = '5014'
DEAD_STAGE = '5007'
INDIA_STAGE = '5020'

include Streakable
include Geocodeable

scope :india, -> { where(stage_key: INDIA_STAGE) }

streak_pipeline_key Rails.application.secrets.streak_club_pipeline_key
streak_default_field_mappings key: :streak_key, name: :name, notes: :notes,
stage: :stage_key
Expand Down
28 changes: 28 additions & 0 deletions api/app/views/athul_club_mailer/check_in_recap.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<p>Hello!</p>

<p>Hope you had a great week. Here is a report of check-ins from Indian clubs over the past 4 weeks:</p>

<table>
<tr>
<th></th>
<% @weeks.each do |week| %>
<th><%= week[:name] %></th>
<% end %>
</tr>
<% @clubs.each do |club| %>
<tr>
<th><%= club[:name] %></th>
<% club[:check_ins].each do |check_in| %>
<td><%= check_in %></td>
<% end %>
</tr>
<% end %>
</table>

<p>
Total Indian clubs: <b><%= @stats[:club_count] %></b><br />
Total Indian clubs that checked in: <b><%= @stats[:checked_in_count] %></b><br />
Total attendance last week: <b><%= @stats[:attendance] %></b><br />
</p>

<p>- Monolith</p>

0 comments on commit 5e327d6

Please sign in to comment.