Skip to content

Commit

Permalink
Merge pull request swapmyvote#971 from aspiers/reminder-emails
Browse files Browse the repository at this point in the history
  • Loading branch information
aspiers authored Jul 4, 2024
2 parents 78c57e6 + 2a058fc commit b317f43
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/controllers/admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def send_email_proofs
UserMailer.not_swapped_follow_up(current_user).deliver_now
UserMailer.partner_has_voted(current_user).deliver_now
UserMailer.reminder_to_get_swapping(current_user).deliver_now
UserMailer.reminder_to_accept_swap(current_user, other_user).deliver_now
UserMailer.reminder_to_vote(current_user).deliver_now
UserMailer.no_swap(current_user).deliver_now
UserMailer.swap_not_confirmed(current_user).deliver_now
Expand Down
7 changes: 7 additions & 0 deletions app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ def reminder_to_get_swapping(user)
mail(to: @user.email, subject: "It's not too late to swap your vote!")
end

def reminder_to_accept_swap(user, incoming_offer_user)
return nil if user.email.blank?
@user = user
@incoming_offer_user = incoming_offer_user
mail(to: @user.email, subject: "Don't keep your vote swap waiting!")
end

def reminder_to_vote(user)
return nil if user.email.blank?
@user = user
Expand Down
2 changes: 2 additions & 0 deletions app/models/sent_email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ class SentEmail < ApplicationRecord

WELCOME = :welcome_email
REMINDER_GET_SWAPPING = :reminder_get_swapping
REMINDER_PENDING_OFFER = :reminder_pending_offer
REMINDER_VOTE = :reminder_vote
end
13 changes: 13 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,24 @@ def send_get_swapping_reminder_email
sent_emails.create!(template: SentEmail::REMINDER_GET_SWAPPING)
end

def send_pending_swap_reminder_email(other_user)
template = SentEmail::REMINDER_PENDING_OFFER
already_sent = sent_emails.where(template: template).first
return if already_sent

save
UserMailer.reminder_to_accept_swap(self, other_user).deliver_now
sent_emails.create!(template: template)
end

def send_vote_reminder_email
# FIXME: check SentEmail to avoid duplicates
# Errrm, looks like we have two ways to check if this mail was sent ?
return if sent_vote_reminder_email
self.sent_vote_reminder_email = true
save
UserMailer.reminder_to_vote(self).deliver_now
sent_emails.create!(template: SentEmail::REMINDER_VOTE)
end

def name
Expand Down
38 changes: 38 additions & 0 deletions app/views/user_mailer/reminder_to_accept_swap.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
%p
Hi #{@user.name},

%p
Action needed! You haven't yet accepted the offer from

= @incoming_offer_user.redacted_name

to vote for your preferred party #{@user.preferred_party.name} in
#{@incoming_offer_user.constituency.name}.

%p
Please don't keep them waiting! Either

= succeed "," do
= link_to "accept their offer", root_url

or

= succeed "," do
= link_to "decline it", root_url

so that you can receive other offers and they can look for another
swap elsewhere.

