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 e9078124de0..554710d7ffc 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 @@ -8,6 +8,13 @@ module Jobs def execute(args) badge = Badge.find(Badge::NewUserOfTheMonth) + + # Don't award it if a month hasn't gone by + return if UserBadge.where("badge_id = ? AND granted_at >= ?", + badge.id, + (1.month.ago + 1.day) # give it a day slack just in case + ).exists? + scores.each do |user_id, score| # Don't bother awarding to users who haven't received any likes if score > 0.0 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 8f8993dc656..414f7be0ac5 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 @@ -6,6 +6,9 @@ describe Jobs::GrantNewUserOfTheMonthBadges do let(:granter) { described_class.new } it "runs correctly" do + u0 = Fabricate(:user, created_at: 2.weeks.ago) + BadgeGranter.grant(Badge.find(Badge::NewUserOfTheMonth), u0, created_at: 1.month.ago) + user = Fabricate(:user, created_at: 1.week.ago) p = Fabricate(:post, user: user) Fabricate(:post, user: user) @@ -21,6 +24,25 @@ describe Jobs::GrantNewUserOfTheMonthBadges do expect(badge).to be_present end + it "does nothing if it's been awarded recently" do + u0 = Fabricate(:user, created_at: 2.weeks.ago) + BadgeGranter.grant(Badge.find(Badge::NewUserOfTheMonth), u0) + + 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) + PostAction.act(old_user, p, PostActionType.types[:like]) + old_user = Fabricate(:user, created_at: 6.months.ago) + PostAction.act(old_user, p, PostActionType.types[:like]) + + granter.execute({}) + + badge = user.user_badges.where(badge_id: Badge::NewUserOfTheMonth) + expect(badge).to be_blank + end + describe '.scores' do it "doesn't award it to accounts over a month old" do