2020-02-04 19:14:12 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-11-21 18:36:46 -05:00
|
|
|
first_solution_query = <<~SQL
|
2022-11-21 15:45:46 -05:00
|
|
|
SELECT post_id, user_id, created_at AS granted_at
|
|
|
|
FROM (
|
|
|
|
SELECT p.id AS post_id, p.user_id, pcf.created_at,
|
|
|
|
ROW_NUMBER() OVER (PARTITION BY p.user_id ORDER BY pcf.created_at) AS row_number
|
|
|
|
FROM post_custom_fields pcf
|
|
|
|
JOIN badge_posts p ON pcf.post_id = p.id
|
|
|
|
JOIN topics t ON p.topic_id = t.id
|
|
|
|
WHERE pcf.name = 'is_accepted_answer'
|
|
|
|
AND p.user_id <> t.user_id -- ignore topics solved by OP
|
|
|
|
AND (:backfill OR p.id IN (:post_ids))
|
|
|
|
) x
|
|
|
|
WHERE row_number = 1
|
|
|
|
SQL
|
2020-02-04 19:14:12 -05:00
|
|
|
|
|
|
|
Badge.seed(:name) do |badge|
|
2022-11-21 18:24:16 -05:00
|
|
|
badge.name = "Solved 1"
|
2022-11-21 18:36:46 -05:00
|
|
|
badge.default_icon = "check-square"
|
|
|
|
badge.badge_type_id = BadgeType::Bronze
|
|
|
|
badge.default_badge_grouping_id = BadgeGrouping::Community
|
2022-11-21 18:24:16 -05:00
|
|
|
badge.query = first_solution_query
|
2020-02-04 19:14:12 -05:00
|
|
|
badge.listable = true
|
|
|
|
badge.target_posts = true
|
2022-11-24 08:08:39 -05:00
|
|
|
badge.default_enabled = false
|
2020-02-04 19:14:12 -05:00
|
|
|
badge.trigger = Badge::Trigger::PostRevision
|
|
|
|
badge.auto_revoke = true
|
|
|
|
badge.show_posts = true
|
2022-11-21 16:45:29 -05:00
|
|
|
badge.system = true
|
2020-02-04 19:14:12 -05:00
|
|
|
end
|
|
|
|
|
2022-11-21 18:36:46 -05:00
|
|
|
def solved_query_with_count(min_count)
|
|
|
|
<<~SQL
|
|
|
|
SELECT p.user_id, MAX(pcf.created_at) AS granted_at
|
|
|
|
FROM post_custom_fields pcf
|
|
|
|
JOIN badge_posts p ON pcf.post_id = p.id
|
|
|
|
JOIN topics t ON p.topic_id = t.id
|
|
|
|
WHERE pcf.name = 'is_accepted_answer'
|
|
|
|
AND p.user_id <> t.user_id -- ignore topics solved by OP
|
|
|
|
AND (:backfill OR p.id IN (:post_ids))
|
|
|
|
GROUP BY p.user_id
|
|
|
|
HAVING COUNT(*) >= #{min_count}
|
|
|
|
SQL
|
|
|
|
end
|
2020-02-04 19:14:12 -05:00
|
|
|
|
2022-11-21 18:36:46 -05:00
|
|
|
[
|
|
|
|
["Solved 2", BadgeType::Silver, 10],
|
|
|
|
["Solved 3", BadgeType::Gold, 50],
|
|
|
|
["Solved 4", BadgeType::Gold, 150],
|
|
|
|
].each do |name, level, count|
|
|
|
|
Badge.seed(:name) do |badge|
|
|
|
|
badge.name = name
|
|
|
|
badge.default_icon = "check-square"
|
|
|
|
badge.badge_type_id = level
|
|
|
|
badge.default_badge_grouping_id = BadgeGrouping::Community
|
|
|
|
badge.query = solved_query_with_count(count)
|
|
|
|
badge.listable = true
|
|
|
|
badge.default_allow_title = true
|
|
|
|
badge.target_posts = false
|
2022-11-24 08:08:39 -05:00
|
|
|
badge.default_enabled = false
|
2022-11-21 18:36:46 -05:00
|
|
|
badge.trigger = Badge::Trigger::PostRevision
|
|
|
|
badge.auto_revoke = true
|
|
|
|
badge.show_posts = false
|
|
|
|
badge.system = true
|
|
|
|
end
|
2020-02-04 19:14:12 -05:00
|
|
|
end
|