Skip to content

Commit

Permalink
Clean up unconfirmed users that have been created more than 2 weeks ago
Browse files Browse the repository at this point in the history
These users have been sent an email with a link to confirm their account
but have not accepted it
  • Loading branch information
koetsier committed Feb 24, 2025
1 parent 1f3209d commit 983f784
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class User < ApplicationRecord
order(build_order_query)
}

scope :unconfirmed, -> { where(confirmed_at: nil) }

def password_present?
!password.nil?
end
Expand Down
6 changes: 6 additions & 0 deletions lib/tasks/delete_inactive_unconfirmed.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace :cleanup do
desc "Clean up inactive unconfirmed users"
task unconfirmed: :environment do
UseCases::DeleteInactiveUnconfirmed.execute(2.weeks)
end
end
7 changes: 7 additions & 0 deletions lib/use_cases/delete_inactive_unconfirmed.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module UseCases
class DeleteInactiveUnconfirmed
def self.execute(inactive_period)
User.unconfirmed.where("created_at < ?", Time.zone.now - inactive_period).delete_all
end
end
end
39 changes: 39 additions & 0 deletions spec/use_cases/delete_inactive_unconfirmed_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
describe UseCases::DeleteInactiveUnconfirmed do
subject { UseCases::DeleteInactiveUnconfirmed }
let(:inactive_period) { 2.weeks }
describe "There are no unconfirmed users" do
before do
create(:user)
end
it "does not delete any users" do
expect { subject.execute(inactive_period) }.to_not change(User, :count)
end
end
describe "There is an inactive unconfirmed user who has been inactive for less than the specified period" do
before do
create(:user, :unconfirmed, created_at: 1.week.ago).id
end
it "does not delete any users" do
expect { subject.execute(inactive_period) }.to_not change(User, :count)
end
end
describe "There is an unconfirmed user who has been inactive for more than the specified period" do
before do
@inactive_user_id = create(:user, :unconfirmed, created_at: 3.weeks.ago).id
end
it "deletes the unconfirmed user" do
subject.execute(inactive_period)
expect(User.where(id: @inactive_user_id)).to_not exist
end
end
describe "There is a mix of unconfirmed and unconfirmed users" do
before do
create_list(:user, 2, :unconfirmed, created_at: 1.week.ago)
create_list(:user, 3, :unconfirmed, created_at: 3.weeks.ago)
create_list(:user, 7, created_at: 3.weeks.ago)
end
it "deletes the inactive unconfirmed users only" do
expect { subject.execute(inactive_period) }.to change(User, :count).by(-3)
end
end
end

0 comments on commit 983f784

Please sign in to comment.