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| %> +

    <%= 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

diff --git a/backend/app/views/user_mailer/monthly_news.text.erb b/backend/app/views/user_mailer/monthly_news.text.erb index ac056bbdd..492a699d6 100644 --- a/backend/app/views/user_mailer/monthly_news.text.erb +++ b/backend/app/views/user_mailer/monthly_news.text.erb @@ -1,7 +1,28 @@ -Hello <%= @user.name %> +Hello <%= @user.name %>, Welcome to another month's happenings with the new stuff this month: -- There are 2 new features +<% 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 %> -- We have now 3000 users! + +<% 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 From efc6ae56df048e9daa357976d80a5a65d08f4e41 Mon Sep 17 00:00:00 2001 From: Mike Zhang Date: Mon, 30 Jul 2018 17:57:54 +0200 Subject: [PATCH 05/18] Added rake task, modified mailer parameters --- backend/app/mailers/user_mailer.rb | 4 ++-- backend/lib/tasks/monthly_mailer.rake | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 backend/lib/tasks/monthly_mailer.rake diff --git a/backend/app/mailers/user_mailer.rb b/backend/app/mailers/user_mailer.rb index 754672c26..e88e92acf 100644 --- a/backend/app/mailers/user_mailer.rb +++ b/backend/app/mailers/user_mailer.rb @@ -4,8 +4,8 @@ def auth0_migration(user) mail(to: @user.email, subject: 'Auth0 Migration') end - def monthly_news(user, _reason) + def monthly_news(user) @user = user - mail(to: @user.email, subject: '') + mail(to: @user.email, subject: 'New happenings at Rundfunk Mitbestimmen') end end diff --git a/backend/lib/tasks/monthly_mailer.rake b/backend/lib/tasks/monthly_mailer.rake new file mode 100644 index 000000000..23cad08a5 --- /dev/null +++ b/backend/lib/tasks/monthly_mailer.rake @@ -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 + User.find_each do |user| + UserMailer.monthly_news(user).deliver_now + end + end +end From 32399792901b89a38b6baaa43c3ab865d3921eaf Mon Sep 17 00:00:00 2001 From: Mike Zhang Date: Mon, 30 Jul 2018 18:06:05 +0200 Subject: [PATCH 06/18] removed comment from user.rb --- backend/app/models/user.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/app/models/user.rb b/backend/app/models/user.rb index d89acc4b8..800f41d8e 100644 --- a/backend/app/models/user.rb +++ b/backend/app/models/user.rb @@ -70,7 +70,6 @@ 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 = 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 From ae239d29c102852b8c9c4f4fd8f6c27416b110d4 Mon Sep 17 00:00:00 2001 From: Mike Zhang Date: Mon, 30 Jul 2018 18:10:15 +0200 Subject: [PATCH 07/18] rubocop'ed all offenses on user.rb --- backend/app/models/user.rb | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/backend/app/models/user.rb b/backend/app/models/user.rb index 800f41d8e..6cbef2d69 100644 --- a/backend/app/models/user.rb +++ b/backend/app/models/user.rb @@ -67,16 +67,10 @@ def update_and_reverse_geocode(params) 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 - 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 self.impressions.to_a.any? {|r| r[:response] == 'positive' && r[:amount] == 17.5} - reasons << :unbalanced_distribution - end + reasons << :recently_created_broadcasts if broadcasts_created_since_last_login.count >= 5 + distributed_sum = impressions.to_a.sum { |i| i[:amount].to_f } + reasons << :no_given_amount_for_supported_broadcasts if impressions.to_a.any? { |r| r[:response] == 'positive' } && distributed_sum.zero? + reasons << :unbalanced_distribution if impressions.to_a.any? { |r| r[:response] == 'positive' && r[:amount] == 17.5 } reasons end end From 72961c9bb4dcb0a1e0054e5169281809fa4216b3 Mon Sep 17 00:00:00 2001 From: Mike Zhang Date: Tue, 31 Jul 2018 16:58:08 +0200 Subject: [PATCH 08/18] added email subject line translations to monthly email --- backend/app/mailers/user_mailer.rb | 2 +- backend/config/locales/de.yml | 4 +++- backend/config/locales/en.yml | 3 +++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/backend/app/mailers/user_mailer.rb b/backend/app/mailers/user_mailer.rb index e88e92acf..3bab052d0 100644 --- a/backend/app/mailers/user_mailer.rb +++ b/backend/app/mailers/user_mailer.rb @@ -6,6 +6,6 @@ def auth0_migration(user) def monthly_news(user) @user = user - mail(to: @user.email, subject: 'New happenings at Rundfunk Mitbestimmen') + mail(to: @user.email, subject: t('user_mailer.monthly_news.subject')) end end diff --git a/backend/config/locales/de.yml b/backend/config/locales/de.yml index 9148db307..acc23d6c4 100644 --- a/backend/config/locales/de.yml +++ b/backend/config/locales/de.yml @@ -25,6 +25,9 @@ de: has_one: Datensatz kann nicht gelöscht werden, da ein abhängiger %{record}-Datensatz existiert. has_many: Datensatz kann nicht gelöscht werden, da abhängige %{record} existieren. + user_mailer: + monthly_news: + subject: Neue Ereignisse im Rundfunk Mitbestimmen date: abbr_day_names: - So @@ -234,4 +237,3 @@ de: long: "%A, %d. %B %Y, %H:%M Uhr" short: "%d. %B, %H:%M Uhr" pm: nachmittags - diff --git a/backend/config/locales/en.yml b/backend/config/locales/en.yml index 6a4f72d59..80d7cd45b 100644 --- a/backend/config/locales/en.yml +++ b/backend/config/locales/en.yml @@ -13,3 +13,6 @@ en: attributes: amount: total: Total amount of %{sum} exceeds the available budget of %{budget} + user_mailer: + monthly_news: + subject: New happenings in Rundfunk Mitbestimmen From a3f9f42eb1fc91e110db67d362d5c258b8823afe Mon Sep 17 00:00:00 2001 From: Mike Zhang Date: Tue, 31 Jul 2018 16:58:08 +0200 Subject: [PATCH 09/18] added email subject line translations to monthly email --- backend/app/mailers/user_mailer.rb | 2 +- backend/config/locales/de.yml | 4 +++- backend/config/locales/en.yml | 3 +++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/backend/app/mailers/user_mailer.rb b/backend/app/mailers/user_mailer.rb index e88e92acf..3bab052d0 100644 --- a/backend/app/mailers/user_mailer.rb +++ b/backend/app/mailers/user_mailer.rb @@ -6,6 +6,6 @@ def auth0_migration(user) def monthly_news(user) @user = user - mail(to: @user.email, subject: 'New happenings at Rundfunk Mitbestimmen') + mail(to: @user.email, subject: t('user_mailer.monthly_news.subject')) end end diff --git a/backend/config/locales/de.yml b/backend/config/locales/de.yml index 9148db307..acc23d6c4 100644 --- a/backend/config/locales/de.yml +++ b/backend/config/locales/de.yml @@ -25,6 +25,9 @@ de: has_one: Datensatz kann nicht gelöscht werden, da ein abhängiger %{record}-Datensatz existiert. has_many: Datensatz kann nicht gelöscht werden, da abhängige %{record} existieren. + user_mailer: + monthly_news: + subject: Neue Ereignisse im Rundfunk Mitbestimmen date: abbr_day_names: - So @@ -234,4 +237,3 @@ de: long: "%A, %d. %B %Y, %H:%M Uhr" short: "%d. %B, %H:%M Uhr" pm: nachmittags - diff --git a/backend/config/locales/en.yml b/backend/config/locales/en.yml index 6a4f72d59..80d7cd45b 100644 --- a/backend/config/locales/en.yml +++ b/backend/config/locales/en.yml @@ -13,3 +13,6 @@ en: attributes: amount: total: Total amount of %{sum} exceeds the available budget of %{budget} + user_mailer: + monthly_news: + subject: New happenings in Rundfunk Mitbestimmen From 9cc2524d09d2ab2cf53fe98a234a5e2ce6317d9f Mon Sep 17 00:00:00 2001 From: Mike Zhang Date: Mon, 6 Aug 2018 22:23:36 +0200 Subject: [PATCH 10/18] Added translation to monthly mailer. Changed mailer templates to use translations. --- backend/app/mailers/user_mailer.rb | 2 +- .../views/user_mailer/monthly_news.html.erb | 21 +++++++------------ .../views/user_mailer/monthly_news.text.erb | 19 +++++++---------- backend/config/application.rb | 1 + backend/config/locales/de.yml | 17 ++++++++++++++- backend/config/locales/en.yml | 18 +++++++++++++++- 6 files changed, 49 insertions(+), 29 deletions(-) diff --git a/backend/app/mailers/user_mailer.rb b/backend/app/mailers/user_mailer.rb index 3bab052d0..1c88c1c0b 100644 --- a/backend/app/mailers/user_mailer.rb +++ b/backend/app/mailers/user_mailer.rb @@ -6,6 +6,6 @@ def auth0_migration(user) def monthly_news(user) @user = user - mail(to: @user.email, subject: t('user_mailer.monthly_news.subject')) + mail(to: @user.email, subject: I18n.t('user_mailer.monthly_news.subject')) end end diff --git a/backend/app/views/user_mailer/monthly_news.html.erb b/backend/app/views/user_mailer/monthly_news.html.erb index b74942d9c..e392f5199 100644 --- a/backend/app/views/user_mailer/monthly_news.html.erb +++ b/backend/app/views/user_mailer/monthly_news.html.erb @@ -4,12 +4,12 @@ -

