Skip to content

Commit

Permalink
Send initial warning mail about deletion in 40 days
Browse files Browse the repository at this point in the history
  • Loading branch information
Splines committed Jun 1, 2024
1 parent 80413c3 commit e338f37
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 1 deletion.
18 changes: 18 additions & 0 deletions app/mailers/user_cleaner_mailer.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion app/models/user_cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 18 additions & 0 deletions app/views/layouts/warning_mail_layout.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<%= stylesheet_link_tag :email %>
</head>

<body>
<div class="container text-center">
<%= email_image_tag('/MaMpf-Logo_96x96.png', class: 'img-fluid mb-2 pt-4 pb-3' ) %>
<div class="jumbotron">
<%= yield %>
</div>
</div>
</body>

</html>
12 changes: 12 additions & 0 deletions app/views/user_cleaner_mailer/pending_deletion_email.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div class="container">
<div class="blockquote text-start">
<h3>
<%= t('mailer.pending_deletion_subject',
num_days_until_deletion: @num_days_until_deletion) %>
</h3>
<p>
<%= t('mailer.pending_deletion_body_html',
num_days_until_deletion: @num_days_until_deletion) %>
</p>
</div>
</div>
9 changes: 9 additions & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <strong>gelöscht, sofern Du Dich nicht vorher einloggst</strong>.
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'
Expand Down
9 changes: 9 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
<strong>unless you log in to your MaMpf account before then</strong>.
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'
Expand Down
2 changes: 2 additions & 0 deletions spec/models/user_cleaner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e338f37

Please sign in to comment.