Badge query request: First user to read a specific topic from a specific date

I managed to get the correct results by making a query in the Data Explorer plugin, but how do I convert this query to work as an automatic badge?

The badge should be assigned (triggered?) as soon as 1 user sees a specific topic from a specific date.

SELECT
    title,
    viewed_at,
    topic_views.user_id
FROM topics
LEFT JOIN topic_views
    ON topics.id = topic_views.topic_id
WHERE category_id IS NOT NULL
    AND topic_views.user_id IS NOT NULL
    AND topics.id = 12
    AND viewed_at >= '2021-10-17'
ORDER BY viewed_at DESC
LIMIT 1