FIX: Don't award new user of the month in the wrong month

diff --git a/app/jobs/scheduled/grant_new_user_of_the_month_badges.rb b/app/jobs/scheduled/grant_new_user_of_the_month_badges.rb
index 866f470..4cd1616 100644
--- a/app/jobs/scheduled/grant_new_user_of_the_month_badges.rb
+++ b/app/jobs/scheduled/grant_new_user_of_the_month_badges.rb
@@ -17,7 +17,7 @@ module Jobs
         badge.id, previous_month_beginning, Time.zone.now
       ).exists?
 
-      scores(previous_month_beginning).each do |user_id, score|
+      scores(previous_month_beginning, previous_month_end).each do |user_id, score|
         # Don't bother awarding to users who haven't received any likes
         if score > 0.0
           user = User.find(user_id)
@@ -33,7 +33,7 @@ module Jobs
       end
     end
 
-    def scores(user_created_after_date)
+    def scores(min_user_created_at, max_user_created_at)
       current_owners = UserBadge.where(badge_id: Badge::NewUserOfTheMonth).pluck(:user_id)
       current_owners = [-1] if current_owners.blank?
 
@@ -71,7 +71,7 @@ module Jobs
           AND NOT u.moderator
           AND u.suspended_at IS NULL
           AND u.suspended_till IS NULL
-          AND u.created_at >= :min_user_created_at
+          AND u.created_at BETWEEN :min_user_created_at AND :max_user_created_at
           AND t.archetype <> '#{Archetype.private_message}'
           AND t.deleted_at IS NULL
           AND p.deleted_at IS NULL
@@ -83,7 +83,13 @@ module Jobs
         LIMIT #{MAX_AWARDED}
       SQL
 
-      Hash[*DB.query_single(sql, min_user_created_at: user_created_after_date)]
+      Hash[
+        *DB.query_single(
+          sql,
+          min_user_created_at: min_user_created_at,
+          max_user_created_at: max_user_created_at
+        )
+      ]
     end
 
   end
diff --git a/spec/jobs/grant_new_user_of_the_month_badges_spec.rb b/spec/jobs/grant_new_user_of_the_month_badges_spec.rb
index 767dd4f..60142cb 100644
--- a/spec/jobs/grant_new_user_of_the_month_badges_spec.rb
+++ b/spec/jobs/grant_new_user_of_the_month_badges_spec.rb
@@ -30,6 +30,24 @@ describe Jobs::GrantNewUserOfTheMonthBadges do
     expect(badges.first.granted_at.to_s).to eq('2019-12-31 23:59:59 UTC')
   end
 
+  it "does not include people created after the previous month" do
+    freeze_time(DateTime.parse('2020-01-15 00:00 UTC'))
+
+    user = Fabricate(:user, created_at: 1.week.ago)
+    p = Fabricate(:post, user: user)
+    Fabricate(:post, user: user)
+
+    old_user = Fabricate(:user, created_at: 6.months.ago)
+    PostActionCreator.like(old_user, p)
+    old_user = Fabricate(:user, created_at: 6.months.ago)
+    PostActionCreator.like(old_user, p)
+
+    granter.execute({})
+
+    badges = user.user_badges.where(badge_id: Badge::NewUserOfTheMonth)
+    expect(badges).to be_blank
+  end
+
   it "does nothing if badges are disabled" do
     SiteSetting.enable_badges = false
 
@@ -93,7 +111,7 @@ describe Jobs::GrantNewUserOfTheMonthBadges do
 
   describe '.scores' do
     def scores
-      granter.scores(1.month.ago)
+      granter.scores(1.month.ago, Time.now)
     end
 
     it "doesn't award it to accounts over a month old" do

GitHub sha: a73da426

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/new-user-of-the-month-badge-awarded-before-registraton-date/157347/4