Hello <%= @user.name %>,

+

<%= I18n.t('user_mailer.monthly_news.header.hello') %> <%= @user.name %>,

-

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

+

<%= I18n.t('user_mailer.monthly_news.header.welcome') %>

<% if self.reasons_for_notifications.include? :recently_created_broadcasts %> -

Here are the new broadcasts at Rundfunk Mitbestimmen since you last logged in: +

<%= I18n.t('user_mailer.monthly_news.body.recently_created_broadcasts') %> <% broadcasts_created_since_last_login.each do |b| %>

    <%= b.title %>
<% end %> @@ -17,22 +17,15 @@ <% 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! -

+

<%= I18n.t('user_mailer.monthly_news.body.no_given_amount_for_supported_broadcasts') %>

<% 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! -

+

<%= I18n.t('user_mailer.monthly_news.body.unbalanced_distribution') %>

<% end %> -

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

+

<%= I18n.t('user_mailer.monthly_news.footer.closing') %>

-

Best Regards,
- The Rundfunk Mitbestimmen team

+

<%= I18n.t('user_mailer.monthly_news.footer.sign_off') %>

diff --git a/backend/app/views/user_mailer/monthly_news.text.erb b/backend/app/views/user_mailer/monthly_news.text.erb index 492a699d6..6e24ef3bc 100644 --- a/backend/app/views/user_mailer/monthly_news.text.erb +++ b/backend/app/views/user_mailer/monthly_news.text.erb @@ -1,9 +1,9 @@ -Hello <%= @user.name %>, +<%= I18n.t('user_mailer.monthly_news.header.hello') %> <%= @user.name %>, -Welcome to another month's happenings with the new stuff this month: +<%= I18n.t('user_mailer.monthly_news.header.welcome') %> <% if self.reasons_for_notifications.include? :recently_created_broadcasts %> - Here are the new broadcasts at Rundfunk Mitbestimmen since you last logged in: + <%= I18n.t('user_mailer.monthly_news.body.recently_created_broadcasts') %> <% broadcasts_created_since_last_login.each do |b| %> <%= b.title %> <% end %> @@ -11,18 +11,13 @@ Welcome to another month's happenings with the new stuff this month: <% 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! + <%= I18n.t('user_mailer.monthly_news.body.no_given_amount_for_supported_broadcasts') %> <% 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! + <%= I18n.t('user_mailer.monthly_news.body.unbalanced_distribution') %> <% end %> -Thank you for being a valued member of Rundfunk Mitbestimmen! We hope you will check back soon to see -these new updates! +<%= I18n.t('user_mailer.monthly_news.footer.closing') %> -Best Regards, -The Rundfunk Mitbestimmen team +<%= I18n.t('user_mailer.monthly_news.footer.sign_off') %> diff --git a/backend/config/application.rb b/backend/config/application.rb index 2e27a9331..8fd324f85 100644 --- a/backend/config/application.rb +++ b/backend/config/application.rb @@ -33,5 +33,6 @@ class Application < Rails::Application config.filter_parameters << :password config.active_record.schema_format = :sql + config.i18n.default_locale = :de end end diff --git a/backend/config/locales/de.yml b/backend/config/locales/de.yml index acc23d6c4..b06e6ff10 100644 --- a/backend/config/locales/de.yml +++ b/backend/config/locales/de.yml @@ -27,7 +27,22 @@ de: has_many: Datensatz kann nicht gelöscht werden, da abhängige %{record} existieren. user_mailer: monthly_news: - subject: Neue Ereignisse im Rundfunk Mitbestimmen + subject: Neue Updates von Rundfunk Mitbestimmen + header: + hello: Hallo + welcome: Willkommen zu den Updates dieses Monats: + body: + recently_created_broadcasts: Hier sind die neuen Sendungen, die seit Ihrer letzten Anmeldung den Rundfunk Mitbeständen hinzugefügt wurden: + no_given_amount_for_supported_broadcasts: Offenbar haben Sie Übertragungen unterstützt, ohne Ihre Mittel für die ausgewählten Sendungen tatsächlich zu verteilen. + Bitte loggen Sie sich ein und verteilen Sie Gelder für Ihre Lieblingsprogramme! + unbalanced_distribution: Es sieht so aus, als ob Sie alle Ihre Gelder an eine bestimmte Sendung verteilt haben, sind Sie sicher, + dass das Ihre Absicht war? Wenn nicht, loggen Sie sich bitte ein und erkunden Sie weitere Sendungen, um Ihre Gelder zu verteilen. + Wenn das Ihre Absicht war, dann ignorieren Sie bitte diese Nachricht! + footer: + closing: Danke, dass Sie ein geschätztes Mitglied von Rundfunk Mitbestimmen sind! Wir hoffen, dass Sie bald zurückschauen, um diese neuen Updates zu sehen! + sign_off: | + Freundliche Grüße, + Das Rundfunk Mitbestimmen Team date: abbr_day_names: - So diff --git a/backend/config/locales/en.yml b/backend/config/locales/en.yml index 80d7cd45b..dbb794e48 100644 --- a/backend/config/locales/en.yml +++ b/backend/config/locales/en.yml @@ -15,4 +15,20 @@ en: total: Total amount of %{sum} exceeds the available budget of %{budget} user_mailer: monthly_news: - subject: New happenings in Rundfunk Mitbestimmen + subject: New happenings in Rundfunk Mitbestimmen + header: + hello: Hello + welcome: Welcome to this month's updates: + body: + recently_created_broadcasts: Here are the new broadcasts added to Rundfunk Mitbestimmen since you last logged in: + 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! + 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 to distribute your funds. + If that was your intention, then please ignore this message! + footer: + closing: Thank you for being a valued member of Rundfunk Mitbestimmen! We hope you will check back soon to see + these new updates! + sign_off: | + Best Regards, + The Rundfunk Mitbestimmen team From 964d1ed56e92b6766732b53b0a8e87e60c56d289 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sch=C3=A4fer?= Date: Thu, 9 Aug 2018 14:44:58 +0200 Subject: [PATCH 11/18] Fix syntax errors in locales --- backend/config/locales/de.yml | 4 ++-- backend/config/locales/en.yml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/backend/config/locales/de.yml b/backend/config/locales/de.yml index b06e6ff10..6a980caec 100644 --- a/backend/config/locales/de.yml +++ b/backend/config/locales/de.yml @@ -30,9 +30,9 @@ de: subject: Neue Updates von Rundfunk Mitbestimmen header: hello: Hallo - welcome: Willkommen zu den Updates dieses Monats: + welcome: 'Willkommen zu den Updates dieses Monats:' body: - recently_created_broadcasts: Hier sind die neuen Sendungen, die seit Ihrer letzten Anmeldung den Rundfunk Mitbeständen hinzugefügt wurden: + recently_created_broadcasts: 'Hier sind die neuen Sendungen, die seit Ihrer letzten Anmeldung den Rundfunk Mitbeständen hinzugefügt wurden:' no_given_amount_for_supported_broadcasts: Offenbar haben Sie Übertragungen unterstützt, ohne Ihre Mittel für die ausgewählten Sendungen tatsächlich zu verteilen. Bitte loggen Sie sich ein und verteilen Sie Gelder für Ihre Lieblingsprogramme! unbalanced_distribution: Es sieht so aus, als ob Sie alle Ihre Gelder an eine bestimmte Sendung verteilt haben, sind Sie sicher, diff --git a/backend/config/locales/en.yml b/backend/config/locales/en.yml index dbb794e48..dc15978bc 100644 --- a/backend/config/locales/en.yml +++ b/backend/config/locales/en.yml @@ -18,9 +18,9 @@ en: subject: New happenings in Rundfunk Mitbestimmen header: hello: Hello - welcome: Welcome to this month's updates: + welcome: "Welcome to this month's updates:" body: - recently_created_broadcasts: Here are the new broadcasts added to Rundfunk Mitbestimmen since you last logged in: + recently_created_broadcasts: 'Here are the new broadcasts added to Rundfunk Mitbestimmen since you last logged in:' 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! unbalanced_distribution: It looks like you have distributed all of your funds to one particular broadcast, are you sure From 4c5ee5cb8d6c1462aad03a60d3de5fd1f1ad3bf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sch=C3=A4fer?= Date: Thu, 9 Aug 2018 14:45:12 +0200 Subject: [PATCH 12/18] Add missing test case, if last_login = nil ? --- backend/spec/models/user_spec.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/backend/spec/models/user_spec.rb b/backend/spec/models/user_spec.rb index 8d23e8c0c..a2949c199 100644 --- a/backend/spec/models/user_spec.rb +++ b/backend/spec/models/user_spec.rb @@ -12,6 +12,10 @@ subject { user.reasons_for_notifications } context 'by default' do + it { is_expected.to be_empty } + end + + context 'given a last_login timestamp' do before do user.update_columns(last_login: Time.current) end From de616102806eab99f1db5a24c5c0a4c2922f5d9e Mon Sep 17 00:00:00 2001 From: Mike Zhang Date: Mon, 20 Aug 2018 19:43:33 +0200 Subject: [PATCH 13/18] Refactored method reasons_for_notifications for shorter methods --- backend/app/models/user.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/backend/app/models/user.rb b/backend/app/models/user.rb index 6cbef2d69..683667771 100644 --- a/backend/app/models/user.rb +++ b/backend/app/models/user.rb @@ -65,12 +65,13 @@ def update_and_reverse_geocode(params) end def reasons_for_notifications + # Populate the "reasons" array with symbols depending on which conditions it satisfies 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 } - reasons << :no_given_amount_for_supported_broadcasts if impressions.to_a.any? { |r| r[:response] == 'positive' } && distributed_sum.zero? - reasons << :unbalanced_distribution if impressions.to_a.any? { |r| r[:response] == 'positive' && r[:amount] == 17.5 } + distributed_sum = impressions.sum(:amount) + reasons << :no_given_amount_for_supported_broadcasts if impressions.positive.where(amount: nil).present? + reasons << :unbalanced_distribution if impressions.where(amount: 17.5) reasons end end From bdade6ed577da3075d76a37a41c72aff0b36b077 Mon Sep 17 00:00:00 2001 From: Mike Zhang Date: Mon, 20 Aug 2018 20:12:24 +0200 Subject: [PATCH 14/18] added whenever gem --- backend/Gemfile | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/Gemfile b/backend/Gemfile index 9f9f2483b..b1b63c216 100644 --- a/backend/Gemfile +++ b/backend/Gemfile @@ -20,6 +20,7 @@ gem 'sentry-raven' gem 'sidekiq' gem 'sidekiq-unique-jobs' gem 'valid_email2' +gem 'whenever' gem 'rails' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' From 1db6bf13240a6ffc98adb382a0f680f51285aabf Mon Sep 17 00:00:00 2001 From: Mike Zhang Date: Mon, 20 Aug 2018 20:13:07 +0200 Subject: [PATCH 15/18] set rake mailer to email non-active users once a month --- backend/config/schedule.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 backend/config/schedule.rb diff --git a/backend/config/schedule.rb b/backend/config/schedule.rb new file mode 100644 index 000000000..db2e3c072 --- /dev/null +++ b/backend/config/schedule.rb @@ -0,0 +1,23 @@ +# Use this file to easily define all of your cron jobs. +# +# It's helpful, but not entirely necessary to understand cron before proceeding. +# http://en.wikipedia.org/wiki/Cron + +# Example: +# +# set :output, "/path/to/my/cron_log.log" +# +# every 2.hours do +# command "/usr/bin/some_great_command" +# runner "MyModel.some_method" +# rake "some:great:rake:task" +# end +# +# every 4.days do +# runner "AnotherModel.prune_old_records" +# end + +# Learn more: http://github.com/javan/whenever +every 1.month do + rake 'monthly_news' +end From 60b71375072c12ea31f98099dc1359b692e46f27 Mon Sep 17 00:00:00 2001 From: Mike Zhang Date: Mon, 20 Aug 2018 21:02:09 +0200 Subject: [PATCH 16/18] added spec to check for error when user does not have last_login --- backend/spec/models/user_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/backend/spec/models/user_spec.rb b/backend/spec/models/user_spec.rb index a2949c199..e95dfefda 100644 --- a/backend/spec/models/user_spec.rb +++ b/backend/spec/models/user_spec.rb @@ -39,6 +39,11 @@ before { create(:impression, user: user, response: :positive, amount: 17.5) } it { is_expected.to include(:unbalanced_distribution) } end + + context 'error occurs when user has not logged in before' do + before { create(user: user, last_login: nil) } + it { is_expected.to raise_error 'NilPointerException' } + end end describe 'update_and_reverse_geocode' do From a8703cf9452d026bebc1e541e96ac6067aea00da Mon Sep 17 00:00:00 2001 From: Mike Zhang Date: Wed, 5 Sep 2018 22:01:54 +0200 Subject: [PATCH 17/18] subbed in Impression::BUDGET for amount --- backend/app/models/user.rb | 2 +- backend/spec/models/user_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/app/models/user.rb b/backend/app/models/user.rb index 683667771..d3fa23407 100644 --- a/backend/app/models/user.rb +++ b/backend/app/models/user.rb @@ -71,7 +71,7 @@ def reasons_for_notifications reasons << :recently_created_broadcasts if broadcasts_created_since_last_login.count >= 5 distributed_sum = impressions.sum(:amount) reasons << :no_given_amount_for_supported_broadcasts if impressions.positive.where(amount: nil).present? - reasons << :unbalanced_distribution if impressions.where(amount: 17.5) + reasons << :unbalanced_distribution if impressions.where(amount: Impression::BUDGET) reasons end end diff --git a/backend/spec/models/user_spec.rb b/backend/spec/models/user_spec.rb index e95dfefda..0caa6f8ef 100644 --- a/backend/spec/models/user_spec.rb +++ b/backend/spec/models/user_spec.rb @@ -36,7 +36,7 @@ end context 'quite an unbalanced distribution, was it on purpose?' do - before { create(:impression, user: user, response: :positive, amount: 17.5) } + before { create(:impression, user: user, response: :positive, amount: Impression::BUDGET) } it { is_expected.to include(:unbalanced_distribution) } end From f101616005e42694aff341aed98e2d9a7e07efb0 Mon Sep 17 00:00:00 2001 From: Mike Zhang Date: Wed, 5 Sep 2018 22:02:40 +0200 Subject: [PATCH 18/18] added in html safe
tag in monthly email translation for proper footer formatting --- backend/app/views/user_mailer/monthly_news.html.erb | 2 +- backend/app/views/user_mailer/monthly_news.text.erb | 2 +- backend/config/locales/de.yml | 4 ++-- backend/config/locales/en.yml | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/backend/app/views/user_mailer/monthly_news.html.erb b/backend/app/views/user_mailer/monthly_news.html.erb index e392f5199..09585a931 100644 --- a/backend/app/views/user_mailer/monthly_news.html.erb +++ b/backend/app/views/user_mailer/monthly_news.html.erb @@ -26,6 +26,6 @@

