I have a badge that my members can earn multiple times, it is a “Book Club” badge - they are awarded it for discussing the book of the month.
Each month, a new topic is created about the next book, and everyone replying should get the badge - this is currently done manually and is a real pain.
I was wondering if someone could help me craft a Badge SQL to fix this.
The user should receive maximum of one badge per topic, the first time they post in that topic. It’s the same badge every time, which is earned multiple times.
No further. I’m stumped by the multiple granting factor.
All the topics are in the same category. That category is locked down so only staff can create posts there, so it’s safe to auto grant to any replies in the category.
But I want it to:
Grant a badge for the first reply by each person in each topic, but only once per topic
A user can get multiple of the same badge each time they post a reply to a different topic of the same category
WITH book_club_first_posts AS (
SELECT
p.topic_id,
p.user_id,
MIN(p.id) AS post_id
FROM posts p
JOIN topics t ON t.id = p.topic_id
WHERE t.category_id = 5 -- replace with the category_id of your book club
AND p.deleted_at ISNULL
AND t.deleted_at ISNULL
AND p.post_type = 1
AND p.post_number <> 1
AND p.user_id > 0
GROUP BY p.topic_id, p.user_id
)
SELECT bcfp.user_id, bcfp.post_id, p.created_at granted_at
FROM book_club_first_posts bcfp
JOIN posts p ON p.id = bcfp.post_id
WHERE (:backfill OR p.id IN (:post_ids))