diff --git a/app/jobs/hackathons/digests_delivery_job.rb b/app/jobs/hackathons/digests_delivery_job.rb index 2454b531..aeef402f 100644 --- a/app/jobs/hackathons/digests_delivery_job.rb +++ b/app/jobs/hackathons/digests_delivery_job.rb @@ -12,6 +12,7 @@ def perform sent_digests << digest if digest.persisted? end ensure + Hackathons::OrganizerSummaryDeliveriesJob(sent_digests).perform_later if sent_digests.any? Hackathons::DigestMailer.admin_summary(sent_digests).deliver_later if sent_digests.any? end diff --git a/app/jobs/hackathons/organizer_summary_deliveries_job.rb b/app/jobs/hackathons/organizer_summary_deliveries_job.rb new file mode 100644 index 00000000..62c4feb5 --- /dev/null +++ b/app/jobs/hackathons/organizer_summary_deliveries_job.rb @@ -0,0 +1,18 @@ +class Hackathons::OrganizerSummaryDeliveriesJob < ApplicationJob + def perform(sent_digests) + # This reloads the (possible) sent_digests array as an + # ActiveRecord::Relation so that we can use includes to prevent an N+1. + @sent_digests = Hackathon::Digest.where(id: sent_digests.map(&:id)) + + @sent_digests_by_hackathons = @sent_digests + .includes(listings: :hackathon) + .flat_map(&:listings).group_by(&:hackathon) + .transform_values { |listings| listings.map(&:digest).uniq } + + @listed_hackathons = @sent_digests_by_hackathons.keys + @listed_hackathons.each do |hackathon| + sent_digests = @digests_by_hackathons[hackathon] + Hackathons::DigestMailer.organizer_summary(hackathon, sent_digests).deliver_later if @sent_digests.any? + end + end +end diff --git a/app/mailers/hackathons/digest_mailer.rb b/app/mailers/hackathons/digest_mailer.rb index d7210a7a..4bce48d8 100644 --- a/app/mailers/hackathons/digest_mailer.rb +++ b/app/mailers/hackathons/digest_mailer.rb @@ -31,4 +31,10 @@ def admin_summary(sent_digests) mail to: Hackathons::SUPPORT_EMAIL, cc: User.admins.collect(&:email_address), subject: end + + def organizer_summary(hackathon, sent_digests) + @hackathon = hackathon + @count = sent_digests.count + mail to: hackathon.applicant.email_address, subject: "We've just notified #{@count} hackers about #{hackathon.name}!" + end end diff --git a/app/views/hackathons/digest_mailer/organizer_summary.html.erb b/app/views/hackathons/digest_mailer/organizer_summary.html.erb new file mode 100644 index 00000000..b49a38aa --- /dev/null +++ b/app/views/hackathons/digest_mailer/organizer_summary.html.erb @@ -0,0 +1,10 @@ + +

+ We've just sent an email about <%= @hackathon.name %> to <%= @count %> hackers. +

+ +<% content_for :signature do %> +

+ Best of luck with everything! 🦕 💸 +

+<% end %> diff --git a/app/views/hackathons/digest_mailer/organizer_summary.text.erb b/app/views/hackathons/digest_mailer/organizer_summary.text.erb new file mode 100644 index 00000000..ecff71bf --- /dev/null +++ b/app/views/hackathons/digest_mailer/organizer_summary.text.erb @@ -0,0 +1,5 @@ +We've just sent an email about <%= @hackathon.name %> to <%= @count %> hackers. + +<% content_for :signature do %> + Best of luck with everything! 🦕 💸 +<% end %> diff --git a/test/mailers/hackathons/digest_mailer_test.rb b/test/mailers/hackathons/digest_mailer_test.rb index 5b44bf57..6a1bf4f5 100644 --- a/test/mailers/hackathons/digest_mailer_test.rb +++ b/test/mailers/hackathons/digest_mailer_test.rb @@ -20,4 +20,18 @@ class Hackathons::DigestMailerTest < ActionMailer::TestCase assert_match "unsubscribe", mail.body.encoded assert_match Hackathons::HACK_CLUB_ADDRESS[:full], mail.body.encoded end + + test "organizer_summary" do + sent_digests = [hackathon_digests(:one)] + hackathon = hackathon_digests(:one).listings.first.hackathon + mail = Hackathons::DigestMailer.organizer_summary(hackathon, sent_digests) + + assert_equal "We've just notified #{sent_digests.count} hackers about #{hackathon.name}!", mail.subject + assert_equal [hackathon.applicant.email_address], mail.to + + assert_match "We've just sent an email about #{hackathon.name} to #{sent_digests.count} hackers.", mail.body.encoded + + # Email CAN-SPAM compliance + assert_match Hackathons::HACK_CLUB_ADDRESS[:full], mail.body.encoded + end end