Skip to content

Build Event-Driven Daily Streak System with Gamified Milestone Rewards#92

Merged
phertyameen merged 1 commit intoMindBlockLabs:mainfrom
LaGodxy:Daily-Streak-System
Jul 5, 2025
Merged

Build Event-Driven Daily Streak System with Gamified Milestone Rewards#92
phertyameen merged 1 commit intoMindBlockLabs:mainfrom
LaGodxy:Daily-Streak-System

Conversation

@LaGodxy
Copy link
Copy Markdown
Contributor

@LaGodxy LaGodxy commented Jul 5, 2025

🎯 Daily Streak System Implementation

📋 Overview

This PR implements a comprehensive daily streak system that rewards users for solving at least one puzzle per day. The system tracks consecutive days of participation, resets if a day is missed, and provides bonus XP or tokens for milestone streaks.

✨ Features Added

🏆 Core Streak Functionality

  • Daily Streak Tracking: Tracks consecutive days of puzzle solving
  • Automatic Reset: Resets streak when a day is skipped
  • Milestone Rewards: Awards bonus XP and tokens at 3, 7, 14, 30, 60, 100-day streaks
  • Longest Streak Tracking: Maintains record of user's longest streak

🎮 Event-Driven Integration

  • Puzzle System Integration: Automatically updates streaks when puzzles are solved correctly
  • IQ Assessment Integration: Updates streaks when IQ questions are answered correctly
  • Loose Coupling: Uses NestJS event emitter for clean separation of concerns

📊 API Endpoints

  • GET /streak - Get current user streak status
  • GET /streak/leaderboard - Get streak leaderboard with pagination
  • GET /streak/stats - Get admin statistics (total users, active users, average streak, top streak)

�� Leaderboard System

  • Shows top streak holders
  • Paginated results
  • Sorted by current streak, then longest streak, then last active date

��️ Architecture

New Components

  1. DailyStreak Entity (src/gamification/entities/daily-streak.entity.ts)

    • Database model for tracking user streaks
    • Tracks current streak, longest streak, and milestone progress
  2. DailyStreakService (src/gamification/providers/daily-streak.service.ts)

    • Core business logic for streak management
    • Handles streak updates, milestone checking, and leaderboard queries
  3. StreakController (src/gamification/controllers/streak.controller.ts)

    • REST API endpoints with Swagger documentation
    • Protected with JWT authentication
  4. StreakListener (src/gamification/listeners/streak.listener.ts)

    • Event listener for puzzle and IQ question submissions
    • Automatically updates streaks when users solve puzzles correctly
  5. Constants (src/gamification/constants/streak.constants.ts)

    • Configuration for milestones and rewards
    • Event names and system configuration

Database Changes

  • New Table: daily_streaks with proper indexes for performance
  • Migration: 20250601204323-CreateDailyStreaksTable.ts
  • Foreign Key: Links to existing user table

�� Milestone System

Streak Days XP Reward Token Reward Description
3 50 5 3-Day Streak
7 150 15 7-Day Streak
14 300 30 14-Day Streak
30 600 60 30-Day Streak
60 1200 120 60-Day Streak
100 2000 200 100-Day Streak

🔧 Technical Implementation

Event System

  • Events Emitted:
    • puzzle.submitted: When a puzzle is submitted
    • iq.question.answered: When an IQ question is answered
    • streak.puzzle.solved: When a streak is updated
    • streak.milestone.reached: When a milestone is reached

Error Handling

  • Streak updates don't break puzzle submission flow
  • Graceful error handling in event listeners
  • Comprehensive logging for debugging

Performance Optimizations

  • Database indexes on user_id, streak_count, longest_streak, last_active_date
  • Efficient leaderboard queries with pagination
  • Idempotent streak updates (only once per day)

🧪 Testing

Unit Tests Added

  • daily-streak.service.spec.ts - Core streak logic tests
  • streak.listener.spec.ts - Event handling tests
  • streak.controller.spec.ts - API endpoint tests
  • puzzle.service.spec.ts - Puzzle integration tests

Test Coverage

  • ✅ Streak creation for new users
  • ✅ Streak increment for consecutive days
  • ✅ Streak reset when day is skipped
  • ✅ Milestone reward awarding
  • ✅ Leaderboard functionality
  • ✅ Error handling scenarios

�� Documentation

API Documentation

  • Complete Swagger documentation for all endpoints
  • Request/response examples
  • Authentication requirements

README

  • Comprehensive documentation in src/gamification/README.md
  • Usage examples for frontend integration
  • Configuration instructions
  • Best practices

�� Integration Points

Existing Systems Enhanced

  • Puzzle System: Now emits events for streak tracking
  • IQ Assessment: Now emits events for streak tracking
  • Gamification: Enhanced with milestone reward system
  • User System: Integrated with existing user entities

Backward Compatibility

  • ✅ No breaking changes to existing APIs
  • ✅ Existing puzzle and IQ assessment functionality unchanged
  • ✅ Optional feature - doesn't affect users who don't use streaks

�� Deployment Notes

Database Migration

# Run the new migration
npm run migration:run

Environment Variables

No additional environment variables required. Uses existing database and JWT configuration.

Dependencies

  • Added @nestjs/event-emitter (already in package.json)
  • No new external dependencies

�� Benefits

User Engagement

  • Motivation: Daily streaks encourage regular puzzle solving
  • Achievement: Milestone rewards provide sense of progress
  • Competition: Leaderboard fosters healthy competition

Technical Benefits

  • Scalable: Event-driven architecture supports future enhancements
  • Maintainable: Clean separation of concerns
  • Testable: Comprehensive test coverage
  • Performance: Optimized database queries and indexes

🔮 Future Enhancements

  • Streak multipliers for longer streaks
  • Special challenges for maintaining streaks
  • Push notifications for streak reminders
  • Social features for sharing streak achievements
  • More detailed analytics and insights

✅ Acceptance Criteria Met

  • User's streak updates only once per day on first puzzle submission
  • Streak resets properly when a day is skipped
  • Milestone rewards are applied correctly
  • API endpoints return correct streak data
  • All logic is covered with tests
  • Event-driven architecture implemented
  • Swagger documentation and auth guards added

Ready for Review 🚀

closes #76

Copy link
Copy Markdown
Member

@phertyameen phertyameen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This us superb. glad to have you as a contributor

@phertyameen phertyameen merged commit 1a91982 into MindBlockLabs:main Jul 5, 2025
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Build Event-Driven Daily Streak System with Gamified Milestone Rewards

2 participants