<%= I18n.t('user_mailer.monthly_news.footer.closing') %>

-

<%= I18n.t('user_mailer.monthly_news.footer.sign_off') %>

+

<%= I18n.t('user_mailer.monthly_news.footer.sign_off_html') %>

diff --git a/backend/app/views/user_mailer/monthly_news.text.erb b/backend/app/views/user_mailer/monthly_news.text.erb index 6e24ef3bc..524dc9c5e 100644 --- a/backend/app/views/user_mailer/monthly_news.text.erb +++ b/backend/app/views/user_mailer/monthly_news.text.erb @@ -20,4 +20,4 @@ <%= I18n.t('user_mailer.monthly_news.footer.closing') %> -<%= I18n.t('user_mailer.monthly_news.footer.sign_off') %> +<%= I18n.t('user_mailer.monthly_news.footer.sign_off_html') %> diff --git a/backend/config/locales/de.yml b/backend/config/locales/de.yml index 6a980caec..e2951117b 100644 --- a/backend/config/locales/de.yml +++ b/backend/config/locales/de.yml @@ -40,8 +40,8 @@ de: Wenn das Ihre Absicht war, dann ignorieren Sie bitte diese Nachricht! footer: closing: Danke, dass Sie ein geschätztes Mitglied von Rundfunk Mitbestimmen sind! Wir hoffen, dass Sie bald zurückschauen, um diese neuen Updates zu sehen! - sign_off: | - Freundliche Grüße, + sign_off_html: | + Freundliche Grüße,
Das Rundfunk Mitbestimmen Team date: abbr_day_names: diff --git a/backend/config/locales/en.yml b/backend/config/locales/en.yml index dc15978bc..192e788e3 100644 --- a/backend/config/locales/en.yml +++ b/backend/config/locales/en.yml @@ -29,6 +29,6 @@ en: footer: closing: Thank you for being a valued member of Rundfunk Mitbestimmen! We hope you will check back soon to see these new updates! - sign_off: | - Best Regards, + sign_off_html: | + Best Regards,
The Rundfunk Mitbestimmen team