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

[WIP] 390 monthly email list revised #727

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
1d425c4
Merge branch '390_monthly_email_list' of https://github.com/mz99/rund…
mz99 Jul 28, 2018
916ac70
added implementation for tests
mz99 Jul 28, 2018
94b64ed
fixed unbalanced distribution implementation
mz99 Jul 29, 2018
9b64a47
added templates to monthly email
mz99 Jul 29, 2018
efc6ae5
Added rake task, modified mailer parameters
mz99 Jul 30, 2018
3239979
removed comment from user.rb
mz99 Jul 30, 2018
ae239d2
rubocop'ed all offenses on user.rb
mz99 Jul 30, 2018
72961c9
added email subject line translations to monthly email
mz99 Jul 31, 2018
a3f9f42
added email subject line translations to monthly email
mz99 Jul 31, 2018
1310b27
Merge branch '390_monthly_email_list_revised' of https://github.com/r…
mz99 Aug 6, 2018
9cc2524
Added translation to monthly mailer. Changed mailer templates to use …
mz99 Aug 6, 2018
964d1ed
Fix syntax errors in locales
roschaefer Aug 9, 2018
4c5ee5c
Add missing test case, if last_login = nil ?
roschaefer Aug 9, 2018
de61610
Refactored method reasons_for_notifications for shorter methods
mz99 Aug 20, 2018
bdade6e
added whenever gem
mz99 Aug 20, 2018
1db6bf1
set rake mailer to email non-active users once a month
mz99 Aug 20, 2018
60b7137
added spec to check for error when user does not have last_login
mz99 Aug 20, 2018
a8703cf
subbed in Impression::BUDGET for amount
mz99 Sep 5, 2018
f101616
added in html safe <br> tag in monthly email translation for proper f…
mz99 Sep 5, 2018
b8d4dde
Merge remote-tracking branch 'origin/master' into 390_monthly_email_l…
roschaefer Jan 23, 2019
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
7 changes: 7 additions & 0 deletions backend/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class ApplicationController < ActionController::API
before_action :set_paper_trail_whodunnit
before_action :set_locale
before_action :set_raven_context
before_action :set_last_login

def set_locale
I18n.locale = user_locale || guest_locale
Expand All @@ -25,6 +26,12 @@ def user_locale
current_user&.locale
end

def set_last_login
if current_user
current_user.update_columns(last_login: Time.current)
end
end

def set_raven_context
Raven.user_context(id: current_user.id) if current_user
Raven.extra_context(params: params.to_unsafe_h, url: request.url)
Expand Down
5 changes: 5 additions & 0 deletions backend/app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ def auth0_migration(user)
@user = user
mail(to: @user.email, subject: 'Auth0 Migration')
end

def monthly_news(user)
@user = user
mail(to: @user.email, subject: 'New happenings at Rundfunk Mitbestimmen')
mz99 marked this conversation as resolved.
Show resolved Hide resolved
end
end
10 changes: 10 additions & 0 deletions backend/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,14 @@ def update_and_reverse_geocode(params)
end
save
end

def reasons_for_notifications
reasons = []
broadcasts_created_since_last_login = Broadcast.where('created_at >= ?', last_login)
reasons << :recently_created_broadcasts if broadcasts_created_since_last_login.count >= 5
distributed_sum = impressions.to_a.sum { |i| i[:amount].to_f }
mz99 marked this conversation as resolved.
Show resolved Hide resolved
reasons << :no_given_amount_for_supported_broadcasts if impressions.to_a.any? { |r| r[:response] == 'positive' } && distributed_sum.zero?
mz99 marked this conversation as resolved.
Show resolved Hide resolved
reasons << :unbalanced_distribution if impressions.to_a.any? { |r| r[:response] == 'positive' && r[:amount] == 17.5 }
mz99 marked this conversation as resolved.
Show resolved Hide resolved
reasons
end
end
38 changes: 38 additions & 0 deletions backend/app/views/user_mailer/monthly_news.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h2>Hello <%= @user.name %>, </h2>

<p>Welcome to another month's happenings with the new stuff this month: </p>

