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