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:
57
backend/models/Badge.js
Normal file
57
backend/models/Badge.js
Normal file
@@ -0,0 +1,57 @@
|
||||
const mongoose = require("mongoose");
|
||||
|
||||
const BadgeSchema = new mongoose.Schema(
|
||||
{
|
||||
name: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true,
|
||||
},
|
||||
description: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
icon: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
criteria: {
|
||||
type: {
|
||||
type: String,
|
||||
enum: [
|
||||
"street_adoptions",
|
||||
"task_completions",
|
||||
"post_creations",
|
||||
"event_participations",
|
||||
"points_earned",
|
||||
"consecutive_days",
|
||||
"special",
|
||||
],
|
||||
required: true,
|
||||
},
|
||||
threshold: {
|
||||
type: Number,
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
rarity: {
|
||||
type: String,
|
||||
enum: ["common", "rare", "epic", "legendary"],
|
||||
default: "common",
|
||||
},
|
||||
order: {
|
||||
type: Number,
|
||||
default: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
},
|
||||
);
|
||||
|
||||
// Index for efficient badge queries
|
||||
BadgeSchema.index({ "criteria.type": 1, "criteria.threshold": 1 });
|
||||
BadgeSchema.index({ rarity: 1 });
|
||||
BadgeSchema.index({ order: 1 });
|
||||
|
||||
module.exports = mongoose.model("Badge", BadgeSchema);
|
||||
57
backend/models/PointTransaction.js
Normal file
57
backend/models/PointTransaction.js
Normal file
@@ -0,0 +1,57 @@
|
||||
const mongoose = require("mongoose");
|
||||
|
||||
const PointTransactionSchema = new mongoose.Schema(
|
||||
{
|
||||
user: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: "User",
|
||||
required: true,
|
||||
index: true,
|
||||
},
|
||||
amount: {
|
||||
type: Number,
|
||||
required: true,
|
||||
},
|
||||
type: {
|
||||
type: String,
|
||||
enum: [
|
||||
"street_adoption",
|
||||
"task_completion",
|
||||
"post_creation",
|
||||
"event_participation",
|
||||
"reward_redemption",
|
||||
"admin_adjustment",
|
||||
],
|
||||
required: true,
|
||||
index: true,
|
||||
},
|
||||
description: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
relatedEntity: {
|
||||
entityType: {
|
||||
type: String,
|
||||
enum: ["Street", "Task", "Post", "Event", "Reward"],
|
||||
},
|
||||
entityId: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
},
|
||||
},
|
||||
balanceAfter: {
|
||||
type: Number,
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
},
|
||||
);
|
||||
|
||||
// Compound index for user transaction history queries
|
||||
PointTransactionSchema.index({ user: 1, createdAt: -1 });
|
||||
|
||||
// Index for querying by transaction type
|
||||
PointTransactionSchema.index({ type: 1, createdAt: -1 });
|
||||
|
||||
module.exports = mongoose.model("PointTransaction", PointTransactionSchema);
|
||||
@@ -22,6 +22,7 @@ const UserSchema = new mongoose.Schema(
|
||||
points: {
|
||||
type: Number,
|
||||
default: 0,
|
||||
min: 0,
|
||||
},
|
||||
adoptedStreets: [
|
||||
{
|
||||
@@ -35,11 +36,43 @@ const UserSchema = new mongoose.Schema(
|
||||
ref: "Task",
|
||||
},
|
||||
],
|
||||
badges: [String],
|
||||
posts: [
|
||||
{
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: "Post",
|
||||
},
|
||||
],
|
||||
events: [
|
||||
{
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: "Event",
|
||||
},
|
||||
],
|
||||
profilePicture: {
|
||||
type: String,
|
||||
},
|
||||
cloudinaryPublicId: {
|
||||
type: String,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
},
|
||||
);
|
||||
|
||||
// Indexes for performance
|
||||
UserSchema.index({ email: 1 });
|
||||
UserSchema.index({ points: -1 }); // For leaderboards
|
||||
|
||||
// Virtual for earned badges (populated from UserBadge collection)
|
||||
UserSchema.virtual("earnedBadges", {
|
||||
ref: "UserBadge",
|
||||
localField: "_id",
|
||||
foreignField: "user",
|
||||
});
|
||||
|
||||
// Ensure virtuals are included when converting to JSON
|
||||
UserSchema.set("toJSON", { virtuals: true });
|
||||
UserSchema.set("toObject", { virtuals: true });
|
||||
|
||||
module.exports = mongoose.model("User", UserSchema);
|
||||
|
||||
37
backend/models/UserBadge.js
Normal file
37
backend/models/UserBadge.js
Normal file
@@ -0,0 +1,37 @@
|
||||
const mongoose = require("mongoose");
|
||||
|
||||
const UserBadgeSchema = new mongoose.Schema(
|
||||
{
|
||||
user: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: "User",
|
||||
required: true,
|
||||
index: true,
|
||||
},
|
||||
badge: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: "Badge",
|
||||
required: true,
|
||||
index: true,
|
||||
},
|
||||
earnedAt: {
|
||||
type: Date,
|
||||
default: Date.now,
|
||||
},
|
||||
progress: {
|
||||
type: Number,
|
||||
default: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
},
|
||||
);
|
||||
|
||||
// Compound unique index to prevent duplicate badge awards
|
||||
UserBadgeSchema.index({ user: 1, badge: 1 }, { unique: true });
|
||||
|
||||
// Index for user badge queries
|
||||
UserBadgeSchema.index({ user: 1, earnedAt: -1 });
|
||||
|
||||
module.exports = mongoose.model("UserBadge", UserBadgeSchema);
|
||||
Reference in New Issue
Block a user