From e338f37c1a4f2114d92ede91ab22ecf97ab2aae2 Mon Sep 17 00:00:00 2001 From: Splines Date: Sun, 2 Jun 2024 01:42:28 +0200 Subject: [PATCH] Send initial warning mail about deletion in 40 days --- app/mailers/user_cleaner_mailer.rb | 18 ++++++++++++++++++ app/models/user_cleaner.rb | 3 ++- app/views/layouts/warning_mail_layout.html.erb | 18 ++++++++++++++++++ .../pending_deletion_email.html.erb | 12 ++++++++++++ config/locales/de.yml | 9 +++++++++ config/locales/en.yml | 9 +++++++++ spec/models/user_cleaner_spec.rb | 2 ++ 7 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 app/mailers/user_cleaner_mailer.rb create mode 100644 app/views/layouts/warning_mail_layout.html.erb create mode 100644 app/views/user_cleaner_mailer/pending_deletion_email.html.erb diff --git a/app/mailers/user_cleaner_mailer.rb b/app/mailers/user_cleaner_mailer.rb new file mode 100644 index 000000000..1338d290e --- /dev/null +++ b/app/mailers/user_cleaner_mailer.rb @@ -0,0 +1,18 @@ +class UserCleanerMailer < ApplicationMailer + layout "warning_mail_layout" + + # Creates an email to inform a user that their account will be deleted. + # + # @param [Integer] num_days_until_deletion: + # The number of days until the account will be deleted. + def pending_deletion_email(num_days_until_deletion) + user = params[:user] + sender = "#{t("mailer.warning")} <#{DefaultSetting::PROJECT_EMAIL}>" + I18n.locale = user.locale + + @num_days_until_deletion = num_days_until_deletion + subject = t("mailer.pending_deletion_subject", + num_days_until_deletion: @num_days_until_deletion) + mail(from: sender, to: user.email, subject: subject, priority: "high") + end +end diff --git a/app/models/user_cleaner.rb b/app/models/user_cleaner.rb index 66ecbe607..6a626e0fd 100644 --- a/app/models/user_cleaner.rb +++ b/app/models/user_cleaner.rb @@ -8,13 +8,14 @@ def inactive_users User.where("last_sign_in_at < ?", 6.months.ago.to_date) end - # Sets the deletion date for inactive users. + # Sets the deletion date for inactive users and sends an initial warning mail. # # This method finds all inactive users whose deletion date is nil (not set yet) # and updates their deletion date to be 40 days from the current date. def set_deletion_date_for_inactive_users inactive_users.where(deletion_date: nil).find_each do |user| user.update(deletion_date: Date.current + 40.days) + UserCleanerMailer.with(user: user).pending_deletion_email(40).deliver_now end end diff --git a/app/views/layouts/warning_mail_layout.html.erb b/app/views/layouts/warning_mail_layout.html.erb new file mode 100644 index 000000000..9ae0d3135 --- /dev/null +++ b/app/views/layouts/warning_mail_layout.html.erb @@ -0,0 +1,18 @@ + + + + + + <%= stylesheet_link_tag :email %> + + + +
+ <%= email_image_tag('/MaMpf-Logo_96x96.png', class: 'img-fluid mb-2 pt-4 pb-3' ) %> +
+ <%= yield %> +
+
+ + + diff --git a/app/views/user_cleaner_mailer/pending_deletion_email.html.erb b/app/views/user_cleaner_mailer/pending_deletion_email.html.erb new file mode 100644 index 000000000..ad98f7ee5 --- /dev/null +++ b/app/views/user_cleaner_mailer/pending_deletion_email.html.erb @@ -0,0 +1,12 @@ +
+
+

+ <%= t('mailer.pending_deletion_subject', + num_days_until_deletion: @num_days_until_deletion) %> +

+

+ <%= t('mailer.pending_deletion_body_html', + num_days_until_deletion: @num_days_until_deletion) %> +

+
+
diff --git a/config/locales/de.yml b/config/locales/de.yml index 23ec38b1d..69d9984c5 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -3035,6 +3035,15 @@ de: en: 'Englisch' de: 'Deutsch' mailer: + warning: "MaMpf Warning" + pending_deletion_subject: "%{num_days_until_deletion} Tage bis zur + Löschung Deines MaMpf-Accounts" + pending_deletion_body_html: > + Wir haben festgestellt, dass Du Dich in den letzten 6 Monaten nicht in + MaMpf eingeloggt hast. Dein Account wird in %{num_days_until_deletion} + Tagen gelöscht, sofern Du Dich nicht vorher einloggst. + Bitte beachte, dass nach der Löschung Deines Accounts alle bei MaMpf + gespeicherten Daten von Dir unwiderruflich verloren gehen. notification: 'MaMpf-Benachrichtigung' notification_header: 'Benachrichtigung von MaMpf' medium_subject: 'Neues Medium' diff --git a/config/locales/en.yml b/config/locales/en.yml index 6eb83a3c3..41c7a8ed5 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2853,6 +2853,15 @@ en: no_zip: This file is not a .zip archive. to_big: This file is too big. mailer: + warning: "MaMpf Warning" + pending_deletion_subject: "%{num_days_until_deletion} days before your + account will be deleted" + pending_deletion_body_html: > + We've noticed that you haven't logged in to your MaMpf account in the last + 6 months. Your account will be deleted in %{num_days_until_deletion} days + unless you log in to your MaMpf account before then. + Please note that once your account is deleted, all of your data stored + at MaMpf will be permanently lost. notification: 'MaMpf notification' notification_header: 'MaMpf notification' medium_subject: 'New Medium' diff --git a/spec/models/user_cleaner_spec.rb b/spec/models/user_cleaner_spec.rb index 7ec168b0e..0ac47e5ae 100644 --- a/spec/models/user_cleaner_spec.rb +++ b/spec/models/user_cleaner_spec.rb @@ -82,4 +82,6 @@ expect(User.where(id: user_teacher.id)).to exist expect(User.where(id: user_editor.id)).to exist end + + # TODO: https://stackoverflow.com/questions/27647749/how-to-test-actionmailer-deliver-later-with-rspec end