FIX: Safeguard to not award NewUserOfTheMonth if it hasn't been a month
This commit is contained in:
parent
5ff29ce321
commit
a13a8dc96c
|
@ -8,6 +8,13 @@ module Jobs
|
||||||
|
|
||||||
def execute(args)
|
def execute(args)
|
||||||
badge = Badge.find(Badge::NewUserOfTheMonth)
|
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|
|
scores.each do |user_id, score|
|
||||||
# Don't bother awarding to users who haven't received any likes
|
# Don't bother awarding to users who haven't received any likes
|
||||||
if score > 0.0
|
if score > 0.0
|
||||||
|
|
|
@ -6,6 +6,9 @@ describe Jobs::GrantNewUserOfTheMonthBadges do
|
||||||
let(:granter) { described_class.new }
|
let(:granter) { described_class.new }
|
||||||
|
|
||||||
it "runs correctly" do
|
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)
|
user = Fabricate(:user, created_at: 1.week.ago)
|
||||||
p = Fabricate(:post, user: user)
|
p = Fabricate(:post, user: user)
|
||||||
Fabricate(:post, user: user)
|
Fabricate(:post, user: user)
|
||||||
|
@ -21,6 +24,25 @@ describe Jobs::GrantNewUserOfTheMonthBadges do
|
||||||
expect(badge).to be_present
|
expect(badge).to be_present
|
||||||
end
|
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
|
describe '.scores' do
|
||||||
|
|
||||||
it "doesn't award it to accounts over a month old" do
|
it "doesn't award it to accounts over a month old" do
|
||||||
|
|
Loading…
Reference in New Issue