Skip to content

Commit

Permalink
Implement un-setting the deletion date & add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Splines committed Jun 1, 2024
1 parent f77f26c commit 833aeef
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
24 changes: 22 additions & 2 deletions app/models/user_cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class UserCleaner
# Returns all users who have been inactive for 6 months,
# i.e. their last sign-in date is more than 6 months ago.
def inactive_users
User.where("last_sign_in_at < ?", 6.months.ago)
User.where("last_sign_in_at < ?", 6.months.ago.to_date)
end

# Sets the deletion date for inactive users.
Expand All @@ -18,6 +18,25 @@ def set_deletion_date_for_inactive_users
end
end

# Unsets the deletion date for users who have been active recently.
#
# This method finds all users whose deletion date is set and unsets it if the
# user has been active in the last 6 months.
#
# Note that this method just serves as a safety measure. The deletion date
# should be unset after every successful user sign-in, see the Warden callback
# in `config/initializers/after_sign_in.rb`. If for some reason the callback
# does not work, this method will prevent active users from being deleted
# as a last resort.
def unset_deletion_date_for_recently_active_users
User.where.not(deletion_date: nil).find_each do |user|
# Note that technically, 40 days is the maximum possible value here,
# if our intended flow works as expected. We use 6 months to be on the
# safe side as we do not want to delete active users.
user.update(deletion_date: nil) if user.last_sign_in_at >= 6.months.ago.to_date
end
end

# Deletes all users whose deletion date is in the past.
#
# The deletion date must have been set beforehand by calling
Expand All @@ -33,8 +52,9 @@ def delete_users_according_to_deletion_date
puts "#{deleted_count} stale users deleted."
end

def handle_inactive_users
def handle_inactive_users!
set_deletion_date_for_inactive_users
unset_deletion_date_for_recently_active_users
delete_users_according_to_deletion_date
end
end
23 changes: 23 additions & 0 deletions spec/models/user_cleaner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,29 @@
expect(active_user.deletion_date).to be_nil
end

it "unassigns a deletion date from recently active users" do
deletion_date = Date.current + 5.days
user_inactive = FactoryBot.create(:user, deletion_date: deletion_date,
last_sign_in_at: 7.months.ago)
user_inactive2 = FactoryBot.create(:user, deletion_date: deletion_date,
last_sign_in_at: 6.months.ago - 1.day)
user_active = FactoryBot.create(:user, deletion_date: deletion_date,
last_sign_in_at: 6.months.ago)
user_active_recently = FactoryBot.create(:user, deletion_date: deletion_date,
last_sign_in_at: 2.days.ago)

UserCleaner.new.unset_deletion_date_for_recently_active_users
user_inactive.reload
user_inactive2.reload
user_active.reload
user_active_recently.reload

expect(user_inactive.deletion_date).to eq(deletion_date)
expect(user_inactive2.deletion_date).to eq(deletion_date)
expect(user_active.deletion_date).to be_nil
expect(user_active_recently.deletion_date).to be_nil
end

it "deletes users with a deletion date in the past or present" do
user_past1 = FactoryBot.create(:user, deletion_date: Date.current - 1.day)
user_past2 = FactoryBot.create(:user, deletion_date: Date.current - 1.year)
Expand Down

0 comments on commit 833aeef

Please sign in to comment.