From 910f0db7ba69e847ec71e2129614e3e37da96c7e Mon Sep 17 00:00:00 2001
From: Mathieu Magnin <mathieu@magnin.sh>
Date: Tue, 17 Sep 2024 09:35:35 +0200
Subject: [PATCH] [#10753] fix missing condition on created at

---
 ...ified_when_user_signed_in_during_last_5_months.rake |  6 ++++--
 ...ed_when_user_signed_in_during_last_5_months_spec.rb | 10 ++++++++--
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/lib/tasks/deployment/20240911141817_mark_email_verified_when_user_signed_in_during_last_5_months.rake b/lib/tasks/deployment/20240911141817_mark_email_verified_when_user_signed_in_during_last_5_months.rake
index 3ef23f4dc..63841f7ee 100644
--- a/lib/tasks/deployment/20240911141817_mark_email_verified_when_user_signed_in_during_last_5_months.rake
+++ b/lib/tasks/deployment/20240911141817_mark_email_verified_when_user_signed_in_during_last_5_months.rake
@@ -3,11 +3,13 @@
 namespace :after_party do
   desc 'Deployment task: mark_email_verified_when_user_signed_in_during_last_5_months'
   task mark_email_verified_when_user_signed_in_during_last_5_months: :environment do
-    affected_users_count = User.where(email_verified_at: nil, sign_in_count: 1..).count
+    users_signed_in_during_last_5_months = User.where(email_verified_at: nil, created_at: 5.months.ago.., sign_in_count: 1..)
+
+    affected_users_count = users_signed_in_during_last_5_months.count
 
     puts "Processing #{affected_users_count} users signed in during the last 5 months and not verified"
 
-    User.where(email_verified_at: nil, sign_in_count: 1..).update_all(email_verified_at: Time.zone.now)
+    users_signed_in_during_last_5_months.update_all(email_verified_at: Time.zone.now)
     puts "Done"
 
     AfterParty::TaskRecord
diff --git a/spec/lib/tasks/deployment/20240911141817_mark_email_verified_when_user_signed_in_during_last_5_months_spec.rb b/spec/lib/tasks/deployment/20240911141817_mark_email_verified_when_user_signed_in_during_last_5_months_spec.rb
index 0546ac0c5..0c3ee6eee 100644
--- a/spec/lib/tasks/deployment/20240911141817_mark_email_verified_when_user_signed_in_during_last_5_months_spec.rb
+++ b/spec/lib/tasks/deployment/20240911141817_mark_email_verified_when_user_signed_in_during_last_5_months_spec.rb
@@ -13,16 +13,22 @@ describe 'after_party:mark_email_verified_when_user_signed_in_during_last_5_mont
   let!(:user2) { create(:user, email_verified_at: nil, sign_in_count: 1) }
   let!(:user3) { create(:user, email_verified_at: nil, sign_in_count: 0) }
   let!(:user4) { create(:user, email_verified_at: old_date, sign_in_count: 1) }
+  let!(:user5) do
+    travel_to(old_date) do
+      create(:user, email_verified_at: nil, sign_in_count: 1)
+    end
+  end
 
   it 'mark_email_verified_when_user_signed_in_during_last_5_months' do
-    expect(User.where(email_verified_at: nil, sign_in_count: 1..).count).to eq(2)
+    expect(User.where(email_verified_at: nil, sign_in_count: 1.., created_at: 5.months.ago..).count).to eq(2)
 
     subject
 
-    expect(User.where(email_verified_at: nil, sign_in_count: 1..).count).to eq(0)
+    expect(User.where(email_verified_at: nil, sign_in_count: 1.., created_at: 5.months.ago..).count).to eq(0)
     expect(user.reload.email_verified_at).to be_present
     expect(user2.reload.email_verified_at).to be_present
     expect(user3.reload.email_verified_at).to be_nil
     expect(user4.reload.email_verified_at.to_i).to eq(old_date.to_i)
+    expect(user5.reload.email_verified_at).to be_nil
   end
 end