feat(backend): implement complete gamification system
Implement comprehensive points and badges system with MongoDB transactions: Point System: - Create PointTransaction model for transaction history - Award points atomically using MongoDB transactions - Point values: street adoption (+100), task completion (+50), post creation (+10), event participation (+75) - Track balance after each transaction - Support point deduction for reward redemption Badge System: - Create Badge and UserBadge models - Define badge criteria types: street_adoptions, task_completions, post_creations, event_participations, points_earned - Auto-award badges based on user achievements - Badge rarity levels: common, rare, epic, legendary - Track badge progress for users - Prevent duplicate badge awards Gamification Service: - Implement gamificationService.js with 390 lines of logic - awardPoints() with transaction support - checkAndAwardBadges() for auto-awarding - getUserBadgeProgress() for progress tracking - getUserStats() for achievement statistics - Atomic operations prevent double-awarding Integration: - Streets route: Award points and badges on adoption - Tasks route: Award points and badges on completion - Posts route: Award points and badges on creation - Events route: Award points and badges on RSVP - Rewards route: Deduct points on redemption - Badges API: List badges, track progress, view earned badges Updated User Model: - Add points field (default 0) - Add earnedBadges virtual relationship - Add indexes for performance (points for leaderboards) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
76
backend/routes/badges.js
Normal file
76
backend/routes/badges.js
Normal file
@@ -0,0 +1,76 @@
|
||||
const express = require("express");
|
||||
const Badge = require("../models/Badge");
|
||||
const UserBadge = require("../models/UserBadge");
|
||||
const auth = require("../middleware/auth");
|
||||
const { asyncHandler } = require("../middleware/errorHandler");
|
||||
const { getUserBadgeProgress } = require("../services/gamificationService");
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
/**
|
||||
* GET /api/badges
|
||||
* Get all available badges
|
||||
*/
|
||||
router.get(
|
||||
"/",
|
||||
asyncHandler(async (req, res) => {
|
||||
const badges = await Badge.find().sort({ order: 1, rarity: 1 });
|
||||
res.json(badges);
|
||||
})
|
||||
);
|
||||
|
||||
/**
|
||||
* GET /api/badges/progress
|
||||
* Get current user's badge progress (requires authentication)
|
||||
*/
|
||||
router.get(
|
||||
"/progress",
|
||||
auth,
|
||||
asyncHandler(async (req, res) => {
|
||||
const progress = await getUserBadgeProgress(req.user.id);
|
||||
res.json(progress);
|
||||
})
|
||||
);
|
||||
|
||||
/**
|
||||
* GET /api/users/:userId/badges
|
||||
* Get badges earned by a specific user
|
||||
*/
|
||||
router.get(
|
||||
"/users/:userId",
|
||||
asyncHandler(async (req, res) => {
|
||||
const { userId } = req.params;
|
||||
|
||||
const userBadges = await UserBadge.find({ user: userId })
|
||||
.populate("badge")
|
||||
.sort({ earnedAt: -1 });
|
||||
|
||||
res.json(
|
||||
userBadges.map((ub) => ({
|
||||
badge: ub.badge,
|
||||
earnedAt: ub.earnedAt,
|
||||
progress: ub.progress,
|
||||
}))
|
||||
);
|
||||
})
|
||||
);
|
||||
|
||||
/**
|
||||
* GET /api/badges/:badgeId
|
||||
* Get a specific badge by ID
|
||||
*/
|
||||
router.get(
|
||||
"/:badgeId",
|
||||
asyncHandler(async (req, res) => {
|
||||
const { badgeId } = req.params;
|
||||
|
||||
const badge = await Badge.findById(badgeId);
|
||||
if (!badge) {
|
||||
return res.status(404).json({ msg: "Badge not found" });
|
||||
}
|
||||
|
||||
res.json(badge);
|
||||
})
|
||||
);
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user