%p
If you accept, you agree to vote for their preferred party
#{@user.willing_party.name} in your constituency of
(#{@user.constituency.name}).

%p
All the best,
%br
Swap My Vote Team
%p
<a href="https://www.swapmyvote.uk/?utm_source=email&utm_medium=reminder_to_vote&utm_campaign=site">Swap My Vote</a>
%br
= render partial: "footer_fixed_links"
26 changes: 26 additions & 0 deletions lib/tasks/users.rake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ namespace :users do
task :send_get_swapping_reminder_emails, [:dry_run] => :environment do |t, args|
args.with_defaults(dry_run: false)

puts "dry_run is #{args[:dry_run].inspect} on send_get_swapping_reminder_emails"

for user in User.left_outer_joins(:incoming_swap).where(swap_id: nil, "swaps.chosen_user_id": nil)
can_receive = user.can_receive_email?("get swapping")
if args[:dry_run]
Expand Down Expand Up @@ -38,4 +40,28 @@ namespace :users do
end
end
end

desc "Send pending offer reminder emails to everyone with an unconfirmed incoming swap"
task :send_pending_offer_reminder_emails, [:dry_run] => :environment do |t, args|
args.with_defaults(dry_run: false)

puts "dry_run is #{args[:dry_run].inspect} on send_pending_offer_reminder_emails"

for swap in Swap.where(confirmed: false).eager_load(:chosen_user)
user = swap.chosen_user
can_receive = user.can_receive_email?("pending offer")

if args[:dry_run]
puts "Would send to #{user.name_and_email}" if can_receive
next
end

begin
puts user.name_and_email if can_receive
user.send_pending_swap_reminder_email(swap.choosing_user)
rescue => e
puts "Failed to send vote reminder - #{e.inspect}"
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<p>
Hi John (test user),
</p>
<p>
Action needed! You haven't yet accepted the offer from
Jane (test user)
to vote for your preferred party PartyA in
Constituency2.
</p>
<p>
Please don't keep them waiting! Either
<a href="http://test.host/">accept their offer</a>,
or
<a href="http://test.host/">decline it</a>,
so that you can receive other offers and they can look for another
swap elsewhere.
</p>
<p>
If you accept, you agree to vote for their preferred party
PartyB in your constituency of
(Constituency1).
</p>
<p>
All the best,
<br>
Swap My Vote Team
</p>
<p>
<a href="https://www.swapmyvote.uk/?utm_source=email&utm_medium=reminder_to_vote&utm_campaign=site">Swap My Vote</a>
<br>
<a href="https://twitter.com/intent/follow?screen_name=swapmyvote">Follow us on Twitter</a>
<br>
<a href="https://facebook.com/swapmyvote">Like us on Facebook</a>

</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<p>
Hi John (test user),
</p>
<p>
Action needed! You haven't yet accepted the offer from
Jane (test user)
to vote for your preferred party PartyA in
Constituency2.
</p>
<p>
Please don't keep them waiting! Either
<a href="http://test.host/">accept their offer</a>,
or
<a href="http://test.host/">decline it</a>,
so that you can receive other offers and they can look for another
swap elsewhere.
</p>
<p>
If you accept, you agree to vote for their preferred party
PartyB in your constituency of
(Constituency1).
</p>
<p>
All the best,
<br>
Swap My Vote Team
</p>
<p>
<a href="https://www.swapmyvote.uk/?utm_source=email&utm_medium=reminder_to_vote&utm_campaign=site">Swap My Vote</a>
<br>
<a href="https://twitter.com/intent/follow?screen_name=swapmyvote">Follow us on Twitter</a>
<br>
<a href="https://facebook.com/swapmyvote">Like us on Facebook</a>

</p>
35 changes: 35 additions & 0 deletions spec/views/user_mailer/reminder_to_accept_swap.html.haml_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require "rails_helper"

RSpec.describe "user_mailer/reminder_to_accept_swap", type: :view do
before do
allow(OnsConstituency).to receive(:all).and_return([build(:wakefield), build(:tiverton)])
end

context "in a by-election" do
specify "matches snapshot" do
allow(view).to receive(:general_election?).and_return(false)
swap = build(:swap_with_two_users, confirmed: false)

assign(:user, swap.choosing_user)
assign(:incoming_offer_user, swap.chosen_user)

expect { render }.not_to raise_error

expect(rendered).to match_snapshot("reminder_to_accept_swap_by_election")
end
end

context "in a general election" do
specify "matches snapshot" do
allow(view).to receive(:general_election?).and_return(true)
swap = build(:swap_with_two_users, confirmed: false)

assign(:user, swap.choosing_user)
assign(:incoming_offer_user, swap.chosen_user)

expect { render }.not_to raise_error

expect(rendered).to match_snapshot("reminder_to_accept_swap_general")
end
end
end

0 comments on commit b317f43

Please sign in to comment.