<% if self.reasons_for_notifications.include? :recently_created_broadcasts %>
<p> Here are the new broadcasts at Rundfunk Mitbestimmen since you last logged in:
mz99 marked this conversation as resolved.
Show resolved Hide resolved
<% broadcasts_created_since_last_login.each do |b| %>
<ul><%= b.title %></ul>
<% end %>
</p>
<% end %>

<% if self.reasons_for_notifications.include? :no_given_amount_for_supported_broadcasts %>
<p> It looks like you have supported broadcasts without actually distributing your funds for your
selected broadcasts. Please log in and distribute funds for your favorite programs!
</p>
<% end %>

<% if self.reasons_for_notifications.include? :unbalanced_distribution %>
<p> It looks like you have distributed all of your funds to one particular broadcast, are you sure
that was your intention? If not, then please log in and explore more broadcasts and distribute your funds
to those. If that was your intention, then please ignore this message!
</p>
<% end %>

<p>Thank you for being a valued member of Rundfunk Mitbestimmen! We hope you will check back soon to see
these new updates! </p>

<p>Best Regards, <br>
The Rundfunk Mitbestimmen team </p>
mz99 marked this conversation as resolved.
Show resolved Hide resolved
</body>
</html>
28 changes: 28 additions & 0 deletions backend/app/views/user_mailer/monthly_news.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Hello <%= @user.name %>,

Welcome to another month's happenings with the new stuff this month:

<% if self.reasons_for_notifications.include? :recently_created_broadcasts %>
Here are the new broadcasts at Rundfunk Mitbestimmen since you last logged in:
<% broadcasts_created_since_last_login.each do |b| %>
<%= b.title %>
<% end %>
<% end %>


<% if self.reasons_for_notifications.include? :no_given_amount_for_supported_broadcasts %>
It looks like you have supported broadcasts without actually distributing your funds for your
selected broadcasts. Please log in and distribute funds for your favorite programs!
<% end %>

<% if self.reasons_for_notifications.include? :unbalanced_distribution %>
It looks like you have distributed all of your funds to one particular broadcast, are you sure
that was your intention? If not, then please log in and explore more broadcasts and distribute your funds
to those. If that was your intention, then please ignore this message!
<% end %>

Thank you for being a valued member of Rundfunk Mitbestimmen! We hope you will check back soon to see
these new updates!

Best Regards,
The Rundfunk Mitbestimmen team
5 changes: 5 additions & 0 deletions backend/db/migrate/20180719134105_add_last_login_to_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddLastLoginToUser < ActiveRecord::Migration[5.1]
def change
add_column :users, :last_login, :datetime
end
end
9 changes: 9 additions & 0 deletions backend/lib/tasks/monthly_mailer.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace :monthly_mailer do
desc "Send mail alerting non-active users new happenings on Rundfunk Mitbestimmen"

task monthly_news: :environment do
mz99 marked this conversation as resolved.
Show resolved Hide resolved
User.find_each do |user|
UserMailer.monthly_news(user).deliver_now
end
end
end
29 changes: 29 additions & 0 deletions backend/spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,35 @@
let(:liked_broadcast) { create(:impression, response: :positive, user: user).broadcast }
let(:unsupported_broadcast) { create(:impression, response: :neutral, user: user).broadcast }

describe '#reasons_for_notifications' do
subject { user.reasons_for_notifications }

context 'by default' do
before do
user.update_columns(last_login: Time.current)
mz99 marked this conversation as resolved.
Show resolved Hide resolved
end
it { is_expected.to be_empty }
end

context 'many new broadcasts since last login' do
before do
user.update_columns(last_login: 2.months.ago)
create_list(:broadcast, 20)
end
it { is_expected.to include(:recently_created_broadcasts) }
end

context 'forgot to distribute money for supported broadcasts' do
before { create_list(:impression, 3, user: user, response: :positive, amount: nil) }
it { is_expected.to include(:no_given_amount_for_supported_broadcasts) }
end

context 'quite an unbalanced distribution, was it on purpose?' do
before { create(:impression, user: user, response: :positive, amount: 17.5) }
it { is_expected.to include(:unbalanced_distribution) }
end
end

describe 'update_and_reverse_geocode' do
subject { user.update_and_reverse_geocode(params) }
let(:user) { create(:user) }
Expand Down