2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-01 12:04:45 -04:00
|
|
|
module Jobs
|
2019-10-02 00:01:53 -04:00
|
|
|
class FixRetroAnniversary < ::Jobs::Onceoff
|
2017-05-01 12:04:45 -04:00
|
|
|
def execute_onceoff(args)
|
|
|
|
return unless SiteSetting.enable_badges
|
|
|
|
|
2018-06-19 19:09:13 -04:00
|
|
|
users = DB.query <<~SQL
|
|
|
|
SELECT ub.user_id, MIN(granted_at) AS first_granted_at, COUNT(*) count
|
2017-05-01 12:04:45 -04:00
|
|
|
FROM user_badges AS ub
|
|
|
|
WHERE ub.badge_id = #{Badge::Anniversary}
|
|
|
|
GROUP BY ub.user_id
|
|
|
|
HAVING COUNT(ub.id) > 1
|
|
|
|
SQL
|
|
|
|
|
2018-06-19 19:09:13 -04:00
|
|
|
users.each do |u|
|
2018-06-19 02:13:14 -04:00
|
|
|
first = u.first_granted_at
|
2017-05-01 12:04:45 -04:00
|
|
|
badges =
|
|
|
|
UserBadge.where(
|
|
|
|
"badge_id = ? AND user_id = ? AND granted_at > ?",
|
|
|
|
Badge::Anniversary,
|
2018-06-19 02:13:14 -04:00
|
|
|
u.user_id,
|
2017-05-01 12:04:45 -04:00
|
|
|
first,
|
|
|
|
).order("granted_at")
|
|
|
|
|
|
|
|
badges.each_with_index do |b, idx|
|
|
|
|
award_date = (first + (idx + 1).years)
|
2018-06-19 02:13:14 -04:00
|
|
|
UserBadge.where(id: b.id).update_all(["granted_at = ?", award_date])
|
2017-05-01 12:04:45 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|