Conversation
…ded or streak is earned. If condition is met, badge is awarded
Contributor
There was a problem hiding this comment.
Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Summary
Implements backend badge trigger logic that evaluates user engagement stats against configured badge rules and automatically persists newly earned badges to public.user_badges without duplicating awards.
Overview
Adds automatic badge awarding when users earn XP / progress streaks, so achievements are granted consistently and stored in the database.
Added a modular badge trigger service (backend/services/badge_trigger.py) with a registry of badge conditions keyed by badge slug.
Wired badge evaluation into existing gamification event flows after XP is applied (/api/v1/quiz/result, /api/v1/flashcards/session-complete).
Persisted awards to public.user_badges and prevented duplicates by checking existing awards and relying on the (user_id, badge_id) primary key.
Badge rules are defined in a centralized registry for easy extension (new badge = DB row + new condition entry).
Awarding emits server logs on success for traceability.
Checklist
Additional Notes
Manual testing performed (sample events):
Triggered quiz completion XP and verified new badge grants persisted to public.user_badges (getting_started, first_xp, 50_xp).
Replayed the same event and confirmed no duplicate badge awards (new_badges=[]).
Triggered flashcard session XP and confirmed no unexpected additional badges.
Jira Ticket
Jira Ticket(s) - [SOC-23]