|
| 1 | +class AdminMailer < ApplicationMailer |
| 2 | + include Roadie::Rails::Automatic |
| 3 | + add_template_helper(HackathonManagerHelper) |
| 4 | + |
| 5 | + layout "admin_mailer" |
| 6 | + |
| 7 | + def weekly_report(user_id) |
| 8 | + # Don't send emails more than 7 days after event starts |
| 9 | + stop_date = Date.parse(HackathonConfig["event_start_date"]) + 7.days |
| 10 | + return if Date.today > stop_date |
| 11 | + |
| 12 | + @user = User.find_by_id(user_id) |
| 13 | + |
| 14 | + return unless @user.safe_receive_weekly_report |
| 15 | + |
| 16 | + @period_start = 7.days.ago.at_beginning_of_day |
| 17 | + @period_end = 1.day.ago.at_end_of_day |
| 18 | + @period = @period_start..@period_end |
| 19 | + |
| 20 | + @applications = report_metric(Questionnaire, :created_at) |
| 21 | + @rsvp_confirmed = report_metric(Questionnaire.where(acc_status: "rsvp_confirmed"), :acc_status_date) |
| 22 | + @rsvp_denied = report_metric(Questionnaire.where(acc_status: "rsvp_denied"), :acc_status_date) |
| 23 | + |
| 24 | + @bus_metrics = BusList.all.map do |bus_list| |
| 25 | + { |
| 26 | + name: bus_list.name, |
| 27 | + total: bus_list.passengers.count, |
| 28 | + } |
| 29 | + end |
| 30 | + |
| 31 | + @messages_sent = Message.bulk.where(delivered_at: @period).count |
| 32 | + |
| 33 | + @new_admissions_metrics = [@applications, @rsvp_confirmed, @rsvp_denied].any? { |x| x[:new].positive? } |
| 34 | + @new_communication_metrics = @messages_sent.positive? |
| 35 | + |
| 36 | + # Don't send email if no new activity |
| 37 | + return if !@new_admissions_metrics && !@new_bus_list_metrics && !@new_communication_metrics |
| 38 | + |
| 39 | + mail( |
| 40 | + to: pretty_email(@user.full_name, @user.email), |
| 41 | + subject: "Your Weekly Report", |
| 42 | + ) |
| 43 | + end |
| 44 | + |
| 45 | + private |
| 46 | + |
| 47 | + def report_metric(query_base, new_query_field) |
| 48 | + { |
| 49 | + new: query_base.where(new_query_field => @period).count, |
| 50 | + total: query_base.count, |
| 51 | + } |
| 52 | + end |
| 53 | +end |
0 commit comments