Class: Jobs::FixRetroAnniversary

Inherits:
Onceoff show all
Defined in:
app/jobs/onceoff/fix_retro_anniversary.rb

Instance Method Summary collapse

Methods inherited from Onceoff

enqueue_all, #execute, name_for, #running_key_name

Methods inherited from Base

acquire_cluster_concurrency_lock!, clear_cluster_concurrency_lock!, cluster_concurrency, cluster_concurrency_redis_key, delayed_perform, #error_context, #execute, get_cluster_concurrency, #last_db_duration, #log, #perform, #perform_immediately

Instance Method Details

#execute_onceoff(args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/jobs/onceoff/fix_retro_anniversary.rb', line 5

def execute_onceoff(args)
  return unless SiteSetting.enable_badges

  users = DB.query <<~SQL
    SELECT ub.user_id, MIN(granted_at) AS first_granted_at, COUNT(*) count
    FROM user_badges AS ub
    WHERE ub.badge_id = #{Badge::Anniversary}
    GROUP BY ub.user_id
    HAVING COUNT(ub.id) > 1
  SQL

  users.each do |u|
    first = u.first_granted_at
    badges =
      UserBadge.where(
        "badge_id = ? AND user_id = ? AND granted_at > ?",
        Badge::Anniversary,
        u.user_id,
        first,
      ).order("granted_at")

    badges.each_with_index do |b, idx|
      award_date = (first + (idx + 1).years)
      UserBadge.where(id: b.id).update_all(["granted_at = ?", award_date])
    end
  end
end