From 1d425c4db86344fccc2f4766a0ca07acfda31687 Mon Sep 17 00:00:00 2001
From: Mike Zhang
Date: Sat, 28 Jul 2018 03:05:14 +0200
Subject: [PATCH 01/18] Merge branch '390_monthly_email_list' of
https://github.com/mz99/rundfunk-mitbestimmen
---
.../app/controllers/application_controller.rb | 7 +++++
backend/app/mailers/user_mailer.rb | 5 ++++
backend/app/models/user.rb | 11 +++++++
.../views/user_mailer/monthly_news.html.erb | 16 ++++++++++
.../views/user_mailer/monthly_news.text.erb | 7 +++++
.../20180719134105_add_last_login_to_user.rb | 5 ++++
backend/spec/models/user_spec.rb | 29 +++++++++++++++++++
7 files changed, 80 insertions(+)
create mode 100644 backend/app/views/user_mailer/monthly_news.html.erb
create mode 100644 backend/app/views/user_mailer/monthly_news.text.erb
create mode 100644 backend/db/migrate/20180719134105_add_last_login_to_user.rb
diff --git a/backend/app/controllers/application_controller.rb b/backend/app/controllers/application_controller.rb
index 0847c8c6f..eea637044 100644
--- a/backend/app/controllers/application_controller.rb
+++ b/backend/app/controllers/application_controller.rb
@@ -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
@@ -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)
diff --git a/backend/app/mailers/user_mailer.rb b/backend/app/mailers/user_mailer.rb
index f2c861ba3..754672c26 100644
--- a/backend/app/mailers/user_mailer.rb
+++ b/backend/app/mailers/user_mailer.rb
@@ -3,4 +3,9 @@ def auth0_migration(user)
@user = user
mail(to: @user.email, subject: 'Auth0 Migration')
end
+
+ def monthly_news(user, _reason)
+ @user = user
+ mail(to: @user.email, subject: '')
+ end
end
diff --git a/backend/app/models/user.rb b/backend/app/models/user.rb
index 9e82a6eb6..a22735d4a 100644
--- a/backend/app/models/user.rb
+++ b/backend/app/models/user.rb
@@ -63,4 +63,15 @@ 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)
+ if broadcasts_created_since_last_login.count >= 5
+ reasons << :recently_created_broadcasts
+ end
+ reasons
+ end
+
+
end
diff --git a/backend/app/views/user_mailer/monthly_news.html.erb b/backend/app/views/user_mailer/monthly_news.html.erb
new file mode 100644
index 000000000..408071b15
--- /dev/null
+++ b/backend/app/views/user_mailer/monthly_news.html.erb
@@ -0,0 +1,16 @@
+
+
+
+
+
+
+ Hello <%= @user.name%>
+
+ Welcome to another month's happenings with the new stuff this month:
+
+ - There are 2 new features
+
+ - We have now 3000 users!
+
+
+
diff --git a/backend/app/views/user_mailer/monthly_news.text.erb b/backend/app/views/user_mailer/monthly_news.text.erb
new file mode 100644
index 000000000..ac056bbdd
--- /dev/null
+++ b/backend/app/views/user_mailer/monthly_news.text.erb
@@ -0,0 +1,7 @@
+Hello <%= @user.name %>
+
+Welcome to another month's happenings with the new stuff this month:
+
+- There are 2 new features
+
+- We have now 3000 users!
diff --git a/backend/db/migrate/20180719134105_add_last_login_to_user.rb b/backend/db/migrate/20180719134105_add_last_login_to_user.rb
new file mode 100644
index 000000000..32034f3fe
--- /dev/null
+++ b/backend/db/migrate/20180719134105_add_last_login_to_user.rb
@@ -0,0 +1,5 @@
+class AddLastLoginToUser < ActiveRecord::Migration[5.1]
+ def change
+ add_column :users, :last_login, :datetime
+ end
+end
diff --git a/backend/spec/models/user_spec.rb b/backend/spec/models/user_spec.rb
index 787721f7a..8d23e8c0c 100644
--- a/backend/spec/models/user_spec.rb
+++ b/backend/spec/models/user_spec.rb
@@ -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)
+ 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) }
From 916ac7030d4d0a9f045e5dfd986c36eb7a3517a3 Mon Sep 17 00:00:00 2001
From: Mike Zhang
Date: Sat, 28 Jul 2018 03:12:32 +0200
Subject: [PATCH 02/18] added implementation for tests
---
backend/app/models/user.rb | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/backend/app/models/user.rb b/backend/app/models/user.rb
index a22735d4a..809a26b3c 100644
--- a/backend/app/models/user.rb
+++ b/backend/app/models/user.rb
@@ -70,8 +70,15 @@ def reasons_for_notifications
if broadcasts_created_since_last_login.count >= 5
reasons << :recently_created_broadcasts
end
+
+ #check to see if current_user has impressions first, current_user can't be detected
+ distributed_sum = current_user.impressions.to_a.sum {|i| i[:amount].to_f }
+ if u.impressions.to_a.any? {|r| r[:response] == 'positive'} && distributed_sum == 0
+ reasons << :no_given_amount_for_supported_broadcasts
+ end
+ if u.impressions.to_a.any? {|r| r[:response] == 'positive'} && distributed_sum == 17.5
+ reasons << :unbalanced_distribution
+ end
reasons
end
-
-
end
From 94b64ed62eb6df6890dcb8ce834c2dfe6f6e3f31 Mon Sep 17 00:00:00 2001
From: Mike Zhang
Date: Sun, 29 Jul 2018 21:32:18 +0200
Subject: [PATCH 03/18] fixed unbalanced distribution implementation
---
backend/app/models/user.rb | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/backend/app/models/user.rb b/backend/app/models/user.rb
index 809a26b3c..d89acc4b8 100644
--- a/backend/app/models/user.rb
+++ b/backend/app/models/user.rb
@@ -70,13 +70,12 @@ def reasons_for_notifications
if broadcasts_created_since_last_login.count >= 5
reasons << :recently_created_broadcasts
end
-
#check to see if current_user has impressions first, current_user can't be detected
- distributed_sum = current_user.impressions.to_a.sum {|i| i[:amount].to_f }
- if u.impressions.to_a.any? {|r| r[:response] == 'positive'} && distributed_sum == 0
+ distributed_sum = self.impressions.to_a.sum {|i| i[:amount].to_f }
+ if self.impressions.to_a.any? {|r| r[:response] == 'positive'} && distributed_sum == 0
reasons << :no_given_amount_for_supported_broadcasts
end
- if u.impressions.to_a.any? {|r| r[:response] == 'positive'} && distributed_sum == 17.5
+ if self.impressions.to_a.any? {|r| r[:response] == 'positive' && r[:amount] == 17.5}
reasons << :unbalanced_distribution
end
reasons
From 9b64a47f9e13d1627a39fa6518cbc3f044db572c Mon Sep 17 00:00:00 2001
From: Mike Zhang
Date: Sun, 29 Jul 2018 22:10:04 +0200
Subject: [PATCH 04/18] added templates to monthly email
---
.../views/user_mailer/monthly_news.html.erb | 34 +++++++++++++++----
.../views/user_mailer/monthly_news.text.erb | 27 +++++++++++++--
2 files changed, 52 insertions(+), 9 deletions(-)
diff --git a/backend/app/views/user_mailer/monthly_news.html.erb b/backend/app/views/user_mailer/monthly_news.html.erb
index 408071b15..b74942d9c 100644
--- a/backend/app/views/user_mailer/monthly_news.html.erb
+++ b/backend/app/views/user_mailer/monthly_news.html.erb
@@ -4,13 +4,35 @@
- Hello <%= @user.name%>
-
- Welcome to another month's happenings with the new stuff this month:
+
Hello <%= @user.name %>,
- - There are 2 new features
+ Welcome to another month's happenings with the new stuff this month:
- - We have now 3000 users!
-
+ <% 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| %>
+
+ <% 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